BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
variant.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <fc/reflect/reflect.hpp>
3 #include <fc/variant_object.hpp>
4 
5 namespace fc
6 {
7  template<typename T>
8  void to_variant( const T& o, variant& v, uint32_t max_depth );
9  template<typename T>
10  void from_variant( const variant& v, T& o, uint32_t max_depth );
11 
12 
13  template<typename T>
15  {
16  public:
17  to_variant_visitor( mutable_variant_object& mvo, const T& v, uint32_t max_depth )
18  :vo(mvo),val(v),_max_depth(max_depth - 1) {
19  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
20  }
21 
22  template<typename Member, class Class, Member (Class::*member)>
23  void operator()( const char* name )const
24  {
25  this->add(vo,name,(val.*member));
26  }
27 
28  private:
29  template<typename M>
30  void add( mutable_variant_object& vo, const char* name, const optional<M>& v )const
31  {
32  if( v.valid() )
33  vo(name, variant( *v, _max_depth ));
34  }
35  template<typename M>
36  void add( mutable_variant_object& vo, const char* name, const M& v )const
37  { vo(name, variant( v, _max_depth )); }
38 
39  mutable_variant_object& vo;
40  const T& val;
41  const uint32_t _max_depth;
42  };
43 
44  template<typename T>
46  {
47  public:
48  from_variant_visitor( const variant_object& _vo, T& v, uint32_t max_depth )
49  :vo(_vo),val(v),_max_depth(max_depth - 1) {
50  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
51  }
52 
53  template<typename Member, class Class, Member (Class::*member)>
54  void operator()( const char* name )const
55  {
56  auto itr = vo.find(name);
57  if( itr != vo.end() )
58  from_variant( itr->value(), val.*member, _max_depth );
59  }
60 
62  T& val;
63  const uint32_t _max_depth;
64  };
65 
66  template<typename T, typename Dummy = void>
67  struct if_enum;
68  template<typename T>
69  struct if_enum<T, std::enable_if_t<!std::is_enum<T>::value>>
70  {
71  static inline void to_variant( const T& v, fc::variant& vo, uint32_t max_depth )
72  {
74  fc::reflector<T>::visit( to_variant_visitor<T>( mvo, v, max_depth ) );
75  vo = std::move(mvo);
76  }
77  static inline void from_variant( const fc::variant& v, T& o, uint32_t max_depth )
78  {
79  const variant_object& vo = v.get_object();
80  fc::reflector<T>::visit( from_variant_visitor<T>( vo, o, max_depth ) );
81  }
82  };
83 
84  template<typename T>
85  struct if_enum<T, std::enable_if_t<std::is_enum<T>::value>>
86  {
87  static inline void to_variant( const T& o, fc::variant& v, uint32_t max_depth = 1 )
88  {
90  }
91  static inline void from_variant( const fc::variant& v, T& o, uint32_t max_depth = 1 )
92  {
93  if( v.is_string() )
94  o = fc::reflector<T>::from_string( v.get_string().c_str() );
95  else
97  }
98  };
99 
100 
101  template<typename T>
102  void to_variant( const T& o, variant& v, uint32_t max_depth )
103  {
104  if_enum<T>::to_variant( o, v, max_depth );
105  }
106 
107  template<typename T>
108  void from_variant( const variant& v, T& o, uint32_t max_depth )
109  {
110  if_enum<T>::from_variant( v, o, max_depth );
111  }
112 }
fc::to_variant_visitor
Definition: variant.hpp:14
fc::to_variant_visitor::to_variant_visitor
to_variant_visitor(mutable_variant_object &mvo, const T &v, uint32_t max_depth)
Definition: variant.hpp:17
fc::variant_object::find
iterator find(const string &key) const
Definition: variant_object.cpp:64
fc::to_variant_visitor::operator()
void operator()(const char *name) const
Definition: variant.hpp:23
fc::variant_object
An order-perserving dictionary of variant's.
Definition: variant_object.hpp:20
fc::from_variant_visitor::_max_depth
const uint32_t _max_depth
Definition: variant.hpp:63
fc::variant::is_string
bool is_string() const
Definition: variant.cpp:314
fc::mutable_variant_object
An order-perserving dictionary of variant's.
Definition: variant_object.hpp:108
fc
Definition: api.hpp:15
fc::variant::get_string
const std::string & get_string() const
Definition: variant.cpp:575
variant_object.hpp
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
reflect.hpp
Defines types and macros used to provide reflection.
fc::from_variant_visitor::val
T & val
Definition: variant.hpp:62
fc::optional::valid
bool valid() const
Definition: optional.hpp:186
fc::from_variant_visitor
Definition: variant.hpp:45
fc::if_enum< T, std::enable_if_t<!std::is_enum< T >::value > >::to_variant
static void to_variant(const T &v, fc::variant &vo, uint32_t max_depth)
Definition: variant.hpp:71
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::to_variant
void to_variant(const std::array< unsigned char, N > &bi, variant &v, uint32_t max_depth=1)
Definition: variant.hpp:679
_FC_ASSERT
#define _FC_ASSERT(cond, msg)
Definition: variant.hpp:30
fc::if_enum< T, std::enable_if_t< std::is_enum< T >::value > >::to_variant
static void to_variant(const T &o, fc::variant &v, uint32_t max_depth=1)
Definition: variant.hpp:87
fc::from_variant_visitor::operator()
void operator()(const char *name) const
Definition: variant.hpp:54
fc::variant::as_int64
int64_t as_int64() const
Definition: variant.cpp:377
fc::from_variant
void from_variant(const variant &v, std::array< unsigned char, N > &bi, uint32_t max_depth=1)
Definition: variant.hpp:684
fc::variant::get_object
variant_object & get_object()
Definition: variant.cpp:554
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
fc::if_enum< T, std::enable_if_t<!std::is_enum< T >::value > >::from_variant
static void from_variant(const fc::variant &v, T &o, uint32_t max_depth)
Definition: variant.hpp:77
fc::from_variant_visitor::vo
const variant_object & vo
Definition: variant.hpp:61
std
Definition: zeroed_array.hpp:76
fc::if_enum< T, std::enable_if_t< std::is_enum< T >::value > >::from_variant
static void from_variant(const fc::variant &v, T &o, uint32_t max_depth=1)
Definition: variant.hpp:91
fc::reflector
defines visit functions for T Unless this is specialized, visit() will not be defined for T.
Definition: reflect.hpp:25
fc::optional
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
fc::from_variant_visitor::from_variant_visitor
from_variant_visitor(const variant_object &_vo, T &v, uint32_t max_depth)
Definition: variant.hpp:48
fc::variant_object::end
iterator end() const
Definition: variant_object.cpp:59
fc::if_enum
Definition: variant.hpp:67