dune-typetree  2.4-dev
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 (ChildStorageType st, index_constant<i> = {})
205  {
206  static_assert((i < CHILDREN), "child index out of range");
207  _children[i] = st;
208  }
209 
211 
212 
215 
217 
220  T& child (std::size_t i)
221  {
222  assert(i < CHILDREN && "child index out of range");
223  return *_children[i];
224  }
225 
227 
230  const T& child (std::size_t i) const
231  {
232  assert(i < CHILDREN && "child index out of range");
233  return *_children[i];
234  }
235 
237 
240  ChildStorageType childStorage(std::size_t i)
241  {
242  assert(i < CHILDREN && "child index out of range");
243  return _children[i];
244  }
245 
247 
253  ChildConstStorageType childStorage (std::size_t i) const
254  {
255  assert(i < CHILDREN && "child index out of range");
256  return (_children[i]);
257  }
258 
260  void setChild (std::size_t i, T& t)
261  {
262  assert(i < CHILDREN && "child index out of range");
263  _children[i] = stackobject_to_shared_ptr(t);
264  }
265 
267  void setChild (std::size_t i, ChildStorageType st)
268  {
269  assert(i < CHILDREN && "child index out of range");
270  _children[i] = st;
271  }
272 
273  const NodeStorage& nodeStorage() const
274  {
275  return _children;
276  }
277 
279 
282 
283  // The following two methods require a little bit of SFINAE trickery to work correctly:
284  // We have to make sure that they don't shadow the methods for direct child access because
285  // those get called by the generic child() machinery. If that machinery picks up the methods
286  // defined below, we have an infinite recursion.
287  // So the methods make sure that either
288  //
289  // * there are more than one argument. In that case, we got multiple indices and can forward
290  // to the general machine.
291  //
292  // * the first argument is not a valid flat index, i.e. either a std::size_t or an index_constant.
293  // The argument thus has to be some kind of TreePath instance that we can also pass to the
294  // generic machine.
295  //
296  // The above SFINAE logic works, but there is still a problem with the return type deduction.
297  // We have to do a lazy lookup of the return type after SFINAE has succeeded, otherwise the return
298  // type deduction will trigger the infinite recursion.
299 
301 
305 #ifdef DOXYGEN
306  template<typename... Indices>
307  ImplementationDefined& child(Indices... indices)
308 #else
309  template<typename I0, typename... I>
310  auto child(I0 i0, I... i)
311  -> typename std::enable_if<
312  (sizeof...(I) > 0) || !is_flat_index<I0>{},
313  impl::_lazy_member_child_decltype<PowerNode>
314  >::type::template evaluate<I0,I...>::type
315 #endif
316  {
317  static_assert(sizeof...(I) > 0 || impl::_non_empty_tree_path(I0{}),
318  "You cannot use the member function child() with an empty TreePath, use the freestanding version child(node,treePath) instead."
319  );
320  return Dune::TypeTree::child(*this,i0,i...);
321  }
322 
324 
328 #ifdef DOXYGEN
329  template<typename... Indices>
330  const ImplementationDefined& child(Indices... indices)
331 #else
332  template<typename I0, typename... I>
333  auto child(I0 i0, I... i) const
334  -> typename std::enable_if<
335  (sizeof...(I) > 0) || !is_flat_index<I0>{},
336  impl::_lazy_member_child_decltype<const PowerNode>
337  >::type::template evaluate<I0,I...>::type
338 #endif
339  {
340  static_assert(sizeof...(I) > 0 || impl::_non_empty_tree_path(I0{}),
341  "You cannot use the member function child() with an empty TreePath, use the freestanding version child(node,treePath) instead."
342  );
343  return Dune::TypeTree::child(*this,i0,i...);
344  }
345 
347 
350 
351  protected:
352 
354 
363  {}
364 
366  explicit PowerNode(const NodeStorage& children)
367  : _children(children)
368  {}
369 
371  explicit PowerNode (T& t, bool distinct_objects = true)
372  {
373  if (distinct_objects)
374  {
375  for (typename NodeStorage::iterator it = _children.begin(); it != _children.end(); ++it)
376  *it = std::make_shared<T>(t);
377  }
378  else
379  {
380  shared_ptr<T> sp = stackobject_to_shared_ptr(t);
381  std::fill(_children.begin(),_children.end(),sp);
382  }
383  }
384 
385 #ifdef DOXYGEN
386 
388 
398  PowerNode(T& t1, T& t2, ...)
399  {}
400 
401 #else
402 
403  // this weird signature avoids shadowing other 1-argument constructors
404  template<typename C0, typename C1, typename... Children>
405  PowerNode (C0&& c0, C1&& c1, Children&&... children)
406  {
407  assign_reference_pack_to_shared_ptr_array(_children,std::forward<C0>(c0),std::forward<C1>(c1),std::forward<Children>(children)...);
408  }
409 
410  // this weird signature avoids shadowing other 1-argument constructors
411  template<typename C0, typename C1, typename... Children>
412  PowerNode (shared_ptr<C0> c0, shared_ptr<C1> c1, shared_ptr<Children>... children)
413  {
414  assign_shared_ptr_pack_to_shared_ptr_array(_children,c0,c1,children...);
415  }
416 
417 #endif // DOXYGEN
418 
420 
421  private:
422  NodeStorage _children;
423  };
424 
426 
427  } // namespace TypeTree
428 } //namespace Dune
429 
430 #endif // DUNE_TYPETREE_POWERNODE_HH
ChildConstStorageType childStorage(std::size_t i) const
Returns the storage of the i-th child (const version).
Definition: powernode.hh:253
PowerNodeTag NodeTag
The type tag that describes a PowerNode.
Definition: powernode.hh:109
T & child(std::size_t i)
Returns the i-th child.
Definition: powernode.hh:220
static const bool isLeaf
Mark this class as non leaf in the dune-typetree.
Definition: powernode.hh:97
Access to the type and storage type of the i-th child.
Definition: powernode.hh:126
PowerNode(T &t1, T &t2,...)
Initialize all children with the passed-in objects.
Definition: powernode.hh:398
PowerNode()
Default constructor.
Definition: powernode.hh:362
void setChild(T &t, index_constant< i >={})
Sets the i-th child to the passed-in value.
Definition: powernode.hh:196
PowerNode(T &t, bool distinct_objects=true)
Initialize all children with copies of a storage object constructed from the parameter t...
Definition: powernode.hh:371
const T & child(index_constant< i >={}) const
Returns the i-th child (const version).
Definition: powernode.hh:163
Definition: accumulate_static.hh:12
PowerNode(const NodeStorage &children)
Initialize the PowerNode with a copy of the passed-in storage type.
Definition: powernode.hh:366
ChildConstStorageType childStorage(index_constant< i >={}) const
Returns the storage of the i-th child (const version).
Definition: powernode.hh:188
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
ChildStorageType childStorage(index_constant< i >={})
Returns the storage of the i-th child.
Definition: powernode.hh:174
STL namespace.
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: utility.hh:312
ChildConstStorageType ConstStorage
The const storage type of the child.
Definition: powernode.hh:141
ChildStorageType Storage
The storage type of the child.
Definition: powernode.hh:138
static const bool isPower
Mark this class as a power in the dune-typetree.
Definition: powernode.hh:100
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:267
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
std::array< ChildStorageType, k > NodeStorage
The type used for storing the children.
Definition: powernode.hh:121
ChildStorageType childStorage(std::size_t i)
Returns the storage of the i-th child.
Definition: powernode.hh:240
shared_ptr< T > ChildStorageType
The storage type of each child.
Definition: powernode.hh:115
T & child(index_constant< i >={})
Returns the i-th child.
Definition: powernode.hh:152
ImplementationDefined & child(Indices...indices)
Returns the child given by the list of indices.
Definition: powernode.hh:307
void setChild(ChildStorageType st, index_constant< i >={})
Sets the stored value representing the i-th child to the passed-in value.
Definition: powernode.hh:204
T ChildType
The type of each child.
Definition: powernode.hh:112
const T & child(std::size_t i) const
Returns the i-th child (const version).
Definition: powernode.hh:230
T type
The type of the child.
Definition: powernode.hh:135
Collect k instances of type T within a dune-typetree.
Definition: powernode.hh:91
T Type
The type of the child.
Definition: powernode.hh:129
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:273
static const std::size_t CHILDREN
The number of children.
Definition: powernode.hh:106
static const bool isComposite
Mark this class as a non composite in the dune-typetree.
Definition: powernode.hh:103
const ImplementationDefined & child(Indices...indices)
Returns the child given by the list of indices.
Definition: powernode.hh:330
Tag designating a power node.
Definition: nodetags.hh:19
void setChild(std::size_t i, T &t)
Sets the i-th child to the passed-in value.
Definition: powernode.hh:260