dune-typetree  2.8.0
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 auto degree ()
119  {
120  return std::integral_constant<std::size_t,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  };
141 
144 
146 
149  template<std::size_t k,
150  typename std::enable_if<lazy_enable<k>::value, int>::type = 0>
151  auto& child (index_constant<k> = {})
152  {
153  return _node->template child<Child<k>::mapped_index>();
154  }
155 
157 
160  template<std::size_t k>
161  const auto& child (index_constant<k> = {}) const
162  {
163  return _node->template child<Child<k>::mapped_index>();
164  }
165 
167 
170  template<std::size_t k,
171  typename std::enable_if<lazy_enable<k>::value, int>::type = 0>
172  auto childStorage (index_constant<k> = {})
173  {
174  return _node->template childStorage<Child<k>::mapped_index>();
175  }
176 
178 
181  template<std::size_t k>
182  auto childStorage (index_constant<k> = {}) const
183  {
184  return _node->template childStorage<Child<k>::mapped_index>();
185  }
186 
188  template<std::size_t k, class ChildType>
189  void setChild (ChildType&& child, typename std::enable_if<lazy_enable<k>::value,void*>::type = 0)
190  {
191  _node->template setChild<Child<k>::mapped_index>(std::forward<ChildType>(child));
192  }
193 
195 
198 
199  protected:
200 
202 
205  template<bool enabled = !nodeIsConst>
206  typename std::enable_if<enabled,Node&>::type
208  {
209  return *_node;
210  }
211 
213 
216  const Node& unfiltered () const
217  {
218  return *_node;
219  }
220 
222 
225  template<bool enabled = !nodeIsConst>
226  typename std::enable_if<enabled,std::shared_ptr<Node> >::type
228  {
229  return _node;
230  }
231 
233 
236  std::shared_ptr<const Node> unfilteredStorage () const
237  {
238  return _node;
239  }
240 
242 
243  public:
244 
247 
249  FilteredCompositeNode (std::shared_ptr<Node> node)
250  : _node(std::move(node))
251  {}
252 
255  : _node(stackobject_to_shared_ptr(node))
256  {}
257 
259 
260  private:
261  std::shared_ptr<Node> _node;
262  };
263 
265 
266  } // namespace TypeTree
267 } //namespace Dune
268 
269 #endif // DUNE_TYPETREE_FILTEREDCOMPOSITENODE_HH
Definition: accumulate_static.hh:13
Type
Definition: treepath.hh:30
Base class for composite nodes representing a filtered view on an underlying composite node.
Definition: filteredcompositenode.hh:81
std::enable_if< enabled, std::shared_ptr< Node > >::type unfilteredStorage()
Returns the storage object of the unfiltered node.
Definition: filteredcompositenode.hh:227
auto childStorage(index_constant< k >={}) const
Returns the storage of the k-th child (const version).
Definition: filteredcompositenode.hh:182
static constexpr auto degree()
Definition: filteredcompositenode.hh:118
mapped_children::NodeStorage NodeStorage
The type used for storing the children.
Definition: filteredcompositenode.hh:101
const auto & child(index_constant< k >={}) const
Returns the k-th child (const version).
Definition: filteredcompositenode.hh:161
void setChild(ChildType &&child, typename std::enable_if< lazy_enable< k >::value, void * >::type=0)
Sets the k-th child to the passed-in value.
Definition: filteredcompositenode.hh:189
static const bool isLeaf
Mark this class as non leaf in the dune-typetree.
Definition: filteredcompositenode.hh:107
auto & child(index_constant< k >={})
Returns the k-th child.
Definition: filteredcompositenode.hh:151
static const bool isComposite
Mark this class as a composite in the dune-typetree.
Definition: filteredcompositenode.hh:113
static const bool isPower
Mark this class as a non power in the dune-typetree.
Definition: filteredcompositenode.hh:110
std::shared_ptr< const Node > unfilteredStorage() const
Returns the storage object of the unfiltered node (const version).
Definition: filteredcompositenode.hh:236
FilteredCompositeNode(Node &node)
Initialize the CompositeNode with a copy of the passed-in storage type.
Definition: filteredcompositenode.hh:254
FilteredCompositeNode(std::shared_ptr< Node > node)
Initialize the CompositeNode with copies of the passed in Storage objects.
Definition: filteredcompositenode.hh:249
auto childStorage(index_constant< k >={})
Returns the storage of the k-th child.
Definition: filteredcompositenode.hh:172
const Node & unfiltered() const
Returns the unfiltered node (const version).
Definition: filteredcompositenode.hh:216
std::enable_if< enabled, Node & >::type unfiltered()
Returns the unfiltered node.
Definition: filteredcompositenode.hh:207
CompositeNodeTag NodeTag
The type tag that describes a CompositeNode.
Definition: filteredcompositenode.hh:98
mapped_children::ChildTypes ChildTypes
A tuple storing the types of all children.
Definition: filteredcompositenode.hh:104
static const std::size_t CHILDREN
The number of children.
Definition: filteredcompositenode.hh:116
Access to the type and storage type of the i-th child.
Definition: filteredcompositenode.hh:125
OriginalChild::type type
The type of the child.
Definition: filteredcompositenode.hh:139
OriginalChild::Type Type
The type of the child.
Definition: filteredcompositenode.hh:136
Tag designating a composite node.
Definition: nodetags.hh:25