Loading...
Searching...
No Matches
filters.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-GPL-2.0-only-with-PDELab-exception
#ifndef DUNE_TYPETREE_FILTERS_HH
#define DUNE_TYPETREE_FILTERS_HH
#include <tuple>
#include <memory>
#include <dune/common/typetraits.hh>
namespace Dune {
namespace TypeTree {
template<std::size_t new_k, std::size_t old_k>
struct FilterEntry
{
#ifndef DOXYGEN
// The precise contents of this class is an implementation detail.
static const std::size_t filtered_index = new_k;
static const std::size_t original_index = old_k;
#endif // DOXYGEN
};
template<typename... FilterEntries>
struct FilterResult
{
static const std::size_t size = sizeof...(FilterEntries);
typedef std::tuple<FilterEntries...> IndexMap;
template<typename Node>
struct apply
{
typedef std::tuple<typename Node::template Child<FilterEntries::original_index>...> Children;
typedef std::tuple<typename Node::template Child<FilterEntries::original_index>::Type...> ChildTypes;
typedef std::tuple<std::shared_ptr<typename Node::template Child<FilterEntries::original_index>::Type>...> NodeStorage;
};
};
struct SimpleFilterTag {};
struct AdvancedFilterTag {};
struct AdvancedFilter
{
typedef AdvancedFilterTag FilterTag;
#ifdef DOXYGEN
template<typename Node, typename... Children>
struct apply
{
typedef implementation-defined type;
};
#endif // DOXYGEN
};
struct SimpleFilter
{
typedef SimpleFilterTag FilterTag;
template<typename Node>
struct validate
{
static const bool value = true;
};
template<typename Child, std::size_t new_index, std::size_t old_index>
struct apply
{
static const bool value = true;
};
};
namespace {
// ********************************************************************************
// IndexFilter helpers
// ********************************************************************************
template<typename Node, std::size_t new_index, std::size_t... indices>
struct index_filter_helper
{
template<typename... FilterEntries>
struct apply
{
typedef FilterResult<FilterEntries...> type;
};
};
template<typename Node, std::size_t new_index, std::size_t old_index, std::size_t... indices>
struct index_filter_helper<Node,new_index,old_index,indices...>
{
template<typename... FilterEntries>
struct apply
: public index_filter_helper<Node,new_index+1,indices...>::template apply<FilterEntries...,
FilterEntry<new_index,
old_index>
>
{};
};
} // anonymous namespace
template<std::size_t... indices>
struct IndexFilter
: public AdvancedFilter
{
#ifndef DOXYGEN
template<typename Node, typename... Children>
struct apply
{
typedef typename index_filter_helper<Node,0,indices...>::template apply<>::type type;
};
#endif // DOXYGEN
};
// ********************************************************************************
// filter: Wrapper class for turning a simple filter into an advanced filter
// usable by FilteredCompositeNode
// ********************************************************************************
namespace {
template<typename Filter, std::size_t new_k, std::size_t old_k, typename... tail>
struct filter_helper
{
template<typename... FilterDescriptors>
struct apply
{
typedef FilterResult<FilterDescriptors...> type;
};
};
template<typename Filter, std::size_t new_k, std::size_t old_k, typename child, typename... tail>
struct filter_helper<Filter,new_k,old_k,child,tail...>
{
template<typename... FilterDescriptors>
struct apply
: public std::conditional<Filter::template apply<child,new_k,old_k>::value,
typename filter_helper<Filter,new_k+1,old_k+1,tail...>::template apply<FilterDescriptors...,FilterEntry<new_k,old_k> >,
typename filter_helper<Filter,new_k,old_k+1,tail...>::template apply<FilterDescriptors...>
>::type
{};
};
} // anonymous namespace
template<typename Filter>
struct filter
{
template<typename Node, typename... Children>
struct apply
{
static_assert((Filter::template validate<Node>::value),"Invalid simple filter");
typedef typename filter_helper<Filter,0,0,Children...>::template apply<>::type type;
};
};
} // namespace TypeTree
} //namespace Dune
#endif // DUNE_TYPETREE_FILTERS_HH
Dune
Definition accumulate_static.hh:17
Dune::TypeTree
Definition accumulate_static.hh:18
Dune::TypeTree::FilterEntry
A filter entry describing the mapping of one child in the filtered node.
Definition filters.hh:25
Dune::TypeTree::FilterResult
The result of a filter.
Definition filters.hh:41
Dune::TypeTree::FilterResult::IndexMap
std::tuple< FilterEntries... > IndexMap
Definition filters.hh:45
Dune::TypeTree::FilterResult< FilterEntries... >::size
static const std::size_t size
Definition filters.hh:43
Dune::TypeTree::FilterResult::apply
Definition filters.hh:49
Dune::TypeTree::FilterResult::apply::Children
std::tuple< typename Node::template Child< FilterEntries::original_index >... > Children
Definition filters.hh:50
Dune::TypeTree::FilterResult::apply::ChildTypes
std::tuple< typename Node::template Child< FilterEntries::original_index >::Type... > ChildTypes
Definition filters.hh:51
Dune::TypeTree::FilterResult::apply::NodeStorage
std::tuple< std::shared_ptr< typename Node::template Child< FilterEntries::original_index >::Type >... > NodeStorage
Definition filters.hh:52
Dune::TypeTree::SimpleFilterTag
Tag describing a simple filter that can only decide whether or not to include a single given child.
Definition filters.hh:58
Dune::TypeTree::AdvancedFilterTag
Tag describing an advanced filter that has full control over the construction of the list of FilterEn...
Definition filters.hh:61
Dune::TypeTree::AdvancedFilter
Base class for advanced filters.
Definition filters.hh:66
Dune::TypeTree::AdvancedFilter::FilterTag
AdvancedFilterTag FilterTag
Filter tag for deciding on filter application mechanism.
Definition filters.hh:69
Dune::TypeTree::AdvancedFilter::apply
Apply this filter to the given node and children.
Definition filters.hh:76
Dune::TypeTree::AdvancedFilter::apply::type
implementation defined type
The result of the filtering process.
Definition filters.hh:81
Dune::TypeTree::SimpleFilter
Default simple filter that accepts any node and leaves its child structure unchanged.
Definition filters.hh:95
Dune::TypeTree::SimpleFilter::FilterTag
SimpleFilterTag FilterTag
Filter tag for deciding on filter application mechanism.
Definition filters.hh:98
Dune::TypeTree::SimpleFilter::validate
Validates the combination of filter and node.
Definition filters.hh:104
Dune::TypeTree::SimpleFilter::validate::value
static const bool value
True if the combination of filter and node is valid.
Definition filters.hh:106
Dune::TypeTree::SimpleFilter::apply
Applies the filter to the given child node.
Definition filters.hh:120
Dune::TypeTree::SimpleFilter::apply::value
static const bool value
True if the child will be included in the filtered node.
Definition filters.hh:122
Dune::TypeTree::IndexFilter
Filter class for FilteredCompositeNode that selects the children with the given indices.
Definition filters.hh:162
Dune::TypeTree::filter
Adapter class that takes a SimpleFilter, validated it and turns it into an AdvancedFilter.
Definition filters.hh:213
Dune::TypeTree::filter::apply
Apply the filter.
Definition filters.hh:218
Dune::TypeTree::filter::apply::type
filter_helper< Filter, 0, 0, Children... >::template apply ::type type
Definition filters.hh:222