Dukales

simpliest possible implementation of variant w/o operator =

Jun 19th, 2015
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.40 KB | None | 0 0
  1. // variant.hpp
  2. #pragma once
  3. #include <type_traits>
  4. #include <utility>
  5. #include <experimental/optional>
  6.  
  7. #include <cassert>
  8.  
  9. template< typename ...types >
  10. struct variant;
  11.  
  12. template<>
  13. struct variant<>
  14. {
  15.  
  16.     variant() = delete;
  17.  
  18.     variant(variant &) = delete;
  19.     variant(variant const &) = delete;
  20.     variant(variant &&) = delete;
  21.     variant(variant const &&) = delete;
  22.  
  23.     void operator = (variant &) = delete;
  24.     void operator = (variant const &) = delete;
  25.     void operator = (variant &&) = delete;
  26.     void operator = (variant const &&) = delete;
  27.  
  28. };
  29.  
  30. template< typename first, typename ...rest >
  31. struct variant< first, rest... >
  32. {
  33.  
  34.     static constexpr std::size_t index = sizeof...(rest);
  35.  
  36. private :
  37.  
  38.     struct head
  39.     {
  40.  
  41.         std::size_t which_;
  42.         first value_;
  43.  
  44.         template< typename ...types >
  45.         constexpr
  46.         head(std::experimental::in_place_t, types &&... _values)
  47.             : which_{index}
  48.             , value_(std::forward< types >(_values)...)
  49.         { ; }
  50.  
  51.         template< typename type >
  52.         constexpr
  53.         head(type && _value)
  54.             : head(std::experimental::in_place, std::forward< type >(_value))
  55.         { ; }
  56.  
  57.     };
  58.  
  59.     using tail = variant< rest... >;
  60.  
  61.     union
  62.     {
  63.  
  64.         head head_;
  65.         tail tail_;
  66.  
  67.     };
  68.  
  69.     template< typename ...types >
  70.     constexpr
  71.     variant(std::true_type, types &&... _values)
  72.         : head_(std::forward< types >(_values)...)
  73.     { ; }
  74.  
  75.     template< typename ...types >
  76.     constexpr
  77.     variant(std::false_type, types &&... _values)
  78.         : tail_(std::forward< types >(_values)...)
  79.     { ; }
  80.  
  81. public :
  82.  
  83.     using this_type = first; // a place for recursive_wrapper filtering
  84.  
  85.     constexpr
  86.     std::size_t
  87.     which() const
  88.     {
  89.         return head_.which_;
  90.     }
  91.  
  92.     constexpr
  93.     variant()
  94.         : variant(typename std::is_default_constructible< this_type >::type{}, std::experimental::in_place)
  95.     { ; }
  96.  
  97.     // if exception thrown during copy/move-construction then std::terminate called
  98.     variant(variant & _rhs) noexcept;
  99.     variant(variant const & _rhs) noexcept;
  100.     variant(variant && _rhs) noexcept;
  101.     variant(variant const && _rhs) noexcept;
  102.  
  103.     template< typename type >
  104.     constexpr
  105.     variant(type && _value)
  106.         : variant(typename std::is_same< this_type, std::decay_t< type > >::type{}, std::forward< type >(_value))
  107.     { ; }
  108.  
  109.     template< typename ...types >
  110.     constexpr
  111.     variant(std::experimental::in_place_t, types &&... _values)
  112.         : variant(typename std::is_constructible< this_type, types... >::type{}, std::experimental::in_place, std::forward< types >(_values)...)
  113.     { ; }
  114.  
  115.     // if exception thrown during copy/move-assignment then std::terminate called
  116.     variant & operator = (variant & _rhs) & noexcept;
  117.     variant & operator = (variant const & _rhs) & noexcept;
  118.     variant & operator = (variant && _rhs) & noexcept;
  119.     variant & operator = (variant const && _rhs) & noexcept;
  120.  
  121.     template< typename type >
  122.     constexpr
  123.     void
  124.     operator = (type && _value) &
  125.     {
  126.         operator std::decay_t< type > & () = std::forward< type >(_value);
  127.     }
  128.  
  129.     template< typename type >
  130.     constexpr
  131.     std::enable_if_t< (std::is_assignable< this_type &, type >{}) >
  132.     assign(type && _value)
  133.     {
  134.         assert(index == which());
  135.         operator this_type & () = std::forward< type >(_value);
  136.     }
  137.  
  138.     template< typename type >
  139.     constexpr
  140.     std::enable_if_t< !(std::is_assignable< this_type &, type >{}) >
  141.     assign(type && _value)
  142.     {
  143.         tail_.assign(std::forward< type >(_value));
  144.     }
  145.  
  146.     constexpr
  147.     explicit
  148.     operator this_type & () &
  149.     {
  150.         assert(index == which());
  151.         return head_.value_;
  152.     }
  153.  
  154.     constexpr
  155.     explicit
  156.     operator this_type const & () const &
  157.     {
  158.         assert(index == which());
  159.         return head_.value_;
  160.     }
  161.  
  162.     constexpr
  163.     explicit
  164.     operator this_type && () &&
  165.     {
  166.         assert(index == which());
  167.         return std::move(head_.value_);
  168.     }
  169.  
  170.     constexpr
  171.     explicit
  172.     operator this_type const && () const &&
  173.     {
  174.         assert(index == which());
  175.         return std::move(head_.value_);
  176.     }
  177.  
  178.     template< typename type >
  179.     constexpr
  180.     explicit
  181.     operator type & () &
  182.     {
  183.         return static_cast< type & >(tail_);
  184.     }
  185.  
  186.     template< typename type >
  187.     constexpr
  188.     explicit
  189.     operator type const & () const &
  190.     {
  191.         return static_cast< type const & >(tail_);
  192.     }
  193.  
  194.     template< typename type >
  195.     constexpr
  196.     explicit
  197.     operator type && () &&
  198.     {
  199.         //return static_cast< type && >(std::move(tail_)); // There is known clang++ bug #19917 for static_cast to rvalue reference.
  200.         return static_cast< type && >(static_cast< type & >(tail_)); // workaround
  201.     }
  202.  
  203.     template< typename type >
  204.     constexpr
  205.     explicit
  206.     operator type const && () const &&
  207.     {
  208.         //return static_cast< type const && >(std::move(tail_)); // There is known clang++ bug #19917 for static_cast to rvalue reference.
  209.         return static_cast< type const && >(static_cast< type const & >(tail_)); // workaround
  210.     }
  211.  
  212.     ~variant()
  213.     {
  214.         if (which() == index) {
  215.             head_.~head();
  216.         } else {
  217.             tail_.~tail();
  218.         }
  219.     }
  220.  
  221. };
  222.  
  223. template< std::size_t qual_id, typename type > struct add_qual;
  224. template< typename to > struct add_qual<  0, to > { using type =          to         ; };
  225. template< typename to > struct add_qual<  1, to > { using type =          to const   ; };
  226. template< typename to > struct add_qual<  2, to > { using type =          to       & ; };
  227. template< typename to > struct add_qual<  3, to > { using type =          to const & ; };
  228. template< typename to > struct add_qual<  4, to > { using type =          to       &&; };
  229. template< typename to > struct add_qual<  5, to > { using type =          to const &&; };
  230. template< typename to > struct add_qual<  6, to > { using type = volatile to         ; };
  231. template< typename to > struct add_qual<  7, to > { using type = volatile to const   ; };
  232. template< typename to > struct add_qual<  8, to > { using type = volatile to       & ; };
  233. template< typename to > struct add_qual<  9, to > { using type = volatile to const & ; };
  234. template< typename to > struct add_qual< 10, to > { using type = volatile to       &&; };
  235. template< typename to > struct add_qual< 11, to > { using type = volatile to const &&; };
  236.  
  237. template< std::size_t qual_id, typename to >
  238. using add_qual_t = typename add_qual< qual_id, to >::type;
  239.  
  240. template< typename type > constexpr std::size_t qual_id                           =  0;
  241. template< typename type > constexpr std::size_t qual_id<          type const    > =  1;
  242. template< typename type > constexpr std::size_t qual_id<          type       &  > =  2;
  243. template< typename type > constexpr std::size_t qual_id<          type const &  > =  3;
  244. template< typename type > constexpr std::size_t qual_id<          type       && > =  4;
  245. template< typename type > constexpr std::size_t qual_id<          type const && > =  5;
  246. template< typename type > constexpr std::size_t qual_id< volatile type          > =  6;
  247. template< typename type > constexpr std::size_t qual_id< volatile type const    > =  7;
  248. template< typename type > constexpr std::size_t qual_id< volatile type       &  > =  8;
  249. template< typename type > constexpr std::size_t qual_id< volatile type const &  > =  9;
  250. template< typename type > constexpr std::size_t qual_id< volatile type       && > = 10;
  251. template< typename type > constexpr std::size_t qual_id< volatile type const && > = 11;
  252.  
  253. template< typename from, typename to >
  254. using copy_cv_reference_t = add_qual_t< qual_id< from >, to >;
  255.  
  256. template< typename first, typename ...rest >
  257. struct identity
  258. {
  259.  
  260.     using type = first;
  261.  
  262. };
  263.  
  264. template< typename type, typename ...arguments >
  265. using result_of_t = decltype(std::declval< type >()(std::declval< arguments >()...));
  266.  
  267. template< typename type, typename = std::decay_t< type > >
  268. struct is_variant
  269.         : std::false_type
  270. {
  271.  
  272. };
  273.  
  274. template< typename type, typename ...types >
  275. struct is_variant< type, variant< types... > >
  276.         : std::true_type
  277. {
  278.  
  279. };
  280.  
  281. template< typename type, typename = std::decay_t< type > >
  282. struct this_type
  283.         : identity< type >
  284. {
  285.  
  286. };
  287.  
  288. template< typename visitable, typename ...types >
  289. struct this_type< visitable, variant< types... > >
  290.         : identity< copy_cv_reference_t< visitable, typename variant< types... >::this_type > >
  291. {
  292.  
  293. };
  294.  
  295. template< typename type >
  296. using this_type_t = typename this_type< type >::type;
  297.  
  298. template< typename visitor, typename visitable, typename visitable_type, typename ...arguments >
  299. class dispatcher;
  300.  
  301. template< typename visitor, typename visitable, typename ...types, typename ...arguments >
  302. class dispatcher< visitor, visitable, variant< types... >, arguments... >
  303. {
  304.  
  305.     using result_type = result_of_t< visitor, this_type_t< visitable >, arguments... >;
  306.     using caller_type = result_type (*)(visitor &&, visitable &, arguments &&...);
  307.  
  308.     template< typename type >
  309.     static
  310.     result_type
  311.     caller(visitor && _visitor, visitable & _visitable, arguments &&... _arguments)
  312.     {
  313.         //return std::forward< visitor >(_visitor)(static_cast< type >(_visitable), std::forward< arguments >(_arguments)...); // There is known clang++ bug #19917 for static_cast to rvalue reference.
  314.         return std::forward< visitor >(_visitor)(static_cast< type >(static_cast< type & >(_visitable)), std::forward< arguments >(_arguments)...); // workaround
  315.     }
  316.  
  317. public :
  318.  
  319.     result_type
  320.     operator () (visitor && _visitor, visitable && _visitable, arguments &&... _arguments) const
  321.     {
  322.         static constexpr caller_type callers_[sizeof...(types)] = {dispatcher::caller< copy_cv_reference_t< visitable &&, types > >...};
  323.         return callers_[_visitable.index - _visitable.which()](std::forward< visitor >(_visitor), _visitable, std::forward< arguments >(_arguments)...);
  324.     }
  325.  
  326. };
  327.  
  328. template< typename visitor, typename visitable, typename ...arguments >
  329. decltype(auto)
  330. visit(visitor && _visitor, visitable && _visitable, arguments &&... _arguments)
  331. {
  332.     return dispatcher< visitor, visitable, std::decay_t< visitable >, arguments... >{}(std::forward< visitor >(_visitor), std::forward< visitable >(_visitable), std::forward< arguments >(_arguments)...);
  333. }
  334.  
  335. template< typename first, typename ...rest >
  336. struct constructor
  337. {
  338.  
  339.     variant< first, rest... > * destination_;
  340.  
  341.     template< typename type >
  342.     void
  343.     operator () (type && _value) const
  344.     {
  345.         ::new (destination_) variant< first, rest... >{std::forward< type >(_value)};
  346.     }
  347.  
  348. };
  349.  
  350. template< typename first, typename ...rest >
  351. variant< first, rest... >::variant(variant & _rhs) noexcept
  352. {
  353.     visit(constructor< first, rest... >{this}, _rhs);
  354. }
  355.  
  356. template< typename first, typename ...rest >
  357. variant< first, rest... >::variant(variant const & _rhs) noexcept
  358. {
  359.     visit(constructor< first, rest... >{this}, _rhs);
  360. }
  361.  
  362. template< typename first, typename ...rest >
  363. variant< first, rest... >::variant(variant && _rhs) noexcept
  364. {
  365.     visit(constructor< first, rest... >{this}, std::move(_rhs));
  366. }
  367.  
  368. template< typename first, typename ...rest >
  369. variant< first, rest... >::variant(variant const && _rhs) noexcept
  370. {
  371.     visit(constructor< first, rest... >{this}, std::move(_rhs));
  372. }
  373.  
  374. template< typename first, typename ...rest >
  375. struct assigner
  376. {
  377.  
  378.     variant< first, rest... > * destination_;
  379.  
  380.     template< typename type >
  381.     void
  382.     operator () (type && _value) const
  383.     {
  384.         *destination_ = std::forward< type >(_value);
  385.     }
  386.  
  387. };
  388.  
  389. template< typename first, typename ...rest >
  390. auto
  391. variant< first, rest... >::operator = (variant & _rhs) & noexcept
  392. -> variant &
  393. {
  394.     if (which() == _rhs.which()) {
  395.         visit(assigner< first, rest... >{this}, _rhs);
  396.     } else {
  397.         this->~variant();
  398.         visit(constructor< first, rest... >{this}, _rhs);
  399.     }
  400. }
  401.  
  402. template< typename first, typename ...rest >
  403. auto
  404. variant< first, rest... >::operator = (variant const & _rhs) & noexcept
  405. -> variant &
  406. {
  407.     if (which() == _rhs.which()) {
  408.         visit(assigner< first, rest... >{this}, _rhs);
  409.     } else {
  410.         this->~variant();
  411.         visit(constructor< first, rest... >{this}, _rhs);
  412.     }
  413. }
  414.  
  415. template< typename first, typename ...rest >
  416. auto
  417. variant< first, rest... >::operator = (variant && _rhs) & noexcept
  418. -> variant &
  419. {
  420.     if (which() == _rhs.which()) {
  421.         visit(assigner< first, rest... >{this}, std::move(_rhs));
  422.     } else {
  423.         this->~variant();
  424.         visit(constructor< first, rest... >{this}, std::move(_rhs));
  425.     }
  426. }
  427.  
  428. template< typename first, typename ...rest >
  429. auto
  430. variant< first, rest... >::operator = (variant const && _rhs) & noexcept
  431. -> variant &
  432. {
  433.     if (which() == _rhs.which()) {
  434.         visit(assigner< first, rest... >{this}, std::move(_rhs));
  435.     } else {
  436.         this->~variant();
  437.         visit(constructor< first, rest... >{this}, std::move(_rhs));
  438.     }
  439. }
  440.  
  441. template< typename result_type, typename supervisitor, typename type, bool = (is_variant< std::remove_reference_t< type > >{}) >
  442. struct subvisitor;
  443.  
  444. template< typename result_type, typename supervisitor, typename visitable >
  445. struct subvisitor< result_type, supervisitor, visitable, true >
  446. {
  447.  
  448.     supervisitor && supervisitor_;
  449.     visitable && visitable_;
  450.  
  451.     template< typename ...visited >
  452.     constexpr
  453.     result_type
  454.     operator () (visited &&... _visited) const
  455.     {
  456.         return visit(std::forward< supervisitor >(supervisitor_), std::forward< visitable >(visitable_), std::forward< visited >(_visited)...);
  457.     }
  458.  
  459. };
  460.  
  461. template< typename result_type, typename supervisitor, typename type >
  462. struct subvisitor< result_type, supervisitor, type, false >
  463. {
  464.  
  465.     supervisitor && supervisitor_;
  466.     type && value_;
  467.  
  468.     template< typename ...visited >
  469.     constexpr
  470.     result_type
  471.     operator () (visited &&... _visited) const
  472.     {
  473.         return std::forward< supervisitor >(supervisitor_)(std::forward< type >(value_), std::forward< visited >(_visited)...);
  474.     }
  475.  
  476. };
  477.  
  478. template< typename result_type, typename ...visitables >
  479. struct visitor_partially_applier;
  480.  
  481. template< typename result_type >
  482. struct visitor_partially_applier< result_type >
  483. {
  484.  
  485.     template< typename visitor >
  486.     constexpr
  487.     result_type
  488.     operator () (visitor && _visitor) const
  489.     {
  490.         return std::forward< visitor >(_visitor)();
  491.     }
  492.  
  493. };
  494.  
  495. template< typename result_type, typename first, typename ...rest >
  496. struct visitor_partially_applier< result_type, first, rest... >
  497. {
  498.  
  499.     template< typename visitor >
  500.     constexpr
  501.     result_type
  502.     operator () (visitor && _visitor, first && _first, rest &&... _rest) const
  503.     {
  504.         return visitor_partially_applier< result_type, rest... >{}(subvisitor< result_type, visitor, first >{std::forward< visitor >(_visitor), std::forward< first >(_first)}, std::forward< rest >(_rest)...);
  505.     }
  506.  
  507. };
  508.  
  509. template< typename visitor, typename ...types >
  510. constexpr
  511. decltype(auto)
  512. multivisit(visitor && _visitor, types &&... _values)
  513. {
  514.     using result_type = result_of_t< visitor, this_type_t< types >... >;
  515.     return visitor_partially_applier< result_type, types... >{}(std::forward< visitor >(_visitor), std::forward< types >(_values)...);
  516. }
  517.  
  518. // main.cpp
  519. #include "variant.hpp"
  520.  
  521. #include <iostream>
  522. #include <array>
  523. #include <tuple>
  524. #include <utility>
  525. #include <limits>
  526. #include <type_traits>
  527.  
  528. #include <cstdlib>
  529.  
  530. #ifdef NDEBUG
  531. #undef NDEBUG
  532. #endif
  533. #include <cassert>
  534.  
  535. namespace
  536. {
  537.  
  538. struct A {};
  539. struct B {};
  540. static_assert(std::is_same< copy_cv_reference_t<          A         , B >,          B          >{});
  541. static_assert(std::is_same< copy_cv_reference_t<          A const   , B >,          B const    >{});
  542. static_assert(std::is_same< copy_cv_reference_t< volatile A         , B >, volatile B          >{});
  543. static_assert(std::is_same< copy_cv_reference_t< volatile A const   , B >, volatile B const    >{});
  544. static_assert(std::is_same< copy_cv_reference_t<          A        &, B >,          B        & >{});
  545. static_assert(std::is_same< copy_cv_reference_t<          A const  &, B >,          B const  & >{});
  546. static_assert(std::is_same< copy_cv_reference_t< volatile A        &, B >, volatile B        & >{});
  547. static_assert(std::is_same< copy_cv_reference_t< volatile A const  &, B >, volatile B const  & >{});
  548. static_assert(std::is_same< copy_cv_reference_t<          A       &&, B >,          B       && >{});
  549. static_assert(std::is_same< copy_cv_reference_t<          A const &&, B >,          B const && >{});
  550. static_assert(std::is_same< copy_cv_reference_t< volatile A       &&, B >, volatile B       && >{});
  551. static_assert(std::is_same< copy_cv_reference_t< volatile A const &&, B >, volatile B const && >{});
  552.  
  553. }
  554.  
  555. template< std::size_t I >
  556. struct T
  557. {
  558.  
  559.     constexpr
  560.     operator std::size_t () const noexcept
  561.     {
  562.         return I;
  563.     }
  564.  
  565. };
  566.  
  567. struct pair
  568. {
  569.  
  570.     pair() = delete;
  571.  
  572.     std::size_t qual_id_;
  573.     std::size_t type_id_;
  574.  
  575.     constexpr
  576.     bool
  577.     operator == (pair const & _rhs) const
  578.     {
  579.         return (qual_id_ == _rhs.qual_id_) && (type_id_ == _rhs.type_id_);
  580.     }
  581.  
  582. };
  583.  
  584. template< std::size_t M >
  585. struct multivisitor
  586. {
  587.  
  588.     using result_type = std::array< pair, (1 + M) >;
  589.  
  590.     constexpr
  591.     std::size_t
  592.     which() const // mimic to be possible to treat along with variant
  593.     {
  594.         return 0;
  595.     }
  596.  
  597.     static constexpr std::size_t index = M; // mimic to be possible to treat along with variant
  598.  
  599.     template< typename ...types >
  600.     constexpr
  601.     result_type
  602.     operator () (types &&... _values) & noexcept
  603.     {
  604.         //static_assert(M == sizeof...(types));
  605.         //static_assert(!(is_variant< types >{} || ...));
  606.         return {{{qual_id<          multivisitor       & >, M}, {qual_id< types && >, _values}...}};
  607.     }
  608.  
  609.     template< typename ...types >
  610.     constexpr
  611.     result_type
  612.     operator () (types &&... _values) const & noexcept
  613.     {
  614.         return {{{qual_id<          multivisitor const & >, M}, {qual_id< types && >, _values}...}};
  615.     }
  616.  
  617.     template< typename ...types >
  618.     constexpr
  619.     result_type
  620.     operator () (types &&... _values) volatile & noexcept
  621.     {
  622.         return {{{qual_id< volatile multivisitor       & >, M}, {qual_id< types && >, _values}...}};
  623.     }
  624.  
  625.     template< typename ...types >
  626.     constexpr
  627.     result_type
  628.     operator () (types &&... _values) volatile const & noexcept
  629.     {
  630.         return {{{qual_id< volatile multivisitor       & >, M}, {qual_id< types && >, _values}...}};
  631.     }
  632.  
  633.     template< typename ...types >
  634.     constexpr
  635.     result_type
  636.     operator () (types &&... _values) && noexcept
  637.     {
  638.         return {{{qual_id<          multivisitor       && >, M}, {qual_id< types && >, _values}...}};
  639.     }
  640.  
  641.     template< typename ...types >
  642.     constexpr
  643.     result_type
  644.     operator () (types &&... _values) const && noexcept
  645.     {
  646.         return {{{qual_id<          multivisitor const && >, M}, {qual_id< types && >, _values}...}};
  647.     }
  648.  
  649.     template< typename ...types >
  650.     constexpr
  651.     result_type
  652.     operator () (types &&... _values) volatile && noexcept
  653.     {
  654.         return {{{qual_id< volatile multivisitor       && >, M}, {qual_id< types && >, _values}...}};
  655.     }
  656.  
  657.     template< typename ...types >
  658.     constexpr
  659.     result_type
  660.     operator () (types &&... _values) volatile const && noexcept
  661.     {
  662.         return {{{qual_id< volatile multivisitor const && >, M}, {qual_id< types && >, _values}...}};
  663.     }
  664.  
  665. };
  666.  
  667. template< typename F, std::size_t ...extents >
  668. struct enumerator; // enumerate all multidimensional indices
  669.  
  670. template< typename F >
  671. struct enumerator< F >
  672. {
  673.  
  674.     constexpr
  675.     enumerator(F && _f)
  676.         : f(std::forward< F >(_f))
  677.     { ; }
  678.  
  679.     template< std::size_t ...indices >
  680.     constexpr
  681.     bool
  682.     operator () () const
  683.     {
  684.         return f(std::index_sequence< indices... >{});
  685.     }
  686.  
  687. private :
  688.  
  689.     F f;
  690.  
  691. };
  692.  
  693. template< typename F, std::size_t first, std::size_t ...rest >
  694. struct enumerator< F, first, rest... >
  695.         : enumerator< F, rest... >
  696. {
  697.  
  698.     constexpr
  699.     enumerator(F && _f)
  700.         : enumerator< F, rest... >(std::forward< F >(_f))
  701.     { ; }
  702.  
  703.     template< std::size_t ...indices >
  704.     constexpr
  705.     bool
  706.     operator () () const
  707.     {
  708.         return enumerator::template operator () < indices... >(std::make_index_sequence< first >{}); // ltr
  709.     }
  710.  
  711.     template< std::size_t ...indices, std::size_t ...mixin >
  712.     constexpr
  713.     bool
  714.     operator () (std::index_sequence< mixin... >) const
  715.     {
  716.         return (enumerator< F, rest... >::template operator () < indices..., mixin >() && ...); // rtl, `< J, I... >` - ltr
  717.     }
  718.  
  719. };
  720.  
  721. template< std::size_t ...extents, typename F >
  722. constexpr
  723. enumerator< F, extents... >
  724. make_enumerator(F && _f)
  725. {
  726.     static_assert(0 < sizeof...(extents));
  727.     static_assert(((0 < extents) && ...));
  728.     return std::forward< F >(_f);
  729. }
  730.  
  731. static constexpr std::size_t base_qual_id = qual_id< void * & >;
  732. static constexpr std::size_t qual_id_count = qual_id< void * volatile >;
  733.  
  734. template< typename ...types >
  735. struct fusor
  736. {
  737.  
  738.     std::tuple< types *... > const & stuff_;
  739.  
  740.     template< std::size_t ...qual_ids, std::size_t ...sequential_number >
  741.     bool
  742.     operator () (std::index_sequence< qual_ids... >, std::index_sequence< sequential_number... >) const
  743.     {/*
  744.         std::cout << "#1 ";
  745.         for (void const * const & p : {static_cast< void * >(std::get< K >(stuff_))...}) {
  746.             std::cout << p << ' ';
  747.         }
  748.         std::cout << "\n";
  749.         std::cout << __PRETTY_FUNCTION__ << std::endl;
  750.         for (std::size_t const & qual_id : {(base_qual_id + Q)...}) {
  751.             std::cout << qual_id << ' ';
  752.         }
  753.         std::cout << "- ";
  754.         for (std::size_t const & type_id : {(std::get< K >(stuff_)->index - std::get< K >(stuff_)->which())...}) {
  755.             std::cout << type_id << ' ';
  756.         }
  757.         std::cout << "\n";*/
  758.         auto const lhs = multivisit(static_cast< add_qual_t< (base_qual_id + qual_ids), types > >(*std::get< sequential_number >(stuff_))...);
  759.         static_assert(sizeof...(types) == lhs.size());
  760.         std::array< pair, sizeof...(types) > const rhs = {{{(base_qual_id + qual_ids), (std::get< sequential_number >(stuff_)->index - std::get< sequential_number >(stuff_)->which())}...}};
  761.         return (lhs == rhs);
  762.     }
  763.  
  764.     template< std::size_t ...qual_ids >
  765.     bool
  766.     operator () (std::index_sequence< qual_ids... >) const
  767.     {
  768.         return operator () (std::index_sequence< qual_ids... >{}, std::index_sequence_for< types... >{});
  769.     }
  770.  
  771. };
  772.  
  773. template< typename ...types >
  774. fusor< types... >
  775. make_fusor(std::tuple< types *... > const & _stuff)
  776. {
  777.     static_assert(((qual_id< types > == 0) && ...));
  778.     return {_stuff};
  779. }
  780.  
  781. template< std::size_t M, std::size_t N = M > // M - multivisitor arity, N - number of alternative (bounded) types
  782. class test
  783. {
  784.  
  785.     template< std::size_t ...J >
  786.     static
  787.     auto
  788.     generate_variants(std::index_sequence< J... >)
  789.     {
  790.         using variant_type = variant< T< J >... >;
  791.         return std::array< variant_type, N >{{T< J >{}...}}; // !!!
  792.     }
  793.  
  794.     using variant_type = typename decltype(generate_variants(std::make_index_sequence< N >{}))::value_type;
  795.  
  796.     static_assert(N == (1 + variant_type::index));
  797.  
  798.     template< std::size_t ...I >
  799.     bool
  800.     operator () (std::index_sequence< I... >) const
  801.     {
  802.         static_assert(sizeof...(I) == M);
  803.         std::size_t indices[M];
  804.         for (std::size_t & m : indices) {
  805.             m = 0;
  806.         }
  807.         multivisitor< M > mv;
  808.         auto variants = generate_variants(std::make_index_sequence< N >{});
  809.         auto permutation_ = std::make_tuple(&mv, &variants[indices[I]]...);
  810.         auto const fusor_ = make_fusor(permutation_);
  811.         constexpr std::size_t ref_count = (qual_id_count - base_qual_id); // test only reference types
  812.         auto const enumerator_ = make_enumerator< ref_count, (I, ref_count)... >(fusor_);
  813.         for (;;) {
  814.             permutation_ = std::make_tuple(&mv, &variants[indices[I]]...);
  815.             if (!enumerator_()) {
  816.                 return false;
  817.             }
  818.             std::size_t m = 0;
  819.             for (;;) {
  820.                 std::size_t & n = indices[m];
  821.                 ++n;
  822.                 if (n != N) {
  823.                     break;
  824.                 }
  825.                 n = 0;
  826.                 if (++m == M) {
  827.                     return true;
  828.                 }
  829.             }
  830.         }
  831.         return true;
  832.     }
  833.  
  834. public :
  835.  
  836.     bool
  837.     operator () () const
  838.     {
  839.         return operator () (std::make_index_sequence< M >{});
  840.     }
  841.  
  842. };
  843.  
  844. struct truth
  845. {
  846.  
  847.     template< std::size_t ...I >
  848.     constexpr
  849.     bool
  850.     operator () (std::index_sequence< I... >) const
  851.     {
  852.         return true;
  853.     }
  854.  
  855. };
  856.  
  857. struct printer
  858. {
  859.  
  860.     template< std::size_t ...I >
  861.     bool
  862.     operator () (std::index_sequence< I... >) const
  863.     {
  864.         for (std::size_t const & i : {I...}) {
  865.             std::cout << i << ' ';
  866.         }
  867.         std::cout << std::endl;
  868.         return true;
  869.     }
  870.  
  871. };
  872.  
  873. int
  874. main()
  875. {
  876.     constexpr truth truth_{};
  877.     static_assert(make_enumerator< 3, 3, 3 >(truth_)());
  878.     //assert((make_enumerator< 10, 10 >(printer{})()));
  879.     assert((test< ROWS, COLS >{}()));
  880.     return EXIT_SUCCESS;
  881. }
Advertisement
Add Comment
Please, Sign In to add comment