"Fossies" - the Fresh Open Source Software Archive

Member "dune-typetree-2.8.0/dune/typetree/filteredcompositenode.hh" (31 Aug 2021, 7767 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 "filteredcompositenode.hh" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.8.0_vs_2.9.0.

    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 
   11 #include <dune/typetree/nodetags.hh>
   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>
   18 #include <dune/typetree/nodetags.hh>
   19 
   20 namespace Dune {
   21   namespace TypeTree {
   22 
   23     /** \addtogroup Nodes
   24      *  \ingroup TypeTree
   25      *  \{
   26      */
   27 
   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 
   78     //! Base class for composite nodes representing a filtered view on an underlying composite node.
   79     template<typename Node, typename Filter>
   80     class FilteredCompositeNode
   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 
   97       //! The type tag that describes a CompositeNode.
   98       typedef CompositeNodeTag NodeTag;
   99 
  100       //! The type used for storing the children.
  101       typedef typename mapped_children::NodeStorage NodeStorage;
  102 
  103       //! A tuple storing the types of all children.
  104       typedef typename mapped_children::ChildTypes ChildTypes;
  105 
  106       //! Mark this class as non leaf in the \ref TypeTree.
  107       static const bool isLeaf = false;
  108 
  109       //! Mark this class as a non power in the \ref TypeTree.
  110       static const bool isPower = false;
  111 
  112       //! Mark this class as a composite in the \ref TypeTree.
  113       static const bool isComposite = true;
  114 
  115       //! The number of children.
  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 
  123       //! Access to the type and storage type of the i-th child.
  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 
  135         //! The type of the child.
  136         typedef typename OriginalChild::Type Type;
  137 
  138         //! The type of the child.
  139         typedef typename OriginalChild::type type;
  140       };
  141 
  142       //! @name Child Access
  143       //! @{
  144 
  145       //! Returns the k-th child.
  146       /**
  147        * \returns a reference to the k-th child.
  148        */
  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 
  156       //! Returns the k-th child (const version).
  157       /**
  158        * \returns a const reference to the k-th child.
  159        */
  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 
  166       //! Returns the storage of the k-th child.
  167       /**
  168        * \returns a copy of the object storing the k-th child.
  169        */
  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 
  177       //! Returns the storage of the k-th child (const version).
  178       /**
  179        * \returns a copy of the object storing the k-th child.
  180        */
  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 
  187       //! Sets the k-th child to the passed-in value.
  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 
  194       //! @}
  195 
  196       //! @name Access to unfiltered node
  197       //! @{
  198 
  199     protected:
  200 
  201       //! Returns the unfiltered node.
  202       /**
  203        * \returns A reference to the original, unfiltered node.
  204        */
  205       template<bool enabled = !nodeIsConst>
  206       typename std::enable_if<enabled,Node&>::type
  207       unfiltered ()
  208       {
  209         return *_node;
  210       }
  211 
  212       //! Returns the unfiltered node (const version).
  213       /**
  214        * \returns A const reference to the original, unfiltered node.
  215        */
  216       const Node& unfiltered () const
  217       {
  218         return *_node;
  219       }
  220 
  221       //! Returns the storage object of the unfiltered node.
  222       /**
  223        * \returns A shared_ptr to the original, unfiltered node.
  224        */
  225       template<bool enabled = !nodeIsConst>
  226       typename std::enable_if<enabled,std::shared_ptr<Node> >::type
  227       unfilteredStorage ()
  228       {
  229         return _node;
  230       }
  231 
  232       //! Returns the storage object of the unfiltered node (const version).
  233       /**
  234        * \returns A shared_ptr to the original, unfiltered node.
  235        */
  236       std::shared_ptr<const Node> unfilteredStorage () const
  237       {
  238         return _node;
  239       }
  240 
  241       //! @}
  242 
  243     public:
  244 
  245       //! @name Constructors
  246       //! @{
  247 
  248       //! Initialize the CompositeNode with copies of the passed in Storage objects.
  249       FilteredCompositeNode (std::shared_ptr<Node> node)
  250         : _node(std::move(node))
  251       {}
  252 
  253       //! Initialize the CompositeNode with a copy of the passed-in storage type.
  254       FilteredCompositeNode (Node& node)
  255         : _node(stackobject_to_shared_ptr(node))
  256       {}
  257 
  258       //! @}
  259 
  260     private:
  261       std::shared_ptr<Node> _node;
  262     };
  263 
  264     //! \} group Nodes
  265 
  266   } // namespace TypeTree
  267 } //namespace Dune
  268 
  269 #endif // DUNE_TYPETREE_FILTEREDCOMPOSITENODE_HH