dune-typetree  2.4-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 <dune/common/shared_ptr.hh>
8 
10 #include <dune/typetree/filters.hh>
11 #include <dune/common/tuples.hh>
12 #include <dune/common/typetraits.hh>
13 
14 #include <dune/typetree/filters.hh>
16 
17 namespace Dune {
18  namespace TypeTree {
19 
25 #ifndef DOXYGEN
26  namespace {
27 
28  // ********************************************************************************
29  // Utility structs for filter construction and application
30  // ********************************************************************************
31 
32  // Gets the filter and wraps it in case of a SimpleFilter.
33  template<typename Filter, typename Tag>
34  struct get_filter;
35 
36  // Helper struct to extract the child template parameter pack from the ChildTypes tuple.
37  template<typename Filter, typename Node, typename ChildTypes>
38  struct apply_filter_wrapper;
39 
40  template<typename Filter, typename Node, typename... Children>
41  struct apply_filter_wrapper<Filter,Node,tuple<Children...> >
42  : public Filter::template apply<Node,Children...>
43  {};
44 
45  // specialization for SimpleFilter
46  template<typename Filter>
47  struct get_filter<Filter,SimpleFilterTag>
48  {
49  struct type
50  {
51  template<typename Node, typename ChildTypes>
52  struct apply
53  : public apply_filter_wrapper<filter<Filter>,Node,ChildTypes>
54  {};
55  };
56  };
57 
58  // specialization for AdvancedFilter
59  template<typename Filter>
60  struct get_filter<Filter,AdvancedFilterTag>
61  {
62  struct type
63  {
64  template<typename Node, typename ChildTypes>
65  struct apply
66  : public apply_filter_wrapper<Filter,Node,ChildTypes>
67  {};
68  };
69  };
70 
71  } // anonymous namespace
72 #endif // DOXYGEN
73 
74 
76  template<typename Node, typename Filter>
78  {
79 
80  typedef typename get_filter<Filter,typename Filter::FilterTag>::type filter;
81  typedef typename filter::template apply<Node,typename Node::ChildTypes>::type filter_result;
82  typedef typename filter_result::template apply<Node> mapped_children;
83 
84  static const bool nodeIsConst = IsConst<typename remove_reference<Node>::type>::value;
85 
86  template<std::size_t k>
87  struct lazy_enable
88  {
89  static const bool value = !nodeIsConst;
90  };
91 
92  public:
93 
96 
98  typedef typename mapped_children::NodeStorage NodeStorage;
99 
101  typedef typename mapped_children::ChildTypes ChildTypes;
102 
104  static const bool isLeaf = false;
105 
107  static const bool isPower = false;
108 
110  static const bool isComposite = true;
111 
113  static const std::size_t CHILDREN = filter_result::size;
114 
116  template<std::size_t k>
117  struct Child {
118 
119 #ifndef DOXYGEN
120 
121  typedef typename tuple_element<k,typename mapped_children::Children>::type OriginalChild;
122 
123  static const std::size_t mapped_index = tuple_element<k,typename filter_result::IndexMap>::type::original_index;
124 
125 #endif // DOXYGEN
126 
128  typedef typename OriginalChild::Type Type;
129 
131  typedef typename OriginalChild::type type;
132 
134  typedef typename OriginalChild::Storage Storage;
135 
137  typedef typename OriginalChild::ConstStorage ConstStorage;
138  };
139 
142 
144 
147  template<std::size_t k>
148  typename enable_if<lazy_enable<k>::value,typename Child<k>::Type&>::type
150  {
151  return _node->template child<Child<k>::mapped_index>();
152  }
153 
155 
158  template<std::size_t k>
159  const typename Child<k>::Type& child() const
160  {
161  return _node->template child<Child<k>::mapped_index>();
162  }
163 
165 
168  template<std::size_t k>
169  typename enable_if<lazy_enable<k>::value,typename Child<k>::Storage>::type
171  {
172  return _node->template childStorage<Child<k>::mapped_index>();
173  }
174 
176 
182  template<std::size_t k>
184  {
185  return _node->template childStorage<Child<k>::mapped_index>();
186  }
187 
189  template<std::size_t k>
190  void setChild(typename Child<k>::type& child, typename enable_if<lazy_enable<k>::value,void*>::type = 0)
191  {
192  _node->template childStorage<Child<k>::mapped_index>() = stackobject_to_shared_ptr(child);
193  }
194 
196  template<std::size_t k>
197  void setChild(typename Child<k>::storage_type child, typename enable_if<lazy_enable<k>::value,void*>::type = 0)
198  {
199  _node->template childStorage<Child<k>::mapped_index>() = child;
200  }
201 
203 
206 
207  protected:
208 
210 
213  template<bool enabled = !nodeIsConst>
214  typename enable_if<enabled,Node&>::type
216  {
217  return *_node;
218  }
219 
221 
224  const Node& unfiltered() const
225  {
226  return *_node;
227  }
228 
230 
233  template<bool enabled = !nodeIsConst>
234  typename enable_if<enabled,shared_ptr<Node> >::type
236  {
237  return _node;
238  }
239 
241 
244  shared_ptr<const Node> unfilteredStorage() const
245  {
246  return _node;
247  }
248 
250 
251  public:
252 
255 
257  FilteredCompositeNode(shared_ptr<Node> node)
258  : _node(node)
259  {}
260 
263  : _node(stackobject_to_shared_ptr(node))
264  {}
265 
267 
268  private:
269  shared_ptr<Node> _node;
270  };
271 
273 
274  } // namespace TypeTree
275 } //namespace Dune
276 
277 #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:170
CompositeNodeTag NodeTag
The type tag that describes a CompositeNode.
Definition: filteredcompositenode.hh:95
static const bool isPower
Mark this class as a non power in the dune-typetree.
Definition: filteredcompositenode.hh:107
Base class for composite nodes representing a filtered view on an underlying composite node...
Definition: filteredcompositenode.hh:77
OriginalChild::Storage Storage
The storage type of the child.
Definition: filteredcompositenode.hh:134
OriginalChild::Type Type
The type of the child.
Definition: filteredcompositenode.hh:128
Definition: accumulate_static.hh:12
OriginalChild::ConstStorage ConstStorage
The const storage type of the child.
Definition: filteredcompositenode.hh:137
const Child< k >::Type & child() const
Returns the i-th child (const version).
Definition: filteredcompositenode.hh:159
FilteredCompositeNode(shared_ptr< Node > node)
Initialize the CompositeNode with copies of the passed in Storage objects.
Definition: filteredcompositenode.hh:257
Tag designating a composite node.
Definition: nodetags.hh:22
FilteredCompositeNode(Node &node)
Initialize the CompositeNode with a copy of the passed-in storage type.
Definition: filteredcompositenode.hh:262
enable_if< lazy_enable< k >::value, typename Child< k >::Type & >::type child()
Returns the i-th child.
Definition: filteredcompositenode.hh:149
shared_ptr< const Node > unfilteredStorage() const
Returns the storage object of the unfiltered node (const version).
Definition: filteredcompositenode.hh:244
Access to the type and storage type of the i-th child.
Definition: filteredcompositenode.hh:117
mapped_children::NodeStorage NodeStorage
The type used for storing the children.
Definition: filteredcompositenode.hh:98
OriginalChild::type type
The type of the child.
Definition: filteredcompositenode.hh:131
mapped_children::ChildTypes ChildTypes
A tuple storing the types of all children.
Definition: filteredcompositenode.hh:101
const Node & unfiltered() const
Returns the unfiltered node (const version).
Definition: filteredcompositenode.hh:224
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:190
Type
Definition: treepath.hh:25
enable_if< enabled, shared_ptr< Node > >::type unfilteredStorage()
Returns the storage object of the unfiltered node.
Definition: filteredcompositenode.hh:235
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:197
Child< k >::ConstStorage childStorage() const
Returns the storage of the i-th child (const version).
Definition: filteredcompositenode.hh:183
enable_if< enabled, Node & >::type unfiltered()
Returns the unfiltered node.
Definition: filteredcompositenode.hh:215
static const std::size_t CHILDREN
The number of children.
Definition: filteredcompositenode.hh:113
static const bool isLeaf
Mark this class as non leaf in the dune-typetree.
Definition: filteredcompositenode.hh:104
static const bool isComposite
Mark this class as a composite in the dune-typetree.
Definition: filteredcompositenode.hh:110