2013-06-05 19:19:00 +00:00
|
|
|
#pragma once
|
|
|
|
|
#include <fc/reflect/reflect.hpp>
|
|
|
|
|
#include <fc/variant_object.hpp>
|
|
|
|
|
|
|
|
|
|
namespace fc
|
|
|
|
|
{
|
|
|
|
|
template<typename T>
|
2018-03-19 16:35:57 +00:00
|
|
|
void to_variant( const T& o, variant& v, uint32_t max_depth );
|
2013-06-05 19:19:00 +00:00
|
|
|
template<typename T>
|
2018-03-19 16:35:57 +00:00
|
|
|
void from_variant( const variant& v, T& o, uint32_t max_depth );
|
2013-06-05 19:19:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
class to_variant_visitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-03-19 16:35:57 +00:00
|
|
|
to_variant_visitor( mutable_variant_object& mvo, const T& v, uint32_t max_depth )
|
|
|
|
|
:vo(mvo),val(v),_max_depth(max_depth - 1) {
|
|
|
|
|
_FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
|
|
|
|
|
}
|
2013-06-05 19:19:00 +00:00
|
|
|
|
|
|
|
|
template<typename Member, class Class, Member (Class::*member)>
|
|
|
|
|
void operator()( const char* name )const
|
|
|
|
|
{
|
2015-03-31 15:31:56 +00:00
|
|
|
this->add(vo,name,(val.*member));
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-31 15:31:56 +00:00
|
|
|
private:
|
|
|
|
|
template<typename M>
|
|
|
|
|
void add( mutable_variant_object& vo, const char* name, const optional<M>& v )const
|
|
|
|
|
{
|
|
|
|
|
if( v.valid() )
|
2018-03-19 16:35:57 +00:00
|
|
|
vo(name, variant( *v, _max_depth ));
|
2015-03-31 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
template<typename M>
|
|
|
|
|
void add( mutable_variant_object& vo, const char* name, const M& v )const
|
2018-03-19 16:35:57 +00:00
|
|
|
{ vo(name, variant( v, _max_depth )); }
|
2015-03-31 15:31:56 +00:00
|
|
|
|
2013-06-05 19:19:00 +00:00
|
|
|
mutable_variant_object& vo;
|
|
|
|
|
const T& val;
|
2018-03-19 16:35:57 +00:00
|
|
|
const uint32_t _max_depth;
|
2013-06-05 19:19:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
class from_variant_visitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-03-19 16:35:57 +00:00
|
|
|
from_variant_visitor( const variant_object& _vo, T& v, uint32_t max_depth )
|
|
|
|
|
:vo(_vo),val(v),_max_depth(max_depth - 1) {
|
|
|
|
|
_FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
|
|
|
|
|
}
|
2013-06-05 19:19:00 +00:00
|
|
|
|
|
|
|
|
template<typename Member, class Class, Member (Class::*member)>
|
|
|
|
|
void operator()( const char* name )const
|
|
|
|
|
{
|
|
|
|
|
auto itr = vo.find(name);
|
|
|
|
|
if( itr != vo.end() )
|
2018-03-19 16:35:57 +00:00
|
|
|
from_variant( itr->value(), val.*member, _max_depth );
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const variant_object& vo;
|
|
|
|
|
T& val;
|
2018-03-19 16:35:57 +00:00
|
|
|
const uint32_t _max_depth;
|
2013-06-05 19:19:00 +00:00
|
|
|
};
|
|
|
|
|
|
2018-03-19 16:35:57 +00:00
|
|
|
template<typename IsEnum=fc::false_type>
|
|
|
|
|
struct if_enum
|
2013-06-05 19:19:00 +00:00
|
|
|
{
|
2018-03-19 16:35:57 +00:00
|
|
|
template<typename T>
|
|
|
|
|
static inline void to_variant( const T& v, fc::variant& vo, uint32_t max_depth )
|
|
|
|
|
{
|
2013-06-05 19:19:00 +00:00
|
|
|
mutable_variant_object mvo;
|
2018-03-19 16:35:57 +00:00
|
|
|
fc::reflector<T>::visit( to_variant_visitor<T>( mvo, v, max_depth ) );
|
2013-06-05 19:19:00 +00:00
|
|
|
vo = fc::move(mvo);
|
2018-03-19 16:35:57 +00:00
|
|
|
}
|
|
|
|
|
template<typename T>
|
|
|
|
|
static inline void from_variant( const fc::variant& v, T& o, uint32_t max_depth )
|
|
|
|
|
{
|
2013-06-05 19:19:00 +00:00
|
|
|
const variant_object& vo = v.get_object();
|
2018-03-19 16:35:57 +00:00
|
|
|
fc::reflector<T>::visit( from_variant_visitor<T>( vo, o, max_depth ) );
|
|
|
|
|
}
|
2013-06-05 19:19:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct if_enum<fc::true_type>
|
|
|
|
|
{
|
|
|
|
|
template<typename T>
|
2018-03-19 16:35:57 +00:00
|
|
|
static inline void to_variant( const T& o, fc::variant& v, uint32_t max_depth = 1 )
|
2013-06-05 19:19:00 +00:00
|
|
|
{
|
2014-10-31 20:30:18 +00:00
|
|
|
v = fc::reflector<T>::to_fc_string(o);
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
template<typename T>
|
2018-03-19 16:35:57 +00:00
|
|
|
static inline void from_variant( const fc::variant& v, T& o, uint32_t max_depth = 1 )
|
2013-06-05 19:19:00 +00:00
|
|
|
{
|
|
|
|
|
if( v.is_string() )
|
|
|
|
|
o = fc::reflector<T>::from_string( v.get_string().c_str() );
|
2016-08-26 20:27:16 +00:00
|
|
|
else
|
|
|
|
|
o = fc::reflector<T>::from_int( v.as_int64() );
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2018-03-19 16:35:57 +00:00
|
|
|
void to_variant( const T& o, variant& v, uint32_t max_depth )
|
2013-06-05 19:19:00 +00:00
|
|
|
{
|
2018-03-19 16:35:57 +00:00
|
|
|
if_enum<typename fc::reflector<T>::is_enum>::to_variant( o, v, max_depth );
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2018-03-19 16:35:57 +00:00
|
|
|
void from_variant( const variant& v, T& o, uint32_t max_depth )
|
2013-06-05 19:19:00 +00:00
|
|
|
{
|
2018-03-19 16:35:57 +00:00
|
|
|
if_enum<typename fc::reflector<T>::is_enum>::from_variant( v, o, max_depth );
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
}
|