dune-typetree  2.4.1
powernode.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_POWERNODE_HH
5 #define DUNE_TYPETREE_POWERNODE_HH
6 
7 #include <cassert>
8 #include <array>
9 
10 #include <dune/common/typetraits.hh>
11 
13 #include <dune/typetree/utility.hh>
15 
16 namespace Dune {
17  namespace TypeTree {
18 
24 #ifndef DOXYGEN
25 
26  namespace {
27 
28  // prototype and end of recursion
29  template<typename T, typename It, typename... Args>
30  void assign_reference_pack_to_shared_ptr_array_unpack(It it, Args&&... args) {}
31 
32  template<typename T, typename It, typename Arg, typename... Args>
33  void assign_reference_pack_to_shared_ptr_array_unpack(It it, Arg&& arg, Args&&... args)
34  {
35  static_assert(is_same<T,typename remove_const<typename remove_reference<Arg>::type>::type>::value,"type mismatch during array conversion");
36  *it = convert_arg(std::forward<Arg>(arg));
37  assign_reference_pack_to_shared_ptr_array_unpack<T>(++it,std::forward<Args>(args)...);
38  }
39 
40  template<typename T, std::size_t n, typename... Args>
41  void assign_reference_pack_to_shared_ptr_array(std::array<shared_ptr<T>,n>& res, Args&&... args)
42  {
43  static_assert(sizeof...(Args) == n, "invalid number of arguments");
44  return assign_reference_pack_to_shared_ptr_array_unpack<T>(res.begin(),std::forward<Args>(args)...);
45  }
46 
47 
48  // prototype and end of recursion
49  template<typename T, typename It, typename... Args>
50  void assign_shared_ptr_pack_to_shared_ptr_array_unpack(It it, Args&&... args) {}
51 
52  template<typename T, typename It, typename Arg, typename... Args>
53  void assign_shared_ptr_pack_to_shared_ptr_array_unpack(It it, Arg&& arg, Args&&... args)
54  {
55  static_assert(is_same<T,typename std::remove_reference<Arg>::type::element_type>::value,"type mismatch during array conversion");
56  *it = arg;
57  assign_shared_ptr_pack_to_shared_ptr_array_unpack<T>(++it,args...);
58  }
59 
60  template<typename T, std::size_t n, typename... Args>
61  void assign_shared_ptr_pack_to_shared_ptr_array(std::array<shared_ptr<T>,n>& res, Args&&... args)
62  {
63  static_assert(sizeof...(Args) == n, "invalid number of arguments");
64  return assign_shared_ptr_pack_to_shared_ptr_array_unpack<T>(res.begin(),args...);
65  }
66 
67  } // anonymous namespace
68 
69 #endif
70 
71 #ifndef DOXYGEN
72 
74  template<typename PowerNode, typename T, std::size_t k>
75  struct AssertPowerNodeChildCount
76  : public enable_if<is_same<
77  typename PowerNode::ChildType,
78  T>::value &&
79  PowerNode::CHILDREN == k,
80  T>
81  {};
82 
83 #endif
84 
90  template<typename T, std::size_t k>
91  class PowerNode
92  {
93 
94  public:
95 
97  static const bool isLeaf = false;
98 
100  static const bool isPower = true;
101 
103  static const bool isComposite = false;
104 
106  static const std::size_t CHILDREN = k;
107 
110 
112  typedef T ChildType;
113 
115  typedef shared_ptr<T> ChildStorageType;
116 
118  typedef shared_ptr<const T> ChildConstStorageType;
119 
121  typedef std::array<ChildStorageType,k> NodeStorage;
122 
123 
125  template<std::size_t i>
126  struct Child
127  {
128 
129  static_assert((i < CHILDREN), "child index out of range");
130 
132  typedef T Type;
133 
135  typedef T type;
136 
138  typedef ChildStorageType Storage;
139 
141  typedef ChildConstStorageType ConstStorage;
142  };
143 
146 
148 
151  template<std::size_t i>
153  {
154  static_assert((i < CHILDREN), "child index out of range");
155  return *_children[i];
156  }
157 
159 
162  template<std::size_t i>
163  const T& child (index_constant<i> = {}) const
164  {
165  static_assert((i < CHILDREN), "child index out of range");
166  return *_children[i];
167  }
168 
170 
173  template<std::size_t i>
174  ChildStorageType childStorage(index_constant<i> = {})
175  {
176  static_assert((i < CHILDREN), "child index out of range");
177  return _children[i];
178  }
179 
181 
187  template<std::size_t i>
188  ChildConstStorageType childStorage(index_constant<i> = {}) const
189  {
190  static_assert((i < CHILDREN), "child index out of range");
191  return _children[i];
192  }
193 
195  template<std::size_t i>
196  void setChild (T& t, index_constant<i> = {})
197  {
198  static_assert((i < CHILDREN), "child index out of range");
199  _children[i] = stackobject_to_shared_ptr(t);
200  }
201 
203  template<std::size_t i>
204  void setChild(T&& t, index_constant<i> = {})
205  {
206  static_assert((i < CHILDREN), "child index out of range");
207  _children[i] = convert_arg(std::move(t));
208  }
209 
211  template<std::size_t i>
212  void setChild (ChildStorageType st, index_constant<i> = {})
213  {
214  static_assert((i < CHILDREN), "child index out of range");
215  _children[i] = st;
216  }
217 
219 
220 
223 
225 
228  T& child (std::size_t i)
229  {
230  assert(i < CHILDREN && "child index out of range");
231  return *_children[i];
232  }
233 
235 
238  const T& child (std::size_t i) const
239  {
240  assert(i < CHILDREN && "child index out of range");
241  return *_children[i];
242  }
243 
245 
248  ChildStorageType childStorage(std::size_t i)
249  {
250  assert(i < CHILDREN && "child index out of range");
251  return _children[i];
252  }
253 
255 
261  ChildConstStorageType childStorage (std::size_t i) const
262  {
263  assert(i < CHILDREN && "child index out of range");
264  return (_children[i]);
265  }
266 
268  void setChild (std::size_t i, T& t)
269  {
270  assert(i < CHILDREN && "child index out of range");
271  _children[i] = stackobject_to_shared_ptr(t);
272  }
273 
275  void setChild(std::size_t i, T&& t)
276  {
277  assert(i < CHILDREN && "child index out of range");
278  _children[i] = convert_arg(std::move(t));
279  }
280 
282  void setChild (std::size_t i, ChildStorageType st)
283  {
284  assert(i < CHILDREN && "child index out of range");
285  _children[i] = st;
286  }
287 
288  const NodeStorage& nodeStorage() const
289  {
290  return _children;
291  }
292 
294 
297 
298  // The following two methods require a little bit of SFINAE trickery to work correctly:
299  // We have to make sure that they don't shadow the methods for direct child access because
300  // those get called by the generic child() machinery. If that machinery picks up the methods
301  // defined below, we have an infinite recursion.
302  // So the methods make sure that either
303  //
304  // * there are more than one argument. In that case, we got multiple indices and can forward
305  // to the general machine.
306  //
307  // * the first argument is not a valid flat index, i.e. either a std::size_t or an index_constant.
308  // The argument thus has to be some kind of TreePath instance that we can also pass to the
309  // generic machine.
310  //
311  // The above SFINAE logic works, but there is still a problem with the return type deduction.
312  // We have to do a lazy lookup of the return type after SFINAE has succeeded, otherwise the return
313  // type deduction will trigger the infinite recursion.
314 
316 
320 #ifdef DOXYGEN
321  template<typename... Indices>
322  ImplementationDefined& child(Indices... indices)
323 #else
324  template<typename I0, typename... I>
325  auto child(I0 i0, I... i)
326  -> typename std::enable_if<
327  (sizeof...(I) > 0) || !is_flat_index<I0>{},
328  impl::_lazy_member_child_decltype<PowerNode>
329  >::type::template evaluate<I0,I...>::type
330 #endif
331  {
332  static_assert(sizeof...(I) > 0 || impl::_non_empty_tree_path(I0{}),
333  "You cannot use the member function child() with an empty TreePath, use the freestanding version child(node,treePath) instead."
334  );
335  return Dune::TypeTree::child(*this,i0,i...);
336  }
337 
339 
343 #ifdef DOXYGEN
344  template<typename... Indices>
345  const ImplementationDefined& child(Indices... indices)
346 #else
347  template<typename I0, typename... I>
348  auto child(I0 i0, I... i) const
349  -> typename std::enable_if<
350  (sizeof...(I) > 0) || !is_flat_index<I0>{},
351  impl::_lazy_member_child_decltype<const PowerNode>
352  >::type::template evaluate<I0,I...>::type
353 #endif
354  {
355  static_assert(sizeof...(I) > 0 || impl::_non_empty_tree_path(I0{}),
356  "You cannot use the member function child() with an empty TreePath, use the freestanding version child(node,treePath) instead."
357  );
358  return Dune::TypeTree::child(*this,i0,i...);
359  }
360 
362 
365 
366  protected:
367 
369 
378  {}
379 
381  explicit PowerNode(const NodeStorage& children)
382  : _children(children)
383  {}
384 
386  explicit PowerNode (T& t, bool distinct_objects = true)
387  {
388  if (distinct_objects)
389  {
390  for (typename NodeStorage::iterator it = _children.begin(); it != _children.end(); ++it)
391  *it = std::make_shared<T>(t);
392  }
393  else
394  {
395  shared_ptr<T> sp = stackobject_to_shared_ptr(t);
396  std::fill(_children.begin(),_children.end(),sp);
397  }
398  }
399 
400 #ifdef DOXYGEN
401 
403 
413  PowerNode(T& t1, T& t2, ...)
414  {}
415 
416 #else
417 
418  // this weird signature avoids shadowing other 1-argument constructors
419  template<typename C0, typename C1, typename... Children>
420  PowerNode (C0&& c0, C1&& c1, Children&&... children)
421  {
422  assign_reference_pack_to_shared_ptr_array(_children,std::forward<C0>(c0),std::forward<C1>(c1),std::forward<Children>(children)...);
423  }
424 
425  // this weird signature avoids shadowing other 1-argument constructors
426  template<typename C0, typename C1, typename... Children>
427  PowerNode (shared_ptr<C0> c0, shared_ptr<C1> c1, shared_ptr<Children>... children)
428  {
429  assign_shared_ptr_pack_to_shared_ptr_array(_children,c0,c1,children...);
430  }
431 
432 #endif // DOXYGEN
433 
435 
436  private:
437  NodeStorage _children;
438  };
439 
441 
442  } // namespace TypeTree
443 } //namespace Dune
444 
445 #endif // DUNE_TYPETREE_POWERNODE_HH
void setChild(ChildStorageType st, index_constant< i >={})
Sets the stored value representing the i-th child to the passed-in value.
Definition: powernode.hh:212
ChildConstStorageType childStorage(index_constant< i >={}) const
Returns the storage of the i-th child (const version).
Definition: powernode.hh:188
ChildStorageType childStorage(index_constant< i >={})
Returns the storage of the i-th child.
Definition: powernode.hh:174
ChildStorageType Storage
The storage type of the child.
Definition: powernode.hh:138
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: utility.hh:312
ChildStorageType childStorage(std::size_t i)
Returns the storage of the i-th child.
Definition: powernode.hh:248
T & child(std::size_t i)
Returns the i-th child.
Definition: powernode.hh:228
T & child(index_constant< i >={})
Returns the i-th child.
Definition: powernode.hh:152
PowerNode(const NodeStorage &children)
Initialize the PowerNode with a copy of the passed-in storage type.
Definition: powernode.hh:381
typename impl::_is_flat_index< typename std::decay< T >::type >::type is_flat_index
Type trait that determines whether T is a flat index in the context of child extraction.
Definition: childextraction.hh:627
PowerNode(T &t, bool distinct_objects=true)
Initialize all children with copies of a storage object constructed from the parameter t...
Definition: powernode.hh:386
STL namespace.
ChildConstStorageType ConstStorage
The const storage type of the child.
Definition: powernode.hh:141
std::array< ChildStorageType, k > NodeStorage
The type used for storing the children.
Definition: powernode.hh:121
ChildConstStorageType childStorage(std::size_t i) const
Returns the storage of the i-th child (const version).
Definition: powernode.hh:261
PowerNode(T &t1, T &t2,...)
Initialize all children with the passed-in objects.
Definition: powernode.hh:413
shared_ptr< const T > ChildConstStorageType
The const version of the storage type of each child.
Definition: powernode.hh:118
const NodeStorage & nodeStorage() const
Definition: powernode.hh:288
PowerNodeTag NodeTag
The type tag that describes a PowerNode.
Definition: powernode.hh:109
const T & child(std::size_t i) const
Returns the i-th child (const version).
Definition: powernode.hh:238
T ChildType
The type of each child.
Definition: powernode.hh:112
T type
The type of the child.
Definition: powernode.hh:135
void setChild(T &t, index_constant< i >={})
Sets the i-th child to the passed-in value.
Definition: powernode.hh:196
PowerNode()
Default constructor.
Definition: powernode.hh:377
ImplementationDefined child(Node &&node, Indices...indices)
Extracts the child of a node given by a sequence of compile-time and run-time indices.
Definition: childextraction.hh:472
shared_ptr< T > ChildStorageType
The storage type of each child.
Definition: powernode.hh:115
void setChild(std::size_t i, T &&t)
Store the passed value in i-th child.
Definition: powernode.hh:275
Tag designating a power node.
Definition: nodetags.hh:19
void setChild(T &&t, index_constant< i >={})
Store the passed value in i-th child.
Definition: powernode.hh:204
const T & child(index_constant< i >={}) const
Returns the i-th child (const version).
Definition: powernode.hh:163
void setChild(std::size_t i, ChildStorageType st)
Sets the stored value representing the i-th child to the passed-in value.
Definition: powernode.hh:282
Definition: accumulate_static.hh:12
Access to the type and storage type of the i-th child.
Definition: powernode.hh:126
void setChild(std::size_t i, T &t)
Sets the i-th child to the passed-in value.
Definition: powernode.hh:268
Collect k instances of type T within a dune-typetree.
Definition: powernode.hh:91
ImplementationDefined & child(Indices...indices)
Returns the child given by the list of indices.
Definition: powernode.hh:322
const ImplementationDefined & child(Indices...indices)
Returns the child given by the list of indices.
Definition: powernode.hh:345
T Type
The type of the child.
Definition: powernode.hh:129