dune-typetree  2.4.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>
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 DynamicChildAccessors<Node>
239  {
240  typedef typename Node::ChildType ChildType;
241  typedef typename Node::NodeStorage NodeStorage;
242  };
243 
244 
246 
252  template<typename Node>
253  class ProxyNode
254  : public ProxyNodeBase<Node,typename Node::NodeTag>
255  {
256 
257  static const bool proxiedNodeIsConst = IsConst<typename remove_reference<Node>::type>::value;
258 
259  // accessor mixins need to be friends for access to proxiedNode()
260  friend class StaticChildAccessors<Node>;
261  friend class DynamicChildAccessors<Node>;
262 
263  public:
264 
265  typedef Node ProxiedNode;
266 
267  typedef typename Node::NodeTag NodeTag;
268 
270  static const bool isLeaf = Node::isLeaf;
271 
273  static const bool isPower = Node::isPower;
274 
276  static const bool isComposite = Node::isComposite;
277 
279  static const std::size_t CHILDREN = Node::CHILDREN;
280 
281 
282  protected:
283 
286 
288  template<bool enabled = !proxiedNodeIsConst>
289  typename enable_if<enabled,Node&>::type
291  {
292  return *_node;
293  }
294 
296  const Node& proxiedNode() const
297  {
298  return *_node;
299  }
300 
302  template<bool enabled = !proxiedNodeIsConst>
303  typename enable_if<enabled,shared_ptr<Node> >::type
305  {
306  return _node;
307  }
308 
310  shared_ptr<const Node> proxiedNodeStorage() const
311  {
312  return _node;
313  }
314 
316 
319 
320  ProxyNode(Node& node)
321  : _node(stackobject_to_shared_ptr(node))
322  {}
323 
324  ProxyNode(shared_ptr<Node> node)
325  : _node(node)
326  {}
327 
329 
330  private:
331 
332  shared_ptr<Node> _node;
333  };
334 
336 
337  } // namespace TypeTree
338 } //namespace Dune
339 
340 #endif // DUNE_TYPETREE_PROXYNODE_HH
Mixin class providing methods for child access with run-time parameter.
Definition: proxynode.hh:132
const Child< k >::Type & child() const
Returns the i-th child (const version).
Definition: proxynode.hh:75
static const bool isLeaf
Mark this class as non leaf in the dune-typetree.
Definition: proxynode.hh:270
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
Tag designating a leaf node.
Definition: nodetags.hh:16
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:568
static const std::size_t CHILDREN
The number of children.
Definition: proxynode.hh:279
ProxyNode(Node &node)
Definition: proxynode.hh:320
Tag-based dispatch to appropiate base class that provides necessary functionality.
Definition: proxynode.hh:218
ProxiedNode::ChildConstStorageType childStorage(std::size_t i) const
Returns the storage of the i-th child (const version).
Definition: proxynode.hh:195
const Node & proxiedNode() const
Returns the proxied node (const version).
Definition: proxynode.hh:296
enable_if< enabled, shared_ptr< Node > >::type proxiedNodeStorage()
Returns the storage of the proxied node.
Definition: proxynode.hh:304
static const bool isPower
Mark this class as a non power in the dune-typetree.
Definition: proxynode.hh:273
Node::ChildType ChildType
Definition: proxynode.hh:240
Base class for nodes acting as a proxy for an existing node.
Definition: proxynode.hh:19
enable_if< lazy_enabled< k >::value, typename Child< k >::Type & >::type child()
Returns the i-th child.
Definition: proxynode.hh:65
Node::NodeStorage NodeStorage
Definition: proxynode.hh:241
Node::ChildTypes ChildTypes
Definition: proxynode.hh:231
enable_if< lazy_enabled< k >::value, typename Child< k >::Storage >::type childStorage()
Returns the storage of the i-th child.
Definition: proxynode.hh:86
Access to the type and storage type of the i-th child.
Definition: proxynode.hh:52
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
enable_if< enabled, typename ProxiedNode::ChildStorageType >::type childStorage(std::size_t i)
Returns the storage of the i-th child.
Definition: proxynode.hh:183
shared_ptr< const Node > proxiedNodeStorage() const
Returns the storage of the proxied node (const version).
Definition: proxynode.hh:310
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 ProxiedNode
Definition: proxynode.hh:265
const ProxiedNode::NodeStorage & nodeStorage() const
Definition: proxynode.hh:118
Mixin class providing methods for child access with compile-time parameter.
Definition: proxynode.hh:23
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
Child< k >::ConstStorage childStorage() const
Returns the storage of the i-th child (const version).
Definition: proxynode.hh:99
static const bool isComposite
Mark this class as a composite in the dune-typetree.
Definition: proxynode.hh:276
Tag designating a power node.
Definition: nodetags.hh:19
ProxyNode(shared_ptr< Node > node)
Definition: proxynode.hh:324
enable_if< enabled, typename ProxiedNode::ChildType & >::type child(std::size_t i)
Returns the i-th child.
Definition: proxynode.hh:163
enable_if< enabled, Node & >::type proxiedNode()
Returns the proxied node.
Definition: proxynode.hh:290
Definition: accumulate_static.hh:12
const ProxiedNode::ChildType & child(std::size_t i) const
Returns the i-th child (const version).
Definition: proxynode.hh:172
Node::NodeTag NodeTag
Definition: proxynode.hh:267
Node::NodeStorage NodeStorage
Definition: proxynode.hh:232
Tag designating a composite node.
Definition: nodetags.hh:22