4 #ifndef DUNE_TYPETREE_POWERNODE_HH
5 #define DUNE_TYPETREE_POWERNODE_HH
10 #include <type_traits>
12 #include <dune/common/typetraits.hh>
13 #include <dune/common/std/type_traits.hh>
32 template<
typename PowerNode,
typename T, std::
size_t k>
33 struct AssertPowerNodeChildCount
34 :
public std::enable_if<std::is_same<
35 typename PowerNode::ChildType,
37 PowerNode::CHILDREN == k,
48 template<
typename T, std::
size_t k>
68 return std::integral_constant<std::size_t,k>{};
82 template<std::
size_t i>
86 static_assert((i <
CHILDREN),
"child index out of range");
102 template<std::
size_t i>
105 static_assert((i <
CHILDREN),
"child index out of range");
106 return *_children[i];
113 template<std::
size_t i>
114 const T&
child (index_constant<i> = {})
const
116 static_assert((i <
CHILDREN),
"child index out of range");
117 return *_children[i];
124 template<std::
size_t i>
127 static_assert((i <
CHILDREN),
"child index out of range");
135 template<std::
size_t i>
138 static_assert((i <
CHILDREN),
"child index out of range");
143 template<std::
size_t i>
146 static_assert((i <
CHILDREN),
"child index out of range");
147 _children[i] = stackobject_to_shared_ptr(t);
151 template<std::
size_t i>
154 static_assert((i <
CHILDREN),
"child index out of range");
155 _children[i] = convert_arg(std::move(t));
159 template<std::
size_t i>
160 void setChild (std::shared_ptr<T> st, index_constant<i> = {})
162 static_assert((i <
CHILDREN),
"child index out of range");
163 _children[i] = std::move(st);
178 assert(i <
CHILDREN &&
"child index out of range");
179 return *_children[i];
186 const T&
child (std::size_t i)
const
188 assert(i <
CHILDREN &&
"child index out of range");
189 return *_children[i];
198 assert(i <
CHILDREN &&
"child index out of range");
208 assert(i <
CHILDREN &&
"child index out of range");
215 assert(i <
CHILDREN &&
"child index out of range");
216 _children[i] = stackobject_to_shared_ptr(t);
222 assert(i <
CHILDREN &&
"child index out of range");
223 _children[i] = convert_arg(std::move(t));
227 void setChild (std::size_t i, std::shared_ptr<T> st)
229 assert(i <
CHILDREN &&
"child index out of range");
230 _children[i] = std::move(st);
266 template<
typename... Indices>
267 ImplementationDefined&
child (Indices... indices)
269 template<
typename I0,
typename... I,
271 decltype(
auto)
child (I0 i0, I... i)
274 static_assert(
sizeof...(I) > 0 || impl::_non_empty_tree_path(I0{}),
275 "You cannot use the member function child() with an empty TreePath, use the freestanding version child(node,treePath) instead."
286 template<
typename... Indices>
287 const ImplementationDefined&
child (Indices... indices)
289 template<
typename I0,
typename... I,
291 decltype(
auto)
child (I0 i0, I... i) const
294 static_assert(
sizeof...(I) > 0 || impl::_non_empty_tree_path(I0{}),
295 "You cannot use the member function child() with an empty TreePath, use the freestanding version child(node,treePath) instead."
321 : _children(children)
327 if (distinct_objects)
329 for (
typename NodeStorage::iterator it = _children.begin(); it != _children.end(); ++it)
330 *it = std::make_shared<T>(t);
334 std::shared_ptr<T> sp = stackobject_to_shared_ptr(t);
335 std::fill(_children.begin(),_children.end(),sp);
347 template<
typename... Children,
349 std::conjunction<std::is_same<ChildType, std::decay_t<Children>>...>::value
353 static_assert(
CHILDREN ==
sizeof...(Children),
"PowerNode constructor is called with incorrect number of children");
354 _children =
NodeStorage{convert_arg(std::forward<Children>(children))...};
357 template<
typename... Children,
359 std::conjunction<std::is_same<ChildType, Children>...>::value
361 PowerNode (std::shared_ptr<Children>... children)
363 static_assert(
CHILDREN ==
sizeof...(Children),
"PowerNode constructor is called with incorrect number of children");
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:126
Definition: accumulate_static.hh:13
Tag designating a power node.
Definition: nodetags.hh:19
Collect k instances of type T within a dune-typetree.
Definition: powernode.hh:50
void setChild(T &t, index_constant< i >={})
Sets the i-th child to the passed-in value.
Definition: powernode.hh:144
ImplementationDefined & child(Indices... indices)
Returns the child given by the list of indices.
Definition: powernode.hh:267
const T & child(std::size_t i) const
Returns the i-th child (const version).
Definition: powernode.hh:186
void setChild(std::shared_ptr< T > st, index_constant< i >={})
Sets the stored value representing the i-th child to the passed-in value.
Definition: powernode.hh:160
PowerNode(T &t1, T &t2,...)
Initialize all children with the passed-in objects.
Definition: powernode.hh:342
const ImplementationDefined & child(Indices... indices)
Returns the child given by the list of indices.
Definition: powernode.hh:287
std::shared_ptr< T > childStorage(index_constant< i >={})
Returns the storage of the i-th child.
Definition: powernode.hh:125
std::array< std::shared_ptr< T >, k > NodeStorage
The type used for storing the children.
Definition: powernode.hh:78
std::shared_ptr< const T > childStorage(index_constant< i >={}) const
Returns the storage of the i-th child (const version).
Definition: powernode.hh:136
PowerNode(T &t, bool distinct_objects=true)
Initialize all children with copies of a storage object constructed from the parameter t.
Definition: powernode.hh:325
PowerNodeTag NodeTag
The type tag that describes a PowerNode.
Definition: powernode.hh:72
static constexpr auto degree()
Definition: powernode.hh:66
static const std::size_t CHILDREN
The number of children.
Definition: powernode.hh:64
const NodeStorage & nodeStorage() const
Definition: powernode.hh:233
void setChild(std::size_t i, std::shared_ptr< T > st)
Sets the stored value representing the i-th child to the passed-in value.
Definition: powernode.hh:227
static const bool isComposite
Mark this class as a non composite in the dune-typetree.
Definition: powernode.hh:61
std::shared_ptr< const T > childStorage(std::size_t i) const
Returns the storage of the i-th child (const version).
Definition: powernode.hh:206
static const bool isLeaf
Mark this class as non leaf in the dune-typetree.
Definition: powernode.hh:55
T & child(index_constant< i >={})
Returns the i-th child.
Definition: powernode.hh:103
static const bool isPower
Mark this class as a power in the dune-typetree.
Definition: powernode.hh:58
PowerNode(const NodeStorage &children)
Initialize the PowerNode with a copy of the passed-in storage type.
Definition: powernode.hh:320
T ChildType
The type of each child.
Definition: powernode.hh:75
T & child(std::size_t i)
Returns the i-th child.
Definition: powernode.hh:176
void setChild(std::size_t i, T &&t)
Store the passed value in i-th child.
Definition: powernode.hh:220
void setChild(T &&t, index_constant< i >={})
Store the passed value in i-th child.
Definition: powernode.hh:152
std::shared_ptr< T > childStorage(std::size_t i)
Returns the storage of the i-th child.
Definition: powernode.hh:196
const T & child(index_constant< i >={}) const
Returns the i-th child (const version).
Definition: powernode.hh:114
PowerNode()
Default constructor.
Definition: powernode.hh:316
void setChild(std::size_t i, T &t)
Sets the i-th child to the passed-in value.
Definition: powernode.hh:213
Access to the type and storage type of the i-th child.
Definition: powernode.hh:84
T type
The type of the child.
Definition: powernode.hh:92
T Type
The type of the child.
Definition: powernode.hh:86
Check if type represents a tree path.
Definition: typetraits.hh:190