DUNE PDELab (git)

proxynode.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-GPL-2.0-only-with-PDELab-exception
5
6#ifndef DUNE_TYPETREE_PROXYNODE_HH
7#define DUNE_TYPETREE_PROXYNODE_HH
8
9#include <type_traits>
10#include <dune/typetree/nodeinterface.hh>
11#include <dune/typetree/nodetags.hh>
13#include <dune/common/indices.hh>
14#include <dune/common/std/type_traits.hh>
15
16namespace Dune {
17 namespace TypeTree {
18
24 template<typename Node>
25 class ProxyNode;
26
28 template<typename ProxiedNode>
30 {
31
32 static const bool proxiedNodeIsConst = std::is_const<typename std::remove_reference<ProxiedNode>::type>::value;
33
34 template<std::size_t k>
35 struct lazy_enabled
36 {
37 static const bool value = !proxiedNodeIsConst;
38 };
39
41
42 template<bool enabled = !proxiedNodeIsConst>
43 typename std::enable_if<enabled,Node&>::type
44 node ()
45 {
46 return static_cast<Node&>(*this);
47 }
48
49 const Node& node () const
50 {
51 return static_cast<const Node&>(*this);
52 }
53
54 public:
55
57 template<std::size_t k>
58 struct Child
59 : public ProxiedNode::template Child<k>
60 {};
61
64
66
69 template<std::size_t k,
70 typename std::enable_if<lazy_enabled<k>::value, int>::type = 0>
71 auto& child (index_constant<k> = {})
72 {
73 return node().proxiedNode().template child<k>();
74 }
75
77
80 template<std::size_t k>
81 const auto& child (index_constant<k> = {}) const
82 {
83 return node().proxiedNode().template child<k>();
84 }
85
87
90 template<std::size_t k,
91 typename std::enable_if<lazy_enabled<k>::value, int>::type = 0>
92 auto childStorage (index_constant<k> = {})
93 {
94 return node().proxiedNode().template childStorage<k>();
95 }
96
98
104 template<std::size_t k>
105 auto childStorage (index_constant<k> = {}) const
106 {
107 return node().proxiedNode().template childStorage<k>();
108 }
109
111 template<std::size_t k, class ProxyChild>
112 void setChild (ProxyChild&& child, typename std::enable_if<lazy_enabled<k>::value,void*>::type = 0)
113 {
114 node().proxiedNode().template setChild<k>(std::forward<ProxyChild>(child));
115 }
116
117 const typename ProxiedNode::NodeStorage& nodeStorage () const
118 {
119 return node().proxiedNode().nodeStorage();
120 }
121
122 };
123
125
130 template<typename ProxiedNode>
132 : public StaticChildAccessors<ProxiedNode>
133 {
134
136
137 static const bool proxiedNodeIsConst = std::is_const<typename std::remove_reference<ProxiedNode>::type>::value;
138
139 template<bool enabled = !proxiedNodeIsConst>
140 typename std::enable_if<enabled,Node&>::type
141 node ()
142 {
143 return static_cast<Node&>(*this);
144 }
145
146 const Node& node () const
147 {
148 return static_cast<const Node&>(*this);
149 }
150
151 public:
152
155
157
160 template<bool enabled = !proxiedNodeIsConst,
161 typename std::enable_if<enabled, int>::type = 0>
162 auto& child (std::size_t i)
163 {
164 return node().proxiedNode().child(i);
165 }
166
168
171 const auto& child (std::size_t i) const
172 {
173 return node().proxiedNode().child(i);
174 }
175
177
180 template<bool enabled = !proxiedNodeIsConst,
181 typename std::enable_if<enabled, int>::type = 0>
182 auto childStorage (std::size_t i)
183 {
184 return node().proxiedNode().childStorage(i);
185 }
186
188
194 auto childStorage (std::size_t i) const
195 {
196 return node().proxiedNode().childStorage(i);
197 }
198
200 template<class ProxyChild, bool enabled = !proxiedNodeIsConst>
201 void setChild (std::size_t i, ProxyChild&& child, typename std::enable_if<enabled,void*>::type = 0)
202 {
203 node().proxiedNode().setChild(i, std::forward<ProxyChild>(child));
204 }
205
206 };
207
209 template<typename Node, typename NodeTag>
211
213 template<typename Node>
215 {
216 };
217
219 template<typename Node>
221 : public StaticChildAccessors<Node>
222 {
223 typedef typename Node::ChildTypes ChildTypes;
224 typedef typename Node::NodeStorage NodeStorage;
225 };
226
228 template<typename Node>
230 : public DynamicChildAccessors<Node>
231 {
232 typedef typename Node::ChildType ChildType;
233 typedef typename Node::NodeStorage NodeStorage;
234 };
235
237 template<typename Node>
239 : public DynamicChildAccessors<Node>
240 {
241 typedef typename Node::ChildType ChildType;
242 typedef typename Node::NodeStorage NodeStorage;
243 };
244
246
252 template<typename Node>
254 : public ProxyNodeBase<Node,NodeTag<Node>>
255 {
256 static const bool proxiedNodeIsConst = std::is_const<typename std::remove_reference<Node>::type>::value;
257
258 template <class N>
259 using HasStaticDegree = index_constant<N::degree()>;
260
261 template <class N>
262 static constexpr bool hasStaticDegree = Std::is_detected<HasStaticDegree, N>::value;
263
264 // accessor mixins need to be friends for access to proxiedNode()
265 friend class StaticChildAccessors<Node>;
266 friend class DynamicChildAccessors<Node>;
267
268 public:
269
270 typedef Node ProxiedNode;
271
273
275 static const bool isLeaf = Node::isLeaf;
276
278 static const bool isPower = Node::isPower;
279
281 static const bool isComposite = Node::isComposite;
282
283 template <class N = Node,
284 std::enable_if_t<hasStaticDegree<N>, int> = 0>
285 static constexpr auto degree ()
286 {
287 return N::degree();
288 }
289
290 template <class N = Node,
291 std::enable_if_t<not hasStaticDegree<N>, int> = 0>
292 auto degree () const
293 {
294 return proxiedNode().degree();
295 }
296
297
298 protected:
299
302
304 template<bool enabled = !proxiedNodeIsConst>
305 typename std::enable_if<enabled,Node&>::type
307 {
308 return *_node;
309 }
310
312 const Node& proxiedNode () const
313 {
314 return *_node;
315 }
316
318 template<bool enabled = !proxiedNodeIsConst>
319 typename std::enable_if<enabled,std::shared_ptr<Node> >::type
321 {
322 return _node;
323 }
324
326 std::shared_ptr<const Node> proxiedNodeStorage () const
327 {
328 return _node;
329 }
330
332
335
336 ProxyNode (Node& node)
337 : _node(stackobject_to_shared_ptr(node))
338 {}
339
340 ProxyNode (std::shared_ptr<Node> node)
341 : _node(std::move(node))
342 {}
343
345
346 private:
347
348 std::shared_ptr<Node> _node;
349 };
350
352
353 } // namespace TypeTree
354} //namespace Dune
355
356#endif // DUNE_TYPETREE_PROXYNODE_HH
Mixin class providing methods for child access with run-time parameter.
Definition: proxynode.hh:133
auto & child(std::size_t i)
Returns the i-th child.
Definition: proxynode.hh:162
auto childStorage(std::size_t i) const
Returns the storage of the i-th child (const version).
Definition: proxynode.hh:194
void setChild(std::size_t i, ProxyChild &&child, typename std::enable_if< enabled, void * >::type=0)
Sets the i-th child to the passed-in value.
Definition: proxynode.hh:201
const auto & child(std::size_t i) const
Returns the i-th child (const version).
Definition: proxynode.hh:171
auto childStorage(std::size_t i)
Returns the storage of the i-th child.
Definition: proxynode.hh:182
Base class for nodes acting as a proxy for an existing node.
Definition: proxynode.hh:255
static const bool isComposite
Mark this class as a composite in the dune-typetree.
Definition: proxynode.hh:281
static const bool isLeaf
Mark this class as non leaf in the dune-typetree.
Definition: proxynode.hh:275
static const bool isPower
Mark this class as a non power in the dune-typetree.
Definition: proxynode.hh:278
std::enable_if< enabled, Node & >::type proxiedNode()
Returns the proxied node.
Definition: proxynode.hh:306
std::shared_ptr< const Node > proxiedNodeStorage() const
Returns the storage of the proxied node (const version).
Definition: proxynode.hh:326
std::enable_if< enabled, std::shared_ptr< Node > >::type proxiedNodeStorage()
Returns the storage of the proxied node.
Definition: proxynode.hh:320
const Node & proxiedNode() const
Returns the proxied node (const version).
Definition: proxynode.hh:312
Mixin class providing methods for child access with compile-time parameter.
Definition: proxynode.hh:30
auto & child(index_constant< k >={})
Returns the i-th child.
Definition: proxynode.hh:71
void setChild(ProxyChild &&child, typename std::enable_if< lazy_enabled< k >::value, void * >::type=0)
Sets the i-th child to the passed-in value.
Definition: proxynode.hh:112
auto childStorage(index_constant< k >={})
Returns the storage of the i-th child.
Definition: proxynode.hh:92
const auto & child(index_constant< k >={}) const
Returns the i-th child (const version).
Definition: proxynode.hh:81
auto childStorage(index_constant< k >={}) const
Returns the storage of the i-th child (const version).
Definition: proxynode.hh:105
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: indices.hh:29
typename detected_or< nonesuch, Op, Args... >::value_t is_detected
Detects whether Op<Args...> is valid.
Definition: type_traits.hh:145
std::size_t degree(const Node &node)
Returns the degree of node as run time information.
Definition: nodeinterface.hh:79
typename std::decay_t< Node >::NodeTag NodeTag
Returns the node tag of the given Node.
Definition: nodeinterface.hh:70
Dune namespace.
Definition: alignedallocator.hh:13
std::shared_ptr< T > stackobject_to_shared_ptr(T &t)
Create a shared_ptr for a stack-allocated object.
Definition: shared_ptr.hh:72
STL namespace.
This file implements several utilities related to std::shared_ptr.
Tag designating a composite node.
Definition: nodetags.hh:27
Tag designating a power node with runtime degree.
Definition: nodetags.hh:24
Tag designating a leaf node.
Definition: nodetags.hh:18
Tag designating a power node.
Definition: nodetags.hh:21
Tag-based dispatch to appropriate base class that provides necessary functionality.
Definition: proxynode.hh:210
Access to the type and storage type of the i-th child.
Definition: proxynode.hh:60
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 23, 23:29, 2024)