#pragma once #include #include namespace fc { template void to_variant( const T& o, variant& v ); template void from_variant( const variant& v, T& o ); template class to_variant_visitor { public: to_variant_visitor( mutable_variant_object& mvo, const T& v ) :vo(mvo),val(v){} template void operator()( const char* name )const { this->add(vo,name,(val.*member)); } private: template void add( mutable_variant_object& vo, const char* name, const optional& v )const { if( v.valid() ) vo(name,*v); } template void add( mutable_variant_object& vo, const char* name, const M& v )const { vo(name,v); } mutable_variant_object& vo; const T& val; }; template class from_variant_visitor { public: from_variant_visitor( const variant_object& _vo, T& v ) :vo(_vo),val(v){} template void operator()( const char* name )const { auto itr = vo.find(name); if( itr != vo.end() ) from_variant( itr->value(), val.*member ); } const variant_object& vo; T& val; }; template struct if_enum { template static inline void to_variant( const T& v, fc::variant& vo ) { mutable_variant_object mvo; fc::reflector::visit( to_variant_visitor( mvo, v ) ); vo = fc::move(mvo); } template static inline void from_variant( const fc::variant& v, T& o ) { const variant_object& vo = v.get_object(); fc::reflector::visit( from_variant_visitor( vo, o ) ); } }; template<> struct if_enum { template static inline void to_variant( const T& o, fc::variant& v ) { v = fc::reflector::to_fc_string(o); } template static inline void from_variant( const fc::variant& v, T& o ) { if( v.is_string() ) o = fc::reflector::from_string( v.get_string().c_str() ); else o = fc::reflector::from_int( v.as_int64() ); } }; template void to_variant( const T& o, variant& v ) { if_enum::is_enum>::to_variant( o, v ); } template void from_variant( const variant& v, T& o ) { if_enum::is_enum>::from_variant( v, o ); } }