dune-typetree  2.4-dev
childextraction.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=8 sw=2 sts=2:
3 
4 #ifndef DUNE_TYPETREE_CHILDEXTRACTION_HH
5 #define DUNE_TYPETREE_CHILDEXTRACTION_HH
6 
7 #include <dune/common/documentation.hh>
8 #include <dune/common/typetraits.hh>
9 #include <dune/common/shared_ptr.hh>
10 
12 
13 
14 namespace Dune {
15  namespace TypeTree {
16 
17 
22 
24  template<typename Node, typename TreePath>
26  {
27 
29  typedef typename extract_child_type<
30  typename Node::template Child<TypeTree::TreePathFront<TreePath>::value>::Type,
33 
35  typedef typename extract_child_type<
36  typename Node::template Child<TypeTree::TreePathFront<TreePath>::value>::Type,
39 
41  typedef typename extract_child_type<
42  typename Node::template Child<TypeTree::TreePathFront<TreePath>::value>::Type,
45 
46 
47  };
48 
49 #ifndef DOXYGEN
50 
51  // end of recursion
52  template<typename Node>
53  struct extract_child_type<Node,TypeTree::TreePath<> >
54  {
55  typedef Node type;
56  typedef shared_ptr<Node> storage_type;
57  typedef shared_ptr<const Node> const_storage_type;
58  };
59 
60 #endif // DOXYGEN
61 
62 
63 
64 #ifdef DOXYGEN
65 
67 
90  template<typename Node, typename TreePath>
91  ImplementationDefined& extract_child(Node& node, Treepath tp)
92  {}
93 
95 
118  template<typename Node, typename TreePath>
119  const ImplementationDefined& extract_child(const Node& node, Treepath tp)
120  {}
121 
122 #else // DOXYGEN
123 
124  // ********************************************************************************
125  // non-const implementation
126  // ********************************************************************************
127 
128  template<typename Node, typename TreePath>
129  typename enable_if<
131  typename extract_child_type<Node,TreePath>::type&
132  >::type
133  extract_child(Node& node, TreePath tp)
134  {
135  return extract_child(node.template child<TypeTree::TreePathFront<TreePath>::value>(),
137  }
138 
139  template<typename Node, typename TreePath>
140  typename enable_if<
141  TypeTree::TreePathSize<TreePath>::value == 1,
142  typename Node::template Child<TypeTree::TreePathFront<TreePath>::value>::Type&
143  >::type
144  extract_child(Node& node, TreePath tp)
145  {
146  return node.template child<TypeTree::TreePathFront<TreePath>::value>();
147  }
148 
149  template<typename Node, typename TreePath>
150  typename enable_if<
151  TypeTree::TreePathSize<TreePath>::value == 0,
152  Node&
153  >::type
154  extract_child(Node& node, TreePath tp)
155  {
156  return node;
157  }
158 
159  // ********************************************************************************
160  // const implementation
161  // ********************************************************************************
162 
163  template<typename Node, typename TreePath>
164  typename enable_if<
165  (TypeTree::TreePathSize<TreePath>::value > 1),
166  const typename extract_child_type<Node,TreePath>::type&
167  >::type
168  extract_child(const Node& node, TreePath tp)
169  {
170  return extract_child(node.template child<TypeTree::TreePathFront<TreePath>::value>(),
171  typename TypeTree::TreePathPopFront<TreePath>::type());
172  }
173 
174  template<typename Node, typename TreePath>
175  typename enable_if<
176  TypeTree::TreePathSize<TreePath>::value == 1,
177  const typename Node::template Child<TypeTree::TreePathFront<TreePath>::value>::Type&
178  >::type
179  extract_child(const Node& node, TreePath tp)
180  {
181  return node.template child<TypeTree::TreePathFront<TreePath>::value>();
182  }
183 
184  template<typename Node, typename TreePath>
185  typename enable_if<
186  TypeTree::TreePathSize<TreePath>::value == 0,
187  const Node&
188  >::type
189  extract_child(const Node& node, TreePath tp)
190  {
191  return node;
192  }
193 
194 
195 #endif // DOXYGEN
196 
197 
198 
199 #ifdef DOXYGEN
200 
203 
226  template<typename Node, typename TreePath>
227  ImplementationDefined extract_child_storage(Node& node, Treepath tp)
228  {}
229 
232 
255  template<typename Node, typename TreePath>
256  ImplementationDefined extract_child_storage(const Node& node, Treepath tp)
257  {}
258 
259 #else // DOXYGEN
260 
261  // ********************************************************************************
262  // non-const implementation
263  // ********************************************************************************
264 
265  template<typename Node, typename TreePath>
266  typename enable_if<
267  (TypeTree::TreePathSize<TreePath>::value > 1),
268  typename extract_child_type<Node,TreePath>::storage_type
269  >::type
270  extract_child_storage(Node& node, TreePath tp)
271  {
274  }
275 
276  template<typename Node, typename TreePath>
277  typename enable_if<
278  TypeTree::TreePathSize<TreePath>::value == 1,
279  typename Node::template Child<TypeTree::TreePathFront<TreePath>::value>::Storage&
280  >::type
281  extract_child_storage(Node& node, TreePath tp)
282  {
283  return node.template childStorage<TypeTree::TreePathFront<TreePath>::value>();
284  }
285 
286  template<typename Node, typename TreePath>
287  typename enable_if<
288  TypeTree::TreePathSize<TreePath>::value == 0
289  >::type
290  extract_child_storage(Node& node, TreePath tp)
291  {
292  static_assert((Dune::AlwaysFalse<Node>::value),
293  "extract_child_storage only works for real children, not the node itself.");
294  }
295 
296  // ********************************************************************************
297  // const implementation
298  // ********************************************************************************
299 
300  template<typename Node, typename TreePath>
301  typename enable_if<
302  (TypeTree::TreePathSize<TreePath>::value > 1),
303  typename extract_child_type<Node,TreePath>::const_storage_type
304  >::type
305  extract_child_storage(const Node& node, TreePath tp)
306  {
307  return extract_child_storage(node.template child<TypeTree::TreePathFront<TreePath>::value>(),
308  typename TypeTree::TreePathPopFront<TreePath>::type());
309  }
310 
311  template<typename Node, typename TreePath>
312  typename enable_if<
313  TypeTree::TreePathSize<TreePath>::value == 1,
314  typename Node::template Child<TypeTree::TreePathFront<TreePath>::value>::ConstStorage
315  >::type
316  extract_child_storage(const Node& node, TreePath tp)
317  {
318  return node.template childStorage<TypeTree::TreePathFront<TreePath>::value>();
319  }
320 
321  template<typename Node, typename TreePath>
322  typename enable_if<
323  TypeTree::TreePathSize<TreePath>::value == 0
324  >::type
325  extract_child_storage(const Node& node, TreePath tp)
326  {
327  static_assert((Dune::AlwaysFalse<Node>::value),
328  "extract_child_storage only works for real children, not the node itself.");
329  }
330 
331 
332 #endif // DOXYGEN
333 
334 
336 
337  } // namespace TypeTree
338 } //namespace Dune
339 
340 #endif // DUNE_TYPETREE_CHILDEXTRACTION_HH
extract_child_type< typename Node::template Child< TypeTree::TreePathFront< TreePath >::value >::Type, typename TypeTree::TreePathPopFront< TreePath >::type >::type type
The type of the child.
Definition: childextraction.hh:32
Definition: accumulate_static.hh:12
extract_child_type< typename Node::template Child< TypeTree::TreePathFront< TreePath >::value >::Type, typename TypeTree::TreePathPopFront< TreePath >::type >::const_storage_type const_storage_type
The const storage type of the child.
Definition: childextraction.hh:44
ImplementationDefined & extract_child(Node &node, Treepath tp)
Extract the child of a node located at tp (non-const version).
Definition: childextraction.hh:91
extract_child_type< typename Node::template Child< TypeTree::TreePathFront< TreePath >::value >::Type, typename TypeTree::TreePathPopFront< TreePath >::type >::storage_type storage_type
The storage type of the child.
Definition: childextraction.hh:38
ImplementationDefined extract_child_storage(Node &node, Treepath tp)
Definition: childextraction.hh:227
Definition: treepath.hh:106
Type
Definition: treepath.hh:25
Definition: treepath.hh:81
Extract the type of the child of Node at position TreePath.
Definition: childextraction.hh:25
Definition: treepath.hh:29
Definition: treepath.hh:36