BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
variant_object.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <fc/variant.hpp>
3 #include <memory>
4 
5 namespace fc
6 {
7  class mutable_variant_object;
8 
21  {
22  public:
24  class entry
25  {
26  public:
27  entry();
28  entry( string k, variant v );
29  entry( entry&& e );
30  entry( const entry& e);
31  entry& operator=(const entry&);
32  entry& operator=(entry&&);
33 
34  const string& key()const;
35  const variant& value()const;
36  void set( variant v );
37 
38  variant& value();
39 
40  private:
41  string _key;
42  variant _value;
43  };
44 
45  typedef std::vector< entry >::const_iterator iterator;
46 
53  iterator begin()const;
55  iterator end()const;
56  iterator find( const string& key )const;
57  iterator find( const char* key )const;
58  const variant& operator[]( const string& key )const;
59  const variant& operator[]( const char* key )const;
60  size_t size()const;
61  bool contains( const char* key ) const { return find(key) != end(); }
63 
65 
67  variant_object( string key, variant val );
68 
69  template<typename T>
70  variant_object( string key, T&& val )
71  :_key_value( std::make_shared<std::vector<entry> >() )
72  {
73  *this = variant_object( std::move(key), variant(std::forward<T>(val)) );
74  }
77 
80 
83 
86 
87  private:
88  std::shared_ptr< std::vector< entry > > _key_value;
89  friend class mutable_variant_object;
90  };
92  void to_variant( const variant_object& var, variant& vo, uint32_t max_depth = 1 );
94  void from_variant( const variant& var, variant_object& vo, uint32_t max_depth = 1 );
95 
96 
109  {
110  public:
113 
114  typedef std::vector< entry >::iterator iterator;
115  typedef std::vector< entry >::const_iterator const_iterator;
116 
123  iterator begin()const;
125  iterator end()const;
126  iterator find( const string& key )const;
127  iterator find( const char* key )const;
128  const variant& operator[]( const string& key )const;
129  const variant& operator[]( const char* key )const;
130  size_t size()const;
132  variant& operator[]( const string& key );
133  variant& operator[]( const char* key );
134 
141  void reserve( size_t s);
143  iterator begin();
144  iterator end();
145  void erase( const string& key );
150  iterator find( const string& key );
151  iterator find( const char* key );
152 
153 
155  mutable_variant_object& set( string key, variant var );
171  mutable_variant_object& operator()( string key, variant var, uint32_t max_depth = 1 );
172  template<typename T>
173  mutable_variant_object& operator()( string key, T&& var, uint32_t max_depth )
174  {
175  _FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" );
176  set( std::move(key), variant( std::forward<T>(var), max_depth - 1 ) );
177  return *this;
178  }
188 
189 
190  template<typename T>
191  explicit mutable_variant_object( T&& v )
192  :_key_value( new std::vector<entry>() )
193  {
194  *this = variant(std::forward<T>(v)).get_object();
195  }
196 
198 
200  mutable_variant_object( string key, variant val );
201  template<typename T>
202  mutable_variant_object( string key, T&& val )
203  :_key_value( new std::vector<entry>() )
204  {
205  set( std::move(key), variant(std::forward<T>(val)) );
206  }
207 
211 
215  private:
216  std::unique_ptr< std::vector< entry > > _key_value;
217  friend class variant_object;
218  };
219 
221  {
222  public:
223  limited_mutable_variant_object( uint32_t max_depth, bool skip_on_exception = false );
224 
225  template<typename T>
227  {
228  if( _reached_depth_limit )
229  // _skip_on_exception will always be true here
230  return *this;
231 
233  try
234  {
235  v = variant( std::forward<T>(var), _max_depth );
236  }
237  catch( ... )
238  {
239  if( !_skip_on_exception )
240  throw;
241  v = variant( "[ERROR: Caught exception while converting data to variant]" );
242  }
243  set( std::move(key), *v );
244  return *this;
245  }
247 
248  private:
249  const uint32_t _max_depth;
250  const bool _reached_depth_limit;
251  const bool _skip_on_exception;
252  };
253 
255  void to_variant( const mutable_variant_object& var, variant& vo, uint32_t max_depth = 1 );
257  void from_variant( const variant& var, mutable_variant_object& vo, uint32_t max_depth = 1 );
258 
259 } // namespace fc
fc::variant_object::find
iterator find(const string &key) const
Definition: variant_object.cpp:64
fc::variant_object
An order-perserving dictionary of variant's.
Definition: variant_object.hpp:20
fc::variant_object::entry::entry
entry()
Definition: variant_object.cpp:11
fc::variant_object::iterator
std::vector< entry >::const_iterator iterator
Definition: variant_object.hpp:45
fc::mutable_variant_object
An order-perserving dictionary of variant's.
Definition: variant_object.hpp:108
fc::variant_object::entry::operator=
entry & operator=(const entry &)
Definition: variant_object.cpp:15
fc::mutable_variant_object::entry
variant_object::entry entry
a key/value pair
Definition: variant_object.hpp:112
fc::limited_mutable_variant_object::operator()
limited_mutable_variant_object & operator()(string key, T &&var)
Definition: variant_object.hpp:226
fc::variant_object::contains
bool contains(const char *key) const
Definition: variant_object.hpp:61
fc::mutable_variant_object::reserve
void reserve(size_t s)
Definition: variant_object.cpp:311
fc::variant_object::entry::key
const string & key() const
Definition: variant_object.cpp:31
fc
Definition: api.hpp:15
fc::variant_object::variant_object
variant_object(string key, T &&val)
Definition: variant_object.hpp:70
fc::mutable_variant_object::mutable_variant_object
mutable_variant_object(string key, T &&val)
Definition: variant_object.hpp:202
fc::variant_object::entry::value
const variant & value() const
Definition: variant_object.cpp:36
fc::mutable_variant_object::mutable_variant_object
mutable_variant_object()
Definition: variant_object.cpp:261
fc::variant_object::begin
iterator begin() const
Definition: variant_object.cpp:53
fc::mutable_variant_object::operator()
mutable_variant_object & operator()(string key, T &&var, uint32_t max_depth)
Definition: variant_object.hpp:173
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
fc::mutable_variant_object::set
mutable_variant_object & set(string key, variant var)
Definition: variant_object.cpp:329
fc::mutable_variant_object::size
size_t size() const
Definition: variant_object.cpp:256
fc::mutable_variant_object::find
iterator find(const string &key) const
Definition: variant_object.cpp:198
fc::mutable_variant_object::operator=
mutable_variant_object & operator=(mutable_variant_object &&)
Definition: variant_object.cpp:293
fc::mutable_variant_object::mutable_variant_object
mutable_variant_object(T &&v)
Definition: variant_object.hpp:191
fc::variant_object::operator=
variant_object & operator=(variant_object &&)
Definition: variant_object.cpp:133
fc::variant_object::operator[]
const variant & operator[](const string &key) const
Definition: variant_object.cpp:81
fc::mutable_variant_object::erase
void erase(const string &key)
Definition: variant_object.cpp:316
fc::mutable_variant_object::operator[]
const variant & operator[](const string &key) const
Definition: variant_object.cpp:232
fc::variant_object::entry::set
void set(variant v)
Definition: variant_object.cpp:45
fc::variant_object::size
size_t size() const
Definition: variant_object.cpp:93
fc::mutable_variant_object::begin
iterator begin() const
Definition: variant_object.cpp:178
fc::variant_object::variant_object
variant_object()
Definition: variant_object.cpp:98
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
_FC_ASSERT
#define _FC_ASSERT(cond, msg)
Definition: variant.hpp:30
fc::mutable_variant_object::const_iterator
std::vector< entry >::const_iterator const_iterator
Definition: variant_object.hpp:115
fc::limited_mutable_variant_object
Definition: variant_object.hpp:220
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
std
Definition: zeroed_array.hpp:76
variant.hpp
fc::mutable_variant_object::iterator
std::vector< entry >::iterator iterator
Definition: variant_object.hpp:114
fc::mutable_variant_object::operator()
mutable_variant_object & operator()(string key, variant var, uint32_t max_depth=1)
Definition: variant_object.cpp:346
fc::optional< variant >
fc::variant_object::end
iterator end() const
Definition: variant_object.cpp:59
fc::limited_mutable_variant_object::limited_mutable_variant_object
limited_mutable_variant_object(uint32_t max_depth, bool skip_on_exception=false)
Definition: variant_object.cpp:368
fc::mutable_variant_object::end
iterator end() const
Definition: variant_object.cpp:183
fc::variant_object::entry
a key/value pair
Definition: variant_object.hpp:24