"Fossies" - the Fresh Open Source Software Archive 
Member "dune-typetree-2.8.0/dune/typetree/dynamicpowernode.hh" (31 Aug 2021, 5716 Bytes) of package /linux/misc/dune/dune-typetree-2.8.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "dynamicpowernode.hh" see the
Fossies "Dox" file reference documentation.
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
12 #include <dune/common/typetraits.hh>
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
22 /** \addtogroup Nodes
23 * \ingroup TypeTree
24 * \{
25 */
26
27 /** \brief Collect multiple instances of type T within a \ref TypeTree.
28 *
29 * \tparam T Type of the tree-node children
30 */
31 template<typename T>
32 class DynamicPowerNode
33 {
34
35 public:
36
37 //! Mark this class as non leaf in the \ref TypeTree.
38 static const bool isLeaf = false;
39
40 //! Mark this class as a power in the \ref TypeTree.
41 static const bool isPower = true;
42
43 //! Mark this class as a non composite in the \ref TypeTree.
44 static const bool isComposite = false;
45
46 //! The number of children.
47 std::size_t degree() const
48 {
49 return _children.size();
50 }
51
52 //! The type tag that describes the node.
53 typedef DynamicPowerNodeTag NodeTag;
54
55 //! The type of each child.
56 typedef T ChildType;
57
58 //! The storage type of each child.
59 typedef std::shared_ptr<T> ChildStorageType;
60
61 //! The const version of the storage type of each child.
62 typedef std::shared_ptr<const T> ChildConstStorageType;
63
64 //! The type used for storing the children.
65 typedef std::vector<ChildStorageType> NodeStorage;
66
67
68 //! @name Child Access (Dynamic methods)
69 //! @{
70
71 //! Returns the i-th child.
72 /**
73 * \returns a reference to the i-th child.
74 */
75 ChildType& child (std::size_t i)
76 {
77 assert(i < degree() && "child index out of range");
78 return *_children[i];
79 }
80
81 //! Returns the i-th child (const version).
82 /**
83 * \returns a const reference to the i-th child.
84 */
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
91 //! Returns the storage of the i-th child.
92 /**
93 * \returns a copy of the object storing the i-th child.
94 */
95 ChildStorageType childStorage (std::size_t i)
96 {
97 assert(i < degree() && "child index out of range");
98 return _children[i];
99 }
100
101 //! Returns the storage of the i-th child (const version).
102 /**
103 * This method is only important if the child is stored as
104 * some kind of pointer, as this allows the pointee type to
105 * become const.
106 * \returns a copy of the object storing the i-th child.
107 */
108 ChildConstStorageType childStorage (std::size_t i) const
109 {
110 assert(i < degree() && "child index out of range");
111 return _children[i];
112 }
113
114 //! Sets the i-th child to the passed-in value.
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
121 //! Store the passed value in i-th child.
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
128 //! Sets the stored value representing the i-th child to the passed-in value.
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
140 //! @}
141
142 //! @name Constructors
143 //! @{
144
145 protected:
146
147 //! The Default constructor is deleted, since you need to pass the number of children. There
148 //! is currently no dynamic resize of this node type implemented.
149 DynamicPowerNode () = delete;
150
151 //! Construct a node with the given number of children.
152 /**
153 * The constructor is protected, as DynamicPowerNode is a utility
154 * class that needs to be filled with meaning by subclassing it
155 * and adding useful functionality to the subclass.
156 *
157 * \warning When using this constructor, make sure to set ALL children
158 * by means of the setChild() methods!
159 */
160 explicit DynamicPowerNode (std::size_t size)
161 : _children(size)
162 {}
163
164 //! Initialize the DynamicPowerNode with a copy of the passed-in storage type.
165 explicit DynamicPowerNode (NodeStorage children)
166 : _children(std::move(children))
167 {}
168
169 #ifdef DOXYGEN
170
171 //! Initialize all children with the passed-in objects.
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
193 //! @}
194
195 private:
196 NodeStorage _children;
197 };
198
199 //! \} group Nodes
200
201 } // namespace TypeTree
202 } //namespace Dune
203
204 #endif // DUNE_TYPETREE_DYNAMICPOWERNODE_HH