BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface 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  }
182  mutable_variant_object& operator()( const variant_object& vo );
186  mutable_variant_object& operator()( const mutable_variant_object& mvo );
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  }
246  limited_mutable_variant_object& operator()( const variant_object& vo );
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
mutable_variant_object(string key, T &&val)
const variant & operator[](const string &key) const
iterator find(const string &key) const
An order-perserving dictionary of variant&#39;s.
variant_object & get_object()
Definition: variant.cpp:554
variant_object(string key, T &&val)
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
iterator begin() const
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
variant_object::entry entry
a key/value pair
const variant & value() const
limited_mutable_variant_object & operator()(string key, T &&var)
std::vector< entry >::const_iterator iterator
#define _FC_ASSERT(cond, msg)
Definition: variant.hpp:30
entry & operator=(const entry &)
friend class mutable_variant_object
std::vector< entry >::iterator iterator
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
iterator end() const
Definition: api.hpp:15
std::vector< entry >::const_iterator const_iterator
mutable_variant_object & operator()(string key, T &&var, uint32_t max_depth)
const string & key() const
An order-perserving dictionary of variant&#39;s.
size_t size() const
bool contains(const char *key) const