adding static_variant type
This commit is contained in:
parent
374f141f0a
commit
c0a4ed77b1
4 changed files with 350 additions and 0 deletions
|
|
@ -530,6 +530,48 @@ namespace fc {
|
|||
raw::unpack(ds,v);
|
||||
return v;
|
||||
} FC_RETHROW_EXCEPTIONS( warn, "error unpacking ${type}", ("type",fc::get_typename<T>::name() ) ) }
|
||||
|
||||
template<typename Stream>
|
||||
struct pack_static_variant
|
||||
{
|
||||
Stream& stream;
|
||||
pack_static_variant( Stream& s ):stream(s){}
|
||||
|
||||
typedef void result_type;
|
||||
template<typename T> void operator()( const T& v )
|
||||
{
|
||||
pack( stream, v );
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Stream>
|
||||
struct unpack_static_variant
|
||||
{
|
||||
Stream& stream;
|
||||
unpack_static_variant( Stream& s ):stream(s){}
|
||||
|
||||
typedef void result_type;
|
||||
template<typename T> void operator()( T& v )
|
||||
{
|
||||
unpack( stream, v );
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename Stream, typename... T>
|
||||
void pack( Stream& s, const static_variant<T...>& sv )
|
||||
{
|
||||
pack( sv, unsigned_int(s.which()) );
|
||||
s.visit( pack_static_variant<Stream>(sv) );
|
||||
}
|
||||
|
||||
template<typename Stream, typename... T> void unpack( Stream& s, static_variant<T...>& sv )
|
||||
{
|
||||
unsigned_int w;
|
||||
unpack( s, w );
|
||||
sv.set_which(w.value);
|
||||
sv.visit( unpack_static_variant<Stream>(s) );
|
||||
}
|
||||
|
||||
} } // namespace fc::raw
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ namespace fc {
|
|||
class variant;
|
||||
class variant_object;
|
||||
class path;
|
||||
template<typename... Types> class static_variant;
|
||||
|
||||
template<typename IntType, typename EnumType> class enum_type;
|
||||
namespace ip { class endpoint; }
|
||||
|
||||
|
|
@ -32,6 +34,9 @@ namespace fc {
|
|||
template<typename Stream, typename T> inline void pack( Stream& s, const std::unordered_set<T>& value );
|
||||
template<typename Stream, typename T> inline void unpack( Stream& s, std::unordered_set<T>& value );
|
||||
|
||||
template<typename Stream, typename... T> void pack( Stream& s, const static_variant<T...>& sv );
|
||||
template<typename Stream, typename... T> void unpack( Stream& s, static_variant<T...>& sv );
|
||||
|
||||
template<typename Stream, typename T> inline void pack( Stream& s, const flat_set<T>& value );
|
||||
template<typename Stream, typename T> inline void unpack( Stream& s, flat_set<T>& value );
|
||||
|
||||
|
|
|
|||
256
include/fc/static_variant.hpp
Normal file
256
include/fc/static_variant.hpp
Normal file
|
|
@ -0,0 +1,256 @@
|
|||
/** 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 <stdexcept>
|
||||
#include <typeinfo>
|
||||
|
||||
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;
|
||||
|
||||
template<int N, typename T, typename... Ts>
|
||||
struct storage_ops<N, T&, Ts...> {
|
||||
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) {}
|
||||
};
|
||||
|
||||
template<int N, typename T, typename... Ts>
|
||||
struct storage_ops<N, T, Ts...> {
|
||||
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();
|
||||
else storage_ops<N + 1, Ts...>::del(n, data);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
template<int N>
|
||||
struct storage_ops<N> {
|
||||
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<typename visitor>
|
||||
static typename visitor::result_type apply(int n, void *data, visitor& v) {
|
||||
throw std::runtime_error(
|
||||
"Internal error: static_variant tag is invalid."
|
||||
);
|
||||
}
|
||||
template<typename visitor>
|
||||
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<typename visitor>
|
||||
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<typename visitor>
|
||||
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<typename X>
|
||||
struct position<X> {
|
||||
static const int pos = -1;
|
||||
};
|
||||
|
||||
template<typename X, typename... Ts>
|
||||
struct position<X, X, Ts...> {
|
||||
static const int pos = 0;
|
||||
};
|
||||
|
||||
template<typename X, typename T, typename... Ts>
|
||||
struct position<X, T, Ts...> {
|
||||
static const int pos = position<X, Ts...>::pos != -1 ? position<X, Ts...>::pos + 1 : -1;
|
||||
};
|
||||
|
||||
template<typename T, typename... Ts>
|
||||
struct type_info<T&, Ts...> {
|
||||
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&);
|
||||
};
|
||||
|
||||
template<typename T, typename... Ts>
|
||||
struct type_info<T, Ts...> {
|
||||
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&);
|
||||
};
|
||||
|
||||
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<typename... Types>
|
||||
class static_variant {
|
||||
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.");
|
||||
|
||||
int tag;
|
||||
char storage[impl::type_info<Types...>::size];
|
||||
|
||||
static_variant() = delete;
|
||||
|
||||
template<typename X>
|
||||
void init(const X& x) {
|
||||
tag = impl::position<X, Types...>::pos;
|
||||
new(storage) X(x);
|
||||
}
|
||||
public:
|
||||
template<typename X>
|
||||
static_variant(const X& v) {
|
||||
static_assert(
|
||||
impl::position<X, Types...>::pos != -1,
|
||||
"Type not in static_variant."
|
||||
);
|
||||
init(v);
|
||||
}
|
||||
~static_variant() {
|
||||
impl::storage_ops<0, Types...>::del(tag, storage);
|
||||
}
|
||||
template<typename X>
|
||||
void operator=(const X& v) {
|
||||
static_assert(
|
||||
impl::position<X, Types...>::pos != -1,
|
||||
"Type not in static_variant."
|
||||
);
|
||||
this->~static_variant();
|
||||
init(v);
|
||||
}
|
||||
template<typename X>
|
||||
X& get() {
|
||||
static_assert(
|
||||
impl::position<X, Types...>::pos != -1,
|
||||
"Type not in static_variant."
|
||||
);
|
||||
if(tag == impl::position<X, Types...>::pos) {
|
||||
return *reinterpret_cast<X*>(storage);
|
||||
} else {
|
||||
throw std::runtime_error(
|
||||
std::string("static_variant does not contain value of type ") + typeid(X).name()
|
||||
);
|
||||
}
|
||||
}
|
||||
template<typename X>
|
||||
const X& get() const {
|
||||
static_assert(
|
||||
impl::position<X, Types...>::pos != -1,
|
||||
"Type not in static_variant."
|
||||
);
|
||||
if(tag == impl::position<X, Types...>::pos) {
|
||||
return *reinterpret_cast<const X*>(storage);
|
||||
} else {
|
||||
throw std::runtime_error(
|
||||
std::string("static_variant does not contain value of type ") + typeid(X).name()
|
||||
);
|
||||
}
|
||||
}
|
||||
template<typename visitor>
|
||||
typename visitor::result_type visit(visitor& v) {
|
||||
return impl::storage_ops<0, Types...>::apply(tag, storage, v);
|
||||
}
|
||||
|
||||
template<typename visitor>
|
||||
typename visitor::result_type visit(const visitor& v) {
|
||||
return impl::storage_ops<0, Types...>::apply(tag, storage, v);
|
||||
}
|
||||
|
||||
template<typename visitor>
|
||||
typename visitor::result_type visit(visitor& v)const {
|
||||
return impl::storage_ops<0, Types...>::apply(tag, storage, v);
|
||||
}
|
||||
|
||||
template<typename visitor>
|
||||
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<typename Result>
|
||||
struct visitor {
|
||||
typedef Result result_type;
|
||||
};
|
||||
|
||||
} // namespace fc
|
||||
|
|
@ -9,6 +9,7 @@
|
|||
#include <map>
|
||||
#include <set>
|
||||
#include <fc/container/flat_fwd.hpp>
|
||||
#include <fc/static_variant.hpp>
|
||||
|
||||
namespace fc
|
||||
{
|
||||
|
|
@ -40,6 +41,9 @@ namespace fc
|
|||
template<typename T> void to_variant( const safe<T>& s, variant& v );
|
||||
template<typename T> void from_variant( const variant& v, safe<T>& s );
|
||||
|
||||
template<typename... T> void to_variant( const static_variant<T...>& s, variant& v );
|
||||
template<typename... T> void from_variant( const variant& v, static_variant<T...>& s );
|
||||
|
||||
void to_variant( const uint8_t& var, variant& vo );
|
||||
void from_variant( const variant& var, uint8_t& vo );
|
||||
void to_variant( const int8_t& var, variant& vo );
|
||||
|
|
@ -497,6 +501,49 @@ namespace fc
|
|||
from_variant( var, *vo );
|
||||
}
|
||||
}
|
||||
|
||||
struct from_static_variant
|
||||
{
|
||||
variant& var;
|
||||
from_static_variant( variant& dv ):var(dv){}
|
||||
|
||||
typedef void result_type;
|
||||
template<typename T> void operator()( const T& v )
|
||||
{
|
||||
to_variant( v, var );
|
||||
}
|
||||
};
|
||||
|
||||
struct to_static_variant
|
||||
{
|
||||
const variant& var;
|
||||
to_static_variant( const variant& dv ):var(dv){}
|
||||
|
||||
typedef void result_type;
|
||||
template<typename T> void operator()( T& v )
|
||||
{
|
||||
to_variant( var, v );
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename... T> void to_variant( const fc::static_variant<T...>& s, fc::variant& v )
|
||||
{
|
||||
variant tmp;
|
||||
variants vars(2);
|
||||
vars[0] = s.which();
|
||||
s.visit( from_static_variant(vars[1]) );
|
||||
v = std::move(vars);
|
||||
}
|
||||
template<typename... T> void from_variant( const fc::variant& v, fc::static_variant<T...>& s )
|
||||
{
|
||||
auto ar = v.get_array();
|
||||
if( ar.size() ) return;
|
||||
s.set_which( ar[0].as_uint64() );
|
||||
if( ar.size() < 1 ) return;
|
||||
s.visit( to_static_variant(ar[1]) );
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void to_variant( const safe<T>& s, variant& v ) { v = s.value; }
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue