// Copyright 2016-2018 by Martin Moene // // https://github.com/martinmoene/variant-lite // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #pragma once #ifndef NONSTD_VARIANT_LITE_HPP #define NONSTD_VARIANT_LITE_HPP #define variant_lite_MAJOR 1 #define variant_lite_MINOR 1 #define variant_lite_PATCH 0 #define variant_lite_VERSION variant_STRINGIFY(variant_lite_MAJOR) "." variant_STRINGIFY(variant_lite_MINOR) "." variant_STRINGIFY(variant_lite_PATCH) #define variant_STRINGIFY( x ) variant_STRINGIFY_( x ) #define variant_STRINGIFY_( x ) #x // variant-lite configuration: #define variant_VARIANT_DEFAULT 0 #define variant_VARIANT_NONSTD 1 #define variant_VARIANT_STD 2 #if !defined( variant_CONFIG_SELECT_VARIANT ) # define variant_CONFIG_SELECT_VARIANT ( variant_HAVE_STD_VARIANT ? variant_VARIANT_STD : variant_VARIANT_NONSTD ) #endif #ifndef variant_CONFIG_OMIT_VARIANT_SIZE_V_MACRO # define variant_CONFIG_OMIT_VARIANT_SIZE_V_MACRO 0 #endif #ifndef variant_CONFIG_OMIT_VARIANT_ALTERNATIVE_T_MACRO # define variant_CONFIG_OMIT_VARIANT_ALTERNATIVE_T_MACRO 0 #endif // Control presence of exception handling (try and auto discover): #ifndef variant_CONFIG_NO_EXCEPTIONS # if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND) # define variant_CONFIG_NO_EXCEPTIONS 0 # else # define variant_CONFIG_NO_EXCEPTIONS 1 # endif #endif // C++ language version detection (C++20 is speculative): // Note: VC14.0/1900 (VS2015) lacks too much from C++14. #ifndef variant_CPLUSPLUS # if defined(_MSVC_LANG ) && !defined(__clang__) # define variant_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG ) # else # define variant_CPLUSPLUS __cplusplus # endif #endif #define variant_CPP98_OR_GREATER ( variant_CPLUSPLUS >= 199711L ) #define variant_CPP11_OR_GREATER ( variant_CPLUSPLUS >= 201103L ) #define variant_CPP11_OR_GREATER_ ( variant_CPLUSPLUS >= 201103L ) #define variant_CPP14_OR_GREATER ( variant_CPLUSPLUS >= 201402L ) #define variant_CPP17_OR_GREATER ( variant_CPLUSPLUS >= 201703L ) #define variant_CPP20_OR_GREATER ( variant_CPLUSPLUS >= 202000L ) // Use C++17 std::variant if available and requested: #if variant_CPP17_OR_GREATER && defined(__has_include ) # if __has_include( ) # define variant_HAVE_STD_VARIANT 1 # else # define variant_HAVE_STD_VARIANT 0 # endif #else # define variant_HAVE_STD_VARIANT 0 #endif #define variant_USES_STD_VARIANT ( (variant_CONFIG_SELECT_VARIANT == variant_VARIANT_STD) || ((variant_CONFIG_SELECT_VARIANT == variant_VARIANT_DEFAULT) && variant_HAVE_STD_VARIANT) ) // // in_place: code duplicated in any-lite, expected-lite, optional-lite, value-ptr-lite, variant-lite: // #ifndef nonstd_lite_HAVE_IN_PLACE_TYPES #define nonstd_lite_HAVE_IN_PLACE_TYPES 1 // C++17 std::in_place in : #if variant_CPP17_OR_GREATER #include namespace nonstd { using std::in_place; using std::in_place_type; using std::in_place_index; using std::in_place_t; using std::in_place_type_t; using std::in_place_index_t; #define nonstd_lite_in_place_t( T) std::in_place_t #define nonstd_lite_in_place_type_t( T) std::in_place_type_t #define nonstd_lite_in_place_index_t(K) std::in_place_index_t #define nonstd_lite_in_place( T) std::in_place_t{} #define nonstd_lite_in_place_type( T) std::in_place_type_t{} #define nonstd_lite_in_place_index(K) std::in_place_index_t{} } // namespace nonstd #else // variant_CPP17_OR_GREATER #include namespace nonstd { namespace detail { template< class T > struct in_place_type_tag {}; template< std::size_t K > struct in_place_index_tag {}; } // namespace detail struct in_place_t {}; template< class T > inline in_place_t in_place( detail::in_place_type_tag = detail::in_place_type_tag() ) { return in_place_t(); } template< std::size_t K > inline in_place_t in_place( detail::in_place_index_tag = detail::in_place_index_tag() ) { return in_place_t(); } template< class T > inline in_place_t in_place_type( detail::in_place_type_tag = detail::in_place_type_tag() ) { return in_place_t(); } template< std::size_t K > inline in_place_t in_place_index( detail::in_place_index_tag = detail::in_place_index_tag() ) { return in_place_t(); } // mimic templated typedef: #define nonstd_lite_in_place_t( T) nonstd::in_place_t(&)( nonstd::detail::in_place_type_tag ) #define nonstd_lite_in_place_type_t( T) nonstd::in_place_t(&)( nonstd::detail::in_place_type_tag ) #define nonstd_lite_in_place_index_t(K) nonstd::in_place_t(&)( nonstd::detail::in_place_index_tag ) #define nonstd_lite_in_place( T) nonstd::in_place_type #define nonstd_lite_in_place_type( T) nonstd::in_place_type #define nonstd_lite_in_place_index(K) nonstd::in_place_index } // namespace nonstd #endif // variant_CPP17_OR_GREATER #endif // nonstd_lite_HAVE_IN_PLACE_TYPES // // Use C++17 std::variant: // #if variant_USES_STD_VARIANT #include // std::hash<> #include #if ! variant_CONFIG_OMIT_VARIANT_SIZE_V_MACRO # define variant_size_V(T) nonstd::variant_size::value #endif #if ! variant_CONFIG_OMIT_VARIANT_ALTERNATIVE_T_MACRO # define variant_alternative_T(K,T) typename nonstd::variant_alternative::type #endif namespace nonstd { using std::variant; using std::monostate; using std::bad_variant_access; using std::variant_size; using std::variant_size_v; using std::variant_alternative; using std::variant_alternative_t; using std::hash; using std::visit; using std::holds_alternative; using std::get; using std::get_if; using std::operator==; using std::operator!=; using std::operator<; using std::operator<=; using std::operator>; using std::operator>=; using std::swap; constexpr auto variant_npos = std::variant_npos; } #else // variant_USES_STD_VARIANT #include #include #include #include #if variant_CONFIG_NO_EXCEPTIONS # include #else # include #endif // variant-lite alignment configuration: #ifndef variant_CONFIG_MAX_ALIGN_HACK # define variant_CONFIG_MAX_ALIGN_HACK 0 #endif #ifndef variant_CONFIG_ALIGN_AS // no default, used in #if defined() #endif #ifndef variant_CONFIG_ALIGN_AS_FALLBACK # define variant_CONFIG_ALIGN_AS_FALLBACK double #endif // half-open range [lo..hi): #define variant_BETWEEN( v, lo, hi ) ( (lo) <= (v) && (v) < (hi) ) // Compiler versions: // // MSVC++ 6.0 _MSC_VER == 1200 (Visual Studio 6.0) // MSVC++ 7.0 _MSC_VER == 1300 (Visual Studio .NET 2002) // MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio .NET 2003) // MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005) // MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008) // MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010) // MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012) // MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013) // MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015) // MSVC++ 14.1 _MSC_VER >= 1910 (Visual Studio 2017) #if defined(_MSC_VER ) && !defined(__clang__) # define variant_COMPILER_MSVC_VER (_MSC_VER ) # define variant_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) ) #else # define variant_COMPILER_MSVC_VER 0 # define variant_COMPILER_MSVC_VERSION 0 #endif #define variant_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) ) #if defined(__clang__) # define variant_COMPILER_CLANG_VERSION variant_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__) #else # define variant_COMPILER_CLANG_VERSION 0 #endif #if defined(__GNUC__) && !defined(__clang__) # define variant_COMPILER_GNUC_VERSION variant_COMPILER_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) #else # define variant_COMPILER_GNUC_VERSION 0 #endif #if variant_BETWEEN( variant_COMPILER_MSVC_VER, 1300, 1900 ) # pragma warning( push ) # pragma warning( disable: 4345 ) // initialization behavior changed #endif // Presence of language and library features: #define variant_HAVE( feature ) ( variant_HAVE_##feature ) #ifdef _HAS_CPP0X # define variant_HAS_CPP0X _HAS_CPP0X #else # define variant_HAS_CPP0X 0 #endif // Unless defined otherwise below, consider VC14 as C++11 for variant-lite: #if variant_COMPILER_MSVC_VER >= 1900 # undef variant_CPP11_OR_GREATER # define variant_CPP11_OR_GREATER 1 #endif #define variant_CPP11_90 (variant_CPP11_OR_GREATER_ || variant_COMPILER_MSVC_VER >= 1500) #define variant_CPP11_100 (variant_CPP11_OR_GREATER_ || variant_COMPILER_MSVC_VER >= 1600) #define variant_CPP11_110 (variant_CPP11_OR_GREATER_ || variant_COMPILER_MSVC_VER >= 1700) #define variant_CPP11_120 (variant_CPP11_OR_GREATER_ || variant_COMPILER_MSVC_VER >= 1800) #define variant_CPP11_140 (variant_CPP11_OR_GREATER_ || variant_COMPILER_MSVC_VER >= 1900) #define variant_CPP11_141 (variant_CPP11_OR_GREATER_ || variant_COMPILER_MSVC_VER >= 1910) #define variant_CPP14_000 (variant_CPP14_OR_GREATER) #define variant_CPP17_000 (variant_CPP17_OR_GREATER) // Presence of C++11 language features: #define variant_HAVE_CONSTEXPR_11 variant_CPP11_140 #define variant_HAVE_INITIALIZER_LIST variant_CPP11_120 #define variant_HAVE_NOEXCEPT variant_CPP11_140 #define variant_HAVE_NULLPTR variant_CPP11_100 #define variant_HAVE_OVERRIDE variant_CPP11_140 // Presence of C++14 language features: #define variant_HAVE_CONSTEXPR_14 variant_CPP14_000 // Presence of C++17 language features: // no flag // Presence of C++ library features: #define variant_HAVE_CONDITIONAL variant_CPP11_120 #define variant_HAVE_REMOVE_CV variant_CPP11_120 #define variant_HAVE_STD_ADD_POINTER variant_CPP11_90 #define variant_HAVE_TYPE_TRAITS variant_CPP11_90 #define variant_HAVE_TR1_TYPE_TRAITS (!! variant_COMPILER_GNUC_VERSION ) #define variant_HAVE_TR1_ADD_POINTER (!! variant_COMPILER_GNUC_VERSION ) // C++ feature usage: #if variant_HAVE_CONSTEXPR_11 # define variant_constexpr constexpr #else # define variant_constexpr /*constexpr*/ #endif #if variant_HAVE_CONSTEXPR_14 # define variant_constexpr14 constexpr #else # define variant_constexpr14 /*constexpr*/ #endif #if variant_HAVE_NOEXCEPT # define variant_noexcept noexcept #else # define variant_noexcept /*noexcept*/ #endif #if variant_HAVE_NULLPTR # define variant_nullptr nullptr #else # define variant_nullptr NULL #endif #if variant_HAVE_OVERRIDE # define variant_override override #else # define variant_override /*override*/ #endif // additional includes: #if variant_CPP11_OR_GREATER # include // std::hash #endif #if variant_HAVE_INITIALIZER_LIST # include #endif #if variant_HAVE_TYPE_TRAITS # include #elif variant_HAVE_TR1_TYPE_TRAITS # include #endif // Method enabling #if variant_CPP11_OR_GREATER # define variant_REQUIRES_T(...) \ , typename = typename std::enable_if<__VA_ARGS__>::type # define variant_REQUIRES_R(R, ...) \ typename std::enable_if<__VA_ARGS__, R>::type # define variant_REQUIRES_A(...) \ , typename std::enable_if<__VA_ARGS__, void*>::type = variant_nullptr #endif // // variant: // namespace nonstd { namespace variants { namespace detail { // C++11 emulation: #if variant_HAVE_STD_ADD_POINTER using std::add_pointer; #elif variant_HAVE_TR1_ADD_POINTER using std::tr1::add_pointer; #else template< class T > struct remove_reference { typedef T type; }; template< class T > struct remove_reference { typedef T type; }; template< class T > struct add_pointer { typedef typename remove_reference::type * type; }; #endif // variant_HAVE_STD_ADD_POINTER #if variant_HAVE_REMOVE_CV using std::remove_cv; #else template< class T > struct remove_const { typedef T type; }; template< class T > struct remove_const { typedef T type; }; template< class T > struct remove_volatile { typedef T type; }; template< class T > struct remove_volatile { typedef T type; }; template< class T > struct remove_cv { typedef typename remove_volatile::type>::type type; }; #endif // variant_HAVE_REMOVE_CV #if variant_HAVE_CONDITIONAL using std::conditional; #else template< bool Cond, class Then, class Else > struct conditional; template< class Then, class Else > struct conditional< true , Then, Else > { typedef Then type; }; template< class Then, class Else > struct conditional< false, Then, Else > { typedef Else type; }; #endif // variant_HAVE_CONDITIONAL // typelist: #define variant_TL1( T1 ) detail::typelist< T1, detail::nulltype > #define variant_TL2( T1, T2) detail::typelist< T1, variant_TL1( T2) > #define variant_TL3( T1, T2, T3) detail::typelist< T1, variant_TL2( T2, T3) > #define variant_TL4( T1, T2, T3, T4) detail::typelist< T1, variant_TL3( T2, T3, T4) > #define variant_TL5( T1, T2, T3, T4, T5) detail::typelist< T1, variant_TL4( T2, T3, T4, T5) > #define variant_TL6( T1, T2, T3, T4, T5, T6) detail::typelist< T1, variant_TL5( T2, T3, T4, T5, T6) > #define variant_TL7( T1, T2, T3, T4, T5, T6, T7) detail::typelist< T1, variant_TL6( T2, T3, T4, T5, T6, T7) > #define variant_TL8( T1, T2, T3, T4, T5, T6, T7, T8) detail::typelist< T1, variant_TL7( T2, T3, T4, T5, T6, T7, T8) > #define variant_TL9( T1, T2, T3, T4, T5, T6, T7, T8, T9) detail::typelist< T1, variant_TL8( T2, T3, T4, T5, T6, T7, T8, T9) > #define variant_TL10( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) detail::typelist< T1, variant_TL9( T2, T3, T4, T5, T6, T7, T8, T9, T10) > #define variant_TL11( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) detail::typelist< T1, variant_TL10( T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) > #define variant_TL12( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) detail::typelist< T1, variant_TL11( T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) > #define variant_TL13( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) detail::typelist< T1, variant_TL12( T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) > #define variant_TL14( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) detail::typelist< T1, variant_TL13( T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) > #define variant_TL15( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) detail::typelist< T1, variant_TL14( T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) > #define variant_TL16( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) detail::typelist< T1, variant_TL15( T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) > // variant parameter unused type tags: template< class T > struct TX : T { inline TX operator+ ( ) const { return TX(); } inline TX operator- ( ) const { return TX(); } inline TX operator! ( ) const { return TX(); } inline TX operator~ ( ) const { return TX(); } inline TX*operator& ( ) const { return variant_nullptr; } template< class U > inline TX operator* ( U const & ) const { return TX(); } template< class U > inline TX operator/ ( U const & ) const { return TX(); } template< class U > inline TX operator% ( U const & ) const { return TX(); } template< class U > inline TX operator+ ( U const & ) const { return TX(); } template< class U > inline TX operator- ( U const & ) const { return TX(); } template< class U > inline TX operator<<( U const & ) const { return TX(); } template< class U > inline TX operator>>( U const & ) const { return TX(); } inline bool operator==( T const & ) const { return false; } inline bool operator< ( T const & ) const { return false; } template< class U > inline TX operator& ( U const & ) const { return TX(); } template< class U > inline TX operator| ( U const & ) const { return TX(); } template< class U > inline TX operator^ ( U const & ) const { return TX(); } template< class U > inline TX operator&&( U const & ) const { return TX(); } template< class U > inline TX operator||( U const & ) const { return TX(); } }; struct S0{}; typedef TX T0; struct S1{}; typedef TX T1; struct S2{}; typedef TX T2; struct S3{}; typedef TX T3; struct S4{}; typedef TX T4; struct S5{}; typedef TX T5; struct S6{}; typedef TX T6; struct S7{}; typedef TX T7; struct S8{}; typedef TX T8; struct S9{}; typedef TX T9; struct S10{}; typedef TX T10; struct S11{}; typedef TX T11; struct S12{}; typedef TX T12; struct S13{}; typedef TX T13; struct S14{}; typedef TX T14; struct S15{}; typedef TX T15; struct nulltype{}; template< class Head, class Tail > struct typelist { typedef Head head; typedef Tail tail; }; // typelist max element size: template< class List > struct typelist_max; template<> struct typelist_max< nulltype > { enum V { value = 0 } ; typedef void type; }; template< class Head, class Tail > struct typelist_max< typelist > { private: enum TV { tail_value = size_t( typelist_max::value ) }; typedef typename typelist_max::type tail_type; public: enum V { value = (sizeof( Head ) > tail_value) ? sizeof( Head ) : std::size_t( tail_value ) } ; typedef typename conditional< (sizeof( Head ) > tail_value), Head, tail_type>::type type; }; #if variant_CPP11_OR_GREATER // typelist max alignof element type: template< class List > struct typelist_max_alignof; template<> struct typelist_max_alignof< nulltype > { enum V { value = 0 } ; }; template< class Head, class Tail > struct typelist_max_alignof< typelist > { private: enum TV { tail_value = size_t( typelist_max_alignof::value ) }; public: enum V { value = (alignof( Head ) > tail_value) ? alignof( Head ) : std::size_t( tail_value ) }; }; #endif // typelist size (length): template< class List > struct typelist_size { enum V { value = 1 }; }; template<> struct typelist_size< T0 > { enum V { value = 0 }; }; template<> struct typelist_size< T1 > { enum V { value = 0 }; }; template<> struct typelist_size< T2 > { enum V { value = 0 }; }; template<> struct typelist_size< T3 > { enum V { value = 0 }; }; template<> struct typelist_size< T4 > { enum V { value = 0 }; }; template<> struct typelist_size< T5 > { enum V { value = 0 }; }; template<> struct typelist_size< T6 > { enum V { value = 0 }; }; template<> struct typelist_size< T7 > { enum V { value = 0 }; }; template<> struct typelist_size< T8 > { enum V { value = 0 }; }; template<> struct typelist_size< T9 > { enum V { value = 0 }; }; template<> struct typelist_size< T10 > { enum V { value = 0 }; }; template<> struct typelist_size< T11 > { enum V { value = 0 }; }; template<> struct typelist_size< T12 > { enum V { value = 0 }; }; template<> struct typelist_size< T13 > { enum V { value = 0 }; }; template<> struct typelist_size< T14 > { enum V { value = 0 }; }; template<> struct typelist_size< T15 > { enum V { value = 0 }; }; template<> struct typelist_size< nulltype > { enum V { value = 0 } ; }; template< class Head, class Tail > struct typelist_size< typelist > { enum V { value = typelist_size::value + typelist_size::value }; }; // typelist index of type: template< class List, class T > struct typelist_index_of; template< class T > struct typelist_index_of< nulltype, T > { enum V { value = -1 }; }; template< class Tail, class T > struct typelist_index_of< typelist, T > { enum V { value = 0 }; }; template< class Head, class Tail, class T > struct typelist_index_of< typelist, T > { private: enum TV { nextVal = typelist_index_of::value }; public: enum V { value = nextVal == -1 ? -1 : 1 + nextVal } ; }; // typelist type at index: template< class List, std::size_t i> struct typelist_type_at; template< class Head, class Tail > struct typelist_type_at< typelist, 0 > { typedef Head type; }; template< class Head, class Tail, std::size_t i > struct typelist_type_at< typelist, i > { typedef typename typelist_type_at::type type; }; #if variant_CONFIG_MAX_ALIGN_HACK // Max align, use most restricted type for alignment: #define variant_UNIQUE( name ) variant_UNIQUE2( name, __LINE__ ) #define variant_UNIQUE2( name, line ) variant_UNIQUE3( name, line ) #define variant_UNIQUE3( name, line ) name ## line #define variant_ALIGN_TYPE( type ) \ type variant_UNIQUE( _t ); struct_t< type > variant_UNIQUE( _st ) template< class T > struct struct_t { T _; }; union max_align_t { variant_ALIGN_TYPE( char ); variant_ALIGN_TYPE( short int ); variant_ALIGN_TYPE( int ); variant_ALIGN_TYPE( long int ); variant_ALIGN_TYPE( float ); variant_ALIGN_TYPE( double ); variant_ALIGN_TYPE( long double ); variant_ALIGN_TYPE( char * ); variant_ALIGN_TYPE( short int * ); variant_ALIGN_TYPE( int * ); variant_ALIGN_TYPE( long int * ); variant_ALIGN_TYPE( float * ); variant_ALIGN_TYPE( double * ); variant_ALIGN_TYPE( long double * ); variant_ALIGN_TYPE( void * ); #ifdef HAVE_LONG_LONG variant_ALIGN_TYPE( long long ); #endif struct Unknown; Unknown ( * variant_UNIQUE(_) )( Unknown ); Unknown * Unknown::* variant_UNIQUE(_); Unknown ( Unknown::* variant_UNIQUE(_) )( Unknown ); struct_t< Unknown ( * )( Unknown) > variant_UNIQUE(_); struct_t< Unknown * Unknown::* > variant_UNIQUE(_); struct_t< Unknown ( Unknown::* )(Unknown) > variant_UNIQUE(_); }; #undef variant_UNIQUE #undef variant_UNIQUE2 #undef variant_UNIQUE3 #undef variant_ALIGN_TYPE #elif defined( variant_CONFIG_ALIGN_AS ) // variant_CONFIG_MAX_ALIGN_HACK // Use user-specified type for alignment: #define variant_ALIGN_AS( unused ) \ variant_CONFIG_ALIGN_AS #else // variant_CONFIG_MAX_ALIGN_HACK // Determine POD type to use for alignment: #define variant_ALIGN_AS( to_align ) \ typename detail::type_of_size< detail::alignment_types, detail::alignment_of< to_align >::value >::type template< typename T > struct alignment_of; template< typename T > struct alignment_of_hack { char c; T t; alignment_of_hack(); }; template< size_t A, size_t S > struct alignment_logic { enum V { value = A < S ? A : S }; }; template< typename T > struct alignment_of { enum V { value = alignment_logic< sizeof( alignment_of_hack ) - sizeof(T), sizeof(T) >::value }; }; template< typename List, size_t N > struct type_of_size { typedef typename conditional< N == sizeof( typename List::head ), typename List::head, typename type_of_size::type >::type type; }; template< size_t N > struct type_of_size< nulltype, N > { typedef variant_CONFIG_ALIGN_AS_FALLBACK type; }; template< typename T> struct struct_t { T _; }; #define variant_ALIGN_TYPE( type ) \ typelist< type , typelist< struct_t< type > struct Unknown; typedef variant_ALIGN_TYPE( char ), variant_ALIGN_TYPE( short ), variant_ALIGN_TYPE( int ), variant_ALIGN_TYPE( long ), variant_ALIGN_TYPE( float ), variant_ALIGN_TYPE( double ), variant_ALIGN_TYPE( long double ), variant_ALIGN_TYPE( char *), variant_ALIGN_TYPE( short * ), variant_ALIGN_TYPE( int * ), variant_ALIGN_TYPE( long * ), variant_ALIGN_TYPE( float * ), variant_ALIGN_TYPE( double * ), variant_ALIGN_TYPE( long double * ), variant_ALIGN_TYPE( Unknown ( * )( Unknown ) ), variant_ALIGN_TYPE( Unknown * Unknown::* ), variant_ALIGN_TYPE( Unknown ( Unknown::* )( Unknown ) ), nulltype > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > alignment_types; #undef variant_ALIGN_TYPE #endif // variant_CONFIG_MAX_ALIGN_HACK template< typename T> inline std::size_t hash( T const & v ) { // primes: unsigned const int a = 54059; unsigned const int b = 76963; unsigned const int h0 = 37; unsigned int h = h0; unsigned char const * s = reinterpret_cast( &v ); for ( std::size_t i = 0; i < sizeof(v); ++i, ++s ) { h = (h * a) ^ (*s * b); } return h; } template< class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > struct helper { typedef signed char type_index_t; typedef variant_TL16( T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 ) variant_types; template< class U > static U * as( void * data ) { return reinterpret_cast( data ); } template< class U > static U const * as( void const * data ) { return reinterpret_cast( data ); } static type_index_t to_index_t( std::size_t index ) { return static_cast( index ); } static void destroy( type_index_t index, void * data ) { switch ( index ) { case 0: as( data )->~T0(); break; case 1: as( data )->~T1(); break; case 2: as( data )->~T2(); break; case 3: as( data )->~T3(); break; case 4: as( data )->~T4(); break; case 5: as( data )->~T5(); break; case 6: as( data )->~T6(); break; case 7: as( data )->~T7(); break; case 8: as( data )->~T8(); break; case 9: as( data )->~T9(); break; case 10: as( data )->~T10(); break; case 11: as( data )->~T11(); break; case 12: as( data )->~T12(); break; case 13: as( data )->~T13(); break; case 14: as( data )->~T14(); break; case 15: as( data )->~T15(); break; } } #if variant_CPP11_OR_GREATER template< class T, class... Args > static type_index_t construct_t( void * data, Args&&... args ) { new( data ) T( std::forward(args)... ); return to_index_t( detail::typelist_index_of< variant_types, T>::value ); } template< std::size_t K, class... Args > static type_index_t construct_i( void * data, Args&&... args ) { using type = typename detail::typelist_type_at< variant_types, K >::type; construct_t< type >( data, std::forward(args)... ); return to_index_t( K ); } static type_index_t move( type_index_t const from_index, void * from_value, void * to_value ) { switch ( from_index ) { case 0: new( to_value ) T0( std::forward( *as( from_value ) ) ); break; case 1: new( to_value ) T1( std::forward( *as( from_value ) ) ); break; case 2: new( to_value ) T2( std::forward( *as( from_value ) ) ); break; case 3: new( to_value ) T3( std::forward( *as( from_value ) ) ); break; case 4: new( to_value ) T4( std::forward( *as( from_value ) ) ); break; case 5: new( to_value ) T5( std::forward( *as( from_value ) ) ); break; case 6: new( to_value ) T6( std::forward( *as( from_value ) ) ); break; case 7: new( to_value ) T7( std::forward( *as( from_value ) ) ); break; case 8: new( to_value ) T8( std::forward( *as( from_value ) ) ); break; case 9: new( to_value ) T9( std::forward( *as( from_value ) ) ); break; case 10: new( to_value ) T10( std::forward( *as( from_value ) ) ); break; case 11: new( to_value ) T11( std::forward( *as( from_value ) ) ); break; case 12: new( to_value ) T12( std::forward( *as( from_value ) ) ); break; case 13: new( to_value ) T13( std::forward( *as( from_value ) ) ); break; case 14: new( to_value ) T14( std::forward( *as( from_value ) ) ); break; case 15: new( to_value ) T15( std::forward( *as( from_value ) ) ); break; } return from_index; } #endif static type_index_t copy( type_index_t const from_index, const void * from_value, void * to_value ) { switch ( from_index ) { case 0: new( to_value ) T0( *as( from_value ) ); break; case 1: new( to_value ) T1( *as( from_value ) ); break; case 2: new( to_value ) T2( *as( from_value ) ); break; case 3: new( to_value ) T3( *as( from_value ) ); break; case 4: new( to_value ) T4( *as( from_value ) ); break; case 5: new( to_value ) T5( *as( from_value ) ); break; case 6: new( to_value ) T6( *as( from_value ) ); break; case 7: new( to_value ) T7( *as( from_value ) ); break; case 8: new( to_value ) T8( *as( from_value ) ); break; case 9: new( to_value ) T9( *as( from_value ) ); break; case 10: new( to_value ) T10( *as( from_value ) ); break; case 11: new( to_value ) T11( *as( from_value ) ); break; case 12: new( to_value ) T12( *as( from_value ) ); break; case 13: new( to_value ) T13( *as( from_value ) ); break; case 14: new( to_value ) T14( *as( from_value ) ); break; case 15: new( to_value ) T15( *as( from_value ) ); break; } return from_index; } }; } // namespace detail // // Variant: // template< class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > class variant; class monostate{}; inline variant_constexpr bool operator< ( monostate, monostate ) variant_noexcept { return false; } inline variant_constexpr bool operator> ( monostate, monostate ) variant_noexcept { return false; } inline variant_constexpr bool operator<=( monostate, monostate ) variant_noexcept { return true; } inline variant_constexpr bool operator>=( monostate, monostate ) variant_noexcept { return true; } inline variant_constexpr bool operator==( monostate, monostate ) variant_noexcept { return true; } inline variant_constexpr bool operator!=( monostate, monostate ) variant_noexcept { return false; } // obtain the size of the variant's list of alternatives at compile time template< class T > struct variant_size; /* undefined */ template< class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > struct variant_size< variant > { enum _ { value = detail::typelist_size< variant_TL16(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) >::value }; }; #if variant_CPP14_OR_GREATER template< class T > constexpr std::size_t variant_size_v = variant_size::value; #endif #if ! variant_CONFIG_OMIT_VARIANT_SIZE_V_MACRO # define variant_size_V(T) nonstd::variant_size::value #endif // obtain the type of the alternative specified by its index, at compile time: template< std::size_t K, class T > struct variant_alternative; /* undefined */ template< std::size_t K, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > struct variant_alternative< K, variant > { typedef typename detail::typelist_type_at::type type; }; #if variant_CPP11_OR_GREATER template< std::size_t K, class T > using variant_alternative_t = typename variant_alternative::type; #endif #if ! variant_CONFIG_OMIT_VARIANT_ALTERNATIVE_T_MACRO # define variant_alternative_T(K,T) typename nonstd::variant_alternative::type #endif // NTS:implement specializes the std::uses_allocator type trait // std::uses_allocator // index of the variant in the invalid state (constant) #if variant_CPP11_OR_GREATER variant_constexpr std::size_t variant_npos = static_cast( -1 ); #else static const std::size_t variant_npos = static_cast( -1 ); #endif class bad_variant_access : public std::exception { public: #if variant_CPP11_OR_GREATER virtual const char* what() const variant_noexcept variant_override #else virtual const char* what() const throw() #endif { return "bad variant access"; } }; template< class T0, class T1 = detail::T1, class T2 = detail::T2, class T3 = detail::T3, class T4 = detail::T4, class T5 = detail::T5, class T6 = detail::T6, class T7 = detail::T7, class T8 = detail::T8, class T9 = detail::T9, class T10 = detail::T10, class T11 = detail::T11, class T12 = detail::T12, class T13 = detail::T13, class T14 = detail::T14, class T15 = detail::T15 > class variant { typedef detail::helper< T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 > helper_type; typedef variant_TL16( T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 ) variant_types; public: variant() : type_index( 0 ) { new( ptr() ) T0(); } variant( T0 const & t0 ) : type_index( 0 ) { new( ptr() ) T0( t0 ); } variant( T1 const & t1 ) : type_index( 1 ) { new( ptr() ) T1( t1 ); } variant( T2 const & t2 ) : type_index( 2 ) { new( ptr() ) T2( t2 ); } variant( T3 const & t3 ) : type_index( 3 ) { new( ptr() ) T3( t3 ); } variant( T4 const & t4 ) : type_index( 4 ) { new( ptr() ) T4( t4 ); } variant( T5 const & t5 ) : type_index( 5 ) { new( ptr() ) T5( t5 ); } variant( T6 const & t6 ) : type_index( 6 ) { new( ptr() ) T6( t6 ); } variant( T7 const & t7 ) : type_index( 7 ) { new( ptr() ) T7( t7 ); } variant( T8 const & t8 ) : type_index( 8 ) { new( ptr() ) T8( t8 ); } variant( T9 const & t9 ) : type_index( 9 ) { new( ptr() ) T9( t9 ); } variant( T10 const & t10 ) : type_index( 10 ) { new( ptr() ) T10( t10 ); } variant( T11 const & t11 ) : type_index( 11 ) { new( ptr() ) T11( t11 ); } variant( T12 const & t12 ) : type_index( 12 ) { new( ptr() ) T12( t12 ); } variant( T13 const & t13 ) : type_index( 13 ) { new( ptr() ) T13( t13 ); } variant( T14 const & t14 ) : type_index( 14 ) { new( ptr() ) T14( t14 ); } variant( T15 const & t15 ) : type_index( 15 ) { new( ptr() ) T15( t15 ); } #if variant_CPP11_OR_GREATER variant( T0 && t0 ) : type_index( 0 ) { new( ptr() ) T0( std::move(t0) ); } variant( T1 && t1 ) : type_index( 1 ) { new( ptr() ) T1( std::move(t1) ); } variant( T2 && t2 ) : type_index( 2 ) { new( ptr() ) T2( std::move(t2) ); } variant( T3 && t3 ) : type_index( 3 ) { new( ptr() ) T3( std::move(t3) ); } variant( T4 && t4 ) : type_index( 4 ) { new( ptr() ) T4( std::move(t4) ); } variant( T5 && t5 ) : type_index( 5 ) { new( ptr() ) T5( std::move(t5) ); } variant( T6 && t6 ) : type_index( 6 ) { new( ptr() ) T6( std::move(t6) ); } variant( T7 && t7 ) : type_index( 7 ) { new( ptr() ) T7( std::move(t7) ); } variant( T8 && t8 ) : type_index( 8 ) { new( ptr() ) T8( std::move(t8) ); } variant( T9 && t9 ) : type_index( 9 ) { new( ptr() ) T9( std::move(t9) ); } variant( T10 && t10 ) : type_index( 10 ) { new( ptr() ) T10( std::move(t10) ); } variant( T11 && t11 ) : type_index( 11 ) { new( ptr() ) T11( std::move(t11) ); } variant( T12 && t12 ) : type_index( 12 ) { new( ptr() ) T12( std::move(t12) ); } variant( T13 && t13 ) : type_index( 13 ) { new( ptr() ) T13( std::move(t13) ); } variant( T14 && t14 ) : type_index( 14 ) { new( ptr() ) T14( std::move(t14) ); } variant( T15 && t15 ) : type_index( 15 ) { new( ptr() ) T15( std::move(t15) ); } #endif variant(variant const & other) : type_index( other.type_index ) { (void) helper_type::copy( other.type_index, other.ptr(), ptr() ); } #if variant_CPP11_OR_GREATER variant( variant && other ) noexcept( std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value) : type_index( other.type_index ) { (void) helper_type::move( other.type_index, other.ptr(), ptr() ); } template< std::size_t K > using type_at_t = typename detail::typelist_type_at< variant_types, K >::type; template< class T, class... Args variant_REQUIRES_T( std::is_constructible< T, Args...>::value ) > explicit variant( nonstd_lite_in_place_type_t(T), Args&&... args) { type_index = variant_npos_internal(); type_index = helper_type::template construct_t( ptr(), std::forward(args)... ); } template< class T, class U, class... Args variant_REQUIRES_T( std::is_constructible< T, std::initializer_list&, Args...>::value ) > explicit variant( nonstd_lite_in_place_type_t(T), std::initializer_list il, Args&&... args ) { type_index = variant_npos_internal(); type_index = helper_type::template construct_t( ptr(), il, std::forward(args)... ); } template< std::size_t K, class... Args variant_REQUIRES_T( std::is_constructible< type_at_t, Args...>::value ) > explicit variant( nonstd_lite_in_place_index_t(K), Args&&... args ) { type_index = variant_npos_internal(); type_index = helper_type::template construct_i( ptr(), std::forward(args)... ); } template< size_t K, class U, class... Args variant_REQUIRES_T( std::is_constructible< type_at_t, std::initializer_list&, Args...>::value ) > explicit variant( nonstd_lite_in_place_index_t(K), std::initializer_list il, Args&&... args ) { type_index = variant_npos_internal(); type_index = helper_type::template construct_i( ptr(), il, std::forward(args)... ); } #endif // variant_CPP11_OR_GREATER ~variant() { helper_type::destroy( type_index, ptr() ); } variant & operator=( variant const & other ) { return copy_assign( other ); } #if variant_CPP11_OR_GREATER variant & operator=( variant && other ) noexcept( std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value && std::is_nothrow_move_assignable::value) { return move_assign( std::forward( other ) ); } variant & operator=( T0 && t0 ) { return move_assign_value( std::forward( t0 ) ); } variant & operator=( T1 && t1 ) { return move_assign_value( std::forward( t1 ) ); } variant & operator=( T2 && t2 ) { return move_assign_value( std::forward( t2 ) ); } variant & operator=( T3 && t3 ) { return move_assign_value( std::forward( t3 ) ); } variant & operator=( T4 && t4 ) { return move_assign_value( std::forward( t4 ) ); } variant & operator=( T5 && t5 ) { return move_assign_value( std::forward( t5 ) ); } variant & operator=( T6 && t6 ) { return move_assign_value( std::forward( t6 ) ); } variant & operator=( T7 && t7 ) { return move_assign_value( std::forward( t7 ) ); } variant & operator=( T8 && t8 ) { return move_assign_value( std::forward( t8 ) ); } variant & operator=( T9 && t9 ) { return move_assign_value( std::forward( t9 ) ); } variant & operator=( T10 && t10 ) { return move_assign_value( std::forward( t10 ) ); } variant & operator=( T11 && t11 ) { return move_assign_value( std::forward( t11 ) ); } variant & operator=( T12 && t12 ) { return move_assign_value( std::forward( t12 ) ); } variant & operator=( T13 && t13 ) { return move_assign_value( std::forward( t13 ) ); } variant & operator=( T14 && t14 ) { return move_assign_value( std::forward( t14 ) ); } variant & operator=( T15 && t15 ) { return move_assign_value( std::forward( t15 ) ); } #else variant & operator=( T0 const & t0 ) { return copy_assign_value( t0 ); } variant & operator=( T1 const & t1 ) { return copy_assign_value( t1 ); } variant & operator=( T2 const & t2 ) { return copy_assign_value( t2 ); } variant & operator=( T3 const & t3 ) { return copy_assign_value( t3 ); } variant & operator=( T4 const & t4 ) { return copy_assign_value( t4 ); } variant & operator=( T5 const & t5 ) { return copy_assign_value( t5 ); } variant & operator=( T6 const & t6 ) { return copy_assign_value( t6 ); } variant & operator=( T7 const & t7 ) { return copy_assign_value( t7 ); } variant & operator=( T8 const & t8 ) { return copy_assign_value( t8 ); } variant & operator=( T9 const & t9 ) { return copy_assign_value( t9 ); } variant & operator=( T10 const & t10 ) { return copy_assign_value( t10 ); } variant & operator=( T11 const & t11 ) { return copy_assign_value( t11 ); } variant & operator=( T12 const & t12 ) { return copy_assign_value( t12 ); } variant & operator=( T13 const & t13 ) { return copy_assign_value( t13 ); } variant & operator=( T14 const & t14 ) { return copy_assign_value( t14 ); } variant & operator=( T15 const & t15 ) { return copy_assign_value( t15 ); } #endif std::size_t index() const { return variant_npos_internal() == type_index ? variant_npos : static_cast( type_index ); } bool valueless_by_exception() const { return type_index == variant_npos_internal(); } #if variant_CPP11_OR_GREATER template< class T, class... Args variant_REQUIRES_T( std::is_constructible< T, Args...>::value ) > T& emplace( Args&&... args ) { helper_type::destroy( type_index, ptr() ); type_index = variant_npos_internal(); type_index = helper_type::template construct_t( ptr(), std::forward(args)... ); return *as(); } template< class T, class U, class... Args variant_REQUIRES_T( std::is_constructible< T, std::initializer_list&, Args...>::value ) > T& emplace( std::initializer_list il, Args&&... args ) { helper_type::destroy( type_index, ptr() ); type_index = variant_npos_internal(); type_index = helper_type::template construct_t( ptr(), il, std::forward(args)... ); return *as(); } template< size_t K, class... Args variant_REQUIRES_T( std::is_constructible< type_at_t, Args...>::value ) > variant_alternative_t & emplace( Args&&... args ) { return this->template emplace< type_at_t >( std::forward(args)... ); } template< size_t K, class U, class... Args variant_REQUIRES_T( std::is_constructible< type_at_t, std::initializer_list&, Args...>::value ) > variant_alternative_t & emplace( std::initializer_list il, Args&&... args ) { return this->template emplace< type_at_t >( il, std::forward(args)... ); } #endif // variant_CPP11_OR_GREATER void swap( variant & other ) variant_noexcept { if ( valueless_by_exception() && other.valueless_by_exception() ) { // no effect } else if ( type_index == other.type_index ) { this->swap_value( type_index, other ); } else { variant tmp( *this ); *this = other; other = tmp; } } // // non-standard: // template< class T > static variant_constexpr std::size_t index_of() variant_noexcept { return to_size_t( detail::typelist_index_of::type >::value ); } template< class T > T & get() { const std::size_t i = index_of(); #if variant_CONFIG_NO_EXCEPTIONS assert( i == index() ); #else if ( i != index() || i == max_index() ) { throw bad_variant_access(); } #endif return *as(); } template< class T > T const & get() const { const std::size_t i = index_of(); #if variant_CONFIG_NO_EXCEPTIONS assert( i == index() ); #else if ( i != index() || i == max_index() ) { throw bad_variant_access(); } #endif return *as(); } template< std::size_t K > typename variant_alternative< K, variant >::type & get() { return this->template get< typename detail::typelist_type_at< variant_types, K >::type >(); } template< std::size_t K > typename variant_alternative< K, variant >::type const & get() const { return this->template get< typename detail::typelist_type_at< variant_types, K >::type >(); } private: typedef typename helper_type::type_index_t type_index_t; void * ptr() variant_noexcept { return &data; } void const * ptr() const variant_noexcept { return &data; } template< class U > U * as() { return reinterpret_cast( ptr() ); } template< class U > U const * as() const { return reinterpret_cast( ptr() ); } template< class U > static variant_constexpr std::size_t to_size_t( U index ) { return static_cast( index ); } variant_constexpr std::size_t max_index() const variant_noexcept { return data_size; } variant_constexpr type_index_t variant_npos_internal() const variant_noexcept { return static_cast( -1 ); } variant & copy_assign( variant const & other ) { if ( valueless_by_exception() && other.valueless_by_exception() ) { // no effect } else if ( ! valueless_by_exception() && other.valueless_by_exception() ) { helper_type::destroy( type_index, ptr() ); type_index = variant_npos_internal(); } else if ( index() == other.index() ) { type_index = helper_type::copy( other.type_index, other.ptr(), ptr() ); } else { // alas exception safety with pre-C++11 needs an extra copy: variant tmp( other ); helper_type::destroy( type_index, ptr() ); type_index = variant_npos_internal(); #if variant_CPP11_OR_GREATER type_index = helper_type::move( other.type_index, tmp.ptr(), ptr() ); #else type_index = helper_type::copy( other.type_index, tmp.ptr(), ptr() ); #endif } return *this; } #if variant_CPP11_OR_GREATER variant & move_assign( variant && other ) { if ( valueless_by_exception() && other.valueless_by_exception() ) { // no effect } else if ( ! valueless_by_exception() && other.valueless_by_exception() ) { helper_type::destroy( type_index, ptr() ); type_index = variant_npos_internal(); } else if ( index() == other.index() ) { type_index = helper_type::move( other.type_index, other.ptr(), ptr() ); } else { helper_type::destroy( type_index, ptr() ); type_index = variant_npos_internal(); type_index = helper_type::move( other.type_index, other.ptr(), ptr() ); } return *this; } template< class T, std::size_t K > variant & move_assign_value( T && value ) { if( index() == K ) { *as() = std::forward( value ); } else { helper_type::destroy( type_index, ptr() ); type_index = variant_npos_internal(); new( ptr() ) T( std::forward( value ) ); type_index = K; } return *this; } #else template< class T, std::size_t K > variant & copy_assign_value( T const & value ) { if( index() == K ) { *as() = value; } else { helper_type::destroy( type_index, ptr() ); type_index = variant_npos_internal(); new( ptr() ) T( value ); type_index = K; } return *this; } #endif // variant_CPP11_OR_GREATER void swap_value( type_index_t index, variant & other ) { using std::swap; switch( index ) { case 0: swap( this->get<0>(), other.get<0>() ); break; case 1: swap( this->get<1>(), other.get<1>() ); break; case 2: swap( this->get<2>(), other.get<2>() ); break; case 3: swap( this->get<3>(), other.get<3>() ); break; case 4: swap( this->get<4>(), other.get<4>() ); break; case 5: swap( this->get<5>(), other.get<5>() ); break; case 6: swap( this->get<6>(), other.get<6>() ); break; case 7: swap( this->get<7>(), other.get<7>() ); break; case 8: swap( this->get<8>(), other.get<8>() ); break; case 9: swap( this->get<9>(), other.get<9>() ); break; case 10: swap( this->get<10>(), other.get<10>() ); break; case 11: swap( this->get<11>(), other.get<11>() ); break; case 12: swap( this->get<12>(), other.get<12>() ); break; case 13: swap( this->get<13>(), other.get<13>() ); break; case 14: swap( this->get<14>(), other.get<14>() ); break; case 15: swap( this->get<15>(), other.get<15>() ); break; } } private: enum { data_size = detail::typelist_max< variant_types >::value }; #if variant_CPP11_OR_GREATER enum { data_align = detail::typelist_max_alignof< variant_types >::value }; using aligned_storage_t = typename std::aligned_storage< data_size, data_align >::type; aligned_storage_t data; #elif variant_CONFIG_MAX_ALIGN_HACK typedef union { unsigned char data[ data_size ]; } aligned_storage_t; detail::max_align_t hack; aligned_storage_t data; #else typedef typename detail::typelist_max< variant_types >::type max_type; typedef variant_ALIGN_AS( max_type ) align_as_type; typedef union { align_as_type data[ 1 + ( data_size - 1 ) / sizeof(align_as_type) ]; } aligned_storage_t; aligned_storage_t data; // # undef variant_ALIGN_AS #endif // variant_CONFIG_MAX_ALIGN_HACK type_index_t type_index; }; template< class T, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline bool holds_alternative( variant const & v ) variant_noexcept { return v.index() == variant::template index_of(); } template< class R, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline R & get( variant & v, nonstd_lite_in_place_type_t(R) = nonstd_lite_in_place_type(R) ) { return v.template get(); } template< class R, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline R const & get( variant const & v, nonstd_lite_in_place_type_t(R) = nonstd_lite_in_place_type(R) ) { return v.template get(); } template< std::size_t K, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline typename variant_alternative< K, variant >::type & get( variant & v, nonstd_lite_in_place_index_t(K) = nonstd_lite_in_place_index(K) ) { #if variant_CONFIG_NO_EXCEPTIONS assert( K == v.index() ); #else if ( K != v.index() ) { throw bad_variant_access(); } #endif return v.template get(); } template< std::size_t K, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline typename variant_alternative< K, variant >::type const & get( variant const & v, nonstd_lite_in_place_index_t(K) = nonstd_lite_in_place_index(K) ) { #if variant_CONFIG_NO_EXCEPTIONS assert( K == v.index() ); #else if ( K != v.index() ) { throw bad_variant_access(); } #endif return v.template get(); } #if variant_CPP11_OR_GREATER template< class R, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline R && get( variant && v, nonstd_lite_in_place_type_t(R) = nonstd_lite_in_place_type(R) ) { return std::move(v.template get()); } template< class R, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline R const && get( variant const && v, nonstd_lite_in_place_type_t(R) = nonstd_lite_in_place_type(R) ) { return std::move(v.template get()); } template< std::size_t K, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline typename variant_alternative< K, variant >::type && get( variant && v, nonstd_lite_in_place_index_t(K) = nonstd_lite_in_place_index(K) ) { #if variant_CONFIG_NO_EXCEPTIONS assert( K == v.index() ); #else if ( K != v.index() ) { throw bad_variant_access(); } #endif return std::move(v.template get()); } template< std::size_t K, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline typename variant_alternative< K, variant >::type const && get( variant const && v, nonstd_lite_in_place_index_t(K) = nonstd_lite_in_place_index(K) ) { #if variant_CONFIG_NO_EXCEPTIONS assert( K == v.index() ); #else if ( K != v.index() ) { throw bad_variant_access(); } #endif return std::move(v.template get()); } #endif // variant_CPP11_OR_GREATER template< class T, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline typename detail::add_pointer::type get_if( variant * pv, nonstd_lite_in_place_type_t(T) = nonstd_lite_in_place_type(T) ) { return ( pv->index() == variant::template index_of() ) ? &get( *pv ) : variant_nullptr; } template< class T, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline typename detail::add_pointer::type get_if( variant const * pv, nonstd_lite_in_place_type_t(T) = nonstd_lite_in_place_type(T)) { return ( pv->index() == variant::template index_of() ) ? &get( *pv ) : variant_nullptr; } template< std::size_t K, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline typename detail::add_pointer< typename variant_alternative >::type >::type get_if( variant * pv, nonstd_lite_in_place_index_t(K) = nonstd_lite_in_place_index(K) ) { return ( pv->index() == K ) ? &get( *pv ) : variant_nullptr; } template< std::size_t K, class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline typename detail::add_pointer< const typename variant_alternative >::type >::type get_if( variant const * pv, nonstd_lite_in_place_index_t(K) = nonstd_lite_in_place_index(K) ) { return ( pv->index() == K ) ? &get( *pv ) : variant_nullptr; } template< class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline void swap( variant & a, variant & b ) variant_noexcept { a.swap( b ); } // Variant 'visitor' implementation namespace detail { template< typename R, typename VT > struct VisitorApplicatorImpl { template< typename Visitor, typename T > static R apply(Visitor const& v, T const& arg) { return v(arg); } }; template< typename R, typename VT > struct VisitorApplicatorImpl > { template< typename Visitor, typename T > static R apply(Visitor const&, T) { return R(); } }; template struct VisitorApplicator; template< typename R, typename Visitor, typename V1 > struct VisitorUnwrapper; #if variant_CPP11_OR_GREATER template< size_t NumVars, typename R, typename Visitor, typename ... T > #else template< size_t NumVars, typename R, typename Visitor, typename T1, typename T2 = S0, typename T3 = S0, typename T4 = S0, typename T5 = S0 > #endif struct TypedVisitorUnwrapper; template< typename R, typename Visitor, typename T2 > struct TypedVisitorUnwrapper<2, R, Visitor, T2> { const Visitor& visitor; T2 const& val2; TypedVisitorUnwrapper(const Visitor& visitor_, T2 const& val2_) : visitor(visitor_) , val2(val2_) { } template R operator()(const T& val1) const { return visitor(val1, val2); } }; template< typename R, typename Visitor, typename T2, typename T3 > struct TypedVisitorUnwrapper<3, R, Visitor, T2, T3> { const Visitor& visitor; T2 const& val2; T3 const& val3; TypedVisitorUnwrapper(const Visitor& visitor_, T2 const& val2_, T3 const& val3_) : visitor(visitor_) , val2(val2_) , val3(val3_) { } template R operator()(const T& val1) const { return visitor(val1, val2, val3); } }; template< typename R, typename Visitor, typename T2, typename T3, typename T4 > struct TypedVisitorUnwrapper<4, R, Visitor, T2, T3, T4> { const Visitor& visitor; T2 const& val2; T3 const& val3; T4 const& val4; TypedVisitorUnwrapper(const Visitor& visitor_, T2 const& val2_, T3 const& val3_, T4 const& val4_) : visitor(visitor_) , val2(val2_) , val3(val3_) , val4(val4_) { } template R operator()(const T& val1) const { return visitor(val1, val2, val3, val4); } }; template< typename R, typename Visitor, typename T2, typename T3, typename T4, typename T5 > struct TypedVisitorUnwrapper<5, R, Visitor, T2, T3, T4, T5> { const Visitor& visitor; T2 const& val2; T3 const& val3; T4 const& val4; T5 const& val5; TypedVisitorUnwrapper(const Visitor& visitor_, T2 const& val2_, T3 const& val3_, T4 const& val4_, T5 const& val5_) : visitor(visitor_) , val2(val2_) , val3(val3_) , val4(val4_) , val5(val5_) { } template R operator()(const T& val1) const { return visitor(val1, val2, val3, val4, val5); } }; template struct VisitorUnwrapper { const Visitor& visitor; const V2& r; VisitorUnwrapper(const Visitor& visitor_, const V2& r_) : visitor(visitor_) , r(r_) { } template< typename T1 > R operator()(T1 const& val1) const { typedef TypedVisitorUnwrapper<2, R, Visitor, T1> visitor_type; return VisitorApplicator::apply(visitor_type(visitor, val1), r); } template< typename T1, typename T2 > R operator()(T1 const& val1, T2 const& val2) const { typedef TypedVisitorUnwrapper<3, R, Visitor, T1, T2> visitor_type; return VisitorApplicator::apply(visitor_type(visitor, val1, val2), r); } template< typename T1, typename T2, typename T3 > R operator()(T1 const& val1, T2 const& val2, T3 const& val3) const { typedef TypedVisitorUnwrapper<4, R, Visitor, T1, T2, T3> visitor_type; return VisitorApplicator::apply(visitor_type(visitor, val1, val2, val3), r); } template< typename T1, typename T2, typename T3, typename T4 > R operator()(T1 const& val1, T2 const& val2, T3 const& val3, T4 const& val4) const { typedef TypedVisitorUnwrapper<5, R, Visitor, T1, T2, T3, T4> visitor_type; return VisitorApplicator::apply(visitor_type(visitor, val1, val2, val3, val4), r); } template< typename T1, typename T2, typename T3, typename T4, typename T5 > R operator()(T1 const& val1, T2 const& val2, T3 const& val3, T4 const& val4, T5 const& val5) const { typedef TypedVisitorUnwrapper<6, R, Visitor, T1, T2, T3, T4, T5> visitor_type; return VisitorApplicator::apply(visitor_type(visitor, val1, val2, val3, val4, val5), r); } }; template struct VisitorApplicator { template static R apply(const Visitor& v, const V1& arg) { switch( arg.index() ) { case 0: return apply_visitor<0>(v, arg); case 1: return apply_visitor<1>(v, arg); case 2: return apply_visitor<2>(v, arg); case 3: return apply_visitor<3>(v, arg); case 4: return apply_visitor<4>(v, arg); case 5: return apply_visitor<5>(v, arg); case 6: return apply_visitor<6>(v, arg); case 7: return apply_visitor<7>(v, arg); case 8: return apply_visitor<8>(v, arg); case 9: return apply_visitor<9>(v, arg); case 10: return apply_visitor<10>(v, arg); case 11: return apply_visitor<11>(v, arg); case 12: return apply_visitor<12>(v, arg); case 13: return apply_visitor<13>(v, arg); case 14: return apply_visitor<14>(v, arg); case 15: return apply_visitor<15>(v, arg); default: return R(); } } template static R apply_visitor(const Visitor& v, const V1& arg) { #if variant_CPP11_OR_GREATER typedef typename variant_alternative::type>::type value_type; #else typedef typename variant_alternative::type value_type; #endif return VisitorApplicatorImpl::apply(v, get(arg)); } #if variant_CPP11_OR_GREATER template static R apply(const Visitor& v, const V1& arg1, const V2& arg2, const V ... args) { typedef VisitorUnwrapper Unwrapper; Unwrapper unwrapper(v, arg1); return apply(unwrapper, arg2, args ...); } #else template< typename Visitor, typename V1, typename V2 > static R apply(const Visitor& v, V1 const& arg1, V2 const& arg2) { typedef VisitorUnwrapper Unwrapper; Unwrapper unwrapper(v, arg1); return apply(unwrapper, arg2); } template< typename Visitor, typename V1, typename V2, typename V3 > static R apply(const Visitor& v, V1 const& arg1, V2 const& arg2, V3 const& arg3) { typedef VisitorUnwrapper Unwrapper; Unwrapper unwrapper(v, arg1); return apply(unwrapper, arg2, arg3); } template< typename Visitor, typename V1, typename V2, typename V3, typename V4 > static R apply(const Visitor& v, V1 const& arg1, V2 const& arg2, V3 const& arg3, V4 const& arg4) { typedef VisitorUnwrapper Unwrapper; Unwrapper unwrapper(v, arg1); return apply(unwrapper, arg2, arg3, arg4); } template< typename Visitor, typename V1, typename V2, typename V3, typename V4, typename V5 > static R apply(const Visitor& v, V1 const& arg1, V2 const& arg2, V3 const& arg3, V4 const& arg4, V5 const& arg5) { typedef VisitorUnwrapper Unwrapper; Unwrapper unwrapper(v, arg1); return apply(unwrapper, arg2, arg3, arg4, arg5); } #endif }; #if variant_CPP11_OR_GREATER template< size_t NumVars, typename Visitor, typename ... V > struct VisitorImpl { typedef decltype(std::declval()(get<0>(static_cast(std::declval()))...)) result_type; typedef VisitorApplicator applicator_type; }; #endif } // detail #if variant_CPP11_OR_GREATER // No perfect forwarding here in order to simplify code template< typename Visitor, typename ... V > inline auto visit(Visitor const& v, V const& ... vars) -> typename detail::VisitorImpl ::result_type { typedef detail::VisitorImpl impl_type; return impl_type::applicator_type::apply(v, vars...); } #else template< typename R, typename Visitor, typename V1 > inline R visit(const Visitor& v, V1 const& arg1) { return detail::VisitorApplicator::apply(v, arg1); } template< typename R, typename Visitor, typename V1, typename V2 > inline R visit(const Visitor& v, V1 const& arg1, V2 const& arg2) { return detail::VisitorApplicator::apply(v, arg1, arg2); } template< typename R, typename Visitor, typename V1, typename V2, typename V3 > inline R visit(const Visitor& v, V1 const& arg1, V2 const& arg2, V3 const& arg3) { return detail::VisitorApplicator::apply(v, arg1, arg2, arg3); } template< typename R, typename Visitor, typename V1, typename V2, typename V3, typename V4 > inline R visit(const Visitor& v, V1 const& arg1, V2 const& arg2, V3 const& arg3, V4 const& arg4) { return detail::VisitorApplicator::apply(v, arg1, arg2, arg3, arg4); } template< typename R, typename Visitor, typename V1, typename V2, typename V3, typename V4, typename V5 > inline R visit(const Visitor& v, V1 const& arg1, V2 const& arg2, V3 const& arg3, V4 const& arg4, V5 const& arg5) { return detail::VisitorApplicator::apply(v, arg1, arg2, arg3, arg4, arg5); } #endif namespace detail { template< class Variant > struct Comparator { static inline bool equal( Variant const & v, Variant const & w ) { switch( v.index() ) { case 0: return get<0>( v ) == get<0>( w ); case 1: return get<1>( v ) == get<1>( w ); case 2: return get<2>( v ) == get<2>( w ); case 3: return get<3>( v ) == get<3>( w ); case 4: return get<4>( v ) == get<4>( w ); case 5: return get<5>( v ) == get<5>( w ); case 6: return get<6>( v ) == get<6>( w ); case 7: return get<7>( v ) == get<7>( w ); case 8: return get<8>( v ) == get<8>( w ); case 9: return get<9>( v ) == get<9>( w ); case 10: return get<10>( v ) == get<10>( w ); case 11: return get<11>( v ) == get<11>( w ); case 12: return get<12>( v ) == get<12>( w ); case 13: return get<13>( v ) == get<13>( w ); case 14: return get<14>( v ) == get<14>( w ); case 15: return get<15>( v ) == get<15>( w ); default: return false; } } static inline bool less_than( Variant const & v, Variant const & w ) { switch( v.index() ) { case 0: return get<0>( v ) < get<0>( w ); case 1: return get<1>( v ) < get<1>( w ); case 2: return get<2>( v ) < get<2>( w ); case 3: return get<3>( v ) < get<3>( w ); case 4: return get<4>( v ) < get<4>( w ); case 5: return get<5>( v ) < get<5>( w ); case 6: return get<6>( v ) < get<6>( w ); case 7: return get<7>( v ) < get<7>( w ); case 8: return get<8>( v ) < get<8>( w ); case 9: return get<9>( v ) < get<9>( w ); case 10: return get<10>( v ) < get<10>( w ); case 11: return get<11>( v ) < get<11>( w ); case 12: return get<12>( v ) < get<12>( w ); case 13: return get<13>( v ) < get<13>( w ); case 14: return get<14>( v ) < get<14>( w ); case 15: return get<15>( v ) < get<15>( w ); default: return false; } } }; } //namespace detail template< class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline bool operator==( variant const & v, variant const & w ) { if ( v.index() != w.index() ) return false; else if ( v.valueless_by_exception() ) return true; else return detail::Comparator< variant >::equal( v, w ); } template< class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline bool operator!=( variant const & v, variant const & w ) { return ! ( v == w ); } template< class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline bool operator<( variant const & v, variant const & w ) { if ( w.valueless_by_exception() ) return false; else if ( v.valueless_by_exception() ) return true; else if ( v.index() < w.index() ) return true; else if ( v.index() > w.index() ) return false; else return detail::Comparator< variant >::less_than( v, w ); } template< class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline bool operator>( variant const & v, variant const & w ) { return w < v; } template< class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline bool operator<=( variant const & v, variant const & w ) { return ! ( v > w ); } template< class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > inline bool operator>=( variant const & v, variant const & w ) { return ! ( v < w ); } } // namespace variants using namespace variants; } // namespace nonstd #if variant_CPP11_OR_GREATER // specialize the std::hash algorithm: namespace std { template<> struct hash< nonstd::monostate > { std::size_t operator()( nonstd::monostate ) const variant_noexcept { return 42; } }; template< class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 > struct hash< nonstd::variant > { std::size_t operator()( nonstd::variant const & v ) const variant_noexcept { return nonstd::variants::detail::hash( v ); } }; } //namespace std #endif // variant_CPP11_OR_GREATER #if variant_BETWEEN( variant_COMPILER_MSVC_VER, 1300, 1900 ) # pragma warning( pop ) #endif #endif // variant_USES_STD_VARIANT #endif // NONSTD_VARIANT_LITE_HPP