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 
3 #include <array>
4 #include <cstddef>
5 #include <deque>
6 #include <map>
7 #include <memory>
8 #include <set>
9 #include <unordered_map>
10 #include <unordered_set>
11 #include <vector>
12 
13 #include <string.h> // memset
14 
15 #include <fc/optional.hpp>
16 #include <fc/uint128.hpp>
18 #include <fc/crypto/hex.hpp>
19 #include <boost/endian/buffers.hpp>
20 #include <boost/multi_index_container_fwd.hpp>
21 
22 #ifdef FC_ASSERT
23 #define _FC_ASSERT(...) FC_ASSERT( __VA_ARGS__ )
24 #else
25 // poor man's FC_ASSERT, want to avoid recursive inclusion of exception.hpp
26 namespace fc
27 {
28 void throw_assertion_failure( const std::string& message );
29 }
30 #define _FC_ASSERT( cond, msg ) { if( !(cond) ) { char t[] = #cond ": " msg; fc::throw_assertion_failure( t ); } }
31 #endif
32 
33 namespace fc
34 {
48  class variant;
49  class variant_object;
50  class mutable_variant_object;
51  class time_point;
52  class time_point_sec;
53  class microseconds;
54  template<typename T> struct safe;
55  template<typename... Types>
56  class static_variant;
57 
58  struct blob { std::vector<char> data; };
59 
60  void to_variant( const blob& var, variant& vo, uint32_t max_depth = 1);
61  void from_variant( const variant& var, blob& vo, uint32_t max_depth = 1 );
62 
63 
64  template<typename T, typename... Args> void to_variant( const boost::multi_index_container<T,Args...>& s, variant& v, uint32_t max_depth );
65  template<typename T, typename... Args> void from_variant( const variant& v, boost::multi_index_container<T,Args...>& s, uint32_t max_depth );
66 
67  template<typename T> void to_variant( const safe<T>& s, variant& v, uint32_t max_depth );
68  template<typename T> void from_variant( const variant& v, safe<T>& s, uint32_t max_depth );
69  template<typename T> void to_variant( const std::unique_ptr<T>& s, variant& v, uint32_t max_depth );
70  template<typename T> void from_variant( const variant& v, std::unique_ptr<T>& s, uint32_t max_depth );
71 
72  template<typename... T> void to_variant( const static_variant<T...>& s, variant& v, uint32_t max_depth );
73  template<typename... T> void from_variant( const variant& v, static_variant<T...>& s, uint32_t max_depth );
74 
75  void to_variant( const uint8_t& var, variant& vo, uint32_t max_depth = 1 );
76  void from_variant( const variant& var, uint8_t& vo, uint32_t max_depth = 1 );
77  void to_variant( const int8_t& var, variant& vo, uint32_t max_depth = 1 );
78  void from_variant( const variant& var, int8_t& vo, uint32_t max_depth = 1 );
79 
80  void to_variant( const uint16_t& var, variant& vo, uint32_t max_depth = 1 );
81  void from_variant( const variant& var, uint16_t& vo, uint32_t max_depth = 1 );
82  void to_variant( const int16_t& var, variant& vo, uint32_t max_depth = 1 );
83  void from_variant( const variant& var, int16_t& vo, uint32_t max_depth = 1 );
84 
85  void to_variant( const uint32_t& var, variant& vo, uint32_t max_depth = 1 );
87  void from_variant( const variant& var, uint32_t& vo, uint32_t max_depth = 1 );
88  void to_variant( const int32_t& var, variant& vo, uint32_t max_depth = 1 );
90  void from_variant( const variant& var, int32_t& vo, uint32_t max_depth = 1 );
91 
92  void to_variant( const uint64_t& var, variant& vo, uint32_t max_depth = 1 );
93  void to_variant( const int64_t& var, variant& vo, uint32_t max_depth = 1 );
94 
95  void to_variant( const bool& var, variant& vo, uint32_t max_depth = 1 );
96 
97  void to_variant( const variant_object& var, variant& vo, uint32_t max_depth );
98  void from_variant( const variant& var, variant_object& vo, uint32_t max_depth );
99  void to_variant( const mutable_variant_object& var, variant& vo, uint32_t max_depth );
100  void from_variant( const variant& var, mutable_variant_object& vo, uint32_t max_depth );
101  void to_variant( const std::vector<char>& var, variant& vo, uint32_t max_depth = 1 );
102  void from_variant( const variant& var, std::vector<char>& vo, uint32_t max_depth = 1 );
103 
104  template<typename K, typename T>
105  void to_variant( const std::unordered_map<K,T>& var, variant& vo, uint32_t max_depth );
106  template<typename K, typename T>
107  void from_variant( const variant& var, std::unordered_map<K,T>& vo, uint32_t max_depth );
108 
109  template<typename K, typename... T>
110  void to_variant( const fc::flat_map<K,T...>& var, variant& vo, uint32_t max_depth );
111  template<typename K, typename T, typename... A>
112  void from_variant(const variant& var, flat_map<K, T, A...>& vo, uint32_t max_depth );
113 
114  template<typename K, typename T>
115  void to_variant( const std::map<K,T>& var, variant& vo, uint32_t max_depth );
116  template<typename K, typename T>
117  void from_variant( const variant& var, std::map<K,T>& vo, uint32_t max_depth );
118  template<typename K, typename T>
119  void to_variant( const std::multimap<K,T>& var, variant& vo, uint32_t max_depth );
120  template<typename K, typename T>
121  void from_variant( const variant& var, std::multimap<K,T>& vo, uint32_t max_depth );
122 
123 
124  template<typename T>
125  void to_variant( const std::unordered_set<T>& var, variant& vo, uint32_t max_depth );
126  template<typename T>
127  void from_variant( const variant& var, std::unordered_set<T>& vo, uint32_t max_depth );
128 
129  template<typename T>
130  void to_variant( const std::deque<T>& var, variant& vo, uint32_t max_depth );
131  template<typename T>
132  void from_variant( const variant& var, std::deque<T>& vo, uint32_t max_depth );
133 
134  template<typename T, typename... A>
135  void to_variant( const fc::flat_set<T, A...>& var, variant& vo, uint32_t max_depth );
136  template<typename T, typename... A>
137  void from_variant( const variant& var, fc::flat_set<T, A...>& vo, uint32_t max_depth );
138 
139  template<typename T>
140  void to_variant( const std::set<T>& var, variant& vo, uint32_t max_depth );
141  template<typename T>
142  void from_variant( const variant& var, std::set<T>& vo, uint32_t max_depth );
143 
144  void to_variant( const time_point& var, variant& vo, uint32_t max_depth );
145  void from_variant( const variant& var, time_point& vo, uint32_t max_depth );
146 
147  void to_variant( const time_point_sec& var, variant& vo, uint32_t max_depth );
148  void from_variant( const variant& var, time_point_sec& vo, uint32_t max_depth );
149 
150  void to_variant( const microseconds& input_microseconds, variant& output_variant, uint32_t max_depth );
151  void from_variant( const variant& input_variant, microseconds& output_microseconds, uint32_t max_depth );
152 
153  void to_variant( const uint128_t& var, variant& vo, uint32_t max_depth = 1 );
154  void from_variant( const variant& var, uint128_t& vo, uint32_t max_depth = 1 );
155 
156  #if defined(__APPLE__) or defined(__OpenBSD__)
157  void to_variant( size_t s, variant& v, uint32_t max_depth = 1 );
158  #elif !defined(_WIN32)
159  void to_variant( long long int s, variant& v, uint32_t max_depth = 1 );
160  void to_variant( unsigned long long int s, variant& v, uint32_t max_depth = 1 );
161  #endif
162  void to_variant( const std::string& s, variant& v, uint32_t max_depth = 1 );
163 
164  template<typename T>
165  void to_variant( const std::shared_ptr<T>& var, variant& vo, uint32_t max_depth );
166 
167  template<typename T>
168  void from_variant( const variant& var, std::shared_ptr<T>& vo, uint32_t max_depth );
169 
170  typedef std::vector<variant> variants;
171  template<typename A, typename B>
172  void to_variant( const std::pair<A,B>& t, variant& v, uint32_t max_depth );
173  template<typename A, typename B>
174  void from_variant( const variant& v, std::pair<A,B>& p, uint32_t max_depth );
175 
176  template<boost::endian::order O, class T, std::size_t N, boost::endian::align A>
177  void to_variant( const boost::endian::endian_buffer<O,T,N,A>& var, variant& vo, uint32_t max_depth )
178  {
179  to_variant( var.value(), vo, max_depth );
180  }
181  template<boost::endian::order O, class T, std::size_t N, boost::endian::align A>
182  void from_variant( const variant& var, boost::endian::endian_buffer<O,T,N,A>& vo, uint32_t max_depth )
183  {
184  T tmp;
185  from_variant( var, tmp, max_depth );
186  vo = tmp;
187  }
188 
198  class variant
199  {
200  public:
201  enum type_id
202  {
203  null_type = 0,
204  int64_type = 1,
205  uint64_type = 2,
206  double_type = 3,
207  bool_type = 4,
208  string_type = 5,
209  array_type = 6,
210  object_type = 7,
211  blob_type = 8
212  };
213 
215  variant();
217  variant( std::nullptr_t, uint32_t max_depth = 1 );
218 
220  variant( const char* str, uint32_t max_depth = 1 );
221  variant( char* str, uint32_t max_depth = 1 );
222  variant( wchar_t* str, uint32_t max_depth = 1 );
223  variant( const wchar_t* str, uint32_t max_depth = 1 );
224  variant( float val, uint32_t max_depth = 1 );
225  variant( uint8_t val, uint32_t max_depth = 1 );
226  variant( int8_t val, uint32_t max_depth = 1 );
227  variant( uint16_t val, uint32_t max_depth = 1 );
228  variant( int16_t val, uint32_t max_depth = 1 );
229  variant( uint32_t val, uint32_t max_depth = 1 );
230  variant( int32_t val, uint32_t max_depth = 1 );
231  variant( uint64_t val, uint32_t max_depth = 1 );
232 #if defined(__APPLE__) or defined(__OpenBSD__)
233  variant( size_t val, uint32_t max_depth = 1 );
234 #endif
235  variant( int64_t val, uint32_t max_depth = 1 );
236  variant( double val, uint32_t max_depth = 1 );
237  variant( bool val, uint32_t max_depth = 1 );
238  variant( blob val, uint32_t max_depth = 1 );
239  variant( std::string val, uint32_t max_depth = 1 );
240  variant( variant_object, uint32_t max_depth = 1 );
241  variant( mutable_variant_object, uint32_t max_depth = 1 );
242  variant( variants, uint32_t max_depth = 1 );
243  variant( const variant&, uint32_t max_depth = 1 );
244  variant( variant&&, uint32_t max_depth = 1 );
245  ~variant();
246 
250  class visitor
251  {
252  public:
253  virtual ~visitor(){}
255  virtual void handle()const = 0;
256  virtual void handle( const int64_t& v )const = 0;
257  virtual void handle( const uint64_t& v )const = 0;
258  virtual void handle( const double& v )const = 0;
259  virtual void handle( const bool& v )const = 0;
260  virtual void handle( const std::string& v )const = 0;
261  virtual void handle( const variant_object& v)const = 0;
262  virtual void handle( const variants& v)const = 0;
263  };
264 
265  void visit( const visitor& v )const;
266 
267  type_id get_type()const;
268 
269  bool is_null()const;
270  bool is_string()const;
271  bool is_bool()const;
272  bool is_int64()const;
273  bool is_uint64()const;
274  bool is_double()const;
275  bool is_object()const;
276  bool is_array()const;
277  bool is_blob()const;
281  bool is_numeric()const;
285  bool is_integer()const;
286 
287  int64_t as_int64()const;
288  uint64_t as_uint64()const;
289  bool as_bool()const;
290  double as_double()const;
291 
292  blob& get_blob();
293  const blob& get_blob()const;
294  blob as_blob()const;
295 
299  std::string as_string()const;
300 
302  const std::string& get_string()const;
303 
305  variants& get_array();
306 
308  const variants& get_array()const;
309 
311  variant_object& get_object();
312 
314  const variant_object& get_object()const;
315 
317  const variant& operator[]( const char* )const;
319  const variant& operator[]( size_t pos )const;
321  size_t size()const;
322 
335  template<typename T>
336  T as( uint32_t max_depth )const
337  {
338  T tmp;
339  from_variant( *this, tmp, max_depth );
340  return tmp;
341  }
342 
343  template<typename T>
344  void as( T& v, uint32_t max_depth )const
345  {
346  from_variant( *this, v, max_depth );
347  }
348 
349  variant& operator=( variant&& v );
350  variant& operator=( const variant& v );
351 
352  template<typename T>
353  variant& operator=( T&& v )
354  {
355  return *this = variant( std::forward<T>(v) );
356  }
357 
358  template<typename T>
359  variant( const optional<T>& v, uint32_t max_depth )
360  {
361  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
362  memset( this, 0, sizeof(*this) );
363  if( v.valid() ) *this = variant( *v, max_depth - 1 );
364  }
365 
366  template<typename T>
367  variant( const T& val, uint32_t max_depth );
368 
369 
370  void clear();
371  private:
372  void init();
373  double _data;
374  char _type[sizeof(void*)];
375  };
377 
379  void from_variant( const variant& var, std::string& vo, uint32_t max_depth = 1 );
381  void from_variant( const variant& var, variants& vo, uint32_t max_depth );
382  void from_variant( const variant& var, variant& vo, uint32_t max_depth );
384  void from_variant( const variant& var, int64_t& vo, uint32_t max_depth = 1 );
386  void from_variant( const variant& var, uint64_t& vo, uint32_t max_depth = 1 );
388  void from_variant( const variant& var, bool& vo, uint32_t max_depth = 1 );
390  void from_variant( const variant& var, double& vo, uint32_t max_depth = 1 );
392  void from_variant( const variant& var, float& vo, uint32_t max_depth = 1 );
394  template<typename T>
395  void from_variant( const variant& var, optional<T>& vo, uint32_t max_depth )
396  {
397  if( var.is_null() ) vo = optional<T>();
398  else
399  {
400  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
401  vo = T();
402  from_variant( var, *vo, max_depth - 1 );
403  }
404  }
405  template<typename T>
406  void to_variant( const std::unordered_set<T>& var, variant& vo, uint32_t max_depth )
407  {
408  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
409  std::vector<variant> vars(var.size());
410  size_t i = 0;
411  for( const auto& item : var )
412  vars[i++] = variant( item, max_depth - 1 );
413  vo = vars;
414  }
415  template<typename T>
416  void from_variant( const variant& var, std::unordered_set<T>& vo, uint32_t max_depth )
417  {
418  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
419  const variants& vars = var.get_array();
420  vo.clear();
421  vo.reserve( vars.size() );
422  for( const auto& item : vars )
423  vo.insert( item.as<T>( max_depth - 1 ) );
424  }
425 
426 
427  template<typename K, typename T>
428  void to_variant( const std::unordered_map<K, T>& var, variant& vo, uint32_t max_depth )
429  {
430  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
431  std::vector< variant > vars(var.size());
432  size_t i = 0;
433  for( const auto& key_value : var )
434  vars[i++] = fc::variant( key_value, max_depth - 1 );
435  vo = vars;
436  }
437  template<typename K, typename T>
438  void from_variant( const variant& var, std::unordered_map<K, T>& vo, uint32_t max_depth )
439  {
440  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
441  const variants& vars = var.get_array();
442  vo.clear();
443  vo.reserve( vars.size() );
444  for( const auto& item : vars )
445  vo.insert( item.as< std::pair<K,T> >( max_depth - 1 ) );
446  }
447  template<typename K, typename T>
448  void to_variant( const std::map<K, T>& var, variant& vo, uint32_t max_depth )
449  {
450  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
451  std::vector< variant > vars(var.size());
452  size_t i = 0;
453  for( const auto& key_value : var )
454  vars[i++] = fc::variant( key_value, max_depth - 1 );
455  vo = vars;
456  }
457  template<typename K, typename T>
458  void from_variant( const variant& var, std::map<K, T>& vo, uint32_t max_depth )
459  {
460  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
461  const variants& vars = var.get_array();
462  vo.clear();
463  for( auto item : vars )
464  vo.insert( item.as< std::pair<K,T> >( max_depth - 1 ) );
465  }
466 
467  template<typename K, typename T>
468  void to_variant( const std::multimap<K, T>& var, variant& vo, uint32_t max_depth )
469  {
470  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
471  std::vector< variant > vars(var.size());
472  size_t i = 0;
473  for( const auto& key_value : var )
474  vars[i++] = fc::variant( key_value, max_depth - 1 );
475  vo = vars;
476  }
477  template<typename K, typename T>
478  void from_variant( const variant& var, std::multimap<K, T>& vo, uint32_t max_depth )
479  {
480  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
481  const variants& vars = var.get_array();
482  vo.clear();
483  for( auto item : vars )
484  vo.insert( item.as< std::pair<K,T> >( max_depth - 1 ) );
485  }
486 
487 
488  template<typename T>
489  void to_variant( const std::set<T>& var, variant& vo, uint32_t max_depth )
490  {
491  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
492  std::vector<variant> vars(var.size());
493  size_t i = 0;
494  for( const auto& item : var )
495  vars[i++] = fc::variant( item, max_depth - 1 );
496  vo = vars;
497  }
498  template<typename T>
499  void from_variant( const variant& var, std::set<T>& vo, uint32_t max_depth )
500  {
501  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
502  const variants& vars = var.get_array();
503  vo.clear();
504  for( const auto& item : vars )
505  vo.insert( item.as<T>( max_depth - 1 ) );
506  }
507 
509  template<typename T>
510  void from_variant( const variant& var, std::deque<T>& dest, uint32_t max_depth )
511  {
512  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
513  const variants& vars = var.get_array();
514  dest.clear();
515  dest.resize( vars.size() );
516  for( const auto& item : vars )
517  dest.push_back( item.as<T>( max_depth - 1 ) );
518  }
519 
521  template<typename T>
522  void to_variant( const std::deque<T>& src, variant& v, uint32_t max_depth )
523  {
524  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
525  std::vector<variant> vars(src.size());
526  for( size_t i = 0; i < src.size(); ++i )
527  vars[i] = variant( src[i], max_depth - 1 );
528  v = std::move(vars);
529  }
530 
531 
533  template<typename T>
534  void from_variant( const variant& var, std::vector<T>& dest, uint32_t max_depth )
535  {
536  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
537  const variants& vars = var.get_array();
538  dest.clear();
539  dest.reserve( vars.size() );
540  for( const auto& item : vars )
541  dest.push_back( item.as<T>( max_depth - 1 ) );
542  }
543 
545  template<typename T>
546  void to_variant( const std::vector<T>& t, variant& v, uint32_t max_depth )
547  {
548  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
549  std::vector<variant> vars(t.size());
550  for( size_t i = 0; i < t.size(); ++i )
551  vars[i] = variant( t[i], max_depth - 1 );
552  v = std::move(vars);
553  }
554 
555 
557  template<typename A, typename B>
558  void to_variant( const std::pair<A,B>& t, variant& v, uint32_t max_depth )
559  {
560  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
561  std::vector<variant> vars(2);
562  vars[0] = variant( t.first, max_depth - 1 );
563  vars[1] = variant( t.second, max_depth - 1 );
564  v = vars;
565  }
566  template<typename A, typename B>
567  void from_variant( const variant& v, std::pair<A,B>& p, uint32_t max_depth )
568  {
569  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
570  const variants& vars = v.get_array();
571  if( vars.size() > 0 )
572  p.first = vars[0].as<A>( max_depth - 1 );
573  if( vars.size() > 1 )
574  p.second = vars[1].as<B>( max_depth - 1 );
575  }
576 
577 
578  template<typename T>
579  variant::variant( const T& val, uint32_t max_depth )
580  {
581  memset( this, 0, sizeof(*this) );
582  to_variant( val, *this, max_depth );
583  }
584 
585  template<typename T>
586  void to_variant( const std::shared_ptr<T>& var, variant& vo, uint32_t max_depth )
587  {
588  if( var )
589  {
590  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
591  to_variant( *var, vo, max_depth - 1 );
592  }
593  else vo = nullptr;
594  }
595 
596  template<typename T>
597  void from_variant( const variant& var, std::shared_ptr<T>& vo, uint32_t max_depth )
598  {
599  if( var.is_null() ) vo = nullptr;
600  else
601  {
602  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
603  if( !vo ) vo = std::make_shared<T>();
604  from_variant( var, *vo, max_depth - 1 );
605  }
606  }
607  template<typename T>
608  void to_variant( const std::unique_ptr<T>& var, variant& vo, uint32_t max_depth )
609  {
610  if( var )
611  {
612  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
613  to_variant( *var, vo, max_depth - 1 );
614  }
615  else vo = nullptr;
616  }
617 
618  template<typename T>
619  void from_variant( const variant& var, std::unique_ptr<T>& vo, uint32_t max_depth )
620  {
621  if( var.is_null() ) vo.reset();
622  else
623  {
624  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
625  if( !vo ) vo.reset( new T() );
626  from_variant( var, *vo, max_depth - 1 );
627  }
628  }
629 
630 
631  template<typename T>
632  void to_variant( const safe<T>& s, variant& v, uint32_t max_depth ) {
633  to_variant( static_cast<T>(s.value), v, max_depth );
634  }
635 
636  template<typename T>
637  void from_variant( const variant& v, safe<T>& s, uint32_t max_depth ) {
638  s.value = v.as<T>( max_depth );
639  }
640 
641  template<typename T, typename... Args>
642  void to_variant( const boost::multi_index_container<T,Args...>& c, variant& v, uint32_t max_depth )
643  {
644  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
645  std::vector<variant> vars;
646  vars.reserve( c.size() );
647  for( const auto& item : c )
648  vars.emplace_back( variant( item, max_depth - 1 ) );
649  v = std::move(vars);
650  }
651 
652  template<typename T, typename... Args>
653  void from_variant( const variant& v, boost::multi_index_container<T,Args...>& c, uint32_t max_depth )
654  {
655  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
656  const variants& vars = v.get_array();
657  c.clear();
658  for( const auto& item : vars )
659  c.insert( item.as<T>( max_depth - 1 ) );
660  }
661 
662  template<size_t N>
663  void to_variant( const std::array<char,N>& bi, variant& v, uint32_t max_depth = 1 )
664  {
665  v = variant( to_hex( bi.data(), N ) );
666  }
667  template<size_t N>
668  void from_variant( const variant& v, std::array<char,N>& bi, uint32_t max_depth = 1 )
669  {
670  std::string ve = v.as_string();
671  if( ve.size() )
672  from_hex( ve, bi.data(), std::min<size_t>( ve.size() / 2, bi.size() ) );
673  else
674  memset( bi.data(), 0, bi.size() );
675  }
676 
677  template<size_t N>
678  void to_variant( const std::array<unsigned char,N>& bi, variant& v, uint32_t max_depth = 1 )
679  {
680  v = variant( to_hex( (char*) bi.data(), N ) );
681  }
682  template<size_t N>
683  void from_variant( const variant& v, std::array<unsigned char,N>& bi, uint32_t max_depth = 1 )
684  {
685  std::string ve = v.as_string();
686  if( ve.size() )
687  from_hex( ve, (char*)bi.data(), std::min<size_t>( ve.size() / 2, bi.size() ) );
688  else
689  memset( bi.data(), 0, bi.size() );
690  }
691 
692  variant operator + ( const variant& a, const variant& b );
693  variant operator - ( const variant& a, const variant& b );
694  variant operator * ( const variant& a, const variant& b );
695  variant operator / ( const variant& a, const variant& b );
696  bool operator == ( const variant& a, const variant& b );
697  bool operator != ( const variant& a, const variant& b );
698  bool operator < ( const variant& a, const variant& b );
699  bool operator > ( const variant& a, const variant& b );
700  bool operator ! ( const variant& a );
701 } // namespace fc
702 
703 #include <fc/reflect/reflect.hpp>
705 FC_REFLECT_ENUM( fc::variant::type_id, (null_type)(int64_type)(uint64_type)(double_type)(bool_type)(string_type)(array_type)(object_type)(blob_type) )
706 FC_REFLECT( fc::blob, (data) );
uint8_t from_hex(char c)
Definition: hex.cpp:6
variant(const optional< T > &v, uint32_t max_depth)
Definition: variant.hpp:359
T as(uint32_t max_depth) const
Definition: variant.hpp:336
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:386
optional< variant > ovariant
Definition: variant.hpp:376
An order-perserving dictionary of variant&#39;s.
Defines types and macros used to provide reflection.
bool is_null() const
Definition: variant.cpp:309
std::vector< variant > variants
Definition: variant.hpp:170
auto operator+(const fwd< T, S, A > &x, U &&u) -> typename detail::add< T, U >::type
Definition: fwd_impl.hpp:44
bool valid() const
Definition: optional.hpp:186
bool operator!(const variant &a)
Definition: variant.cpp:729
void throw_assertion_failure(const std::string &message)
Definition: exception.cpp:308
variant & operator=(T &&v)
Definition: variant.hpp:353
variant()
Constructs a null_type variant.
Definition: variant.cpp:30
bool operator!=(const optional< T > &left, const optional< T > &right)
Definition: optional.hpp:253
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
FC_REFLECT_TYPENAME(fc::log_message)
FC_REFLECT_ENUM(graphene::net::core_message_type_enum,(trx_message_type)(block_message_type)(core_message_type_first)(item_ids_inventory_message_type)(blockchain_item_ids_inventory_message_type)(fetch_blockchain_item_ids_message_type)(fetch_items_message_type)(item_not_available_message_type)(hello_message_type)(connection_accepted_message_type)(connection_rejected_message_type)(address_request_message_type)(address_message_type)(closing_connection_message_type)(current_time_request_message_type)(current_time_reply_message_type)(check_firewall_message_type)(check_firewall_reply_message_type)(get_current_connections_request_message_type)(get_current_connections_reply_message_type)(core_message_type_last))(different_chain)(already_connected)(connected_to_self)(not_accepting_connections)(blocked)(invalid_hello_message)(client_too_old))(inbound)(outbound))(firewalled)(not_firewalled))(unable_to_connect)(connection_successful)) namespace std
bool operator==(const optional< T > &left, const optional< T > &right)
Definition: optional.hpp:245
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
auto operator-(const fwd< T, S, A > &x, U &&u) -> typename detail::sub< T, U >::type
Definition: fwd_impl.hpp:47
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
variant operator/(const variant &a, const variant &b)
Definition: variant.cpp:840
bool operator>(const variant &a, const variant &b)
Definition: variant.cpp:743
std::vector< char > data
Definition: variant.hpp:58
variants & get_array()
Definition: variant.cpp:496
#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
Definition: api.hpp:15
bool operator<(const variant &a, const variant &b)
Definition: variant.cpp:734
void as(T &v, uint32_t max_depth) const
Definition: variant.hpp:344
std::string to_hex(const char *d, uint32_t s)
Definition: hex.cpp:17
variant operator*(const variant &a, const variant &b)
Definition: variant.cpp:815
virtual ~visitor()
Definition: variant.hpp:253
std::string as_string() const
Definition: variant.cpp:469
An order-perserving dictionary of variant&#39;s.
T value
Definition: safe.hpp:22