dune-typetree  2.7.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 #include <type_traits>
10 
12 #include <dune/typetree/filters.hh>
13 #include <dune/common/shared_ptr.hh>
14 #include <dune/common/typetraits.hh>
15 #include <dune/common/indices.hh>
16 
17 #include <dune/typetree/filters.hh>
19 
20 namespace Dune {
21  namespace TypeTree {
22 
28 #ifndef DOXYGEN
29  namespace {
30 
31  // ********************************************************************************
32  // Utility structs for filter construction and application
33  // ********************************************************************************
34 
35  // Gets the filter and wraps it in case of a SimpleFilter.
36  template<typename Filter, typename Tag>
37  struct get_filter;
38 
39  // Helper struct to extract the child template parameter pack from the ChildTypes tuple.
40  template<typename Filter, typename Node, typename ChildTypes>
41  struct apply_filter_wrapper;
42 
43  template<typename Filter, typename Node, typename... Children>
44  struct apply_filter_wrapper<Filter,Node,std::tuple<Children...> >
45  : public Filter::template apply<Node,Children...>
46  {};
47 
48  // specialization for SimpleFilter
49  template<typename Filter>
50  struct get_filter<Filter,SimpleFilterTag>
51  {
52  struct type
53  {
54  template<typename Node, typename ChildTypes>
55  struct apply
56  : public apply_filter_wrapper<filter<Filter>,Node,ChildTypes>
57  {};
58  };
59  };
60 
61  // specialization for AdvancedFilter
62  template<typename Filter>
63  struct get_filter<Filter,AdvancedFilterTag>
64  {
65  struct type
66  {
67  template<typename Node, typename ChildTypes>
68  struct apply
69  : public apply_filter_wrapper<Filter,Node,ChildTypes>
70  {};
71  };
72  };
73 
74  } // anonymous namespace
75 #endif // DOXYGEN
76 
77 
79  template<typename Node, typename Filter>
81  {
82 
83  typedef typename get_filter<Filter,typename Filter::FilterTag>::type filter;
84  typedef typename filter::template apply<Node,typename Node::ChildTypes>::type filter_result;
85  typedef typename filter_result::template apply<Node> mapped_children;
86 
87  static const bool nodeIsConst = std::is_const<typename std::remove_reference<Node>::type>::value;
88 
89  template<std::size_t k>
90  struct lazy_enable
91  {
92  static const bool value = !nodeIsConst;
93  };
94 
95  public:
96 
99 
101  typedef typename mapped_children::NodeStorage NodeStorage;
102 
104  typedef typename mapped_children::ChildTypes ChildTypes;
105 
107  static const bool isLeaf = false;
108 
110  static const bool isPower = false;
111 
113  static const bool isComposite = true;
114 
116  static const std::size_t CHILDREN = filter_result::size;
117 
118  static constexpr std::size_t degree()
119  {
120  return filter_result::size;
121  }
122 
124  template<std::size_t k>
125  struct Child {
126 
127 #ifndef DOXYGEN
128 
129  typedef typename std::tuple_element<k,typename mapped_children::Children>::type OriginalChild;
130 
131  static const std::size_t mapped_index = std::tuple_element<k,typename filter_result::IndexMap>::type::original_index;
132 
133 #endif // DOXYGEN
134 
136  typedef typename OriginalChild::Type Type;
137 
139  typedef typename OriginalChild::type type;
140 
142  typedef typename OriginalChild::Storage Storage;
143 
145  typedef typename OriginalChild::ConstStorage ConstStorage;
146  };
147 
150 
152 
155  template<std::size_t k>
156  typename std::enable_if<lazy_enable<k>::value,typename Child<k>::Type&>::type
157  child(Dune::index_constant<k> = {})
158  {
159  return _node->template child<Child<k>::mapped_index>();
160  }
161 
163 
166  template<std::size_t k>
167  const typename Child<k>::Type& child(Dune::index_constant<k> = {}) const
168  {
169  return _node->template child<Child<k>::mapped_index>();
170  }
171 
173 
176  template<std::size_t k>
177  typename std::enable_if<lazy_enable<k>::value,typename Child<k>::Storage>::type
179  {
180  return _node->template childStorage<Child<k>::mapped_index>();
181  }
182 
184 
190  template<std::size_t k>
192  {
193  return _node->template childStorage<Child<k>::mapped_index>();
194  }
195 
197  template<std::size_t k>
198  void setChild(typename Child<k>::type& child, typename std::enable_if<lazy_enable<k>::value,void*>::type = 0)
199  {
200  _node->template childStorage<Child<k>::mapped_index>() = stackobject_to_shared_ptr(child);
201  }
202 
204  template<std::size_t k>
205  void setChild(typename Child<k>::storage_type child, typename std::enable_if<lazy_enable<k>::value,void*>::type = 0)
206  {
207  _node->template childStorage<Child<k>::mapped_index>() = child;
208  }
209 
211 
214 
215  protected:
216 
218 
221  template<bool enabled = !nodeIsConst>
222  typename std::enable_if<enabled,Node&>::type
224  {
225  return *_node;
226  }
227 
229 
232  const Node& unfiltered() const
233  {
234  return *_node;
235  }
236 
238 
241  template<bool enabled = !nodeIsConst>
242  typename std::enable_if<enabled,std::shared_ptr<Node> >::type
244  {
245  return _node;
246  }
247 
249 
252  std::shared_ptr<const Node> unfilteredStorage() const
253  {
254  return _node;
255  }
256 
258 
259  public:
260 
263 
265  FilteredCompositeNode(std::shared_ptr<Node> node)
266  : _node(node)
267  {}
268 
271  : _node(stackobject_to_shared_ptr(node))
272  {}
273 
275 
276  private:
277  std::shared_ptr<Node> _node;
278  };
279 
281 
282  } // namespace TypeTree
283 } //namespace Dune
284 
285 #endif // DUNE_TYPETREE_FILTEREDCOMPOSITENODE_HH
Dune::TypeTree::FilteredCompositeNode::Child::Type
OriginalChild::Type Type
The type of the child.
Definition: filteredcompositenode.hh:136
Dune::TypeTree::FilteredCompositeNode::CHILDREN
static const std::size_t CHILDREN
The number of children.
Definition: filteredcompositenode.hh:116
Dune::TypeTree::FilteredCompositeNode::child
std::enable_if< lazy_enable< k >::value, typename Child< k >::Type & >::type child(Dune::index_constant< k >={})
Returns the i-th child.
Definition: filteredcompositenode.hh:157
Dune::TypeTree::FilteredCompositeNode::Child::type
OriginalChild::type type
The type of the child.
Definition: filteredcompositenode.hh:139
nodetags.hh
Dune::TypeTree::TreePathType::Type
Type
Definition: treepath.hh:30
Dune::TypeTree::FilteredCompositeNode::FilteredCompositeNode
FilteredCompositeNode(std::shared_ptr< Node > node)
Initialize the CompositeNode with copies of the passed in Storage objects.
Definition: filteredcompositenode.hh:265
Dune::TypeTree::FilteredCompositeNode::NodeTag
CompositeNodeTag NodeTag
The type tag that describes a CompositeNode.
Definition: filteredcompositenode.hh:98
Dune::TypeTree::FilteredCompositeNode::unfiltered
const Node & unfiltered() const
Returns the unfiltered node (const version).
Definition: filteredcompositenode.hh:232
Dune::TypeTree::FilteredCompositeNode::childStorage
std::enable_if< lazy_enable< k >::value, typename Child< k >::Storage >::type childStorage()
Returns the storage of the i-th child.
Definition: filteredcompositenode.hh:178
Dune::TypeTree::CompositeNodeTag
Tag designating a composite node.
Definition: nodetags.hh:22
Dune::TypeTree::FilteredCompositeNode::isComposite
static const bool isComposite
Mark this class as a composite in the dune-typetree.
Definition: filteredcompositenode.hh:113
Dune::TypeTree::FilteredCompositeNode::Child
Access to the type and storage type of the i-th child.
Definition: filteredcompositenode.hh:125
Dune::TypeTree::FilteredCompositeNode::NodeStorage
mapped_children::NodeStorage NodeStorage
The type used for storing the children.
Definition: filteredcompositenode.hh:101
Dune::TypeTree::FilteredCompositeNode::unfiltered
std::enable_if< enabled, Node & >::type unfiltered()
Returns the unfiltered node.
Definition: filteredcompositenode.hh:223
Dune
Definition: accumulate_static.hh:13
Dune::TypeTree::FilteredCompositeNode
Base class for composite nodes representing a filtered view on an underlying composite node.
Definition: filteredcompositenode.hh:80
Dune::TypeTree::FilteredCompositeNode::ChildTypes
mapped_children::ChildTypes ChildTypes
A tuple storing the types of all children.
Definition: filteredcompositenode.hh:104
Dune::TypeTree::FilteredCompositeNode::unfilteredStorage
std::shared_ptr< const Node > unfilteredStorage() const
Returns the storage object of the unfiltered node (const version).
Definition: filteredcompositenode.hh:252
Dune::TypeTree::FilteredCompositeNode::unfilteredStorage
std::enable_if< enabled, std::shared_ptr< Node > >::type unfilteredStorage()
Returns the storage object of the unfiltered node.
Definition: filteredcompositenode.hh:243
Dune::TypeTree::FilteredCompositeNode::childStorage
Child< k >::ConstStorage childStorage() const
Returns the storage of the i-th child (const version).
Definition: filteredcompositenode.hh:191
Dune::TypeTree::FilteredCompositeNode::isLeaf
static const bool isLeaf
Mark this class as non leaf in the dune-typetree.
Definition: filteredcompositenode.hh:107
Dune::TypeTree::FilteredCompositeNode::Child::Storage
OriginalChild::Storage Storage
The storage type of the child.
Definition: filteredcompositenode.hh:142
Dune::TypeTree::FilteredCompositeNode::Child::ConstStorage
OriginalChild::ConstStorage ConstStorage
The const storage type of the child.
Definition: filteredcompositenode.hh:145
Dune::TypeTree::FilteredCompositeNode::setChild
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:205
filters.hh
Dune::TypeTree::FilteredCompositeNode::child
const Child< k >::Type & child(Dune::index_constant< k >={}) const
Returns the i-th child (const version).
Definition: filteredcompositenode.hh:167
Dune::TypeTree::FilteredCompositeNode::degree
static constexpr std::size_t degree()
Definition: filteredcompositenode.hh:118
Dune::TypeTree::FilteredCompositeNode::FilteredCompositeNode
FilteredCompositeNode(Node &node)
Initialize the CompositeNode with a copy of the passed-in storage type.
Definition: filteredcompositenode.hh:270
Dune::TypeTree::FilteredCompositeNode::isPower
static const bool isPower
Mark this class as a non power in the dune-typetree.
Definition: filteredcompositenode.hh:110
Dune::TypeTree::FilteredCompositeNode::setChild
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:198