"Fossies" - the Fresh Open Source Software Archive

Member "dune-typetree-2.8.0/test/typetreetestutility.hh" (31 Aug 2021, 4690 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.

    1 
    2 #include <dune/typetree/typetree.hh>
    3 #include <dune/typetree/pairtraversal.hh>
    4 
    5 #include <iostream>
    6 
    7 struct Counter
    8 {
    9   Counter()
   10     : _id(_ids++)
   11   {
   12     std::cout << "Constructed id = " << id() << std::endl;
   13   }
   14 
   15   Counter(const Counter& rhs)
   16     : _id(_ids++)
   17   {
   18     rhs.assert_valid();
   19     std::cout << "Copy-Constructed id = " << id() << " from id = " << rhs.id() << std::endl;
   20   }
   21 
   22   Counter(Counter&& rhs)
   23     : _id(rhs._id)
   24   {
   25     rhs.assert_valid();
   26     rhs._id = -1;
   27     std::cout << "Move-Constructed id = " << id() << std::endl;
   28   }
   29 
   30   ~Counter()
   31   {
   32     std::cout << "Destructed id = " << _id << std::endl;
   33   }
   34 
   35   Counter& operator=(const Counter& rhs)
   36   {
   37     rhs.assert_valid();
   38     assert_valid();
   39     std::cout << "Assigned id = " << id() << " from id = " << rhs.id() << std::endl;
   40     return *this;
   41   }
   42 
   43   Counter& operator=(Counter&& rhs)
   44   {
   45     assert_valid();
   46     rhs.assert_valid();
   47     std::cout << "Move-Assigned id = " << id() << " from id = " << rhs.id() << std::endl;
   48     rhs._id = -1;
   49     return *this;
   50   }
   51 
   52   int id() const
   53   {
   54     assert_valid();
   55     return _id;
   56   }
   57 
   58   void assert_valid() const
   59   {
   60     assert(_id != -1);
   61   }
   62 
   63   int _id;
   64   static int _ids;
   65 };
   66 
   67 int Counter::_ids = 0;
   68 
   69 struct SimpleLeafTag {};
   70 
   71 struct SimpleLeaf
   72   : public Dune::TypeTree::LeafNode
   73   , public Counter
   74 {
   75 
   76   typedef SimpleLeafTag ImplementationTag;
   77 
   78   static const char* name()
   79   {
   80     return "SimpleLeaf";
   81   }
   82 
   83   SimpleLeaf() {}
   84 
   85   SimpleLeaf(SimpleLeaf&& rhs)
   86     : Dune::TypeTree::LeafNode(std::move(rhs))
   87     , Counter(std::move(rhs))
   88   {
   89     std::cout << "move ctor" << std::endl;
   90   }
   91 
   92   SimpleLeaf(const SimpleLeaf& rhs)
   93     : Dune::TypeTree::LeafNode(rhs)
   94     , Counter(rhs)
   95   {
   96     std::cout << "copy ctor" << std::endl;
   97   }
   98 
   99 };
  100 
  101 struct SimpleLeafDerived
  102   : public SimpleLeaf
  103 {
  104 
  105   static const char* name()
  106   {
  107     return "SimpleLeafDerived";
  108   }
  109 
  110 };
  111 
  112 struct SimplePowerTag {};
  113 
  114 template<typename T, std::size_t k>
  115 struct SimplePower
  116   : public Dune::TypeTree::PowerNode<T,k>
  117   , public Counter
  118 {
  119 
  120   typedef SimplePowerTag ImplementationTag;
  121 
  122   static const char* name()
  123   {
  124     return "SimplePower";
  125   }
  126 
  127   typedef Dune::TypeTree::PowerNode<T,k> BaseT;
  128 
  129   SimplePower() {}
  130 
  131   SimplePower(T& c, bool copy)
  132     : BaseT(c,copy)
  133   {}
  134 
  135   template<typename C1, typename C2, typename... Children>
  136   SimplePower(C1&& c1, C2&& c2, Children&&... children)
  137     : BaseT(std::forward<C1>(c1),std::forward<C2>(c2),std::forward<Children>(children)...)
  138   {}
  139 
  140 };
  141 
  142 struct SimpleCompositeTag {};
  143 
  144 template<typename... Children>
  145 struct SimpleComposite
  146   : public Dune::TypeTree::CompositeNode<Children...>
  147   , public Counter
  148 {
  149 
  150   typedef SimpleCompositeTag ImplementationTag;
  151 
  152   static const char* name()
  153   {
  154     return "SimpleComposite";
  155   }
  156 
  157   typedef Dune::TypeTree::CompositeNode<Children...> BaseT;
  158 
  159   template<typename... Args, typename = typename std::enable_if<(sizeof...(Args) == BaseT::CHILDREN)>::type>
  160   SimpleComposite(Args&&... args)
  161     : BaseT(std::forward<Args>(args)...)
  162   {}
  163 
  164 };
  165 
  166 struct SimpleDynamicPowerTag {};
  167 
  168 template<typename T>
  169 struct SimpleDynamicPower
  170   : public Dune::TypeTree::DynamicPowerNode<T>
  171   , public Counter
  172 {
  173 
  174   typedef SimpleDynamicPowerTag ImplementationTag;
  175 
  176   static const char* name()
  177   {
  178     return "SimpleDynamicPower";
  179   }
  180 
  181   typedef Dune::TypeTree::DynamicPowerNode<T> BaseT;
  182 
  183   SimpleDynamicPower() {}
  184 
  185   SimpleDynamicPower(T& c, bool copy)
  186     : BaseT(c,copy)
  187   {}
  188 
  189   template<typename C1, typename C2, typename... Children>
  190   SimpleDynamicPower(C1&& c1, C2&& c2, Children&&... children)
  191     : BaseT(std::forward<C1>(c1),std::forward<C2>(c2),std::forward<Children>(children)...)
  192   {}
  193 
  194 };
  195 
  196 
  197 struct TreePrinter
  198   : public Dune::TypeTree::TreeVisitor
  199   , public Dune::TypeTree::DynamicTraversal
  200 {
  201 
  202   template<typename T, typename TreePath>
  203   void leaf(const T& t, TreePath treePath) const
  204   {
  205     pre(t,treePath);
  206   }
  207 
  208   template<typename T, typename TreePath>
  209   void pre(const T& t, TreePath treePath) const
  210   {
  211     for (std::size_t i = 0; i < treePath.size(); ++i)
  212       std::cout << "  ";
  213     std::cout << t.name() << " " << t.id() << std::endl;
  214   }
  215 };
  216 
  217 
  218 
  219 
  220 struct PairPrinter
  221   : public Dune::TypeTree::TreePairVisitor
  222   , public Dune::TypeTree::DynamicTraversal
  223 {
  224 
  225   template<typename T1, typename T2, typename TreePath>
  226   void leaf(const T1& t1, const T2& t2, TreePath treePath) const
  227   {
  228     pre(t1,t2,treePath);
  229   }
  230 
  231   template<typename T1, typename T2, typename TreePath>
  232   void pre(const T1& t1, const T2& t2, TreePath treePath) const
  233   {
  234     for (std::size_t i = 0; i < treePath.size(); ++i)
  235       std::cout << "  ";
  236     std::cout << t1.name() << " " << t1.id() << "      " << t2.name() << " " << t2.id() << std::endl;
  237   }
  238 };