dune-typetree  2.5-dev
proxynode.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 
4 #ifndef DUNE_TYPETREE_PROXYNODE_HH
5 #define DUNE_TYPETREE_PROXYNODE_HH
6 
7 #include <type_traits>
10 #include <dune/common/shared_ptr.hh>
11 
12 namespace Dune {
13  namespace TypeTree {
14 
20  template<typename Node>
21  class ProxyNode;
22 
24  template<typename ProxiedNode>
26  {
27 
28  static const bool proxiedNodeIsConst = std::is_const<typename std::remove_reference<ProxiedNode>::type>::value;
29 
30  template<std::size_t k>
31  struct lazy_enabled
32  {
33  static const bool value = !proxiedNodeIsConst;
34  };
35 
37 
38  template<bool enabled = !proxiedNodeIsConst>
39  typename std::enable_if<enabled,Node&>::type
40  node()
41  {
42  return static_cast<Node&>(*this);
43  }
44 
45  const Node& node() const
46  {
47  return static_cast<const Node&>(*this);
48  }
49 
50  public:
51 
53  template<std::size_t k>
54  struct Child
55  : public ProxiedNode::template Child<k>
56  {};
57 
60 
62 
65  template<std::size_t k>
66  typename std::enable_if<lazy_enabled<k>::value,typename Child<k>::Type&>::type
68  {
69  return node().proxiedNode().template child<k>();
70  }
71 
73 
76  template<std::size_t k>
77  const typename Child<k>::Type& child() const
78  {
79  return node().proxiedNode().template child<k>();
80  }
81 
83 
86  template<std::size_t k>
87  typename std::enable_if<lazy_enabled<k>::value,typename Child<k>::Storage>::type
89  {
90  return node().proxiedNode().template childStorage<k>();
91  }
92 
94 
100  template<std::size_t k>
102  {
103  return node().proxiedNode().template childStorage<k>();
104  }
105 
107  template<std::size_t k>
108  void setChild(typename Child<k>::type& child, typename std::enable_if<lazy_enabled<k>::value,void*>::type = 0)
109  {
110  node().proxiedNode().template childStorage<k>() = stackobject_to_shared_ptr(child);
111  }
112 
114  template<std::size_t k>
115  void setChild(typename Child<k>::storage_type child, typename std::enable_if<lazy_enabled<k>::value,void*>::type = 0)
116  {
117  node().proxiedNode().template childStorage<k>() = child;
118  }
119 
120  const typename ProxiedNode::NodeStorage& nodeStorage() const
121  {
122  return node().proxiedNode().nodeStorage();
123  }
124 
125  };
126 
128 
133  template<typename ProxiedNode>
135  : public StaticChildAccessors<ProxiedNode>
136  {
137 
139 
140  static const bool proxiedNodeIsConst = std::is_const<typename std::remove_reference<ProxiedNode>::type>::value;
141 
142  template<bool enabled = !proxiedNodeIsConst>
143  typename std::enable_if<enabled,Node&>::type
144  node()
145  {
146  return static_cast<Node&>(*this);
147  }
148 
149  const Node& node() const
150  {
151  return static_cast<const Node&>(*this);
152  }
153 
154  public:
155 
158 
160 
163  template<bool enabled = !proxiedNodeIsConst>
164  typename std::enable_if<enabled,typename ProxiedNode::ChildType&>::type
165  child (std::size_t i)
166  {
167  return node().proxiedNode().child(i);
168  }
169 
171 
174  const typename ProxiedNode::ChildType& child (std::size_t i) const
175  {
176  return node().proxiedNode().child(i);
177  }
178 
180 
183  template<bool enabled = !proxiedNodeIsConst>
184  typename std::enable_if<enabled,typename ProxiedNode::ChildStorageType>::type
185  childStorage(std::size_t i)
186  {
187  return node().proxiedNode().childStorage(i);
188  }
189 
191 
197  typename ProxiedNode::ChildConstStorageType childStorage (std::size_t i) const
198  {
199  return node().proxiedNode().childStorage(i);
200  }
201 
203  template<bool enabled = !proxiedNodeIsConst>
204  void setChild (std::size_t i, typename ProxiedNode::ChildType& t, typename std::enable_if<enabled,void*>::type = 0)
205  {
206  node().proxiedNode().childStorage(i) = stackobject_to_shared_ptr(t);
207  }
208 
210  template<bool enabled = !proxiedNodeIsConst>
211  void setChild (std::size_t i, typename ProxiedNode::ChildStorageType st, typename std::enable_if<enabled,void*>::type = 0)
212  {
213  node().proxiedNode().childStorage(i) = st;
214  }
215 
216  };
217 
219  template<typename Node, typename NodeTag>
221 
223  template<typename Node>
225  {
226  };
227 
229  template<typename Node>
231  : public StaticChildAccessors<Node>
232  {
233  typedef typename Node::ChildTypes ChildTypes;
234  typedef typename Node::NodeStorage NodeStorage;
235  };
236 
238  template<typename Node>
240  : public DynamicChildAccessors<Node>
241  {
242  typedef typename Node::ChildType ChildType;
243  typedef typename Node::NodeStorage NodeStorage;
244  };
245 
246 
248 
254  template<typename Node>
255  class ProxyNode
256  : public ProxyNodeBase<Node,NodeTag<Node>>
257  {
258 
259  static const bool proxiedNodeIsConst = std::is_const<typename std::remove_reference<Node>::type>::value;
260 
261  // accessor mixins need to be friends for access to proxiedNode()
262  friend class StaticChildAccessors<Node>;
263  friend class DynamicChildAccessors<Node>;
264 
265  public:
266 
267  typedef Node ProxiedNode;
268 
270 
272  static const bool isLeaf = Node::isLeaf;
273 
275  static const bool isPower = Node::isPower;
276 
278  static const bool isComposite = Node::isComposite;
279 
281  static const std::size_t CHILDREN = StaticDegree<Node>::value;
282 
283  static constexpr std::size_t degree()
284  {
286  }
287 
288 
289  protected:
290 
293 
295  template<bool enabled = !proxiedNodeIsConst>
296  typename std::enable_if<enabled,Node&>::type
298  {
299  return *_node;
300  }
301 
303  const Node& proxiedNode() const
304  {
305  return *_node;
306  }
307 
309  template<bool enabled = !proxiedNodeIsConst>
310  typename std::enable_if<enabled,shared_ptr<Node> >::type
312  {
313  return _node;
314  }
315 
317  shared_ptr<const Node> proxiedNodeStorage() const
318  {
319  return _node;
320  }
321 
323 
326 
327  ProxyNode(Node& node)
328  : _node(stackobject_to_shared_ptr(node))
329  {}
330 
331  ProxyNode(shared_ptr<Node> node)
332  : _node(node)
333  {}
334 
336 
337  private:
338 
339  shared_ptr<Node> _node;
340  };
341 
343 
344  } // namespace TypeTree
345 } //namespace Dune
346 
347 #endif // DUNE_TYPETREE_PROXYNODE_HH
std::enable_if< enabled, shared_ptr< Node > >::type proxiedNodeStorage()
Returns the storage of the proxied node.
Definition: proxynode.hh:311
Tag-based dispatch to appropriate base class that provides necessary functionality.
Definition: proxynode.hh:220
static const bool isLeaf
Mark this class as non leaf in the dune-typetree.
Definition: proxynode.hh:272
Node::NodeStorage NodeStorage
Definition: proxynode.hh:234
const Node & proxiedNode() const
Returns the proxied node (const version).
Definition: proxynode.hh:303
Tag designating a composite node.
Definition: nodetags.hh:22
std::enable_if< lazy_enabled< k >::value, typename Child< k >::Storage >::type childStorage()
Returns the storage of the i-th child.
Definition: proxynode.hh:88
void setChild(std::size_t i, typename ProxiedNode::ChildStorageType st, typename std::enable_if< enabled, void *>::type=0)
Sets the stored value representing the i-th child to the passed-in value.
Definition: proxynode.hh:211
void setChild(std::size_t i, typename ProxiedNode::ChildType &t, typename std::enable_if< enabled, void *>::type=0)
Sets the i-th child to the passed-in value.
Definition: proxynode.hh:204
static const bool isComposite
Mark this class as a composite in the dune-typetree.
Definition: proxynode.hh:278
static const bool isPower
Mark this class as a non power in the dune-typetree.
Definition: proxynode.hh:275
shared_ptr< const Node > proxiedNodeStorage() const
Returns the storage of the proxied node (const version).
Definition: proxynode.hh:317
Tag designating a power node.
Definition: nodetags.hh:19
typename impl::_Child< Node, indices... >::type Child
Template alias for the type of a child node given by a list of child indices.
Definition: childextraction.hh:307
Node::ChildType ChildType
Definition: proxynode.hh:242
Definition: accumulate_static.hh:13
Base class for nodes acting as a proxy for an existing node.
Definition: proxynode.hh:21
static constexpr std::size_t degree()
Definition: proxynode.hh:283
ProxyNode(Node &node)
Definition: proxynode.hh:327
const Child< k >::Type & child() const
Returns the i-th child (const version).
Definition: proxynode.hh:77
std::enable_if< enabled, typename ProxiedNode::ChildType & >::type child(std::size_t i)
Returns the i-th child.
Definition: proxynode.hh:165
std::enable_if< enabled, Node & >::type proxiedNode()
Returns the proxied node.
Definition: proxynode.hh:297
Node::ChildTypes ChildTypes
Definition: proxynode.hh:233
void setChild(typename Child< k >::storage_type child, typename std::enable_if< lazy_enabled< k >::value, void *>::type=0)
Sets the storage of the i-th child to the passed-in value.
Definition: proxynode.hh:115
const ProxiedNode::ChildType & child(std::size_t i) const
Returns the i-th child (const version).
Definition: proxynode.hh:174
Mixin class providing methods for child access with run-time parameter.
Definition: proxynode.hh:134
Mixin class providing methods for child access with compile-time parameter.
Definition: proxynode.hh:25
std::enable_if< lazy_enabled< k >::value, typename Child< k >::Type & >::type child()
Returns the i-th child.
Definition: proxynode.hh:67
Child< k >::ConstStorage childStorage() const
Returns the storage of the i-th child (const version).
Definition: proxynode.hh:101
Dune::TypeTree::NodeTag< Node > NodeTag
Definition: proxynode.hh:269
Node ProxiedNode
Definition: proxynode.hh:267
Node::NodeStorage NodeStorage
Definition: proxynode.hh:243
std::integral_constant< std::size_t, degree(static_cast< std::decay_t< Node > * >(nullptr), NodeTag< std::decay_t< Node >>()) > StaticDegree
Returns the statically known degree of the given Node type as a std::integral_constant.
Definition: nodeinterface.hh:105
void setChild(typename Child< k >::type &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:108
Tag designating a leaf node.
Definition: nodetags.hh:16
ProxiedNode::ChildConstStorageType childStorage(std::size_t i) const
Returns the storage of the i-th child (const version).
Definition: proxynode.hh:197
ProxyNode(shared_ptr< Node > node)
Definition: proxynode.hh:331
const ProxiedNode::NodeStorage & nodeStorage() const
Definition: proxynode.hh:120
Access to the type and storage type of the i-th child.
Definition: proxynode.hh:54
std::enable_if< enabled, typename ProxiedNode::ChildStorageType >::type childStorage(std::size_t i)
Returns the storage of the i-th child.
Definition: proxynode.hh:185
typename std::decay_t< Node >::NodeTag NodeTag
Returns the node tag of the given Node.
Definition: nodeinterface.hh:62