"Fossies" - the Fresh Open Source Software Archive

Member "dune-typetree-2.8.0/dune/typetree/filters.hh" (31 Aug 2021, 7124 Bytes) of package /linux/misc/dune/dune-typetree-2.8.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "filters.hh" see the Fossies "Dox" file reference documentation.

    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 
   14     /** \addtogroup Nodes
   15      *  \ingroup TypeTree
   16      *  \{
   17      */
   18 
   19     //! A filter entry describing the mapping of one child in the filtered node.
   20     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 
   35     //! The result of a filter.
   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;
   48         typedef std::tuple<typename Node::template Child<FilterEntries::original_index>::Type...> ChildTypes;
   49         typedef std::tuple<std::shared_ptr<typename Node::template Child<FilterEntries::original_index>::Type>...> NodeStorage;
   50       };
   51 
   52     };
   53 
   54     //! Tag describing a simple filter that can only decide whether or not to include a single given child.
   55     struct SimpleFilterTag {};
   56 
   57     //! Tag describing an advanced filter that has full control over the construction of the list of FilterEntries.
   58     struct AdvancedFilterTag {};
   59 
   60 
   61     //! Base class for advanced filters.
   62     struct AdvancedFilter
   63     {
   64 
   65       //! Filter tag for deciding on filter application mechanism.
   66       typedef AdvancedFilterTag FilterTag;
   67 
   68 #ifdef DOXYGEN
   69 
   70       //! Apply this filter to the given node and children
   71       template<typename Node, typename... Children>
   72       struct apply
   73       {
   74         //! The result of the filtering process.
   75         /**
   76          * This type must be a model of FilterResult.
   77          */
   78         typedef implementation-defined type;
   79       };
   80 
   81 #endif // DOXYGEN
   82 
   83     };
   84 
   85     //! Default simple filter that accepts any node and leaves its child structure unchanged.
   86     /**
   87      * This default filter causes the filtered node to be exactly identical to the original node.
   88      * It is useful as a base class for documentation purposes and if you do not need to validate
   89      * the filter, as it saves having to implement the validate template struct.
   90      */
   91     struct SimpleFilter
   92     {
   93 
   94       //! Filter tag for deciding on filter application mechanism.
   95       typedef SimpleFilterTag FilterTag;
   96 
   97 
   98       //! Validates the combination of filter and node.
   99       template<typename Node>
  100       struct validate
  101       {
  102         //! True if the combination of filter and node is valid.
  103         static const bool value = true;
  104       };
  105 
  106       //! Applies the filter to the given child node.
  107       /**
  108        * This struct applies the filter to the given child to decide whether or not it will be
  109        * included in the filtered node.
  110        *
  111        * \tparam Child     The type of the child node.
  112        * \tparam new_index The index this child would receive in the filtered node.
  113        * \tparam old_index The index of this child in the unfiltered node.
  114        */
  115       template<typename Child, std::size_t new_index, std::size_t old_index>
  116       struct apply
  117       {
  118         //! True if the child will be included in the filtered node.
  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 
  155       //! Filter class for FilteredCompositeNode that selects the children with the given indices.
  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 
  207       //! Adapter class that takes a SimpleFilter, validated it and turns it into an AdvancedFilter.
  208     template<typename Filter>
  209     struct filter
  210     {
  211 
  212       //! Apply the filter.
  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 
  225     //! \} group Nodes
  226 
  227   } // namespace TypeTree
  228 } //namespace Dune
  229 
  230 #endif // DUNE_TYPETREE_FILTERS_HH