Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // variant.hpp
- #pragma once
- #include <type_traits>
- #include <utility>
- #include <experimental/optional>
- #include <cassert>
- template< typename ...types >
- struct variant;
- template<>
- struct variant<>
- {
- variant() = delete;
- variant(variant &) = delete;
- variant(variant const &) = delete;
- variant(variant &&) = delete;
- variant(variant const &&) = delete;
- void operator = (variant &) = delete;
- void operator = (variant const &) = delete;
- void operator = (variant &&) = delete;
- void operator = (variant const &&) = delete;
- };
- template< typename first, typename ...rest >
- struct variant< first, rest... >
- {
- static constexpr std::size_t index = sizeof...(rest);
- private :
- struct head
- {
- std::size_t which_;
- first value_;
- template< typename ...types >
- constexpr
- head(std::experimental::in_place_t, types &&... _values)
- : which_{index}
- , value_(std::forward< types >(_values)...)
- { ; }
- template< typename type >
- constexpr
- head(type && _value)
- : head(std::experimental::in_place, std::forward< type >(_value))
- { ; }
- };
- using tail = variant< rest... >;
- union
- {
- head head_;
- tail tail_;
- };
- template< typename ...types >
- constexpr
- variant(std::true_type, types &&... _values)
- : head_(std::forward< types >(_values)...)
- { ; }
- template< typename ...types >
- constexpr
- variant(std::false_type, types &&... _values)
- : tail_(std::forward< types >(_values)...)
- { ; }
- public :
- using this_type = first; // a place for recursive_wrapper filtering
- constexpr
- std::size_t
- which() const
- {
- return head_.which_;
- }
- constexpr
- variant()
- : variant(typename std::is_default_constructible< this_type >::type{}, std::experimental::in_place)
- { ; }
- // if exception thrown during copy/move-construction then std::terminate called
- variant(variant & _rhs) noexcept;
- variant(variant const & _rhs) noexcept;
- variant(variant && _rhs) noexcept;
- variant(variant const && _rhs) noexcept;
- template< typename type >
- constexpr
- variant(type && _value)
- : variant(typename std::is_same< this_type, std::decay_t< type > >::type{}, std::forward< type >(_value))
- { ; }
- template< typename ...types >
- constexpr
- variant(std::experimental::in_place_t, types &&... _values)
- : variant(typename std::is_constructible< this_type, types... >::type{}, std::experimental::in_place, std::forward< types >(_values)...)
- { ; }
- // if exception thrown during copy/move-assignment then std::terminate called
- variant & operator = (variant & _rhs) & noexcept;
- variant & operator = (variant const & _rhs) & noexcept;
- variant & operator = (variant && _rhs) & noexcept;
- variant & operator = (variant const && _rhs) & noexcept;
- template< typename type >
- constexpr
- void
- operator = (type && _value) &
- {
- operator std::decay_t< type > & () = std::forward< type >(_value);
- }
- template< typename type >
- constexpr
- std::enable_if_t< (std::is_assignable< this_type &, type >{}) >
- assign(type && _value)
- {
- assert(index == which());
- operator this_type & () = std::forward< type >(_value);
- }
- template< typename type >
- constexpr
- std::enable_if_t< !(std::is_assignable< this_type &, type >{}) >
- assign(type && _value)
- {
- tail_.assign(std::forward< type >(_value));
- }
- constexpr
- explicit
- operator this_type & () &
- {
- assert(index == which());
- return head_.value_;
- }
- constexpr
- explicit
- operator this_type const & () const &
- {
- assert(index == which());
- return head_.value_;
- }
- constexpr
- explicit
- operator this_type && () &&
- {
- assert(index == which());
- return std::move(head_.value_);
- }
- constexpr
- explicit
- operator this_type const && () const &&
- {
- assert(index == which());
- return std::move(head_.value_);
- }
- template< typename type >
- constexpr
- explicit
- operator type & () &
- {
- return static_cast< type & >(tail_);
- }
- template< typename type >
- constexpr
- explicit
- operator type const & () const &
- {
- return static_cast< type const & >(tail_);
- }
- template< typename type >
- constexpr
- explicit
- operator type && () &&
- {
- //return static_cast< type && >(std::move(tail_)); // There is known clang++ bug #19917 for static_cast to rvalue reference.
- return static_cast< type && >(static_cast< type & >(tail_)); // workaround
- }
- template< typename type >
- constexpr
- explicit
- operator type const && () const &&
- {
- //return static_cast< type const && >(std::move(tail_)); // There is known clang++ bug #19917 for static_cast to rvalue reference.
- return static_cast< type const && >(static_cast< type const & >(tail_)); // workaround
- }
- ~variant()
- {
- if (which() == index) {
- head_.~head();
- } else {
- tail_.~tail();
- }
- }
- };
- template< std::size_t qual_id, typename type > struct add_qual;
- template< typename to > struct add_qual< 0, to > { using type = to ; };
- template< typename to > struct add_qual< 1, to > { using type = to const ; };
- template< typename to > struct add_qual< 2, to > { using type = to & ; };
- template< typename to > struct add_qual< 3, to > { using type = to const & ; };
- template< typename to > struct add_qual< 4, to > { using type = to &&; };
- template< typename to > struct add_qual< 5, to > { using type = to const &&; };
- template< typename to > struct add_qual< 6, to > { using type = volatile to ; };
- template< typename to > struct add_qual< 7, to > { using type = volatile to const ; };
- template< typename to > struct add_qual< 8, to > { using type = volatile to & ; };
- template< typename to > struct add_qual< 9, to > { using type = volatile to const & ; };
- template< typename to > struct add_qual< 10, to > { using type = volatile to &&; };
- template< typename to > struct add_qual< 11, to > { using type = volatile to const &&; };
- template< std::size_t qual_id, typename to >
- using add_qual_t = typename add_qual< qual_id, to >::type;
- template< typename type > constexpr std::size_t qual_id = 0;
- template< typename type > constexpr std::size_t qual_id< type const > = 1;
- template< typename type > constexpr std::size_t qual_id< type & > = 2;
- template< typename type > constexpr std::size_t qual_id< type const & > = 3;
- template< typename type > constexpr std::size_t qual_id< type && > = 4;
- template< typename type > constexpr std::size_t qual_id< type const && > = 5;
- template< typename type > constexpr std::size_t qual_id< volatile type > = 6;
- template< typename type > constexpr std::size_t qual_id< volatile type const > = 7;
- template< typename type > constexpr std::size_t qual_id< volatile type & > = 8;
- template< typename type > constexpr std::size_t qual_id< volatile type const & > = 9;
- template< typename type > constexpr std::size_t qual_id< volatile type && > = 10;
- template< typename type > constexpr std::size_t qual_id< volatile type const && > = 11;
- template< typename from, typename to >
- using copy_cv_reference_t = add_qual_t< qual_id< from >, to >;
- template< typename first, typename ...rest >
- struct identity
- {
- using type = first;
- };
- template< typename type, typename ...arguments >
- using result_of_t = decltype(std::declval< type >()(std::declval< arguments >()...));
- template< typename type, typename = std::decay_t< type > >
- struct is_variant
- : std::false_type
- {
- };
- template< typename type, typename ...types >
- struct is_variant< type, variant< types... > >
- : std::true_type
- {
- };
- template< typename type, typename = std::decay_t< type > >
- struct this_type
- : identity< type >
- {
- };
- template< typename visitable, typename ...types >
- struct this_type< visitable, variant< types... > >
- : identity< copy_cv_reference_t< visitable, typename variant< types... >::this_type > >
- {
- };
- template< typename type >
- using this_type_t = typename this_type< type >::type;
- template< typename visitor, typename visitable, typename visitable_type, typename ...arguments >
- class dispatcher;
- template< typename visitor, typename visitable, typename ...types, typename ...arguments >
- class dispatcher< visitor, visitable, variant< types... >, arguments... >
- {
- using result_type = result_of_t< visitor, this_type_t< visitable >, arguments... >;
- using caller_type = result_type (*)(visitor &&, visitable &, arguments &&...);
- template< typename type >
- static
- result_type
- caller(visitor && _visitor, visitable & _visitable, arguments &&... _arguments)
- {
- //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.
- return std::forward< visitor >(_visitor)(static_cast< type >(static_cast< type & >(_visitable)), std::forward< arguments >(_arguments)...); // workaround
- }
- public :
- result_type
- operator () (visitor && _visitor, visitable && _visitable, arguments &&... _arguments) const
- {
- static constexpr caller_type callers_[sizeof...(types)] = {dispatcher::caller< copy_cv_reference_t< visitable &&, types > >...};
- return callers_[_visitable.index - _visitable.which()](std::forward< visitor >(_visitor), _visitable, std::forward< arguments >(_arguments)...);
- }
- };
- template< typename visitor, typename visitable, typename ...arguments >
- decltype(auto)
- visit(visitor && _visitor, visitable && _visitable, arguments &&... _arguments)
- {
- return dispatcher< visitor, visitable, std::decay_t< visitable >, arguments... >{}(std::forward< visitor >(_visitor), std::forward< visitable >(_visitable), std::forward< arguments >(_arguments)...);
- }
- template< typename first, typename ...rest >
- struct constructor
- {
- variant< first, rest... > * destination_;
- template< typename type >
- void
- operator () (type && _value) const
- {
- ::new (destination_) variant< first, rest... >{std::forward< type >(_value)};
- }
- };
- template< typename first, typename ...rest >
- variant< first, rest... >::variant(variant & _rhs) noexcept
- {
- visit(constructor< first, rest... >{this}, _rhs);
- }
- template< typename first, typename ...rest >
- variant< first, rest... >::variant(variant const & _rhs) noexcept
- {
- visit(constructor< first, rest... >{this}, _rhs);
- }
- template< typename first, typename ...rest >
- variant< first, rest... >::variant(variant && _rhs) noexcept
- {
- visit(constructor< first, rest... >{this}, std::move(_rhs));
- }
- template< typename first, typename ...rest >
- variant< first, rest... >::variant(variant const && _rhs) noexcept
- {
- visit(constructor< first, rest... >{this}, std::move(_rhs));
- }
- template< typename first, typename ...rest >
- struct assigner
- {
- variant< first, rest... > * destination_;
- template< typename type >
- void
- operator () (type && _value) const
- {
- *destination_ = std::forward< type >(_value);
- }
- };
- template< typename first, typename ...rest >
- auto
- variant< first, rest... >::operator = (variant & _rhs) & noexcept
- -> variant &
- {
- if (which() == _rhs.which()) {
- visit(assigner< first, rest... >{this}, _rhs);
- } else {
- this->~variant();
- visit(constructor< first, rest... >{this}, _rhs);
- }
- }
- template< typename first, typename ...rest >
- auto
- variant< first, rest... >::operator = (variant const & _rhs) & noexcept
- -> variant &
- {
- if (which() == _rhs.which()) {
- visit(assigner< first, rest... >{this}, _rhs);
- } else {
- this->~variant();
- visit(constructor< first, rest... >{this}, _rhs);
- }
- }
- template< typename first, typename ...rest >
- auto
- variant< first, rest... >::operator = (variant && _rhs) & noexcept
- -> variant &
- {
- if (which() == _rhs.which()) {
- visit(assigner< first, rest... >{this}, std::move(_rhs));
- } else {
- this->~variant();
- visit(constructor< first, rest... >{this}, std::move(_rhs));
- }
- }
- template< typename first, typename ...rest >
- auto
- variant< first, rest... >::operator = (variant const && _rhs) & noexcept
- -> variant &
- {
- if (which() == _rhs.which()) {
- visit(assigner< first, rest... >{this}, std::move(_rhs));
- } else {
- this->~variant();
- visit(constructor< first, rest... >{this}, std::move(_rhs));
- }
- }
- template< typename result_type, typename supervisitor, typename type, bool = (is_variant< std::remove_reference_t< type > >{}) >
- struct subvisitor;
- template< typename result_type, typename supervisitor, typename visitable >
- struct subvisitor< result_type, supervisitor, visitable, true >
- {
- supervisitor && supervisitor_;
- visitable && visitable_;
- template< typename ...visited >
- constexpr
- result_type
- operator () (visited &&... _visited) const
- {
- return visit(std::forward< supervisitor >(supervisitor_), std::forward< visitable >(visitable_), std::forward< visited >(_visited)...);
- }
- };
- template< typename result_type, typename supervisitor, typename type >
- struct subvisitor< result_type, supervisitor, type, false >
- {
- supervisitor && supervisitor_;
- type && value_;
- template< typename ...visited >
- constexpr
- result_type
- operator () (visited &&... _visited) const
- {
- return std::forward< supervisitor >(supervisitor_)(std::forward< type >(value_), std::forward< visited >(_visited)...);
- }
- };
- template< typename result_type, typename ...visitables >
- struct visitor_partially_applier;
- template< typename result_type >
- struct visitor_partially_applier< result_type >
- {
- template< typename visitor >
- constexpr
- result_type
- operator () (visitor && _visitor) const
- {
- return std::forward< visitor >(_visitor)();
- }
- };
- template< typename result_type, typename first, typename ...rest >
- struct visitor_partially_applier< result_type, first, rest... >
- {
- template< typename visitor >
- constexpr
- result_type
- operator () (visitor && _visitor, first && _first, rest &&... _rest) const
- {
- return visitor_partially_applier< result_type, rest... >{}(subvisitor< result_type, visitor, first >{std::forward< visitor >(_visitor), std::forward< first >(_first)}, std::forward< rest >(_rest)...);
- }
- };
- template< typename visitor, typename ...types >
- constexpr
- decltype(auto)
- multivisit(visitor && _visitor, types &&... _values)
- {
- using result_type = result_of_t< visitor, this_type_t< types >... >;
- return visitor_partially_applier< result_type, types... >{}(std::forward< visitor >(_visitor), std::forward< types >(_values)...);
- }
- // main.cpp
- #include "variant.hpp"
- #include <iostream>
- #include <array>
- #include <tuple>
- #include <utility>
- #include <limits>
- #include <type_traits>
- #include <cstdlib>
- #ifdef NDEBUG
- #undef NDEBUG
- #endif
- #include <cassert>
- namespace
- {
- struct A {};
- struct B {};
- static_assert(std::is_same< copy_cv_reference_t< A , B >, B >{});
- static_assert(std::is_same< copy_cv_reference_t< A const , B >, B const >{});
- static_assert(std::is_same< copy_cv_reference_t< volatile A , B >, volatile B >{});
- static_assert(std::is_same< copy_cv_reference_t< volatile A const , B >, volatile B const >{});
- static_assert(std::is_same< copy_cv_reference_t< A &, B >, B & >{});
- static_assert(std::is_same< copy_cv_reference_t< A const &, B >, B const & >{});
- static_assert(std::is_same< copy_cv_reference_t< volatile A &, B >, volatile B & >{});
- static_assert(std::is_same< copy_cv_reference_t< volatile A const &, B >, volatile B const & >{});
- static_assert(std::is_same< copy_cv_reference_t< A &&, B >, B && >{});
- static_assert(std::is_same< copy_cv_reference_t< A const &&, B >, B const && >{});
- static_assert(std::is_same< copy_cv_reference_t< volatile A &&, B >, volatile B && >{});
- static_assert(std::is_same< copy_cv_reference_t< volatile A const &&, B >, volatile B const && >{});
- }
- template< std::size_t I >
- struct T
- {
- constexpr
- operator std::size_t () const noexcept
- {
- return I;
- }
- };
- struct pair
- {
- pair() = delete;
- std::size_t qual_id_;
- std::size_t type_id_;
- constexpr
- bool
- operator == (pair const & _rhs) const
- {
- return (qual_id_ == _rhs.qual_id_) && (type_id_ == _rhs.type_id_);
- }
- };
- template< std::size_t M >
- struct multivisitor
- {
- using result_type = std::array< pair, (1 + M) >;
- constexpr
- std::size_t
- which() const // mimic to be possible to treat along with variant
- {
- return 0;
- }
- static constexpr std::size_t index = M; // mimic to be possible to treat along with variant
- template< typename ...types >
- constexpr
- result_type
- operator () (types &&... _values) & noexcept
- {
- //static_assert(M == sizeof...(types));
- //static_assert(!(is_variant< types >{} || ...));
- return {{{qual_id< multivisitor & >, M}, {qual_id< types && >, _values}...}};
- }
- template< typename ...types >
- constexpr
- result_type
- operator () (types &&... _values) const & noexcept
- {
- return {{{qual_id< multivisitor const & >, M}, {qual_id< types && >, _values}...}};
- }
- template< typename ...types >
- constexpr
- result_type
- operator () (types &&... _values) volatile & noexcept
- {
- return {{{qual_id< volatile multivisitor & >, M}, {qual_id< types && >, _values}...}};
- }
- template< typename ...types >
- constexpr
- result_type
- operator () (types &&... _values) volatile const & noexcept
- {
- return {{{qual_id< volatile multivisitor & >, M}, {qual_id< types && >, _values}...}};
- }
- template< typename ...types >
- constexpr
- result_type
- operator () (types &&... _values) && noexcept
- {
- return {{{qual_id< multivisitor && >, M}, {qual_id< types && >, _values}...}};
- }
- template< typename ...types >
- constexpr
- result_type
- operator () (types &&... _values) const && noexcept
- {
- return {{{qual_id< multivisitor const && >, M}, {qual_id< types && >, _values}...}};
- }
- template< typename ...types >
- constexpr
- result_type
- operator () (types &&... _values) volatile && noexcept
- {
- return {{{qual_id< volatile multivisitor && >, M}, {qual_id< types && >, _values}...}};
- }
- template< typename ...types >
- constexpr
- result_type
- operator () (types &&... _values) volatile const && noexcept
- {
- return {{{qual_id< volatile multivisitor const && >, M}, {qual_id< types && >, _values}...}};
- }
- };
- template< typename F, std::size_t ...extents >
- struct enumerator; // enumerate all multidimensional indices
- template< typename F >
- struct enumerator< F >
- {
- constexpr
- enumerator(F && _f)
- : f(std::forward< F >(_f))
- { ; }
- template< std::size_t ...indices >
- constexpr
- bool
- operator () () const
- {
- return f(std::index_sequence< indices... >{});
- }
- private :
- F f;
- };
- template< typename F, std::size_t first, std::size_t ...rest >
- struct enumerator< F, first, rest... >
- : enumerator< F, rest... >
- {
- constexpr
- enumerator(F && _f)
- : enumerator< F, rest... >(std::forward< F >(_f))
- { ; }
- template< std::size_t ...indices >
- constexpr
- bool
- operator () () const
- {
- return enumerator::template operator () < indices... >(std::make_index_sequence< first >{}); // ltr
- }
- template< std::size_t ...indices, std::size_t ...mixin >
- constexpr
- bool
- operator () (std::index_sequence< mixin... >) const
- {
- return (enumerator< F, rest... >::template operator () < indices..., mixin >() && ...); // rtl, `< J, I... >` - ltr
- }
- };
- template< std::size_t ...extents, typename F >
- constexpr
- enumerator< F, extents... >
- make_enumerator(F && _f)
- {
- static_assert(0 < sizeof...(extents));
- static_assert(((0 < extents) && ...));
- return std::forward< F >(_f);
- }
- static constexpr std::size_t base_qual_id = qual_id< void * & >;
- static constexpr std::size_t qual_id_count = qual_id< void * volatile >;
- template< typename ...types >
- struct fusor
- {
- std::tuple< types *... > const & stuff_;
- template< std::size_t ...qual_ids, std::size_t ...sequential_number >
- bool
- operator () (std::index_sequence< qual_ids... >, std::index_sequence< sequential_number... >) const
- {/*
- std::cout << "#1 ";
- for (void const * const & p : {static_cast< void * >(std::get< K >(stuff_))...}) {
- std::cout << p << ' ';
- }
- std::cout << "\n";
- std::cout << __PRETTY_FUNCTION__ << std::endl;
- for (std::size_t const & qual_id : {(base_qual_id + Q)...}) {
- std::cout << qual_id << ' ';
- }
- std::cout << "- ";
- for (std::size_t const & type_id : {(std::get< K >(stuff_)->index - std::get< K >(stuff_)->which())...}) {
- std::cout << type_id << ' ';
- }
- std::cout << "\n";*/
- auto const lhs = multivisit(static_cast< add_qual_t< (base_qual_id + qual_ids), types > >(*std::get< sequential_number >(stuff_))...);
- static_assert(sizeof...(types) == lhs.size());
- std::array< pair, sizeof...(types) > const rhs = {{{(base_qual_id + qual_ids), (std::get< sequential_number >(stuff_)->index - std::get< sequential_number >(stuff_)->which())}...}};
- return (lhs == rhs);
- }
- template< std::size_t ...qual_ids >
- bool
- operator () (std::index_sequence< qual_ids... >) const
- {
- return operator () (std::index_sequence< qual_ids... >{}, std::index_sequence_for< types... >{});
- }
- };
- template< typename ...types >
- fusor< types... >
- make_fusor(std::tuple< types *... > const & _stuff)
- {
- static_assert(((qual_id< types > == 0) && ...));
- return {_stuff};
- }
- template< std::size_t M, std::size_t N = M > // M - multivisitor arity, N - number of alternative (bounded) types
- class test
- {
- template< std::size_t ...J >
- static
- auto
- generate_variants(std::index_sequence< J... >)
- {
- using variant_type = variant< T< J >... >;
- return std::array< variant_type, N >{{T< J >{}...}}; // !!!
- }
- using variant_type = typename decltype(generate_variants(std::make_index_sequence< N >{}))::value_type;
- static_assert(N == (1 + variant_type::index));
- template< std::size_t ...I >
- bool
- operator () (std::index_sequence< I... >) const
- {
- static_assert(sizeof...(I) == M);
- std::size_t indices[M];
- for (std::size_t & m : indices) {
- m = 0;
- }
- multivisitor< M > mv;
- auto variants = generate_variants(std::make_index_sequence< N >{});
- auto permutation_ = std::make_tuple(&mv, &variants[indices[I]]...);
- auto const fusor_ = make_fusor(permutation_);
- constexpr std::size_t ref_count = (qual_id_count - base_qual_id); // test only reference types
- auto const enumerator_ = make_enumerator< ref_count, (I, ref_count)... >(fusor_);
- for (;;) {
- permutation_ = std::make_tuple(&mv, &variants[indices[I]]...);
- if (!enumerator_()) {
- return false;
- }
- std::size_t m = 0;
- for (;;) {
- std::size_t & n = indices[m];
- ++n;
- if (n != N) {
- break;
- }
- n = 0;
- if (++m == M) {
- return true;
- }
- }
- }
- return true;
- }
- public :
- bool
- operator () () const
- {
- return operator () (std::make_index_sequence< M >{});
- }
- };
- struct truth
- {
- template< std::size_t ...I >
- constexpr
- bool
- operator () (std::index_sequence< I... >) const
- {
- return true;
- }
- };
- struct printer
- {
- template< std::size_t ...I >
- bool
- operator () (std::index_sequence< I... >) const
- {
- for (std::size_t const & i : {I...}) {
- std::cout << i << ' ';
- }
- std::cout << std::endl;
- return true;
- }
- };
- int
- main()
- {
- constexpr truth truth_{};
- static_assert(make_enumerator< 3, 3, 3 >(truth_)());
- //assert((make_enumerator< 10, 10 >(printer{})()));
- assert((test< ROWS, COLS >{}()));
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment