Dune Core Modules (2.9.0)

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 
4 #ifndef DUNE_TYPETREE_DYNAMICPOWERNODE_HH
5 #define DUNE_TYPETREE_DYNAMICPOWERNODE_HH
6 
7 #include <cassert>
8 #include <vector>
9 #include <memory>
10 #include <type_traits>
11 
13 #include <dune/common/std/type_traits.hh>
14 
15 #include <dune/typetree/nodetags.hh>
16 #include <dune/typetree/utility.hh>
17 #include <dune/typetree/typetraits.hh>
18 
19 namespace Dune {
20  namespace TypeTree {
21 
31  template<typename T>
33  {
34 
35  public:
36 
38  static const bool isLeaf = false;
39 
41  static const bool isPower = true;
42 
44  static const bool isComposite = false;
45 
47  std::size_t degree() const
48  {
49  return _children.size();
50  }
51 
54 
56  typedef T ChildType;
57 
59  typedef std::shared_ptr<T> ChildStorageType;
60 
62  typedef std::shared_ptr<const T> ChildConstStorageType;
63 
65  typedef std::vector<ChildStorageType> NodeStorage;
66 
67 
70 
72 
75  ChildType& child (std::size_t i)
76  {
77  assert(i < degree() && "child index out of range");
78  return *_children[i];
79  }
80 
82 
85  const ChildType& child (std::size_t i) const
86  {
87  assert(i < degree() && "child index out of range");
88  return *_children[i];
89  }
90 
92 
96  {
97  assert(i < degree() && "child index out of range");
98  return _children[i];
99  }
100 
102 
108  ChildConstStorageType childStorage (std::size_t i) const
109  {
110  assert(i < degree() && "child index out of range");
111  return _children[i];
112  }
113 
115  void setChild (std::size_t i, ChildType& t)
116  {
117  assert(i < degree() && "child index out of range");
118  _children[i] = stackobject_to_shared_ptr(t);
119  }
120 
122  void setChild (std::size_t i, ChildType&& t)
123  {
124  assert(i < degree() && "child index out of range");
125  _children[i] = convert_arg(std::move(t));
126  }
127 
129  void setChild (std::size_t i, ChildStorageType st)
130  {
131  assert(i < degree() && "child index out of range");
132  _children[i] = std::move(st);
133  }
134 
135  const NodeStorage& nodeStorage () const
136  {
137  return _children;
138  }
139 
141 
144 
145  protected:
146 
149  DynamicPowerNode () = delete;
150 
152 
160  explicit DynamicPowerNode (std::size_t size)
161  : _children(size)
162  {}
163 
165  explicit DynamicPowerNode (NodeStorage children)
166  : _children(std::move(children))
167  {}
168 
169 #ifdef DOXYGEN
170 
172  DynamicPowerNode (T& t1, T& t2, ...)
173  {}
174 
175 #else
176 
177  template<typename... Children,
178  std::enable_if_t<(std::is_same_v<ChildType, std::decay_t<Children>> &&...), bool> = true>
179  DynamicPowerNode (Children&&... children)
180  {
181  _children = NodeStorage{convert_arg(std::forward<Children>(children))...};
182  }
183 
184  template<typename... Children,
185  std::enable_if_t<(std::is_same_v<ChildType, std::decay_t<Children>> &&...), bool> = true>
186  DynamicPowerNode (std::shared_ptr<Children>... children)
187  {
188  _children = NodeStorage{std::move(children)...};
189  }
190 
191 #endif // DOXYGEN
192 
194 
195  private:
196  NodeStorage _children;
197  };
198 
200 
201  } // namespace TypeTree
202 } //namespace Dune
203 
204 #endif // DUNE_TYPETREE_DYNAMICPOWERNODE_HH
Collect multiple instances of type T within a dune-typetree.
Definition: dynamicpowernode.hh:33
static const bool isPower
Mark this class as a power in the dune-typetree.
Definition: dynamicpowernode.hh:41
std::vector< ChildStorageType > NodeStorage
The type used for storing the children.
Definition: dynamicpowernode.hh:65
ChildConstStorageType childStorage(std::size_t i) const
Returns the storage of the i-th child (const version).
Definition: dynamicpowernode.hh:108
std::shared_ptr< T > ChildStorageType
The storage type of each child.
Definition: dynamicpowernode.hh:59
DynamicPowerNode(T &t1, T &t2,...)
Initialize all children with the passed-in objects.
Definition: dynamicpowernode.hh:172
std::shared_ptr< const T > ChildConstStorageType
The const version of the storage type of each child.
Definition: dynamicpowernode.hh:62
DynamicPowerNode(NodeStorage children)
Initialize the DynamicPowerNode with a copy of the passed-in storage type.
Definition: dynamicpowernode.hh:165
DynamicPowerNodeTag NodeTag
The type tag that describes the node.
Definition: dynamicpowernode.hh:53
T ChildType
The type of each child.
Definition: dynamicpowernode.hh:56
void setChild(std::size_t i, ChildType &t)
Sets the i-th child to the passed-in value.
Definition: dynamicpowernode.hh:115
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:129
ChildType & child(std::size_t i)
Returns the i-th child.
Definition: dynamicpowernode.hh:75
DynamicPowerNode(std::size_t size)
Construct a node with the given number of children.
Definition: dynamicpowernode.hh:160
static const bool isComposite
Mark this class as a non composite in the dune-typetree.
Definition: dynamicpowernode.hh:44
void setChild(std::size_t i, ChildType &&t)
Store the passed value in i-th child.
Definition: dynamicpowernode.hh:122
std::size_t degree() const
The number of children.
Definition: dynamicpowernode.hh:47
ChildStorageType childStorage(std::size_t i)
Returns the storage of the i-th child.
Definition: dynamicpowernode.hh:95
static const bool isLeaf
Mark this class as non leaf in the dune-typetree.
Definition: dynamicpowernode.hh:38
const ChildType & child(std::size_t i) const
Returns the i-th child (const version).
Definition: dynamicpowernode.hh:85
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
Tag designating a power node with runtime degree.
Definition: nodetags.hh:22
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 2, 22:35, 2024)