dune-typetree  2.4.1
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 
11 #include <dune/typetree/filters.hh>
12 #include <dune/common/shared_ptr.hh>
13 #include <dune/common/typetraits.hh>
14 
15 #include <dune/typetree/filters.hh>
17 
18 namespace Dune {
19  namespace TypeTree {
20 
26 #ifndef DOXYGEN
27  namespace {
28 
29  // ********************************************************************************
30  // Utility structs for filter construction and application
31  // ********************************************************************************
32 
33  // Gets the filter and wraps it in case of a SimpleFilter.
34  template<typename Filter, typename Tag>
35  struct get_filter;
36 
37  // Helper struct to extract the child template parameter pack from the ChildTypes tuple.
38  template<typename Filter, typename Node, typename ChildTypes>
39  struct apply_filter_wrapper;
40 
41  template<typename Filter, typename Node, typename... Children>
42  struct apply_filter_wrapper<Filter,Node,std::tuple<Children...> >
43  : public Filter::template apply<Node,Children...>
44  {};
45 
46  // specialization for SimpleFilter
47  template<typename Filter>
48  struct get_filter<Filter,SimpleFilterTag>
49  {
50  struct type
51  {
52  template<typename Node, typename ChildTypes>
53  struct apply
54  : public apply_filter_wrapper<filter<Filter>,Node,ChildTypes>
55  {};
56  };
57  };
58 
59  // specialization for AdvancedFilter
60  template<typename Filter>
61  struct get_filter<Filter,AdvancedFilterTag>
62  {
63  struct type
64  {
65  template<typename Node, typename ChildTypes>
66  struct apply
67  : public apply_filter_wrapper<Filter,Node,ChildTypes>
68  {};
69  };
70  };
71 
72  } // anonymous namespace
73 #endif // DOXYGEN
74 
75 
77  template<typename Node, typename Filter>
79  {
80 
81  typedef typename get_filter<Filter,typename Filter::FilterTag>::type filter;
82  typedef typename filter::template apply<Node,typename Node::ChildTypes>::type filter_result;
83  typedef typename filter_result::template apply<Node> mapped_children;
84 
85  static const bool nodeIsConst = IsConst<typename remove_reference<Node>::type>::value;
86 
87  template<std::size_t k>
88  struct lazy_enable
89  {
90  static const bool value = !nodeIsConst;
91  };
92 
93  public:
94 
97 
99  typedef typename mapped_children::NodeStorage NodeStorage;
100 
102  typedef typename mapped_children::ChildTypes ChildTypes;
103 
105  static const bool isLeaf = false;
106 
108  static const bool isPower = false;
109 
111  static const bool isComposite = true;
112 
114  static const std::size_t CHILDREN = filter_result::size;
115 
117  template<std::size_t k>
118  struct Child {
119 
120 #ifndef DOXYGEN
121 
122  typedef typename std::tuple_element<k,typename mapped_children::Children>::type OriginalChild;
123 
124  static const std::size_t mapped_index = std::tuple_element<k,typename filter_result::IndexMap>::type::original_index;
125 
126 #endif // DOXYGEN
127 
129  typedef typename OriginalChild::Type Type;
130 
132  typedef typename OriginalChild::type type;
133 
135  typedef typename OriginalChild::Storage Storage;
136 
138  typedef typename OriginalChild::ConstStorage ConstStorage;
139  };
140 
143 
145 
148  template<std::size_t k>
149  typename enable_if<lazy_enable<k>::value,typename Child<k>::Type&>::type
151  {
152  return _node->template child<Child<k>::mapped_index>();
153  }
154 
156 
159  template<std::size_t k>
160  const typename Child<k>::Type& child() const
161  {
162  return _node->template child<Child<k>::mapped_index>();
163  }
164 
166 
169  template<std::size_t k>
170  typename enable_if<lazy_enable<k>::value,typename Child<k>::Storage>::type
172  {
173  return _node->template childStorage<Child<k>::mapped_index>();
174  }
175 
177 
183  template<std::size_t k>
185  {
186  return _node->template childStorage<Child<k>::mapped_index>();
187  }
188 
190  template<std::size_t k>
191  void setChild(typename Child<k>::type& child, typename enable_if<lazy_enable<k>::value,void*>::type = 0)
192  {
193  _node->template childStorage<Child<k>::mapped_index>() = stackobject_to_shared_ptr(child);
194  }
195 
197  template<std::size_t k>
198  void setChild(typename Child<k>::storage_type child, typename enable_if<lazy_enable<k>::value,void*>::type = 0)
199  {
200  _node->template childStorage<Child<k>::mapped_index>() = child;
201  }
202 
204 
207 
208  protected:
209 
211 
214  template<bool enabled = !nodeIsConst>
215  typename enable_if<enabled,Node&>::type
217  {
218  return *_node;
219  }
220 
222 
225  const Node& unfiltered() const
226  {
227  return *_node;
228  }
229 
231 
234  template<bool enabled = !nodeIsConst>
235  typename enable_if<enabled,std::shared_ptr<Node> >::type
237  {
238  return _node;
239  }
240 
242 
245  std::shared_ptr<const Node> unfilteredStorage() const
246  {
247  return _node;
248  }
249 
251 
252  public:
253 
256 
258  FilteredCompositeNode(std::shared_ptr<Node> node)
259  : _node(node)
260  {}
261 
264  : _node(stackobject_to_shared_ptr(node))
265  {}
266 
268 
269  private:
270  std::shared_ptr<Node> _node;
271  };
272 
274 
275  } // namespace TypeTree
276 } //namespace Dune
277 
278 #endif // DUNE_TYPETREE_FILTEREDCOMPOSITENODE_HH
enable_if< lazy_enable< k >::value, typename Child< k >::Storage >::type childStorage()
Returns the storage of the i-th child.
Definition: filteredcompositenode.hh:171
const Child< k >::Type & child() const
Returns the i-th child (const version).
Definition: filteredcompositenode.hh:160
OriginalChild::Storage Storage
The storage type of the child.
Definition: filteredcompositenode.hh:135
Access to the type and storage type of the i-th child.
Definition: filteredcompositenode.hh:118
FilteredCompositeNode(std::shared_ptr< Node > node)
Initialize the CompositeNode with copies of the passed in Storage objects.
Definition: filteredcompositenode.hh:258
OriginalChild::type type
The type of the child.
Definition: filteredcompositenode.hh:132
STL namespace.
OriginalChild::Type Type
The type of the child.
Definition: filteredcompositenode.hh:129
CompositeNodeTag NodeTag
The type tag that describes a CompositeNode.
Definition: filteredcompositenode.hh:96
OriginalChild::ConstStorage ConstStorage
The const storage type of the child.
Definition: filteredcompositenode.hh:138
enable_if< lazy_enable< k >::value, typename Child< k >::Type & >::type child()
Returns the i-th child.
Definition: filteredcompositenode.hh:150
enable_if< enabled, std::shared_ptr< Node > >::type unfilteredStorage()
Returns the storage object of the unfiltered node.
Definition: filteredcompositenode.hh:236
void setChild(typename Child< k >::storage_type child, typename 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:198
const Node & unfiltered() const
Returns the unfiltered node (const version).
Definition: filteredcompositenode.hh:225
Type
Definition: treepath.hh:26
FilteredCompositeNode(Node &node)
Initialize the CompositeNode with a copy of the passed-in storage type.
Definition: filteredcompositenode.hh:263
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:472
void setChild(typename Child< k >::type &child, typename enable_if< lazy_enable< k >::value, void * >::type=0)
Sets the i-th child to the passed-in value.
Definition: filteredcompositenode.hh:191
enable_if< enabled, Node & >::type unfiltered()
Returns the unfiltered node.
Definition: filteredcompositenode.hh:216
mapped_children::NodeStorage NodeStorage
The type used for storing the children.
Definition: filteredcompositenode.hh:99
mapped_children::ChildTypes ChildTypes
A tuple storing the types of all children.
Definition: filteredcompositenode.hh:102
Base class for composite nodes representing a filtered view on an underlying composite node...
Definition: filteredcompositenode.hh:78
Child< k >::ConstStorage childStorage() const
Returns the storage of the i-th child (const version).
Definition: filteredcompositenode.hh:184
Definition: accumulate_static.hh:12
std::shared_ptr< const Node > unfilteredStorage() const
Returns the storage object of the unfiltered node (const version).
Definition: filteredcompositenode.hh:245
Tag designating a composite node.
Definition: nodetags.hh:22