BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
raw_variant.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <algorithm>
4 #include <fc/io/raw_fwd.hpp>
5 #include <fc/variant_object.hpp>
6 #include <fc/variant.hpp>
7 
8 namespace fc { namespace raw {
9 
10  template<typename Stream>
12  {
13  public:
14  variant_packer( Stream& _s, uint32_t _max_depth ):s(_s),max_depth(_max_depth - 1)
15  {
16  FC_ASSERT( _max_depth > 0 );
17  }
18  virtual void handle()const { }
19  virtual void handle( const int64_t& v )const
20  {
21  fc::raw::pack( s, v, max_depth );
22  }
23  virtual void handle( const uint64_t& v )const
24  {
25  fc::raw::pack( s, v, max_depth );
26  }
27  virtual void handle( const double& v )const
28  {
29  FC_THROW_EXCEPTION( invalid_arg_exception, "Can't pack double!" );
30  }
31  virtual void handle( const bool& v )const
32  {
33  fc::raw::pack( s, v, max_depth );
34  }
35  virtual void handle( const string& v )const
36  {
37  fc::raw::pack( s, v, max_depth );
38  }
39  virtual void handle( const variant_object& v)const
40  {
41  fc::raw::pack( s, v, max_depth );
42  }
43  virtual void handle( const variants& v)const
44  {
45  fc::raw::pack( s, v, max_depth );
46  }
47 
48  Stream& s;
49  const uint32_t max_depth;
50 
51  };
52 
53 
54  template<typename Stream>
55  inline void pack( Stream& s, const variant& v, uint32_t _max_depth )
56  {
57  FC_ASSERT( _max_depth > 0 );
58  --_max_depth;
59  pack( s, uint8_t(v.get_type()), _max_depth );
60  v.visit( variant_packer<Stream>( s, _max_depth ) );
61  }
62  template<typename Stream>
63  inline void unpack( Stream& s, variant& v, uint32_t _max_depth )
64  {
65  FC_ASSERT( _max_depth > 0 );
66  --_max_depth;
67  uint8_t t;
68  unpack( s, t, _max_depth );
69  switch( t )
70  {
71  case variant::null_type:
72  return;
74  {
75  int64_t val;
76  raw::unpack( s, val, _max_depth );
77  v = val;
78  return;
79  }
81  {
82  uint64_t val;
83  raw::unpack( s, val, _max_depth );
84  v = val;
85  return;
86  }
88  {
89  FC_THROW_EXCEPTION( invalid_arg_exception, "Can't unpack double!" );
90  }
91  case variant::bool_type:
92  {
93  bool val;
94  raw::unpack( s, val, _max_depth );
95  v = val;
96  return;
97  }
99  {
100  std::string val;
101  raw::unpack( s, val, _max_depth );
102  v = std::move(val);
103  return;
104  }
105  case variant::array_type:
106  {
107  variants val;
108  raw::unpack( s, val, _max_depth );
109  v = std::move(val);
110  return;
111  }
113  {
114  variant_object val;
115  raw::unpack( s, val, _max_depth );
116  v = std::move(val);
117  return;
118  }
119  default:
120  FC_THROW_EXCEPTION( parse_error_exception, "Unknown Variant Type ${t}", ("t", t) );
121  }
122  }
123 
124  template<typename Stream>
125  inline void pack( Stream& s, const variant_object& v, uint32_t _max_depth )
126  {
127  FC_ASSERT( _max_depth > 0 );
128  --_max_depth;
129  unsigned_int vs = v.size();
130  pack( s, vs, _max_depth );
131  for( auto itr = v.begin(); itr != v.end(); ++itr )
132  {
133  pack( s, itr->key(), _max_depth );
134  pack( s, itr->value(), _max_depth );
135  }
136  }
137  template<typename Stream>
138  inline void unpack( Stream& s, variant_object& v, uint32_t _max_depth )
139  {
140  FC_ASSERT( _max_depth > 0 );
141  --_max_depth;
142  unsigned_int vs;
143  unpack( s, vs, _max_depth );
145  mvo.reserve( std::min( vs.value, static_cast<uint64_t>(FC_MAX_PREALLOC_SIZE) ) );
146  for( uint32_t i = 0; i < vs.value; ++i )
147  {
148  std::string key;
149  fc::variant value;
150  fc::raw::unpack( s, key, _max_depth );
151  fc::raw::unpack( s, value, _max_depth );
152  mvo.set( std::move(key), std::move(value) );
153  }
154  v = std::move(mvo);
155  }
156 
157 } } // fc::raw
virtual void handle(const variant_object &v) const
Definition: raw_variant.hpp:39
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
virtual void handle(const string &v) const
Definition: raw_variant.hpp:35
An order-perserving dictionary of variant&#39;s.
std::vector< variant > variants
Definition: variant.hpp:170
variant_packer(Stream &_s, uint32_t _max_depth)
Definition: raw_variant.hpp:14
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:23
virtual void handle(const variants &v) const
Definition: raw_variant.hpp:43
type_id get_type() const
Definition: variant.cpp:304
const uint32_t max_depth
Definition: raw_variant.hpp:49
iterator begin() const
virtual void handle(const int64_t &v) const
Definition: raw_variant.hpp:19
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
Definition: exception.hpp:378
void visit(const visitor &v) const
Definition: variant.cpp:271
Defines exception&#39;s used by fc.
virtual void handle(const double &v) const
Definition: raw_variant.hpp:27
iterator end() const
#define FC_MAX_PREALLOC_SIZE
Definition: config.hpp:13
mutable_variant_object & set(string key, variant var)
Definition: api.hpp:15
virtual void handle(const bool &v) const
Definition: raw_variant.hpp:31
virtual void handle(const uint64_t &v) const
Definition: raw_variant.hpp:23
uint64_t value
Definition: varint.hpp:17
virtual void handle() const
handles null_type variants
Definition: raw_variant.hpp:18
An order-perserving dictionary of variant&#39;s.
size_t size() const