dune-typetree  2.3.1
compositenode.hh
Go to the documentation of this file.
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_COMPOSITENODE_HH
5 #define DUNE_TYPETREE_COMPOSITENODE_HH
6 
9 #include <dune/common/typetraits.hh>
10 #include <dune/common/tuples.hh>
11 #include <dune/common/static_assert.hh>
12 #include <dune/common/exceptions.hh>
13 
14 namespace Dune {
15  namespace TypeTree {
16 
22  namespace {
23 
25 
30  template<typename Children, std::size_t i, std::size_t n, bool atEnd = false>
31  struct count_children
32  {
33 
34  static const bool emptyNode = is_same<typename tuple_element<i,Children>::type,EmptyNode>::value;
35 
36  dune_static_assert(atEnd ? emptyNode : true,"invalid child structure (EmptyNode followed by real node)");
37 
38  static const std::size_t value = count_children<Children,i+1,n,emptyNode>::value + (emptyNode ? 0 : 1);
39 
40  };
41 
43  template<typename Children, std::size_t n, bool atEnd>
44  struct count_children<Children,n,n,atEnd>
45  {
46 
47  static const std::size_t value = 0;
48 
49  };
50 
51  } // anonymous namespace
52 
53 
55 
75  template<typename T>
77  {
79  typedef T& type;
80 
82  static T default_value()
83  {
84  dune_static_assert((AlwaysFalse<T>::value), "You must provide a constructor parameter for every non-empty child!");
85  DUNE_THROW(NotImplemented,"You must provide a constructor parameter for every non-empty child!");
86  }
87  };
88 
89 #ifndef DOXYGEN
90 
92  template<>
93  struct OptionalChild<EmptyNode>
94  {
95  typedef EmptyNode type;
96 
97  static EmptyNode default_value()
98  {
99  return EmptyNode();
100  }
101  };
102 
103 #endif // DOXYGEN
104 
105 
115  template<typename C0, typename C1 = EmptyNode, typename C2 = EmptyNode, typename C3 = EmptyNode, typename C4 = EmptyNode,
116  typename C5 = EmptyNode, typename C6 = EmptyNode, typename C7 = EmptyNode, typename C8 = EmptyNode, typename C9 = EmptyNode>
118  {
119 
120  public:
121 
123  typedef tuple<shared_ptr<C0>,
124  shared_ptr<C1>,
125  shared_ptr<C2>,
126  shared_ptr<C3>,
127  shared_ptr<C4>,
128  shared_ptr<C5>,
129  shared_ptr<C6>,
130  shared_ptr<C7>,
131  shared_ptr<C8>,
132  shared_ptr<C9>
134 
136  typedef tuple<C0,C1,C2,C3,C4,C5,C6,C7,C8,C9> ChildTypes;
137 
139  static const bool isLeaf = false;
140 
142  static const bool isComposite = true;
143 
145  static const bool isPower = false;
146 
149 
150 #ifdef DOXYGEN
151  static const std::size_t CHILDREN = implementation-defined;
153 #else
155 #endif
156 
158  template<std::size_t k>
159  struct Child {
160 
162  typedef typename tuple_element<k,ChildTypes>::type Type;
163 
165  typedef typename tuple_element<k,ChildTypes>::type type;
166 
168  typedef typename tuple_element<k,NodeStorage>::type Storage;
169 
171  typedef shared_ptr<const typename tuple_element<k,ChildTypes>::type> ConstStorage;
172  };
173 
174 
177 
179 
182  template<std::size_t k>
183  const typename Child<k>::Type& child() const
184  {
185  dune_static_assert((k < CHILDREN), "child index out of range");
186  return *get<k>(_children);
187  }
188 
190 
193  template<std::size_t k>
194  typename Child<k>::Type& child()
195  {
196  dune_static_assert((k < CHILDREN), "child index out of range");
197  return *get<k>(_children);
198  }
199 
201 
207  template<std::size_t k>
209  {
210  dune_static_assert((k < 10), "child index out of range");
211  return get<k>(_children);
212  }
213 
215 
218  template<std::size_t k>
220  {
221  dune_static_assert((k < 10), "child index out of range");
222  return get<k>(_children);
223  }
224 
226  template<std::size_t k>
227  void setChild(typename Child<k>::Type& child)
228  {
229  dune_static_assert((k < CHILDREN), "child index out of range");
230  get<k>(_children) = stackobject_to_shared_ptr(child);
231  }
232 
234  template<std::size_t k>
236  {
237  dune_static_assert((k < CHILDREN), "child index out of range");
238  get<k>(_children) = child;
239  }
240 
241  const NodeStorage& nodeStorage() const
242  {
243  return _children;
244  }
245 
247 
248  private:
249 
251 
255  template<typename T>
256  static shared_ptr<T> guarded_wrap_object(T& t)
257  {
258  return stackobject_to_shared_ptr(t);
259  }
260 
262 
266  static shared_ptr<EmptyNode> guarded_wrap_object(EmptyNode& en)
267  {
268  return emptyNodePtr();
269  }
270 
271  protected:
272 
274 
283  {}
284 
296  : _children(stackobject_to_shared_ptr(c0),
297  guarded_wrap_object(c1),
298  guarded_wrap_object(c2),
299  guarded_wrap_object(c3),
300  guarded_wrap_object(c4),
301  guarded_wrap_object(c5),
302  guarded_wrap_object(c6),
303  guarded_wrap_object(c7),
304  guarded_wrap_object(c8),
305  guarded_wrap_object(c9))
306  {}
307 
309  CompositeNode(shared_ptr<C0> c0,
310  shared_ptr<C1> c1,
311  shared_ptr<C2> c2,
312  shared_ptr<C3> c3,
313  shared_ptr<C4> c4,
314  shared_ptr<C5> c5,
315  shared_ptr<C6> c6,
316  shared_ptr<C7> c7,
317  shared_ptr<C8> c8,
318  shared_ptr<C9> c9)
319  : _children(c0,c1,c2,c3,c4,c5,c6,c7,c8,c9)
320  {}
321 
323  CompositeNode(const NodeStorage& children)
324  : _children(children)
325  {}
326 
327  private:
328  NodeStorage _children;
329  };
330 
332 
333  } // namespace TypeTree
334 } //namespace Dune
335 
336 #endif // DUNE_TYPETREE_COMPOSITENODE_HH
T & type
The correct child type.
Definition: compositenode.hh:79
tuple< shared_ptr< C0 >, shared_ptr< C1 >, shared_ptr< C2 >, shared_ptr< C3 >, shared_ptr< C4 >, shared_ptr< C5 >, shared_ptr< C6 >, shared_ptr< C7 >, shared_ptr< C8 >, shared_ptr< C9 > > NodeStorage
The type used for storing the children.
Definition: compositenode.hh:133
Child< k >::Storage childStorage()
Returns the storage of the i-th child.
Definition: compositenode.hh:219
void setChild(typename Child< k >::Type &child)
Sets the i-th child to the passed-in value.
Definition: compositenode.hh:227
static const bool isComposite
Mark this class as a composite in the TypeTree.
Definition: compositenode.hh:142
tuple< C0, C1, C2, C3, C4, C5, C6, C7, C8, C9 > ChildTypes
The types of all children.
Definition: compositenode.hh:136
Implementation Helper for constructors of composite nodes.
Definition: compositenode.hh:76
Base class for composite nodes combining children of different types within a TypeTree.
Definition: compositenode.hh:117
CompositeNode(const NodeStorage &children)
Initializes the CompositeNode from the passed-in NodeStorage object.
Definition: compositenode.hh:323
void setChild(typename Child< k >::Storage child)
Sets the stored value representing the i-th child to the passed-in value.
Definition: compositenode.hh:235
Child< k >::Type & child()
Returns the i-th child.
Definition: compositenode.hh:194
Access to the type and storage type of the i-th child.
Definition: compositenode.hh:159
CompositeNode(C0 &c0, typename OptionalChild< C1 >::type c1=OptionalChild< C1 >::default_value(), typename OptionalChild< C2 >::type c2=OptionalChild< C2 >::default_value(), typename OptionalChild< C3 >::type c3=OptionalChild< C3 >::default_value(), typename OptionalChild< C4 >::type c4=OptionalChild< C4 >::default_value(), typename OptionalChild< C5 >::type c5=OptionalChild< C5 >::default_value(), typename OptionalChild< C6 >::type c6=OptionalChild< C6 >::default_value(), typename OptionalChild< C7 >::type c7=OptionalChild< C7 >::default_value(), typename OptionalChild< C8 >::type c8=OptionalChild< C8 >::default_value(), typename OptionalChild< C9 >::type c9=OptionalChild< C9 >::default_value())
Initializes the CompositeNode with the passed-in child objects.
Definition: compositenode.hh:286
static const std::size_t CHILDREN
The number of children of the CompositeNode.
Definition: compositenode.hh:152
tuple_element< k, NodeStorage >::type Storage
The storage type of the child.
Definition: compositenode.hh:168
const shared_ptr< EmptyNode > & emptyNodePtr()
Reference to a pointer to an empty node that is used for all empty slots.
Definition: utility.cc:12
CompositeNodeTag NodeTag
The type tag that describes a CompositeNode.
Definition: compositenode.hh:148
static T default_value()
Method providing a default value for empty children.
Definition: compositenode.hh:82
static const bool isLeaf
Mark this class as non leaf in the TypeTree.
Definition: compositenode.hh:139
tuple_element< k, ChildTypes >::type Type
The type of the child.
Definition: compositenode.hh:162
shared_ptr< const typename tuple_element< k, ChildTypes >::type > ConstStorage
The const storage type of the child.
Definition: compositenode.hh:171
CompositeNode(shared_ptr< C0 > c0, shared_ptr< C1 > c1, shared_ptr< C2 > c2, shared_ptr< C3 > c3, shared_ptr< C4 > c4, shared_ptr< C5 > c5, shared_ptr< C6 > c6, shared_ptr< C7 > c7, shared_ptr< C8 > c8, shared_ptr< C9 > c9)
Initializes the CompositeNode with copies of the passed-in storage objects.
Definition: compositenode.hh:309
static const bool isPower
Mark this class as a non power in the typeTree.
Definition: compositenode.hh:145
static const std::size_t value
Definition: compositenode.hh:38
const NodeStorage & nodeStorage() const
Definition: compositenode.hh:241
tuple_element< k, ChildTypes >::type type
The type of the child.
Definition: compositenode.hh:165
const Child< k >::Type & child() const
Returns the i-th child (const version).
Definition: compositenode.hh:183
Tag designating a composite node.
Definition: nodetags.hh:22
static const bool emptyNode
Definition: compositenode.hh:34
Child< k >::ConstStorage childStorage() const
Returns the storage of the i-th child (const version).
Definition: compositenode.hh:208
CompositeNode()
Default constructor.
Definition: compositenode.hh:282