dune-typetree  3.0-dev
filters.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_FILTERS_HH
5 #define DUNE_TYPETREE_FILTERS_HH
6 
7 #include <tuple>
8 
9 #include <dune/common/typetraits.hh>
10 
11 namespace Dune {
12  namespace TypeTree {
13 
19  template<std::size_t new_k, std::size_t old_k>
21  struct FilterEntry
22  {
23 
24 #ifndef DOXYGEN
25 
26  // The precise contents of this class is an implementation detail.
27 
28  static const std::size_t filtered_index = new_k;
29  static const std::size_t original_index = old_k;
30 
31 #endif // DOXYGEN
32 
33  };
34 
36  template<typename... FilterEntries>
37  struct FilterResult
38  {
39 
40  static const std::size_t size = sizeof...(FilterEntries);
41 
42  typedef std::tuple<FilterEntries...> IndexMap;
43 
44  template<typename Node>
45  struct apply
46  {
47  typedef std::tuple<typename Node::template Child<FilterEntries::original_index>...> Children;
49  typedef std::tuple<typename Node::template Child<FilterEntries::original_index>::Storage...> NodeStorage;
50  };
51 
52  };
53 
55  struct SimpleFilterTag {};
56 
58  struct AdvancedFilterTag {};
59 
60 
63  {
64 
67 
68 #ifdef DOXYGEN
69 
71  template<typename Node, typename... Children>
72  struct apply
73  {
75 
78  typedef implementation-defined type;
79  };
80 
81 #endif // DOXYGEN
82 
83  };
84 
86 
91  struct SimpleFilter
92  {
93 
96 
97 
99  template<typename Node>
100  struct validate
101  {
103  static const bool value = true;
104  };
105 
107 
115  template<typename Child, std::size_t new_index, std::size_t old_index>
116  struct apply
117  {
119  static const bool value = true;
120  };
121 
122  };
123 
124  namespace {
125 
126  // ********************************************************************************
127  // IndexFilter helpers
128  // ********************************************************************************
129 
130  template<typename Node, std::size_t new_index, std::size_t... indices>
131  struct index_filter_helper
132  {
133  template<typename... FilterEntries>
134  struct apply
135  {
136  typedef FilterResult<FilterEntries...> type;
137  };
138  };
139 
140  template<typename Node, std::size_t new_index, std::size_t old_index, std::size_t... indices>
141  struct index_filter_helper<Node,new_index,old_index,indices...>
142  {
143  template<typename... FilterEntries>
144  struct apply
145  : public index_filter_helper<Node,new_index+1,indices...>::template apply<FilterEntries...,
146  FilterEntry<new_index,
147  old_index>
148  >
149  {};
150  };
151 
152  } // anonymous namespace
153 
154 
156  template<std::size_t... indices>
157  struct IndexFilter
158  : public AdvancedFilter
159  {
160 
161 #ifndef DOXYGEN
162 
163  template<typename Node, typename... Children>
164  struct apply
165  {
166  typedef typename index_filter_helper<Node,0,indices...>::template apply<>::type type;
167  };
168 
169 #endif // DOXYGEN
170 
171  };
172 
173 
174  // ********************************************************************************
175  // filter: Wrapper class for turning a simple filter into an advanced filter
176  // usable by FilteredCompositeNode
177  // ********************************************************************************
178 
179  namespace {
180 
181  template<typename Filter, std::size_t new_k, std::size_t old_k, typename... tail>
182  struct filter_helper
183  {
184  template<typename... FilterDescriptors>
185  struct apply
186  {
187  typedef FilterResult<FilterDescriptors...> type;
188  };
189  };
190 
191  template<typename Filter, std::size_t new_k, std::size_t old_k, typename child, typename... tail>
192  struct filter_helper<Filter,new_k,old_k,child,tail...>
193  {
194 
195  template<typename... FilterDescriptors>
196  struct apply
197  : public std::conditional<Filter::template apply<child,new_k,old_k>::value,
198  typename filter_helper<Filter,new_k+1,old_k+1,tail...>::template apply<FilterDescriptors...,FilterEntry<new_k,old_k> >,
199  typename filter_helper<Filter,new_k,old_k+1,tail...>::template apply<FilterDescriptors...>
200  >::type
201  {};
202 
203  };
204 
205  } // anonymous namespace
206 
208  template<typename Filter>
209  struct filter
210  {
211 
213  template<typename Node, typename... Children>
214  struct apply
215  {
216 
217  static_assert((Filter::template validate<Node>::value),"Invalid simple filter");
218 
219  typedef typename filter_helper<Filter,0,0,Children...>::template apply<>::type type;
220 
221  };
222 
223  };
224 
226 
227  } // namespace TypeTree
228 } //namespace Dune
229 
230 #endif // DUNE_TYPETREE_FILTERS_HH
Dune::TypeTree::SimpleFilterTag
Tag describing a simple filter that can only decide whether or not to include a single given child.
Definition: filters.hh:55
Dune::TypeTree::FilterResult::apply::Children
std::tuple< typename Node::template Child< FilterEntries::original_index >... > Children
Definition: filters.hh:47
Dune::TypeTree::filter
Adapter class that takes a SimpleFilter, validated it and turns it into an AdvancedFilter.
Definition: filters.hh:209
Dune::TypeTree::FilterResult::size
static const std::size_t size
Definition: filters.hh:40
Dune::TypeTree::FilterResult::apply::ChildTypes
std::tuple< typename Node::template Child< FilterEntries::original_index >::Type... > ChildTypes
Definition: filters.hh:48
Dune::TypeTree::SimpleFilter::apply::value
static const bool value
True if the child will be included in the filtered node.
Definition: filters.hh:119
Dune::TypeTree::AdvancedFilter
Base class for advanced filters.
Definition: filters.hh:62
Dune::TypeTree::filter::apply::type
filter_helper< Filter, 0, 0, Children... >::template apply ::type type
Definition: filters.hh:217
Dune::TypeTree::AdvancedFilter::apply::type
implementation defined type
The result of the filtering process.
Definition: filters.hh:78
Dune::TypeTree::AdvancedFilter::FilterTag
AdvancedFilterTag FilterTag
Filter tag for deciding on filter application mechanism.
Definition: filters.hh:66
Dune::TypeTree::SimpleFilter::validate::value
static const bool value
True if the combination of filter and node is valid.
Definition: filters.hh:103
Dune::TypeTree::child
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
Dune::TypeTree::SimpleFilter
Default simple filter that accepts any node and leaves its child structure unchanged.
Definition: filters.hh:91
Dune::TypeTree::TreePathType::Type
Type
Definition: treepath.hh:26
Dune
Definition: accumulate_static.hh:13
Dune::TypeTree::IndexFilter
Filter class for FilteredCompositeNode that selects the children with the given indices.
Definition: filters.hh:157
Dune::TypeTree::FilterEntry
A filter entry describing the mapping of one child in the filtered node.
Definition: filters.hh:21
Dune::TypeTree::SimpleFilter::apply
Applies the filter to the given child node.
Definition: filters.hh:116
Dune::TypeTree::SimpleFilter::FilterTag
SimpleFilterTag FilterTag
Filter tag for deciding on filter application mechanism.
Definition: filters.hh:95
Dune::TypeTree::AdvancedFilter::apply
Apply this filter to the given node and children.
Definition: filters.hh:72
Dune::TypeTree::filter::apply
Apply the filter.
Definition: filters.hh:214
Dune::TypeTree::FilterResult::apply
Definition: filters.hh:45
Dune::TypeTree::SimpleFilter::validate
Validates the combination of filter and node.
Definition: filters.hh:100
Dune::TypeTree::FilterResult::apply::NodeStorage
std::tuple< typename Node::template Child< FilterEntries::original_index >::Storage... > NodeStorage
Definition: filters.hh:49
Dune::TypeTree::FilterResult::IndexMap
std::tuple< FilterEntries... > IndexMap
Definition: filters.hh:42
Dune::TypeTree::AdvancedFilterTag
Tag describing an advanced filter that has full control over the construction of the list of FilterEn...
Definition: filters.hh:58
Dune::TypeTree::FilterResult
The result of a filter.
Definition: filters.hh:37