dune-typetree  2.5-dev
filteredcompositenode.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_FILTEREDCOMPOSITENODE_HH
5 #define DUNE_TYPETREE_FILTEREDCOMPOSITENODE_HH
6 
7 #include <memory>
8 #include <tuple>
9 #include <type_traits>
10 
12 #include <dune/typetree/filters.hh>
13 #include <dune/common/shared_ptr.hh>
14 #include <dune/common/typetraits.hh>
15 
16 #include <dune/typetree/filters.hh>
18 
19 namespace Dune {
20  namespace TypeTree {
21 
27 #ifndef DOXYGEN
28  namespace {
29 
30  // ********************************************************************************
31  // Utility structs for filter construction and application
32  // ********************************************************************************
33 
34  // Gets the filter and wraps it in case of a SimpleFilter.
35  template<typename Filter, typename Tag>
36  struct get_filter;
37 
38  // Helper struct to extract the child template parameter pack from the ChildTypes tuple.
39  template<typename Filter, typename Node, typename ChildTypes>
40  struct apply_filter_wrapper;
41 
42  template<typename Filter, typename Node, typename... Children>
43  struct apply_filter_wrapper<Filter,Node,std::tuple<Children...> >
44  : public Filter::template apply<Node,Children...>
45  {};
46 
47  // specialization for SimpleFilter
48  template<typename Filter>
49  struct get_filter<Filter,SimpleFilterTag>
50  {
51  struct type
52  {
53  template<typename Node, typename ChildTypes>
54  struct apply
55  : public apply_filter_wrapper<filter<Filter>,Node,ChildTypes>
56  {};
57  };
58  };
59 
60  // specialization for AdvancedFilter
61  template<typename Filter>
62  struct get_filter<Filter,AdvancedFilterTag>
63  {
64  struct type
65  {
66  template<typename Node, typename ChildTypes>
67  struct apply
68  : public apply_filter_wrapper<Filter,Node,ChildTypes>
69  {};
70  };
71  };
72 
73  } // anonymous namespace
74 #endif // DOXYGEN
75 
76 
78  template<typename Node, typename Filter>
80  {
81 
82  typedef typename get_filter<Filter,typename Filter::FilterTag>::type filter;
83  typedef typename filter::template apply<Node,typename Node::ChildTypes>::type filter_result;
84  typedef typename filter_result::template apply<Node> mapped_children;
85 
86  static const bool nodeIsConst = std::is_const<typename std::remove_reference<Node>::type>::value;
87 
88  template<std::size_t k>
89  struct lazy_enable
90  {
91  static const bool value = !nodeIsConst;
92  };
93 
94  public:
95 
98 
100  typedef typename mapped_children::NodeStorage NodeStorage;
101 
103  typedef typename mapped_children::ChildTypes ChildTypes;
104 
106  static const bool isLeaf = false;
107 
109  static const bool isPower = false;
110 
112  static const bool isComposite = true;
113 
115  static const std::size_t CHILDREN = filter_result::size;
116 
117  static constexpr std::size_t degree()
118  {
119  return filter_result::size;
120  }
121 
123  template<std::size_t k>
124  struct Child {
125 
126 #ifndef DOXYGEN
127 
128  typedef typename std::tuple_element<k,typename mapped_children::Children>::type OriginalChild;
129 
130  static const std::size_t mapped_index = std::tuple_element<k,typename filter_result::IndexMap>::type::original_index;
131 
132 #endif // DOXYGEN
133 
135  typedef typename OriginalChild::Type Type;
136 
138  typedef typename OriginalChild::type type;
139 
141  typedef typename OriginalChild::Storage Storage;
142 
144  typedef typename OriginalChild::ConstStorage ConstStorage;
145  };
146 
149 
151 
154  template<std::size_t k>
155  typename std::enable_if<lazy_enable<k>::value,typename Child<k>::Type&>::type
157  {
158  return _node->template child<Child<k>::mapped_index>();
159  }
160 
162 
165  template<std::size_t k>
166  const typename Child<k>::Type& child() const
167  {
168  return _node->template child<Child<k>::mapped_index>();
169  }
170 
172 
175  template<std::size_t k>
176  typename std::enable_if<lazy_enable<k>::value,typename Child<k>::Storage>::type
178  {
179  return _node->template childStorage<Child<k>::mapped_index>();
180  }
181 
183 
189  template<std::size_t k>
191  {
192  return _node->template childStorage<Child<k>::mapped_index>();
193  }
194 
196  template<std::size_t k>
197  void setChild(typename Child<k>::type& child, typename std::enable_if<lazy_enable<k>::value,void*>::type = 0)
198  {
199  _node->template childStorage<Child<k>::mapped_index>() = stackobject_to_shared_ptr(child);
200  }
201 
203  template<std::size_t k>
204  void setChild(typename Child<k>::storage_type child, typename std::enable_if<lazy_enable<k>::value,void*>::type = 0)
205  {
206  _node->template childStorage<Child<k>::mapped_index>() = child;
207  }
208 
210 
213 
214  protected:
215 
217 
220  template<bool enabled = !nodeIsConst>
221  typename std::enable_if<enabled,Node&>::type
223  {
224  return *_node;
225  }
226 
228 
231  const Node& unfiltered() const
232  {
233  return *_node;
234  }
235 
237 
240  template<bool enabled = !nodeIsConst>
241  typename std::enable_if<enabled,std::shared_ptr<Node> >::type
243  {
244  return _node;
245  }
246 
248 
251  std::shared_ptr<const Node> unfilteredStorage() const
252  {
253  return _node;
254  }
255 
257 
258  public:
259 
262 
264  FilteredCompositeNode(std::shared_ptr<Node> node)
265  : _node(node)
266  {}
267 
270  : _node(stackobject_to_shared_ptr(node))
271  {}
272 
274 
275  private:
276  std::shared_ptr<Node> _node;
277  };
278 
280 
281  } // namespace TypeTree
282 } //namespace Dune
283 
284 #endif // DUNE_TYPETREE_FILTEREDCOMPOSITENODE_HH
mapped_children::ChildTypes ChildTypes
A tuple storing the types of all children.
Definition: filteredcompositenode.hh:103
Access to the type and storage type of the i-th child.
Definition: filteredcompositenode.hh:124
ImplementationDefined child(Node &&node, Indices... indices)
Extracts the child of a node given by a sequence of compile-time and run-time indices.
Definition: childextraction.hh:179
Tag designating a composite node.
Definition: nodetags.hh:22
OriginalChild::type type
The type of the child.
Definition: filteredcompositenode.hh:138
void setChild(typename Child< k >::storage_type child, typename std::enable_if< lazy_enable< k >::value, void *>::type=0)
Sets the storage of the i-th child to the passed-in value.
Definition: filteredcompositenode.hh:204
OriginalChild::ConstStorage ConstStorage
The const storage type of the child.
Definition: filteredcompositenode.hh:144
std::enable_if< enabled, Node & >::type unfiltered()
Returns the unfiltered node.
Definition: filteredcompositenode.hh:222
Definition: accumulate_static.hh:13
const Child< k >::Type & child() const
Returns the i-th child (const version).
Definition: filteredcompositenode.hh:166
std::shared_ptr< const Node > unfilteredStorage() const
Returns the storage object of the unfiltered node (const version).
Definition: filteredcompositenode.hh:251
std::enable_if< enabled, std::shared_ptr< Node > >::type unfilteredStorage()
Returns the storage object of the unfiltered node.
Definition: filteredcompositenode.hh:242
STL namespace.
mapped_children::NodeStorage NodeStorage
The type used for storing the children.
Definition: filteredcompositenode.hh:100
std::enable_if< lazy_enable< k >::value, typename Child< k >::Type & >::type child()
Returns the i-th child.
Definition: filteredcompositenode.hh:156
void setChild(typename Child< k >::type &child, typename std::enable_if< lazy_enable< k >::value, void *>::type=0)
Sets the i-th child to the passed-in value.
Definition: filteredcompositenode.hh:197
std::enable_if< lazy_enable< k >::value, typename Child< k >::Storage >::type childStorage()
Returns the storage of the i-th child.
Definition: filteredcompositenode.hh:177
OriginalChild::Storage Storage
The storage type of the child.
Definition: filteredcompositenode.hh:141
const Node & unfiltered() const
Returns the unfiltered node (const version).
Definition: filteredcompositenode.hh:231
CompositeNodeTag NodeTag
The type tag that describes a CompositeNode.
Definition: filteredcompositenode.hh:97
FilteredCompositeNode(Node &node)
Initialize the CompositeNode with a copy of the passed-in storage type.
Definition: filteredcompositenode.hh:269
OriginalChild::Type Type
The type of the child.
Definition: filteredcompositenode.hh:135
Child< k >::ConstStorage childStorage() const
Returns the storage of the i-th child (const version).
Definition: filteredcompositenode.hh:190
FilteredCompositeNode(std::shared_ptr< Node > node)
Initialize the CompositeNode with copies of the passed in Storage objects.
Definition: filteredcompositenode.hh:264
static constexpr std::size_t degree()
Definition: filteredcompositenode.hh:117
Base class for composite nodes representing a filtered view on an underlying composite node...
Definition: filteredcompositenode.hh:79
Type
Definition: treepath.hh:26