BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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
fc::variant::get_type
type_id get_type() const
Definition: variant.cpp:304
fc::variant_object
An order-perserving dictionary of variant's.
Definition: variant_object.hpp:20
fc::variant::array_type
@ array_type
Definition: variant.hpp:209
fc::mutable_variant_object
An order-perserving dictionary of variant's.
Definition: variant_object.hpp:108
fc::variant::visitor
Definition: variant.hpp:251
fc::raw::variant_packer::handle
virtual void handle(const uint64_t &v) const
Definition: raw_variant.hpp:23
fc::raw::variant_packer::handle
virtual void handle(const double &v) const
Definition: raw_variant.hpp:27
fc::mutable_variant_object::reserve
void reserve(size_t s)
Definition: variant_object.cpp:311
fc::raw::unpack
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:23
fc::raw::variant_packer::s
Stream & s
Definition: raw_variant.hpp:48
fc
Definition: api.hpp:15
variant_object.hpp
fc::variant_object::begin
iterator begin() const
Definition: variant_object.cpp:53
fc::variant::object_type
@ object_type
Definition: variant.hpp:210
fc::variant::string_type
@ string_type
Definition: variant.hpp:208
fc::mutable_variant_object::set
mutable_variant_object & set(string key, variant var)
Definition: variant_object.cpp:329
fc::variant::double_type
@ double_type
Definition: variant.hpp:206
fc::variant::null_type
@ null_type
Definition: variant.hpp:203
fc::variant::bool_type
@ bool_type
Definition: variant.hpp:207
fc::raw::variant_packer::handle
virtual void handle(const int64_t &v) const
Definition: raw_variant.hpp:19
fc::variants
std::vector< variant > variants
Definition: variant.hpp:170
fc::unsigned_int
Definition: varint.hpp:6
fc::variant_object::size
size_t size() const
Definition: variant_object.cpp:93
fc::raw::variant_packer::handle
virtual void handle(const variants &v) const
Definition: raw_variant.hpp:43
fc::raw::variant_packer::handle
virtual void handle() const
handles null_type variants
Definition: raw_variant.hpp:18
fc::variant::visit
void visit(const visitor &v) const
Definition: variant.cpp:271
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
fc::raw::variant_packer::handle
virtual void handle(const variant_object &v) const
Definition: raw_variant.hpp:39
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
fc::unsigned_int::value
uint64_t value
Definition: varint.hpp:17
exception.hpp
Defines exception's used by fc.
fc::raw::variant_packer::max_depth
const uint32_t max_depth
Definition: raw_variant.hpp:49
fc::variant::uint64_type
@ uint64_type
Definition: variant.hpp:205
raw_fwd.hpp
FC_MAX_PREALLOC_SIZE
#define FC_MAX_PREALLOC_SIZE
Definition: config.hpp:13
variant.hpp
fc::raw::variant_packer::handle
virtual void handle(const bool &v) const
Definition: raw_variant.hpp:31
fc::raw::variant_packer
Definition: raw_variant.hpp:11
fc::raw::variant_packer::handle
virtual void handle(const string &v) const
Definition: raw_variant.hpp:35
FC_THROW_EXCEPTION
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
Definition: exception.hpp:379
fc::variant_object::end
iterator end() const
Definition: variant_object.cpp:59
fc::raw::variant_packer::variant_packer
variant_packer(Stream &_s, uint32_t _max_depth)
Definition: raw_variant.hpp:14
fc::variant::int64_type
@ int64_type
Definition: variant.hpp:204
fc::raw::pack
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11