4 #ifndef DUNE_TYPETREE_POWERNODE_HH 5 #define DUNE_TYPETREE_POWERNODE_HH 10 #include <dune/common/typetraits.hh> 29 template<
typename T,
typename It,
typename... Args>
30 void assign_reference_pack_to_shared_ptr_array_unpack(It it, Args&&... args) {}
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)
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)...);
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)
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)...);
49 template<
typename T,
typename It,
typename... Args>
50 void assign_shared_ptr_pack_to_shared_ptr_array_unpack(It it, Args&&... args) {}
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)
55 static_assert(is_same<T,
typename std::remove_reference<Arg>::type::element_type>::value,
"type mismatch during array conversion");
57 assign_shared_ptr_pack_to_shared_ptr_array_unpack<T>(++it,args...);
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)
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...);
74 template<
typename PowerNode,
typename T, std::
size_t k>
75 struct AssertPowerNodeChildCount
76 :
public enable_if<is_same<
77 typename PowerNode::ChildType,
79 PowerNode::CHILDREN == k,
90 template<
typename T, std::
size_t k>
97 static const bool isLeaf =
false;
100 static const bool isPower =
true;
103 static const bool isComposite =
false;
106 static const std::size_t CHILDREN = k;
125 template<std::
size_t i>
129 static_assert((i < CHILDREN),
"child index out of range");
151 template<std::
size_t i>
154 static_assert((i < CHILDREN),
"child index out of range");
155 return *_children[i];
162 template<std::
size_t i>
165 static_assert((i < CHILDREN),
"child index out of range");
166 return *_children[i];
173 template<std::
size_t i>
176 static_assert((i < CHILDREN),
"child index out of range");
187 template<std::
size_t i>
190 static_assert((i < CHILDREN),
"child index out of range");
195 template<std::
size_t i>
198 static_assert((i < CHILDREN),
"child index out of range");
199 _children[i] = stackobject_to_shared_ptr(t);
203 template<std::
size_t i>
206 static_assert((i < CHILDREN),
"child index out of range");
207 _children[i] = convert_arg(std::move(t));
211 template<std::
size_t i>
214 static_assert((i < CHILDREN),
"child index out of range");
230 assert(i < CHILDREN &&
"child index out of range");
231 return *_children[i];
238 const T&
child (std::size_t i)
const 240 assert(i < CHILDREN &&
"child index out of range");
241 return *_children[i];
250 assert(i < CHILDREN &&
"child index out of range");
263 assert(i < CHILDREN &&
"child index out of range");
264 return (_children[i]);
270 assert(i < CHILDREN &&
"child index out of range");
271 _children[i] = stackobject_to_shared_ptr(t);
277 assert(i < CHILDREN &&
"child index out of range");
278 _children[i] = convert_arg(std::move(t));
284 assert(i < CHILDREN &&
"child index out of range");
321 template<
typename... Indices>
322 ImplementationDefined&
child(Indices... indices)
324 template<
typename I0,
typename... I>
325 auto child(I0 i0, I... i)
326 ->
typename std::enable_if<
328 impl::_lazy_member_child_decltype<PowerNode>
329 >::type::template evaluate<I0,I...>::type
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." 344 template<
typename... Indices>
345 const ImplementationDefined&
child(Indices... indices)
347 template<
typename I0,
typename... I>
348 auto child(I0 i0, I... i) const
349 -> typename
std::enable_if<
351 impl::_lazy_member_child_decltype<const PowerNode>
352 >::type::template evaluate<I0,I...>::type
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." 382 : _children(children)
388 if (distinct_objects)
390 for (
typename NodeStorage::iterator it = _children.begin(); it != _children.end(); ++it)
391 *it = std::make_shared<T>(t);
395 shared_ptr<T> sp = stackobject_to_shared_ptr(t);
396 std::fill(_children.begin(),_children.end(),sp);
419 template<
typename C0,
typename C1,
typename... Children>
420 PowerNode (C0&& c0, C1&& c1, Children&&... children)
422 assign_reference_pack_to_shared_ptr_array(_children,std::forward<C0>(c0),std::forward<C1>(c1),std::forward<Children>(children)...);
426 template<
typename C0,
typename C1,
typename... Children>
427 PowerNode (shared_ptr<C0> c0, shared_ptr<C1> c1, shared_ptr<Children>... children)
429 assign_shared_ptr_pack_to_shared_ptr_array(_children,c0,c1,children...);
437 NodeStorage _children;
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
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