/** This source adapted from https://github.com/kmicklas/variadic-static_variant. Now available at https://github.com/kmicklas/variadic-variant. * * Copyright (C) 2013 Kenneth Micklas * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * **/ #pragma once #include #include #include #include #include namespace fc { // Implementation details, the user should not import this: namespace impl { class dynamic_storage { char* storage; public: dynamic_storage(); ~dynamic_storage(); void* data() const; void alloc( size_t size ); void release(); }; } // namespace impl template class static_variant { public: using tag_type = int64_t; using list = typelist::list; protected: static_assert(typelist::length>() == 0, "Reference types are not permitted in static_variant."); static_assert(typelist::length>() == typelist::length(), "static_variant type arguments contain duplicate types."); template using type_in_typelist = std::enable_if_t() != -1>; tag_type _tag; impl::dynamic_storage storage; template> void init(const X& x) { _tag = typelist::index_of(); storage.alloc( sizeof(X) ); new(storage.data()) X(x); } template> void init(X&& x) { _tag = typelist::index_of(); storage.alloc( sizeof(X) ); new(storage.data()) X( std::move(x) ); } void init_from_tag(tag_type tag) { FC_ASSERT( tag >= 0 ); FC_ASSERT( tag < count() ); _tag = tag; typelist::runtime::dispatch(list(), tag, [this](auto t) { using T = typename decltype(t)::type; storage.alloc(sizeof(T)); new(reinterpret_cast(storage.data())) T(); }); } void clean() { typelist::runtime::dispatch(list(), _tag, [data=storage.data()](auto t) { using T = typename decltype(t)::type; reinterpret_cast(data)->~T(); }); storage.release(); } template struct import_helper { static static_variant construct(const T&) { FC_THROW_EXCEPTION(assert_exception, "Cannot import unsupported type ${T} into static_variant", ("T", get_typename::name())); } static static_variant construct(T&&) { FC_THROW_EXCEPTION(assert_exception, "Cannot import unsupported type ${T} into static_variant", ("T", get_typename::name())); } }; template struct import_helper> { static static_variant construct(const T& t) { return static_variant(t); } static static_variant construct(T&& t) { return static_variant(std::move(t)); } }; public: template> struct tag { static constexpr int value = typelist::index_of(); }; struct type_lt { bool operator()(const static_variant& a, const static_variant& b) const { return a.which() < b.which(); } }; struct type_eq { bool operator()(const static_variant& a, const static_variant& b) const { return a.which() == b.which(); } }; using flat_set_type = flat_set; /// Import the value from a foreign static_variant with types not in this one, and throw if the value is an /// incompatible type template static static_variant import_from(const static_variant& other) { return typelist::runtime::dispatch(typelist::list(), other.which(), [&other](auto t) { using other_type = typename decltype(t)::type; return import_helper::construct(other.template get()); }); } /// Import the value from a foreign static_variant with types not in this one, and throw if the value is an /// incompatible type template static static_variant import_from(static_variant&& other) { return typelist::runtime::dispatch(typelist::list(), other.which(), [&other](auto t) { using other_type = typename decltype(t)::type; return import_helper::construct(std::move(other.template get())); }); } static_variant() { init_from_tag(0); } template static_variant( const static_variant& cpy ) { typelist::runtime::dispatch(typelist::list(), cpy.which(), [this, &cpy](auto t) mutable { this->init(cpy.template get()); }); } static_variant( const static_variant& cpy ) { typelist::runtime::dispatch(list(), cpy.which(), [this, &cpy](auto t) mutable { this->init(cpy.template get()); }); } static_variant( static_variant&& mv ) { typelist::runtime::dispatch(list(), mv.which(), [this, &mv](auto t) mutable { this->init(std::move(mv.template get())); }); } template static_variant( static_variant&& mv ) { typelist::runtime::dispatch(typelist::list(), mv.which(), [this, &mv](auto t) mutable { this->init(std::move(mv.template get())); }); } template> static_variant(const X& v) { init(v); } template> static_variant(X&& v) { init(std::move(v)); } ~static_variant() { clean(); } template> static_variant& operator=(const X& v) { clean(); init(v); return *this; } static_variant& operator=( const static_variant& v ) { if( this == &v ) return *this; clean(); typelist::runtime::dispatch(list(), v.which(), [this, &v](auto t)mutable { this->init(v.template get()); }); return *this; } static_variant& operator=( static_variant&& v ) { if( this == &v ) return *this; clean(); typelist::runtime::dispatch(list(), v.which(), [this, &v](auto t)mutable { this->init(std::move(v.template get())); }); return *this; } friend bool operator==( const static_variant& a, const static_variant& b ) { if (a.which() != b.which()) return false; return typelist::runtime::dispatch(list(), a.which(), [&a, &b](auto t) { return a.get() == b.get(); }); } template> X& get() { if(_tag == typelist::index_of()) { return *reinterpret_cast(storage.data()); } else { FC_THROW_EXCEPTION( fc::assert_exception, "static_variant does not contain a value of type ${t}", ("t",fc::get_typename::name()) ); } } template> const X& get() const { if(_tag == typelist::index_of()) { return *reinterpret_cast(storage.data()); } else { FC_THROW_EXCEPTION( fc::assert_exception, "static_variant does not contain a value of type ${t}", ("t",fc::get_typename::name()) ); } } template typename visitor::result_type visit(visitor& v) { return visit( _tag, v, (void*) storage.data() ); } template typename visitor::result_type visit(const visitor& v) { return visit( _tag, v, (void*) storage.data() ); } template typename visitor::result_type visit(visitor& v)const { return visit( _tag, v, (const void*) storage.data() ); } template typename visitor::result_type visit(const visitor& v)const { return visit( _tag, v, (const void*) storage.data() ); } template static typename visitor::result_type visit( tag_type tag, visitor& v, void* data ) { FC_ASSERT( tag >= 0 && tag < count(), "Unsupported type ${tag}!", ("tag",tag) ); return typelist::runtime::dispatch(list(), tag, [&v, data](auto t) { return v(*reinterpret_cast(data)); }); } template static typename visitor::result_type visit( tag_type tag, const visitor& v, void* data ) { FC_ASSERT( tag >= 0 && tag < count(), "Unsupported type ${tag}!", ("tag",tag) ); return typelist::runtime::dispatch(list(), tag, [&v, data](auto t) { return v(*reinterpret_cast(data)); }); } template static typename visitor::result_type visit( tag_type tag, visitor& v, const void* data ) { FC_ASSERT( tag >= 0 && tag < count(), "Unsupported type ${tag}!", ("tag",tag) ); return typelist::runtime::dispatch(list(), tag, [&v, data](auto t) { return v(*reinterpret_cast(data)); }); } template static typename visitor::result_type visit( tag_type tag, const visitor& v, const void* data ) { FC_ASSERT( tag >= 0 && tag < count(), "Unsupported type ${tag}!", ("tag",tag) ); return typelist::runtime::dispatch(list(), tag, [&v, data](auto t) { return v(*reinterpret_cast(data)); }); } static constexpr int count() { return typelist::length(); } void set_which( tag_type w ) { FC_ASSERT( w >= 0 ); FC_ASSERT( w < count() ); clean(); init_from_tag(w); } tag_type which() const {return _tag;} template bool is_type() const { return _tag == tag::value; } }; template<> class static_variant<> { public: using tag_type = int64_t; static_variant() { FC_THROW_EXCEPTION(assert_exception, "Cannot create static_variant with no types"); } }; template class static_variant> : public static_variant {}; struct from_static_variant { variant& var; const uint32_t _max_depth; from_static_variant( variant& dv, uint32_t max_depth ):var(dv),_max_depth(max_depth){} typedef void result_type; template void operator()( const T& v )const { to_variant( v, var, _max_depth ); } }; struct to_static_variant { const variant& var; const uint32_t _max_depth; to_static_variant( const variant& dv, uint32_t max_depth ):var(dv),_max_depth(max_depth){} typedef void result_type; template void operator()( T& v )const { from_variant( var, v, _max_depth ); } }; template void to_variant( const fc::static_variant& s, fc::variant& v, uint32_t max_depth ) { FC_ASSERT( max_depth > 0 ); variants vars(2); vars[0] = s.which(); s.visit( from_static_variant(vars[1], max_depth - 1) ); v = std::move(vars); } template void from_variant( const fc::variant& v, fc::static_variant& s, uint32_t max_depth ) { FC_ASSERT( max_depth > 0 ); auto ar = v.get_array(); if( ar.size() < 2 ) return; s.set_which( ar[0].as_uint64() ); s.visit( to_static_variant(ar[1], max_depth - 1) ); } template< typename... T > struct get_comma_separated_typenames; template<> struct get_comma_separated_typenames<> { static const char* names() { return ""; } }; template< typename T > struct get_comma_separated_typenames { static const char* names() { static const std::string n = get_typename::name(); return n.c_str(); } }; template< typename T, typename... Ts > struct get_comma_separated_typenames { static const char* names() { static const std::string n = std::string( get_typename::name() )+","+ std::string( get_comma_separated_typenames< Ts... >::names() ); return n.c_str(); } }; template< typename... T > struct get_typename< static_variant< T... > > { static const char* name() { static const std::string n = std::string( "fc::static_variant<" ) + get_comma_separated_typenames::names() + ">"; return n.c_str(); } }; } // namespace fc