dune-typetree  2.3.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 
8 #include <dune/common/shared_ptr.hh>
9 
10 namespace Dune {
11  namespace TypeTree {
12 
18  template<typename Node>
19  class ProxyNode;
20 
22  template<typename ProxiedNode>
24  {
25 
26  static const bool proxiedNodeIsConst = IsConst<typename remove_reference<ProxiedNode>::type>::value;
27 
28  template<std::size_t k>
29  struct lazy_enabled
30  {
31  static const bool value = !proxiedNodeIsConst;
32  };
33 
35 
36  template<bool enabled = !proxiedNodeIsConst>
37  typename enable_if<enabled,Node&>::type
38  node()
39  {
40  return static_cast<Node&>(*this);
41  }
42 
43  const Node& node() const
44  {
45  return static_cast<const Node&>(*this);
46  }
47 
48  public:
49 
51  template<std::size_t k>
52  struct Child
53  : public ProxiedNode::template Child<k>
54  {};
55 
58 
60 
63  template<std::size_t k>
64  typename enable_if<lazy_enabled<k>::value,typename Child<k>::Type&>::type
66  {
67  return node().proxiedNode().template child<k>();
68  }
69 
71 
74  template<std::size_t k>
75  const typename Child<k>::Type& child() const
76  {
77  return node().proxiedNode().template child<k>();
78  }
79 
81 
84  template<std::size_t k>
85  typename enable_if<lazy_enabled<k>::value,typename Child<k>::Storage>::type
87  {
88  return node().proxiedNode().template childStorage<k>();
89  }
90 
92 
98  template<std::size_t k>
99  typename Child<k>::ConstStorage childStorage() const
100  {
101  return node().proxiedNode().template childStorage<k>();
102  }
103 
105  template<std::size_t k>
106  void setChild(typename Child<k>::type& child, typename enable_if<lazy_enabled<k>::value,void*>::type = 0)
107  {
108  node().proxiedNode().template childStorage<k>() = stackobject_to_shared_ptr(child);
109  }
110 
112  template<std::size_t k>
113  void setChild(typename Child<k>::storage_type child, typename enable_if<lazy_enabled<k>::value,void*>::type = 0)
114  {
115  node().proxiedNode().template childStorage<k>() = child;
116  }
117 
118  const typename ProxiedNode::NodeStorage& nodeStorage() const
119  {
120  return node().proxiedNode().nodeStorage();
121  }
122 
123  };
124 
126 
131  template<typename ProxiedNode>
133  : public StaticChildAccessors<ProxiedNode>
134  {
135 
137 
138  static const bool proxiedNodeIsConst = IsConst<typename remove_reference<ProxiedNode>::type>::value;
139 
140  template<bool enabled = !proxiedNodeIsConst>
141  typename enable_if<enabled,Node&>::type
142  node()
143  {
144  return static_cast<Node&>(*this);
145  }
146 
147  const Node& node() const
148  {
149  return static_cast<const Node&>(*this);
150  }
151 
152  public:
153 
156 
158 
161  template<bool enabled = !proxiedNodeIsConst>
162  typename enable_if<enabled,typename ProxiedNode::ChildType&>::type
163  child (std::size_t i)
164  {
165  return node().proxiedNode().child(i);
166  }
167 
169 
172  const typename ProxiedNode::ChildType& child (std::size_t i) const
173  {
174  return node().proxiedNode().child(i);
175  }
176 
178 
181  template<bool enabled = !proxiedNodeIsConst>
182  typename enable_if<enabled,typename ProxiedNode::ChildStorageType>::type
183  childStorage(std::size_t i)
184  {
185  return node().proxiedNode().childStorage(i);
186  }
187 
189 
195  typename ProxiedNode::ChildConstStorageType childStorage (std::size_t i) const
196  {
197  return node().proxiedNode().childStorage(i);
198  }
199 
201  template<bool enabled = !proxiedNodeIsConst>
202  void setChild (std::size_t i, typename ProxiedNode::ChildType& t, typename enable_if<enabled,void*>::type = 0)
203  {
204  node().proxiedNode().childStorage(i) = stackobject_to_shared_ptr(t);
205  }
206 
208  template<bool enabled = !proxiedNodeIsConst>
209  void setChild (std::size_t i, typename ProxiedNode::ChildStorageType st, typename enable_if<enabled,void*>::type = 0)
210  {
211  node().proxiedNode().childStorage(i) = st;
212  }
213 
214  };
215 
217  template<typename Node, typename NodeTag>
219 
221  template<typename Node>
223  {
224  };
225 
227  template<typename Node>
229  : public StaticChildAccessors<Node>
230  {
231  typedef typename Node::ChildTypes ChildTypes;
232  typedef typename Node::NodeStorage NodeStorage;
233  };
234 
236  template<typename Node>
238  : public StaticChildAccessors<Node>
239  {
240  typedef typename Node::ChildTypes ChildTypes;
241  typedef typename Node::NodeStorage NodeStorage;
242  };
243 
245  template<typename Node>
247  : public DynamicChildAccessors<Node>
248  {
249  typedef typename Node::ChildType ChildType;
250  typedef typename Node::NodeStorage NodeStorage;
251  };
252 
253 
255 
261  template<typename Node>
262  class ProxyNode
263  : public ProxyNodeBase<Node,typename Node::NodeTag>
264  {
265 
266  static const bool proxiedNodeIsConst = IsConst<typename remove_reference<Node>::type>::value;
267 
268  // accessor mixins need to be friends for access to proxiedNode()
269  friend class StaticChildAccessors<Node>;
270  friend class DynamicChildAccessors<Node>;
271 
272  public:
273 
274  typedef Node ProxiedNode;
275 
276  typedef typename Node::NodeTag NodeTag;
277 
279  static const bool isLeaf = Node::isLeaf;
280 
282  static const bool isPower = Node::isPower;
283 
285  static const bool isComposite = Node::isComposite;
286 
288  static const std::size_t CHILDREN = Node::CHILDREN;
289 
290 
291  protected:
292 
295 
297  template<bool enabled = !proxiedNodeIsConst>
298  typename enable_if<enabled,Node&>::type
300  {
301  return *_node;
302  }
303 
305  const Node& proxiedNode() const
306  {
307  return *_node;
308  }
309 
311  template<bool enabled = !proxiedNodeIsConst>
312  typename enable_if<enabled,shared_ptr<Node> >::type
314  {
315  return _node;
316  }
317 
319  shared_ptr<const Node> proxiedNodeStorage() const
320  {
321  return _node;
322  }
323 
325 
328 
329  ProxyNode(Node& node)
330  : _node(stackobject_to_shared_ptr(node))
331  {}
332 
333  ProxyNode(shared_ptr<Node> node)
334  : _node(node)
335  {}
336 
338 
339  private:
340 
341  shared_ptr<Node> _node;
342  };
343 
345 
346  } // namespace TypeTree
347 } //namespace Dune
348 
349 #endif // DUNE_TYPETREE_PROXYNODE_HH
Mixin class providing methods for child access with run-time parameter.
Definition: proxynode.hh:132
void setChild(typename Child< k >::storage_type child, typename 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:113
Node::ChildTypes ChildTypes
Definition: proxynode.hh:231
Node::NodeStorage NodeStorage
Definition: proxynode.hh:232
void setChild(std::size_t i, typename ProxiedNode::ChildStorageType st, typename enable_if< enabled, void * >::type=0)
Sets the stored value representing the i-th child to the passed-in value.
Definition: proxynode.hh:209
Node::NodeStorage NodeStorage
Definition: proxynode.hh:241
Tag designating a power node.
Definition: nodetags.hh:19
static const bool isComposite
Mark this class as a composite in the dune-typetree.
Definition: proxynode.hh:285
shared_ptr< const Node > proxiedNodeStorage() const
Returns the storage of the proxied node (const version).
Definition: proxynode.hh:319
ProxyNode(shared_ptr< Node > node)
Definition: proxynode.hh:333
Node::ChildType ChildType
Definition: proxynode.hh:249
ProxiedNode::ChildConstStorageType childStorage(std::size_t i) const
Returns the storage of the i-th child (const version).
Definition: proxynode.hh:195
Node::NodeStorage NodeStorage
Definition: proxynode.hh:250
static const std::size_t CHILDREN
The number of children.
Definition: proxynode.hh:288
const Node & proxiedNode() const
Returns the proxied node (const version).
Definition: proxynode.hh:305
Mixin class providing methods for child access with compile-time parameter.
Definition: proxynode.hh:23
enable_if< enabled, Node & >::type proxiedNode()
Returns the proxied node.
Definition: proxynode.hh:299
Node::ChildTypes ChildTypes
Definition: proxynode.hh:240
enable_if< enabled, shared_ptr< Node > >::type proxiedNodeStorage()
Returns the storage of the proxied node.
Definition: proxynode.hh:313
Tag-based dispatch to appropiate base class that provides necessary functionality.
Definition: proxynode.hh:218
void setChild(typename Child< k >::type &child, typename enable_if< lazy_enabled< k >::value, void * >::type=0)
Sets the i-th child to the passed-in value.
Definition: proxynode.hh:106
Tag designating a composite node that is based on variadic templates.
Definition: nodetags.hh:25
static const bool isLeaf
Mark this class as non leaf in the dune-typetree.
Definition: proxynode.hh:279
Access to the type and storage type of the i-th child.
Definition: proxynode.hh:52
Node::NodeTag NodeTag
Definition: proxynode.hh:276
enable_if< enabled, typename ProxiedNode::ChildStorageType >::type childStorage(std::size_t i)
Returns the storage of the i-th child.
Definition: proxynode.hh:183
const ProxiedNode::NodeStorage & nodeStorage() const
Definition: proxynode.hh:118
Node ProxiedNode
Definition: proxynode.hh:274
Child< k >::ConstStorage childStorage() const
Returns the storage of the i-th child (const version).
Definition: proxynode.hh:99
const Child< k >::Type & child() const
Returns the i-th child (const version).
Definition: proxynode.hh:75
void setChild(std::size_t i, typename ProxiedNode::ChildType &t, typename enable_if< enabled, void * >::type=0)
Sets the i-th child to the passed-in value.
Definition: proxynode.hh:202
enable_if< enabled, typename ProxiedNode::ChildType & >::type child(std::size_t i)
Returns the i-th child.
Definition: proxynode.hh:163
ProxyNode(Node &node)
Definition: proxynode.hh:329
Base class for nodes acting as a proxy for an existing node.
Definition: proxynode.hh:19
static const std::size_t value
Definition: compositenode.hh:38
enable_if< lazy_enabled< k >::value, typename Child< k >::Type & >::type child()
Returns the i-th child.
Definition: proxynode.hh:65
const ProxiedNode::ChildType & child(std::size_t i) const
Returns the i-th child (const version).
Definition: proxynode.hh:172
enable_if< lazy_enabled< k >::value, typename Child< k >::Storage >::type childStorage()
Returns the storage of the i-th child.
Definition: proxynode.hh:86
static const bool isPower
Mark this class as a non power in the dune-typetree.
Definition: proxynode.hh:282
Type
Definition: treepath.hh:26
Tag designating a leaf node.
Definition: nodetags.hh:16
Tag designating a composite node.
Definition: nodetags.hh:22