4 #ifndef DUNE_TYPETREE_POWERNODE_HH
5 #define DUNE_TYPETREE_POWERNODE_HH
9 #include <dune/common/array.hh>
10 #include <dune/common/typetraits.hh>
14 #include <dune/common/static_assert.hh>
24 #if HAVE_VARIADIC_TEMPLATES && HAVE_RVALUE_REFERENCES
31 template<
typename T,
typename It,
typename... Args>
32 void assign_reference_pack_to_shared_ptr_array_unpack(It it, Args&&... args) {}
34 template<
typename T,
typename It,
typename Arg,
typename... Args>
35 void assign_reference_pack_to_shared_ptr_array_unpack(It it, Arg&& arg, Args&&... args)
37 static_assert(is_same<T,
typename remove_const<
typename remove_reference<Arg>::type>::type>::
value,
"type mismatch during array conversion");
38 *it = convert_arg(std::forward<Arg>(arg));
39 assign_reference_pack_to_shared_ptr_array_unpack<T>(++it,std::forward<Args>(args)...);
42 template<
typename T, std::size_t n,
typename... Args>
43 void assign_reference_pack_to_shared_ptr_array(array<shared_ptr<T>,n>& res, Args&&... args)
45 static_assert(
sizeof...(Args) == n,
"invalid number of arguments");
46 return assign_reference_pack_to_shared_ptr_array_unpack<T>(res.begin(),std::forward<Args>(args)...);
51 template<
typename T,
typename It,
typename... Args>
52 void assign_shared_ptr_pack_to_shared_ptr_array_unpack(It it, Args&&... args) {}
54 template<
typename T,
typename It,
typename Arg,
typename... Args>
55 void assign_shared_ptr_pack_to_shared_ptr_array_unpack(It it, Arg&& arg, Args&&... args)
57 static_assert(is_same<T,
typename std::remove_reference<Arg>::type::element_type>::
value,
"type mismatch during array conversion");
59 assign_shared_ptr_pack_to_shared_ptr_array_unpack<T>(++it,args...);
62 template<
typename T, std::size_t n,
typename... Args>
63 void assign_shared_ptr_pack_to_shared_ptr_array(array<shared_ptr<T>,n>& res, Args&&... args)
65 static_assert(
sizeof...(Args) == n,
"invalid number of arguments");
66 return assign_shared_ptr_pack_to_shared_ptr_array_unpack<T>(res.begin(),args...);
78 template<
typename PowerNode,
typename T, std::
size_t k>
79 struct AssertPowerNodeChildCount
80 :
public enable_if<is_same<
81 typename PowerNode::ChildType,
83 PowerNode::CHILDREN == k,
94 template<
typename T, std::
size_t k>
129 template<std::
size_t i>
155 template<std::
size_t i>
158 dune_static_assert((i <
CHILDREN),
"child index out of range");
159 return *_children[i];
166 template<std::
size_t i>
169 dune_static_assert((i <
CHILDREN),
"child index out of range");
170 return *_children[i];
177 template<std::
size_t i>
180 dune_static_assert((i <
CHILDREN),
"child index out of range");
191 template<std::
size_t i>
194 dune_static_assert((i <
CHILDREN),
"child index out of range");
199 template<std::
size_t i>
202 dune_static_assert((i <
CHILDREN),
"child index out of range");
203 _children[i] = stackobject_to_shared_ptr(t);
207 template<std::
size_t i>
210 dune_static_assert((i <
CHILDREN),
"child index out of range");
226 assert(i <
CHILDREN &&
"child index out of range");
227 return *_children[i];
234 const T&
child (std::size_t i)
const
236 assert(i <
CHILDREN &&
"child index out of range");
237 return *_children[i];
246 assert(i <
CHILDREN &&
"child index out of range");
259 assert(i <
CHILDREN &&
"child index out of range");
260 return (_children[i]);
266 assert(i <
CHILDREN &&
"child index out of range");
267 _children[i] = stackobject_to_shared_ptr(t);
273 assert(i <
CHILDREN &&
"child index out of range");
303 : _children(children)
309 if (distinct_objects)
311 for (
typename NodeStorage::iterator it = _children.begin(); it != _children.end(); ++it)
312 *it = make_shared<T>(t);
316 shared_ptr<T> sp = stackobject_to_shared_ptr(t);
317 std::fill(_children.begin(),_children.end(),sp);
339 #if HAVE_VARIADIC_TEMPLATES && HAVE_RVALUE_REFERENCES
343 template<
typename C0,
typename C1,
typename... Children>
344 PowerNode (C0&& c0, C1&& c1, Children&&... children)
346 assign_reference_pack_to_shared_ptr_array(_children,std::forward<C0>(c0),std::forward<C1>(c1),std::forward<Children>(children)...);
350 template<
typename C0,
typename C1,
typename... Children>
351 PowerNode (shared_ptr<C0> c0, shared_ptr<C1> c1, shared_ptr<Children>... children)
353 assign_shared_ptr_pack_to_shared_ptr_array(_children,c0,c1,children...);
359 PowerNode (
typename AssertPowerNodeChildCount<PowerNode,U,2>::type& c0,
362 _children[0] = stackobject_to_shared_ptr(c0);
363 _children[1] = stackobject_to_shared_ptr(c1);
367 PowerNode (
typename AssertPowerNodeChildCount<PowerNode,U,3>::type& c0,
371 _children[0] = stackobject_to_shared_ptr(c0);
372 _children[1] = stackobject_to_shared_ptr(c1);
373 _children[2] = stackobject_to_shared_ptr(c2);
377 PowerNode (
typename AssertPowerNodeChildCount<PowerNode,U,4>::type& c0,
382 _children[0] = stackobject_to_shared_ptr(c0);
383 _children[1] = stackobject_to_shared_ptr(c1);
384 _children[2] = stackobject_to_shared_ptr(c2);
385 _children[3] = stackobject_to_shared_ptr(c3);
389 PowerNode (
typename AssertPowerNodeChildCount<PowerNode,U,5>::type& c0,
395 _children[0] = stackobject_to_shared_ptr(c0);
396 _children[1] = stackobject_to_shared_ptr(c1);
397 _children[2] = stackobject_to_shared_ptr(c2);
398 _children[3] = stackobject_to_shared_ptr(c3);
399 _children[4] = stackobject_to_shared_ptr(c4);
403 PowerNode (
typename AssertPowerNodeChildCount<PowerNode,U,6>::type& c0,
410 _children[0] = stackobject_to_shared_ptr(c0);
411 _children[1] = stackobject_to_shared_ptr(c1);
412 _children[2] = stackobject_to_shared_ptr(c2);
413 _children[3] = stackobject_to_shared_ptr(c3);
414 _children[4] = stackobject_to_shared_ptr(c4);
415 _children[5] = stackobject_to_shared_ptr(c5);
419 PowerNode (
typename AssertPowerNodeChildCount<PowerNode,U,7>::type& c0,
427 _children[0] = stackobject_to_shared_ptr(c0);
428 _children[1] = stackobject_to_shared_ptr(c1);
429 _children[2] = stackobject_to_shared_ptr(c2);
430 _children[3] = stackobject_to_shared_ptr(c3);
431 _children[4] = stackobject_to_shared_ptr(c4);
432 _children[5] = stackobject_to_shared_ptr(c5);
433 _children[6] = stackobject_to_shared_ptr(c6);
437 PowerNode (
typename AssertPowerNodeChildCount<PowerNode,U,8>::type& c0,
446 _children[0] = stackobject_to_shared_ptr(c0);
447 _children[1] = stackobject_to_shared_ptr(c1);
448 _children[2] = stackobject_to_shared_ptr(c2);
449 _children[3] = stackobject_to_shared_ptr(c3);
450 _children[4] = stackobject_to_shared_ptr(c4);
451 _children[5] = stackobject_to_shared_ptr(c5);
452 _children[6] = stackobject_to_shared_ptr(c6);
453 _children[7] = stackobject_to_shared_ptr(c7);
457 PowerNode (
typename AssertPowerNodeChildCount<PowerNode,U,9>::type& c0,
467 _children[0] = stackobject_to_shared_ptr(c0);
468 _children[1] = stackobject_to_shared_ptr(c1);
469 _children[2] = stackobject_to_shared_ptr(c2);
470 _children[3] = stackobject_to_shared_ptr(c3);
471 _children[4] = stackobject_to_shared_ptr(c4);
472 _children[5] = stackobject_to_shared_ptr(c5);
473 _children[6] = stackobject_to_shared_ptr(c6);
474 _children[7] = stackobject_to_shared_ptr(c7);
475 _children[8] = stackobject_to_shared_ptr(c8);
479 PowerNode (
typename AssertPowerNodeChildCount<PowerNode,U,10>::type& c0,
490 _children[0] = stackobject_to_shared_ptr(c0);
491 _children[1] = stackobject_to_shared_ptr(c1);
492 _children[2] = stackobject_to_shared_ptr(c2);
493 _children[3] = stackobject_to_shared_ptr(c3);
494 _children[4] = stackobject_to_shared_ptr(c4);
495 _children[5] = stackobject_to_shared_ptr(c5);
496 _children[6] = stackobject_to_shared_ptr(c6);
497 _children[7] = stackobject_to_shared_ptr(c7);
498 _children[8] = stackobject_to_shared_ptr(c8);
499 _children[9] = stackobject_to_shared_ptr(c9);
517 #endif // DUNE_TYPETREE_POWERNODE_HH
static const bool isLeaf
Mark this class as non leaf in the dune-typetree.
Definition: powernode.hh:101
PowerNode(const NodeStorage &children)
Initialize the PowerNode with a copy of the passed-in storage type.
Definition: powernode.hh:302
ChildStorageType Storage
The storage type of the child.
Definition: powernode.hh:142
ChildConstStorageType childStorage() const
Returns the storage of the i-th child (const version).
Definition: powernode.hh:192
const T & child() const
Returns the i-th child (const version).
Definition: powernode.hh:167
ChildStorageType childStorage(std::size_t i)
Returns the storage of the i-th child.
Definition: powernode.hh:244
ChildConstStorageType childStorage(std::size_t i) const
Returns the storage of the i-th child (const version).
Definition: powernode.hh:257
Tag designating a power node.
Definition: nodetags.hh:19
ChildConstStorageType ConstStorage
The const storage type of the child.
Definition: powernode.hh:145
void setChild(std::size_t i, T &t)
Sets the i-th child to the passed-in value.
Definition: powernode.hh:264
PowerNode()
Default constructor.
Definition: powernode.hh:298
static const bool isPower
Mark this class as a power in the dune-typetree.
Definition: powernode.hh:104
static const std::size_t CHILDREN
The number of children.
Definition: powernode.hh:110
void setChild(ChildStorageType st)
Sets the stored value representing the i-th child to the passed-in value.
Definition: powernode.hh:208
T & child()
Returns the i-th child.
Definition: powernode.hh:156
const NodeStorage & nodeStorage() const
Definition: powernode.hh:277
PowerNode(T &t, bool distinct_objects=true)
Initialize all children with copies of a storage object constructed from the parameter t...
Definition: powernode.hh:307
dune_static_assert((i< CHILDREN),"child index out of range")
Collect k instances of type T within a dune-typetree.
Definition: powernode.hh:95
T type
The type of the child.
Definition: powernode.hh:139
T ChildType
The type of each child.
Definition: powernode.hh:116
PowerNode(T &t1, T &t2,...)
Initialize all children with the passed-in objects.
Definition: powernode.hh:334
static const bool isComposite
Mark this class as a non composite in the dune-typetree.
Definition: powernode.hh:107
PowerNodeTag NodeTag
The type tag that describes a PowerNode.
Definition: powernode.hh:113
ChildStorageType childStorage()
Returns the storage of the i-th child.
Definition: powernode.hh:178
array< ChildStorageType, k > NodeStorage
The type used for storing the children.
Definition: powernode.hh:125
static const std::size_t value
Definition: compositenode.hh:38
T & child(std::size_t i)
Returns the i-th child.
Definition: powernode.hh:224
shared_ptr< const T > ChildConstStorageType
The const version of the storage type of each child.
Definition: powernode.hh:122
Access to the type and storage type of the i-th child.
Definition: powernode.hh:130
const T & child(std::size_t i) const
Returns the i-th child (const version).
Definition: powernode.hh:234
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:271
void setChild(T &t)
Sets the i-th child to the passed-in value.
Definition: powernode.hh:200
shared_ptr< T > ChildStorageType
The storage type of each child.
Definition: powernode.hh:119
T Type
The type of the child.
Definition: powernode.hh:136