DUNE PDELab (git)

dynamicpowernode.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-GPL-2.0-only-with-PDELab-exception
5
6#ifndef DUNE_TYPETREE_DYNAMICPOWERNODE_HH
7#define DUNE_TYPETREE_DYNAMICPOWERNODE_HH
8
9#include <cassert>
10#include <vector>
11#include <memory>
12#include <type_traits>
13
15#include <dune/common/std/type_traits.hh>
16
17#include <dune/typetree/nodetags.hh>
18#include <dune/typetree/utility.hh>
19#include <dune/typetree/typetraits.hh>
20
21namespace Dune {
22 namespace TypeTree {
23
33 template<typename T>
35 {
36
37 public:
38
40 static const bool isLeaf = false;
41
43 static const bool isPower = true;
44
46 static const bool isComposite = false;
47
49 std::size_t degree() const
50 {
51 return _children.size();
52 }
53
56
58 typedef T ChildType;
59
61 typedef std::shared_ptr<T> ChildStorageType;
62
64 typedef std::shared_ptr<const T> ChildConstStorageType;
65
67 typedef std::vector<ChildStorageType> NodeStorage;
68
69
72
74
77 ChildType& child (std::size_t i)
78 {
79 assert(i < degree() && "child index out of range");
80 return *_children[i];
81 }
82
84
87 const ChildType& child (std::size_t i) const
88 {
89 assert(i < degree() && "child index out of range");
90 return *_children[i];
91 }
92
94
98 {
99 assert(i < degree() && "child index out of range");
100 return _children[i];
101 }
102
104
111 {
112 assert(i < degree() && "child index out of range");
113 return _children[i];
114 }
115
117 void setChild (std::size_t i, ChildType& t)
118 {
119 assert(i < degree() && "child index out of range");
120 _children[i] = stackobject_to_shared_ptr(t);
121 }
122
124 void setChild (std::size_t i, ChildType&& t)
125 {
126 assert(i < degree() && "child index out of range");
127 _children[i] = convert_arg(std::move(t));
128 }
129
131 void setChild (std::size_t i, ChildStorageType st)
132 {
133 assert(i < degree() && "child index out of range");
134 _children[i] = std::move(st);
135 }
136
137 const NodeStorage& nodeStorage () const
138 {
139 return _children;
140 }
141
143
146
147 protected:
148
151 DynamicPowerNode () = delete;
152
154
162 explicit DynamicPowerNode (std::size_t size)
163 : _children(size)
164 {}
165
167 explicit DynamicPowerNode (NodeStorage children)
168 : _children(std::move(children))
169 {}
170
171#ifdef DOXYGEN
172
174 DynamicPowerNode (T& t1, T& t2, ...)
175 {}
176
177#else
178
179 template<typename... Children,
180 std::enable_if_t<(std::is_same_v<ChildType, std::decay_t<Children>> &&...), bool> = true>
181 DynamicPowerNode (Children&&... children)
182 {
183 _children = NodeStorage{convert_arg(std::forward<Children>(children))...};
184 }
185
186 template<typename... Children,
187 std::enable_if_t<(std::is_same_v<ChildType, std::decay_t<Children>> &&...), bool> = true>
188 DynamicPowerNode (std::shared_ptr<Children>... children)
189 {
190 _children = NodeStorage{std::move(children)...};
191 }
192
193#endif // DOXYGEN
194
196
197 private:
198 NodeStorage _children;
199 };
200
202
203 } // namespace TypeTree
204} //namespace Dune
205
206#endif // DUNE_TYPETREE_DYNAMICPOWERNODE_HH
Collect multiple instances of type T within a dune-typetree.
Definition: dynamicpowernode.hh:35
static const bool isPower
Mark this class as a power in the dune-typetree.
Definition: dynamicpowernode.hh:43
std::vector< ChildStorageType > NodeStorage
The type used for storing the children.
Definition: dynamicpowernode.hh:67
ChildConstStorageType childStorage(std::size_t i) const
Returns the storage of the i-th child (const version).
Definition: dynamicpowernode.hh:110
const ChildType & child(std::size_t i) const
Returns the i-th child (const version).
Definition: dynamicpowernode.hh:87
std::shared_ptr< T > ChildStorageType
The storage type of each child.
Definition: dynamicpowernode.hh:61
DynamicPowerNode(T &t1, T &t2,...)
Initialize all children with the passed-in objects.
Definition: dynamicpowernode.hh:174
std::shared_ptr< const T > ChildConstStorageType
The const version of the storage type of each child.
Definition: dynamicpowernode.hh:64
DynamicPowerNode(NodeStorage children)
Initialize the DynamicPowerNode with a copy of the passed-in storage type.
Definition: dynamicpowernode.hh:167
DynamicPowerNodeTag NodeTag
The type tag that describes the node.
Definition: dynamicpowernode.hh:55
T ChildType
The type of each child.
Definition: dynamicpowernode.hh:58
void setChild(std::size_t i, ChildType &t)
Sets the i-th child to the passed-in value.
Definition: dynamicpowernode.hh:117
void setChild(std::size_t i, ChildStorageType st)
Sets the stored value representing the i-th child to the passed-in value.
Definition: dynamicpowernode.hh:131
DynamicPowerNode(std::size_t size)
Construct a node with the given number of children.
Definition: dynamicpowernode.hh:162
static const bool isComposite
Mark this class as a non composite in the dune-typetree.
Definition: dynamicpowernode.hh:46
void setChild(std::size_t i, ChildType &&t)
Store the passed value in i-th child.
Definition: dynamicpowernode.hh:124
ChildType & child(std::size_t i)
Returns the i-th child.
Definition: dynamicpowernode.hh:77
std::size_t degree() const
The number of children.
Definition: dynamicpowernode.hh:49
ChildStorageType childStorage(std::size_t i)
Returns the storage of the i-th child.
Definition: dynamicpowernode.hh:97
static const bool isLeaf
Mark this class as non leaf in the dune-typetree.
Definition: dynamicpowernode.hh:40
Traits for type conversions and type information.
Dune namespace.
Definition: alignedallocator.hh:13
std::shared_ptr< T > stackobject_to_shared_ptr(T &t)
Create a shared_ptr for a stack-allocated object.
Definition: shared_ptr.hh:72
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
STL namespace.
Tag designating a power node with runtime degree.
Definition: nodetags.hh:24
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 24, 23:30, 2024)