dune-typetree  2.3.1
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 #if !(HAVE_VARIADIC_TEMPLATES || DOXYGEN || HEADERCHECK)
8 #error The class FilteredCompositeNode requires compiler support for variadic templates, which your compiler lacks.
9 #endif
10 
11 #if (HAVE_VARIADIC_TEMPLATES || DOXYGEN)
12 #include <dune/common/static_assert.hh>
13 #include <dune/common/tuples.hh>
14 #include <dune/common/typetraits.hh>
15 
16 namespace Dune {
17  namespace TypeTree {
18 
24  template<std::size_t new_k, std::size_t old_k>
26  struct FilterEntry
27  {
28 
29 #ifndef DOXYGEN
30 
31  // The precise contents of this class is an implementation detail.
32 
33  static const std::size_t filtered_index = new_k;
34  static const std::size_t original_index = old_k;
35 
36 #endif // DOXYGEN
37 
38  };
39 
41  template<typename... FilterEntries>
42  struct FilterResult
43  {
44 
45  static const std::size_t size = sizeof...(FilterEntries);
46 
47  typedef tuple<FilterEntries...> IndexMap;
48 
49  template<typename Node>
50  struct apply
51  {
52  typedef tuple<typename Node::template Child<FilterEntries::original_index>...> Children;
54  typedef tuple<typename Node::template Child<FilterEntries::original_index>::Storage...> NodeStorage;
55  };
56 
57  };
58 
60  struct SimpleFilterTag {};
61 
63  struct AdvancedFilterTag {};
64 
65 
68  {
69 
72 
73 #ifdef DOXYGEN
74 
76  template<typename Node, typename... Children>
77  struct apply
78  {
80 
83  typedef implementation-defined type;
84  };
85 
86 #endif // DOXYGEN
87 
88  };
89 
91 
96  struct SimpleFilter
97  {
98 
101 
102 
104  template<typename Node>
105  struct validate
106  {
108  static const bool value = true;
109  };
110 
112 
120  template<typename Child, std::size_t new_index, std::size_t old_index>
121  struct apply
122  {
124  static const bool value = true;
125  };
126 
127  };
128 
129  namespace {
130 
131  // ********************************************************************************
132  // IndexFilter helpers
133  // ********************************************************************************
134 
135  template<typename Node, std::size_t new_index, std::size_t... indices>
136  struct index_filter_helper
137  {
138  template<typename... FilterEntries>
139  struct apply
140  {
141  typedef FilterResult<FilterEntries...> type;
142  };
143  };
144 
145  template<typename Node, std::size_t new_index, std::size_t old_index, std::size_t... indices>
146  struct index_filter_helper<Node,new_index,old_index,indices...>
147  {
148  template<typename... FilterEntries>
149  struct apply
150  : public index_filter_helper<Node,new_index+1,indices...>::template apply<FilterEntries...,
151  FilterEntry<new_index,
152  old_index>
153  >
154  {};
155  };
156 
157  } // anonymous namespace
158 
159 
161  template<std::size_t... indices>
162  struct IndexFilter
163  : public AdvancedFilter
164  {
165 
166 #ifndef DOXYGEN
167 
168  template<typename Node, typename... Children>
169  struct apply
170  {
171  typedef typename index_filter_helper<Node,0,indices...>::template apply<>::type type;
172  };
173 
174 #endif // DOXYGEN
175 
176  };
177 
178 
179  // ********************************************************************************
180  // filter: Wrapper class for turning a simple filter into an advanced filter
181  // usable by FilteredCompositeNode
182  // ********************************************************************************
183 
184  namespace {
185 
186  template<typename Filter, std::size_t new_k, std::size_t old_k, typename... tail>
187  struct filter_helper
188  {
189  template<typename... FilterDescriptors>
190  struct apply
191  {
192  typedef FilterResult<FilterDescriptors...> type;
193  };
194  };
195 
196  template<typename Filter, std::size_t new_k, std::size_t old_k, typename child, typename... tail>
197  struct filter_helper<Filter,new_k,old_k,child,tail...>
198  {
199 
200  template<typename... FilterDescriptors>
201  struct apply
202  : public Dune::conditional<Filter::template apply<child,new_k,old_k>::value,
203  typename filter_helper<Filter,new_k+1,old_k+1,tail...>::template apply<FilterDescriptors...,FilterEntry<new_k,old_k> >,
204  typename filter_helper<Filter,new_k,old_k+1,tail...>::template apply<FilterDescriptors...>
205  >::type
206  {};
207 
208  };
209 
210  } // anonymous namespace
211 
213  template<typename Filter>
214  struct filter
215  {
216 
218  template<typename Node, typename... Children>
219  struct apply
220  {
221 
222  dune_static_assert((Filter::template validate<Node>::value),"Invalid simple filter");
223 
224  typedef typename filter_helper<Filter,0,0,Children...>::template apply<>::type type;
225 
226  };
227 
228  };
229 
231 
232  } // namespace TypeTree
233 } //namespace Dune
234 
235 #endif // (HAVE_VARIADIC_TEMPLATES || DOXYGEN)
236 
237 #endif // DUNE_TYPETREE_FILTERS_HH
Apply this filter to the given node and children.
Definition: filters.hh:77
Filter class for FilteredCompositeNode that selects the children with the given indices.
Definition: filters.hh:162
tuple< FilterEntries...> IndexMap
Definition: filters.hh:47
The result of a filter.
Definition: filters.hh:42
implementation defined type
The result of the filtering process.
Definition: filters.hh:83
filter_helper< Filter, 0, 0, Children...>::template apply::type type
Definition: filters.hh:224
static const bool value
True if the child will be included in the filtered node.
Definition: filters.hh:124
tuple< typename Node::template Child< FilterEntries::original_index >::Type...> ChildTypes
Definition: filters.hh:53
Tag describing an advanced filter that has full control over the construction of the list of FilterEn...
Definition: filters.hh:63
Definition: filters.hh:50
Apply the filter.
Definition: filters.hh:219
Validates the combination of filter and node.
Definition: filters.hh:105
dune_static_assert((Filter::template validate< Node >::value),"Invalid simple filter")
Tag describing a simple filter that can only decide whether or not to include a single given child...
Definition: filters.hh:60
A filter entry describing the mapping of one child in the filtered node.
Definition: filters.hh:26
Base class for advanced filters.
Definition: filters.hh:67
Adapter class that takes a SimpleFilter, validated it and turns it into an AdvancedFilter.
Definition: filters.hh:214
Applies the filter to the given child node.
Definition: filters.hh:121
tuple< typename Node::template Child< FilterEntries::original_index >...> Children
Definition: filters.hh:52
tuple< typename Node::template Child< FilterEntries::original_index >::Storage...> NodeStorage
Definition: filters.hh:54
static const std::size_t value
Definition: compositenode.hh:38
AdvancedFilterTag FilterTag
Filter tag for deciding on filter application mechanism.
Definition: filters.hh:71
Default simple filter that accepts any node and leaves its child structure unchanged.
Definition: filters.hh:96
static const std::size_t size
Definition: filters.hh:45
Type
Definition: treepath.hh:26
SimpleFilterTag FilterTag
Filter tag for deciding on filter application mechanism.
Definition: filters.hh:100
static const bool value
True if the combination of filter and node is valid.
Definition: filters.hh:108