dune-typetree  2.7.1
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 #include <dune/common/indices.hh>
12 
13 namespace Dune {
14  namespace TypeTree {
15 
21  template<typename Node>
22  class ProxyNode;
23 
25  template<typename ProxiedNode>
27  {
28 
29  static const bool proxiedNodeIsConst = std::is_const<typename std::remove_reference<ProxiedNode>::type>::value;
30 
31  template<std::size_t k>
32  struct lazy_enabled
33  {
34  static const bool value = !proxiedNodeIsConst;
35  };
36 
38 
39  template<bool enabled = !proxiedNodeIsConst>
40  typename std::enable_if<enabled,Node&>::type
41  node()
42  {
43  return static_cast<Node&>(*this);
44  }
45 
46  const Node& node() const
47  {
48  return static_cast<const Node&>(*this);
49  }
50 
51  public:
52 
54  template<std::size_t k>
55  struct Child
56  : public ProxiedNode::template Child<k>
57  {};
58 
61 
63 
66  template<std::size_t k>
67  typename std::enable_if<lazy_enabled<k>::value,typename Child<k>::Type&>::type
68  child(Dune::index_constant<k> = {})
69  {
70  return node().proxiedNode().template child<k>();
71  }
72 
74 
77  template<std::size_t k>
78  const typename Child<k>::Type& child(Dune::index_constant<k> = {}) const
79  {
80  return node().proxiedNode().template child<k>();
81  }
82 
84 
87  template<std::size_t k>
88  typename std::enable_if<lazy_enabled<k>::value,typename Child<k>::Storage>::type
90  {
91  return node().proxiedNode().template childStorage<k>();
92  }
93 
95 
101  template<std::size_t k>
103  {
104  return node().proxiedNode().template childStorage<k>();
105  }
106 
108  template<std::size_t k>
109  void setChild(typename Child<k>::type& child, typename std::enable_if<lazy_enabled<k>::value,void*>::type = 0)
110  {
111  node().proxiedNode().template childStorage<k>() = stackobject_to_shared_ptr(child);
112  }
113 
115  template<std::size_t k>
116  void setChild(typename Child<k>::storage_type child, typename std::enable_if<lazy_enabled<k>::value,void*>::type = 0)
117  {
118  node().proxiedNode().template childStorage<k>() = child;
119  }
120 
121  const typename ProxiedNode::NodeStorage& nodeStorage() const
122  {
123  return node().proxiedNode().nodeStorage();
124  }
125 
126  };
127 
129 
134  template<typename ProxiedNode>
136  : public StaticChildAccessors<ProxiedNode>
137  {
138 
140 
141  static const bool proxiedNodeIsConst = std::is_const<typename std::remove_reference<ProxiedNode>::type>::value;
142 
143  template<bool enabled = !proxiedNodeIsConst>
144  typename std::enable_if<enabled,Node&>::type
145  node()
146  {
147  return static_cast<Node&>(*this);
148  }
149 
150  const Node& node() const
151  {
152  return static_cast<const Node&>(*this);
153  }
154 
155  public:
156 
159 
161 
164  template<bool enabled = !proxiedNodeIsConst>
165  typename std::enable_if<enabled,typename ProxiedNode::ChildType&>::type
166  child (std::size_t i)
167  {
168  return node().proxiedNode().child(i);
169  }
170 
172 
175  const typename ProxiedNode::ChildType& child (std::size_t i) const
176  {
177  return node().proxiedNode().child(i);
178  }
179 
181 
184  template<bool enabled = !proxiedNodeIsConst>
185  typename std::enable_if<enabled,typename ProxiedNode::ChildStorageType>::type
186  childStorage(std::size_t i)
187  {
188  return node().proxiedNode().childStorage(i);
189  }
190 
192 
198  typename ProxiedNode::ChildConstStorageType childStorage (std::size_t i) const
199  {
200  return node().proxiedNode().childStorage(i);
201  }
202 
204  template<bool enabled = !proxiedNodeIsConst>
205  void setChild (std::size_t i, typename ProxiedNode::ChildType& t, typename std::enable_if<enabled,void*>::type = 0)
206  {
207  node().proxiedNode().childStorage(i) = stackobject_to_shared_ptr(t);
208  }
209 
211  template<bool enabled = !proxiedNodeIsConst>
212  void setChild (std::size_t i, typename ProxiedNode::ChildStorageType st, typename std::enable_if<enabled,void*>::type = 0)
213  {
214  node().proxiedNode().childStorage(i) = st;
215  }
216 
217  };
218 
220  template<typename Node, typename NodeTag>
222 
224  template<typename Node>
226  {
227  };
228 
230  template<typename Node>
232  : public StaticChildAccessors<Node>
233  {
234  typedef typename Node::ChildTypes ChildTypes;
235  typedef typename Node::NodeStorage NodeStorage;
236  };
237 
239  template<typename Node>
241  : public DynamicChildAccessors<Node>
242  {
243  typedef typename Node::ChildType ChildType;
244  typedef typename Node::NodeStorage NodeStorage;
245  };
246 
247 
249 
255  template<typename Node>
256  class ProxyNode
257  : public ProxyNodeBase<Node,NodeTag<Node>>
258  {
259 
260  static const bool proxiedNodeIsConst = std::is_const<typename std::remove_reference<Node>::type>::value;
261 
262  // accessor mixins need to be friends for access to proxiedNode()
263  friend class StaticChildAccessors<Node>;
264  friend class DynamicChildAccessors<Node>;
265 
266  public:
267 
268  typedef Node ProxiedNode;
269 
271 
273  static const bool isLeaf = Node::isLeaf;
274 
276  static const bool isPower = Node::isPower;
277 
279  static const bool isComposite = Node::isComposite;
280 
282  static const std::size_t CHILDREN = StaticDegree<Node>::value;
283 
284  static constexpr std::size_t degree()
285  {
287  }
288 
289 
290  protected:
291 
294 
296  template<bool enabled = !proxiedNodeIsConst>
297  typename std::enable_if<enabled,Node&>::type
299  {
300  return *_node;
301  }
302 
304  const Node& proxiedNode() const
305  {
306  return *_node;
307  }
308 
310  template<bool enabled = !proxiedNodeIsConst>
311  typename std::enable_if<enabled,std::shared_ptr<Node> >::type
313  {
314  return _node;
315  }
316 
318  std::shared_ptr<const Node> proxiedNodeStorage() const
319  {
320  return _node;
321  }
322 
324 
327 
328  ProxyNode(Node& node)
329  : _node(stackobject_to_shared_ptr(node))
330  {}
331 
332  ProxyNode(std::shared_ptr<Node> node)
333  : _node(node)
334  {}
335 
337 
338  private:
339 
340  std::shared_ptr<Node> _node;
341  };
342 
344 
345  } // namespace TypeTree
346 } //namespace Dune
347 
348 #endif // DUNE_TYPETREE_PROXYNODE_HH
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:276
typename std::decay_t< Node >::NodeTag NodeTag
Returns the node tag of the given Node.
Definition: nodeinterface.hh:62
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
Definition: accumulate_static.hh:13
Tag designating a leaf node.
Definition: nodetags.hh:16
Tag designating a power node.
Definition: nodetags.hh:19
Tag designating a composite node.
Definition: nodetags.hh:22
Base class for nodes acting as a proxy for an existing node.
Definition: proxynode.hh:258
ProxyNode(Node &node)
Definition: proxynode.hh:328
const Node & proxiedNode() const
Returns the proxied node (const version).
Definition: proxynode.hh:304
static constexpr std::size_t degree()
Definition: proxynode.hh:284
Dune::TypeTree::NodeTag< Node > NodeTag
Definition: proxynode.hh:270
static const bool isComposite
Mark this class as a composite in the dune-typetree.
Definition: proxynode.hh:279
static const bool isLeaf
Mark this class as non leaf in the dune-typetree.
Definition: proxynode.hh:273
static const bool isPower
Mark this class as a non power in the dune-typetree.
Definition: proxynode.hh:276
std::enable_if< enabled, Node & >::type proxiedNode()
Returns the proxied node.
Definition: proxynode.hh:298
static const std::size_t CHILDREN
The number of children.
Definition: proxynode.hh:282
Node ProxiedNode
Definition: proxynode.hh:268
std::shared_ptr< const Node > proxiedNodeStorage() const
Returns the storage of the proxied node (const version).
Definition: proxynode.hh:318
std::enable_if< enabled, std::shared_ptr< Node > >::type proxiedNodeStorage()
Returns the storage of the proxied node.
Definition: proxynode.hh:312
ProxyNode(std::shared_ptr< Node > node)
Definition: proxynode.hh:332
Mixin class providing methods for child access with compile-time parameter.
Definition: proxynode.hh:27
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:116
const ProxiedNode::NodeStorage & nodeStorage() const
Definition: proxynode.hh:121
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:109
std::enable_if< lazy_enabled< k >::value, typename Child< k >::Type & >::type child(Dune::index_constant< k >={})
Returns the i-th child.
Definition: proxynode.hh:68
std::enable_if< lazy_enabled< k >::value, typename Child< k >::Storage >::type childStorage()
Returns the storage of the i-th child.
Definition: proxynode.hh:89
Child< k >::ConstStorage childStorage() const
Returns the storage of the i-th child (const version).
Definition: proxynode.hh:102
const Child< k >::Type & child(Dune::index_constant< k >={}) const
Returns the i-th child (const version).
Definition: proxynode.hh:78
Access to the type and storage type of the i-th child.
Definition: proxynode.hh:57
Mixin class providing methods for child access with run-time parameter.
Definition: proxynode.hh:137
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:212
std::enable_if< enabled, typename ProxiedNode::ChildType & >::type child(std::size_t i)
Returns the i-th child.
Definition: proxynode.hh:166
const ProxiedNode::ChildType & child(std::size_t i) const
Returns the i-th child (const version).
Definition: proxynode.hh:175
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:205
ProxiedNode::ChildConstStorageType childStorage(std::size_t i) const
Returns the storage of the i-th child (const version).
Definition: proxynode.hh:198
std::enable_if< enabled, typename ProxiedNode::ChildStorageType >::type childStorage(std::size_t i)
Returns the storage of the i-th child.
Definition: proxynode.hh:186
Tag-based dispatch to appropriate base class that provides necessary functionality.
Definition: proxynode.hh:221
Node::NodeStorage NodeStorage
Definition: proxynode.hh:235
Node::ChildTypes ChildTypes
Definition: proxynode.hh:234
Node::NodeStorage NodeStorage
Definition: proxynode.hh:244
Node::ChildType ChildType
Definition: proxynode.hh:243