/** This source adapted from https://github.com/kmicklas/variadic-static_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. * **/ #include #include namespace fc { // Implementation details, the user should not import this: namespace impl { template struct storage_ops; template struct position; template struct type_info; template struct copy_construct { StaticVariant& sv; copy_construct( StaticVariant& s ):sv(s){} template void operator()( const T& v )const { sv.init(v); } }; template struct storage_ops { static void del(int n, void *data) {} static void con(int n, void *data) {} template static typename visitor::result_type apply(int n, void *data, visitor& v) {} template static typename visitor::result_type apply(int n, void *data, const visitor& v) {} template static typename visitor::result_type apply(int n, const void *data, visitor& v) {} template static typename visitor::result_type apply(int n, const void *data, const visitor& v) {} }; template struct storage_ops { static void del(int n, void *data) { if(n == N) reinterpret_cast(data)->~T(); else storage_ops::del(n, data); } static void con(int n, void *data) { if(n == N) new(reinterpret_cast(data)) T(); else storage_ops::del(n, data); } template static typename visitor::result_type apply(int n, void *data, visitor& v) { if(n == N) return v(*reinterpret_cast(data)); else return storage_ops::apply(n, data, v); } template static typename visitor::result_type apply(int n, void *data, const visitor& v) { if(n == N) return v(*reinterpret_cast(data)); else return storage_ops::apply(n, data, v); } template static typename visitor::result_type apply(int n, const void *data, visitor& v) { if(n == N) return v(*reinterpret_cast(data)); else return storage_ops::apply(n, data, v); } template static typename visitor::result_type apply(int n, const void *data, const visitor& v) { if(n == N) return v(*reinterpret_cast(data)); else return storage_ops::apply(n, data, v); } }; template struct storage_ops { static void del(int n, void *data) { throw std::runtime_error( "Internal error: static_variant tag is invalid." ); } static void con(int n, void *data) { throw std::runtime_error( "Internal error: static_variant tag is invalid." ); } template static typename visitor::result_type apply(int n, void *data, visitor& v) { throw std::runtime_error( "Internal error: static_variant tag is invalid." ); } template static typename visitor::result_type apply(int n, void *data, const visitor& v) { throw std::runtime_error( "Internal error: static_variant tag is invalid." ); } template static typename visitor::result_type apply(int n, const void *data, visitor& v) { throw std::runtime_error( "Internal error: static_variant tag is invalid." ); } template static typename visitor::result_type apply(int n, const void *data, const visitor& v) { throw std::runtime_error( "Internal error: static_variant tag is invalid." ); } }; template struct position { static const int pos = -1; }; template struct position { static const int pos = 0; }; template struct position { static const int pos = position::pos != -1 ? position::pos + 1 : -1; }; template struct type_info { static const bool no_reference_types = false; static const bool no_duplicates = position::pos == -1 && type_info::no_duplicates; static const size_t size = type_info::size > sizeof(T&) ? type_info::size : sizeof(T&); }; template struct type_info { static const bool no_reference_types = type_info::no_reference_types; static const bool no_duplicates = position::pos == -1 && type_info::no_duplicates; static const size_t size = type_info::size > sizeof(T) ? type_info::size : sizeof(T&); }; template<> struct type_info<> { static const bool no_reference_types = true; static const bool no_duplicates = true; static const size_t size = 0; }; } // namespace impl template class static_variant { static_assert(impl::type_info::no_reference_types, "Reference types are not permitted in static_variant."); static_assert(impl::type_info::no_duplicates, "static_variant type arguments contain duplicate types."); int _tag; char storage[impl::type_info::size]; template void init(const X& x) { _tag = impl::position::pos; new(storage) X(x); } template friend struct copy_construct; public: template struct tag { static_assert( impl::position::pos != -1, "Type not in static_variant." ); static const int value = impl::position::pos; }; static_variant() { _tag = 0; impl::storage_ops<0, Types...>::con(0, storage); } template static_variant( const static_variant& cpy ) { cpy.apply( impl::copy_construct>(*this) ); } template static_variant(const X& v) { static_assert( impl::position::pos != -1, "Type not in static_variant." ); init(v); } ~static_variant() { impl::storage_ops<0, Types...>::del(_tag, storage); } template void operator=(const X& v) { static_assert( impl::position::pos != -1, "Type not in static_variant." ); this->~static_variant(); init(v); } template X& get() { static_assert( impl::position::pos != -1, "Type not in static_variant." ); if(_tag == impl::position::pos) { return *reinterpret_cast(storage); } else { throw std::runtime_error( std::string("static_variant does not contain value of type ") + typeid(X).name() ); } } template const X& get() const { static_assert( impl::position::pos != -1, "Type not in static_variant." ); if(_tag == impl::position::pos) { return *reinterpret_cast(storage); } else { throw std::runtime_error( std::string("static_variant does not contain value of type ") + typeid(X).name() ); } } template typename visitor::result_type visit(visitor& v) { return impl::storage_ops<0, Types...>::apply(_tag, storage, v); } template typename visitor::result_type visit(const visitor& v) { return impl::storage_ops<0, Types...>::apply(_tag, storage, v); } template typename visitor::result_type visit(visitor& v)const { return impl::storage_ops<0, Types...>::apply(_tag, storage, v); } template typename visitor::result_type visit(const visitor& v)const { return impl::storage_ops<0, Types...>::apply(_tag, storage, v); } void set_which( int w ) { this->~static_variant(); impl::storage_ops<0, Types...>::con(_tag, storage); } int which() const {return _tag;} }; template struct visitor { typedef Result result_type; }; } // namespace fc