peerplays-fc/include/fc/static_variant.hpp

373 lines
13 KiB
C++
Raw Normal View History

2020-10-16 15:12:57 +00:00
/** This source adapted from https://github.com/kmicklas/variadic-static_variant Now available at https://github.com/kmicklas/variadic-variant.
2015-02-27 18:41:55 +00:00
*
* 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
2015-02-27 18:41:55 +00:00
#include <stdexcept>
#include <typeinfo>
#include <fc/exception/exception.hpp>
2015-02-27 18:41:55 +00:00
namespace fc {
// Implementation details, the user should not import this:
namespace impl {
template<int N, typename... Ts>
struct storage_ops;
template<typename X, typename... Ts>
struct position;
template<typename... Ts>
struct type_info;
2015-02-27 21:07:50 +00:00
template<typename StaticVariant>
struct copy_construct
{
2015-03-02 23:24:37 +00:00
typedef void result_type;
2015-02-27 21:07:50 +00:00
StaticVariant& sv;
copy_construct( StaticVariant& s ):sv(s){}
template<typename T>
void operator()( const T& v )const
{
sv.init(v);
}
};
2015-03-02 23:24:37 +00:00
template<typename StaticVariant>
struct move_construct
{
typedef void result_type;
StaticVariant& sv;
move_construct( StaticVariant& s ):sv(s){}
template<typename T>
void operator()( T& v )const
{
sv.init( std::move(v) );
}
};
2015-02-27 18:41:55 +00:00
template<int N, typename T, typename... Ts>
struct storage_ops<N, T&, Ts...> {
2015-02-27 21:07:50 +00:00
static void del(int n, void *data) {}
static void con(int n, void *data) {}
template<typename visitor>
static typename visitor::result_type apply(int n, void *data, visitor& v) {}
template<typename visitor>
static typename visitor::result_type apply(int n, void *data, const visitor& v) {}
template<typename visitor>
static typename visitor::result_type apply(int n, const void *data, visitor& v) {}
template<typename visitor>
static typename visitor::result_type apply(int n, const void *data, const visitor& v) {}
2015-02-27 18:41:55 +00:00
};
template<int N, typename T, typename... Ts>
struct storage_ops<N, T, Ts...> {
2015-02-27 21:07:50 +00:00
static void del(int n, void *data) {
if(n == N) reinterpret_cast<T*>(data)->~T();
else storage_ops<N + 1, Ts...>::del(n, data);
}
static void con(int n, void *data) {
if(n == N) new(reinterpret_cast<T*>(data)) T();
2015-03-05 13:58:29 +00:00
else storage_ops<N + 1, Ts...>::con(n, data);
2015-02-27 21:07:50 +00:00
}
template<typename visitor>
static typename visitor::result_type apply(int n, void *data, visitor& v) {
if(n == N) return v(*reinterpret_cast<T*>(data));
else return storage_ops<N + 1, Ts...>::apply(n, data, v);
}
template<typename visitor>
static typename visitor::result_type apply(int n, void *data, const visitor& v) {
if(n == N) return v(*reinterpret_cast<T*>(data));
else return storage_ops<N + 1, Ts...>::apply(n, data, v);
}
template<typename visitor>
static typename visitor::result_type apply(int n, const void *data, visitor& v) {
if(n == N) return v(*reinterpret_cast<const T*>(data));
else return storage_ops<N + 1, Ts...>::apply(n, data, v);
}
template<typename visitor>
static typename visitor::result_type apply(int n, const void *data, const visitor& v) {
if(n == N) return v(*reinterpret_cast<const T*>(data));
else return storage_ops<N + 1, Ts...>::apply(n, data, v);
}
2015-02-27 18:41:55 +00:00
};
template<int N>
struct storage_ops<N> {
2015-02-27 21:07:50 +00:00
static void del(int n, void *data) {
2015-07-01 15:17:15 +00:00
FC_THROW_EXCEPTION( fc::assert_exception, "Internal error: static_variant tag is invalid.");
2015-02-27 21:07:50 +00:00
}
static void con(int n, void *data) {
2015-07-01 15:17:15 +00:00
FC_THROW_EXCEPTION( fc::assert_exception, "Internal error: static_variant tag is invalid." );
2015-02-27 21:07:50 +00:00
}
template<typename visitor>
static typename visitor::result_type apply(int n, void *data, visitor& v) {
2015-07-01 15:17:15 +00:00
FC_THROW_EXCEPTION( fc::assert_exception, "Internal error: static_variant tag is invalid." );
2015-02-27 21:07:50 +00:00
}
template<typename visitor>
static typename visitor::result_type apply(int n, void *data, const visitor& v) {
2015-07-01 15:17:15 +00:00
FC_THROW_EXCEPTION( fc::assert_exception, "Internal error: static_variant tag is invalid." );
2015-02-27 21:07:50 +00:00
}
template<typename visitor>
static typename visitor::result_type apply(int n, const void *data, visitor& v) {
2015-07-01 15:17:15 +00:00
FC_THROW_EXCEPTION( fc::assert_exception, "Internal error: static_variant tag is invalid." );
2015-02-27 21:07:50 +00:00
}
template<typename visitor>
static typename visitor::result_type apply(int n, const void *data, const visitor& v) {
2015-07-01 15:17:15 +00:00
FC_THROW_EXCEPTION( fc::assert_exception, "Internal error: static_variant tag is invalid." );
2015-02-27 21:07:50 +00:00
}
2015-02-27 18:41:55 +00:00
};
template<typename X>
struct position<X> {
2015-02-27 21:07:50 +00:00
static const int pos = -1;
2015-02-27 18:41:55 +00:00
};
template<typename X, typename... Ts>
struct position<X, X, Ts...> {
2015-02-27 21:07:50 +00:00
static const int pos = 0;
2015-02-27 18:41:55 +00:00
};
template<typename X, typename T, typename... Ts>
struct position<X, T, Ts...> {
2015-02-27 21:07:50 +00:00
static const int pos = position<X, Ts...>::pos != -1 ? position<X, Ts...>::pos + 1 : -1;
2015-02-27 18:41:55 +00:00
};
template<typename T, typename... Ts>
struct type_info<T&, Ts...> {
2015-02-27 21:07:50 +00:00
static const bool no_reference_types = false;
static const bool no_duplicates = position<T, Ts...>::pos == -1 && type_info<Ts...>::no_duplicates;
static const size_t size = type_info<Ts...>::size > sizeof(T&) ? type_info<Ts...>::size : sizeof(T&);
static const size_t count = 1 + type_info<Ts...>::count;
2015-02-27 18:41:55 +00:00
};
template<typename T, typename... Ts>
struct type_info<T, Ts...> {
2015-02-27 21:07:50 +00:00
static const bool no_reference_types = type_info<Ts...>::no_reference_types;
static const bool no_duplicates = position<T, Ts...>::pos == -1 && type_info<Ts...>::no_duplicates;
static const size_t size = type_info<Ts...>::size > sizeof(T) ? type_info<Ts...>::size : sizeof(T&);
static const size_t count = 1 + type_info<Ts...>::count;
2015-02-27 18:41:55 +00:00
};
template<>
struct type_info<> {
2015-02-27 21:07:50 +00:00
static const bool no_reference_types = true;
static const bool no_duplicates = true;
static const size_t count = 0;
2015-02-27 21:07:50 +00:00
static const size_t size = 0;
2015-02-27 18:41:55 +00:00
};
2020-10-16 15:12:57 +00:00
template<typename TTag>
size_t size( TTag )
{
return 0;
}
template<typename TTag, typename A, typename...Ts>
size_t size( TTag tag )
{
if (tag <= 0)
{
return sizeof(A);
}
return size<TTag, Ts...>( --tag );
}
class dynamic_storage
{
char* storage;
public:
dynamic_storage();
~dynamic_storage();
void* data() const;
2015-02-27 18:41:55 +00:00
2020-10-16 15:12:57 +00:00
void alloc( size_t size );
void release();
};
} // namespace impl
2015-02-27 18:41:55 +00:00
template<typename... Types>
class static_variant {
2020-10-16 15:12:57 +00:00
using tag_type = int64_t;
2015-02-27 21:07:50 +00:00
static_assert(impl::type_info<Types...>::no_reference_types, "Reference types are not permitted in static_variant.");
static_assert(impl::type_info<Types...>::no_duplicates, "static_variant type arguments contain duplicate types.");
2015-03-02 23:24:37 +00:00
2015-02-27 21:07:50 +00:00
template<typename X>
2020-10-16 15:12:57 +00:00
using type_in_typelist = typename std::enable_if<impl::position<X, Types...>::pos != -1, X>::type; // type is in typelist of static_variant.
int _tag;
impl::dynamic_storage storage;
template<typename X, typename = type_in_typelist<X>>
2015-02-27 21:07:50 +00:00
void init(const X& x) {
_tag = impl::position<X, Types...>::pos;
2020-10-16 15:12:57 +00:00
storage.alloc( sizeof(X) );
new(storage.data()) X(x);
2015-02-27 21:07:50 +00:00
}
2020-10-16 15:12:57 +00:00
template<typename X, typename = type_in_typelist<X>>
2015-03-02 23:24:37 +00:00
void init(X&& x) {
_tag = impl::position<X, Types...>::pos;
2020-10-16 15:12:57 +00:00
storage.alloc( sizeof(X) );
new(storage.data()) X( std::move(x) );
2015-03-02 23:24:37 +00:00
}
2020-10-16 15:12:57 +00:00
void init(tag_type tag)
{
FC_ASSERT( tag >= 0 );
FC_ASSERT( tag < count() );
_tag = tag;
storage.alloc( impl::size<tag_type, Types...>( tag ) );
impl::storage_ops<0, Types...>::con(_tag, storage.data());
}
void clean()
{
impl::storage_ops<0, Types...>::del(_tag, storage.data() );
storage.release();
}
2015-03-02 23:24:37 +00:00
template<typename StaticVariant>
friend struct impl::copy_construct;
2015-02-27 21:07:50 +00:00
template<typename StaticVariant>
2015-03-02 23:24:37 +00:00
friend struct impl::move_construct;
2015-02-27 18:41:55 +00:00
public:
2020-10-16 15:12:57 +00:00
template<typename X, typename = type_in_typelist<X>>
2015-02-27 21:07:50 +00:00
struct tag
{
static const int value = impl::position<X, Types...>::pos;
};
static_variant()
{
2020-10-16 15:12:57 +00:00
init(0);
}
2015-02-27 21:07:50 +00:00
template<typename... Other>
static_variant( const static_variant<Other...>& cpy )
{
2015-03-02 23:24:37 +00:00
cpy.visit( impl::copy_construct<static_variant>(*this) );
}
static_variant( const static_variant& cpy )
{
cpy.visit( impl::copy_construct<static_variant>(*this) );
}
static_variant( static_variant&& mv )
{
mv.visit( impl::move_construct<static_variant>(*this) );
2015-02-27 21:07:50 +00:00
}
2020-10-16 15:12:57 +00:00
template<typename X, typename = type_in_typelist<X>>
2015-02-27 21:07:50 +00:00
static_variant(const X& v) {
init(v);
}
~static_variant() {
2020-10-16 15:12:57 +00:00
clean();
2015-02-27 21:07:50 +00:00
}
2020-10-16 15:12:57 +00:00
template<typename X, typename = type_in_typelist<X>>
2015-03-02 23:24:37 +00:00
static_variant& operator=(const X& v) {
2020-10-16 15:12:57 +00:00
clean();
2015-02-27 21:07:50 +00:00
init(v);
2015-03-02 23:24:37 +00:00
return *this;
2015-02-27 21:07:50 +00:00
}
2015-03-02 23:24:37 +00:00
static_variant& operator=( const static_variant& v )
{
if( this == &v ) return *this;
2020-10-16 15:12:57 +00:00
clean();
2015-03-02 23:24:37 +00:00
v.visit( impl::copy_construct<static_variant>(*this) );
return *this;
}
static_variant& operator=( static_variant&& v )
{
if( this == &v ) return *this;
2020-10-16 15:12:57 +00:00
clean();
2015-03-02 23:24:37 +00:00
v.visit( impl::move_construct<static_variant>(*this) );
return *this;
}
friend bool operator == ( const static_variant& a, const static_variant& b )
{
2015-07-08 20:43:25 +00:00
return a.which() == b.which();
}
friend bool operator < ( const static_variant& a, const static_variant& b )
{
2015-07-08 20:43:25 +00:00
return a.which() < b.which();
}
2020-10-16 15:12:57 +00:00
template<typename X, typename = type_in_typelist<X>>
2015-02-27 21:07:50 +00:00
X& get() {
if(_tag == impl::position<X, Types...>::pos) {
2020-10-16 15:12:57 +00:00
return *reinterpret_cast<X*>(storage.data());
2015-02-27 21:07:50 +00:00
} else {
2015-07-01 15:17:15 +00:00
FC_THROW_EXCEPTION( fc::assert_exception, "static_variant does not contain a value of type ${t}", ("t",fc::get_typename<X>::name()) );
2015-02-27 21:07:50 +00:00
}
}
2020-10-16 15:12:57 +00:00
template<typename X, typename = type_in_typelist<X>>
2015-02-27 21:07:50 +00:00
const X& get() const {
if(_tag == impl::position<X, Types...>::pos) {
2020-10-16 15:12:57 +00:00
return *reinterpret_cast<const X*>(storage.data());
2015-02-27 21:07:50 +00:00
} else {
2015-07-01 15:17:15 +00:00
FC_THROW_EXCEPTION( fc::assert_exception, "static_variant does not contain a value of type ${t}", ("t",fc::get_typename<X>::name()) );
2015-02-27 21:07:50 +00:00
}
}
template<typename visitor>
typename visitor::result_type visit(visitor& v) {
2020-10-16 15:12:57 +00:00
return impl::storage_ops<0, Types...>::apply(_tag, storage.data(), v);
2015-02-27 21:07:50 +00:00
}
template<typename visitor>
typename visitor::result_type visit(const visitor& v) {
2020-10-16 15:12:57 +00:00
return impl::storage_ops<0, Types...>::apply(_tag, storage.data(), v);
2015-02-27 21:07:50 +00:00
}
template<typename visitor>
typename visitor::result_type visit(visitor& v)const {
2020-10-16 15:12:57 +00:00
return impl::storage_ops<0, Types...>::apply(_tag, storage.data(), v);
2015-02-27 21:07:50 +00:00
}
template<typename visitor>
typename visitor::result_type visit(const visitor& v)const {
2020-10-16 15:12:57 +00:00
return impl::storage_ops<0, Types...>::apply(_tag, storage.data(), v);
2015-02-27 21:07:50 +00:00
}
static int count() { return impl::type_info<Types...>::count; }
void set_which( int w ) {
2020-10-16 15:12:57 +00:00
FC_ASSERT( w >= 0 );
FC_ASSERT( w < count() );
2020-10-16 15:12:57 +00:00
clean();
init(w);
}
int which() const {return _tag;}
2015-02-27 18:41:55 +00:00
};
template<typename Result>
struct visitor {
2015-02-27 21:07:50 +00:00
typedef Result result_type;
2015-02-27 18:41:55 +00:00
};
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<typename T> 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<typename T> void operator()( T& v )const
{
from_variant( var, v, _max_depth );
}
};
template<typename... T> void to_variant( const fc::static_variant<T...>& 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<typename... T> void from_variant( const fc::variant& v, fc::static_variant<T...>& s, uint32_t max_depth )
{
FC_ASSERT( max_depth > 0 );
auto ar = v.get_array();
2015-03-05 14:54:33 +00:00
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_typename { static const char* name() { return typeid(static_variant<T...>).name(); } };
2020-10-16 15:12:57 +00:00
} // namespace fc