dune-typetree  2.4-dev
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 
8 #include <dune/common/tuples.hh>
9 #include <dune/common/shared_ptr.hh>
10 
11 namespace Dune {
12  namespace TypeTree {
13 
19  template<typename... Children>
22  {
23 
24  public:
25 
28 
30  typedef tuple<shared_ptr<Children>... > NodeStorage;
31 
33  typedef tuple<Children...> ChildTypes;
34 
36  static const bool isLeaf = false;
37 
39  static const bool isPower = false;
40 
42  static const bool isComposite = true;
43 
45  static const std::size_t CHILDREN = sizeof...(Children);
46 
48  template<std::size_t k>
49  struct Child {
50 
51  static_assert((k < CHILDREN), "child index out of range");
52 
54  typedef typename tuple_element<k,ChildTypes>::type Type;
55 
57  typedef typename tuple_element<k,ChildTypes>::type type;
58 
60  typedef typename tuple_element<k,NodeStorage>::type Storage;
61 
63  typedef shared_ptr<const typename tuple_element<k,ChildTypes>::type> ConstStorage;
64  };
65 
68 
70 
73  template<std::size_t k>
74  typename Child<k>::Type& child()
75  {
76  return *get<k>(_children);
77  }
78 
80 
83  template<std::size_t k>
84  const typename Child<k>::Type& child() const
85  {
86  return *get<k>(_children);
87  }
88 
90 
93  template<std::size_t k>
95  {
96  return get<k>(_children);
97  }
98 
100 
106  template<std::size_t k>
108  {
109  return get<k>(_children);
110  }
111 
113  template<std::size_t k>
114  void setChild(typename Child<k>::Type& child)
115  {
116  get<k>(_children) = stackobject_to_shared_ptr(child);
117  }
118 
120  template<std::size_t k>
122  {
123  get<k>(_children) = child;
124  }
125 
126  const NodeStorage& nodeStorage() const
127  {
128  return _children;
129  }
130 
132 
133  protected:
134 
137 
139 
147  {}
148 
150  template<typename... Args, typename = typename enable_if<(sizeof...(Args) == CHILDREN)>::type>
151  CompositeNode(Args&&... args)
152  : _children(convert_arg(std::forward<Args>(args))...)
153  {}
154 
156  CompositeNode(shared_ptr<Children>... children)
157  : _children(children...)
158  {}
159 
161  CompositeNode(const NodeStorage& children)
162  : _children(children)
163  {}
164 
166 
167  private:
168  NodeStorage _children;
169  };
170 
172 
173  } // namespace TypeTree
174 } //namespace Dune
175 
176 #endif // DUNE_TYPETREE_COMPOSITENODE_HH
tuple_element< k, NodeStorage >::type Storage
The storage type of the child.
Definition: compositenode.hh:60
static const bool isPower
Mark this class as a non power in the dune-typetree.
Definition: compositenode.hh:39
Definition: accumulate_static.hh:12
void setChild(typename Child< k >::Type &child)
Sets the i-th child to the passed-in value.
Definition: compositenode.hh:114
CompositeNode(const NodeStorage &children)
Initialize the CompositeNode with a copy of the passed-in storage type.
Definition: compositenode.hh:161
tuple_element< k, ChildTypes >::type Type
The type of the child.
Definition: compositenode.hh:51
const NodeStorage & nodeStorage() const
Definition: compositenode.hh:126
CompositeNodeTag NodeTag
The type tag that describes a CompositeNode.
Definition: compositenode.hh:27
Tag designating a composite node.
Definition: nodetags.hh:22
STL namespace.
shared_ptr< const typename tuple_element< k, ChildTypes >::type > ConstStorage
The const storage type of the child.
Definition: compositenode.hh:63
Child< k >::ConstStorage childStorage() const
Returns the storage of the i-th child (const version).
Definition: compositenode.hh:107
const Child< k >::Type & child() const
Returns the i-th child (const version).
Definition: compositenode.hh:84
CompositeNode()
Default constructor.
Definition: compositenode.hh:146
Base class for composite nodes based on variadic templates.
Definition: compositenode.hh:21
CompositeNode(shared_ptr< Children >...children)
Initialize the CompositeNode with copies of the passed in Storage objects.
Definition: compositenode.hh:156
Access to the type and storage type of the i-th child.
Definition: compositenode.hh:49
Child< k >::Storage childStorage()
Returns the storage of the i-th child.
Definition: compositenode.hh:94
static const bool isComposite
Mark this class as a composite in the dune-typetree.
Definition: compositenode.hh:42
static const bool isLeaf
Mark this class as non leaf in the dune-typetree.
Definition: compositenode.hh:36
void setChild(typename Child< k >::Storage child)
Sets the storage of the i-th child to the passed-in value.
Definition: compositenode.hh:121
tuple< shared_ptr< Children >... > NodeStorage
The type used for storing the children.
Definition: compositenode.hh:30
CompositeNode(Args &&...args)
Initialize all children with the passed-in objects.
Definition: compositenode.hh:151
Child< k >::Type & child()
Returns the i-th child.
Definition: compositenode.hh:74
static const std::size_t CHILDREN
The number of children.
Definition: compositenode.hh:45
tuple< Children...> ChildTypes
A tuple storing the types of all children.
Definition: compositenode.hh:33
tuple_element< k, ChildTypes >::type type
The type of the child.
Definition: compositenode.hh:57