BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface 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 
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 }
const std::string & get_string() const
Definition: variant.cpp:575
void operator()(const char *name) const
Definition: variant.hpp:54
An order-perserving dictionary of variant&#39;s.
Defines types and macros used to provide reflection.
to_variant_visitor(mutable_variant_object &mvo, const T &v, uint32_t max_depth)
Definition: variant.hpp:17
bool valid() const
Definition: optional.hpp:186
void from_variant(const variant &v, std::array< unsigned char, N > &bi, uint32_t max_depth=1)
Definition: variant.hpp:683
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
void to_variant(const std::array< unsigned char, N > &bi, variant &v, uint32_t max_depth=1)
Definition: variant.hpp:678
variant_object & get_object()
Definition: variant.cpp:554
static void to_variant(const T &v, fc::variant &vo, uint32_t max_depth)
Definition: variant.hpp:71
static void to_variant(const T &o, fc::variant &v, uint32_t max_depth=1)
Definition: variant.hpp:87
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
void operator()(const char *name) const
Definition: variant.hpp:23
defines visit functions for T Unless this is specialized, visit() will not be defined for T...
Definition: reflect.hpp:25
static void from_variant(const fc::variant &v, T &o, uint32_t max_depth=1)
Definition: variant.hpp:91
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
static void from_variant(const fc::variant &v, T &o, uint32_t max_depth)
Definition: variant.hpp:77
const variant_object & vo
Definition: variant.hpp:61
int64_t as_int64() const
Definition: variant.cpp:377
#define _FC_ASSERT(cond, msg)
Definition: variant.hpp:30
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
bool is_string() const
Definition: variant.cpp:314
Definition: api.hpp:15
const uint32_t _max_depth
Definition: variant.hpp:63
from_variant_visitor(const variant_object &_vo, T &v, uint32_t max_depth)
Definition: variant.hpp:48
An order-perserving dictionary of variant&#39;s.