Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
variant_object.cpp
Go to the documentation of this file.
3
4
5namespace fc
6{
7 // ---------------------------------------------------------------
8 // entry
9
11 variant_object::entry::entry( string k, variant v ) : _key(fc::move(k)),_value(fc::move(v)) {}
12 variant_object::entry::entry( entry&& e ) : _key(fc::move(e._key)),_value(fc::move(e._value)) {}
13 variant_object::entry::entry( const entry& e ) : _key(e._key),_value(e._value) {}
15 {
16 if( this != &e )
17 {
18 _key = e._key;
19 _value = e._value;
20 }
21 return *this;
22 }
24 {
25 fc_swap( _key, e._key );
26 fc_swap( _value, e._value );
27 return *this;
28 }
29
30 const string& variant_object::entry::key()const
31 {
32 return _key;
33 }
34
36 {
37 return _value;
38 }
40 {
41 return _value;
42 }
43
45 {
46 fc_swap( _value, v );
47 }
48
49 // ---------------------------------------------------------------
50 // variant_object
51
53 {
54 FC_ASSERT( _key_value != nullptr );
55 return _key_value->begin();
56 }
57
59 {
60 return _key_value->end();
61 }
62
64 {
65 return find( key.c_str() );
66 }
67
69 {
70 for( auto itr = begin(); itr != end(); ++itr )
71 {
72 if( itr->key() == key )
73 {
74 return itr;
75 }
76 }
77 return end();
78 }
79
80 const variant& variant_object::operator[]( const string& key )const
81 {
82 return (*this)[key.c_str()];
83 }
84
85 const variant& variant_object::operator[]( const char* key )const
86 {
87 auto itr = find( key );
88 if( itr != end() ) return itr->value();
89 FC_THROW_EXCEPTION( key_not_found_exception, "Key ${key}", ("key",key) );
90 }
91
92 size_t variant_object::size() const
93 {
94 return _key_value->size();
95 }
96
98 :_key_value(std::make_shared<std::vector<entry>>() )
99 {
100 }
101
103 : _key_value(std::make_shared<std::vector<entry>>())
104 {
105 //_key_value->push_back(entry(fc::move(key), fc::move(val)));
106 _key_value->emplace_back(entry(fc::move(key), fc::move(val)));
107 }
108
110 :_key_value( obj._key_value )
111 {
112 FC_ASSERT( _key_value != nullptr );
113 }
114
116 : _key_value( fc::move(obj._key_value) )
117 {
118 obj._key_value = std::make_shared<std::vector<entry>>();
119 FC_ASSERT( _key_value != nullptr );
120 }
121
123 : _key_value(std::make_shared<std::vector<entry>>(*obj._key_value))
124 {
125 }
126
128 : _key_value(fc::move(obj._key_value))
129 {
130 FC_ASSERT( _key_value != nullptr );
131 }
132
134 {
135 if (this != &obj)
136 {
137 fc_swap(_key_value, obj._key_value );
138 FC_ASSERT( _key_value != nullptr );
139 }
140 return *this;
141 }
142
144 {
145 if (this != &obj)
146 {
147 _key_value = obj._key_value;
148 }
149 return *this;
150 }
151
153 {
154 _key_value = fc::move(obj._key_value);
155 obj._key_value.reset( new std::vector<entry>() );
156 return *this;
157 }
158
160 {
161 *_key_value = *obj._key_value;
162 return *this;
163 }
164
166 {
167 auto kv_size = size();
168 size_t sum = sizeof(*this) + sizeof(std::vector<entry>);
169 for (size_t iter = 0; iter < kv_size; ++iter) {
170 const auto& kv = _key_value->at(iter);
171 sum += kv.key().length() + sizeof(string);
172 sum += kv.value().estimated_size();
173 }
174 return sum;
175 }
176
177 void to_variant( const variant_object& var, variant& vo )
178 {
179 vo = variant(var);
180 }
181
182 void from_variant( const variant& var, variant_object& vo )
183 {
184 vo = var.get_object();
185 }
186
187 // ---------------------------------------------------------------
188 // mutable_variant_object
189
191 {
192 return _key_value->begin();
193 }
194
196 {
197 return _key_value->end();
198 }
199
201 {
202 return _key_value->begin();
203 }
204
206 {
207 return _key_value->end();
208 }
209
211 {
212 return find( key.c_str() );
213 }
214
216 {
217 for( auto itr = begin(); itr != end(); ++itr )
218 {
219 if( itr->key() == key )
220 {
221 return itr;
222 }
223 }
224 return end();
225 }
226
228 {
229 return find( key.c_str() );
230 }
231
233 {
234 for( auto itr = begin(); itr != end(); ++itr )
235 {
236 if( itr->key() == key )
237 {
238 return itr;
239 }
240 }
241 return end();
242 }
243
244 const variant& mutable_variant_object::operator[]( const string& key )const
245 {
246 return (*this)[key.c_str()];
247 }
248
249 const variant& mutable_variant_object::operator[]( const char* key )const
250 {
251 auto itr = find( key );
252 if( itr != end() ) return itr->value();
253 FC_THROW_EXCEPTION( key_not_found_exception, "Key ${key}", ("key",key) );
254 }
256 {
257 return (*this)[key.c_str()];
258 }
259
261 {
262 auto itr = find( key );
263 if( itr != end() ) return itr->value();
264 _key_value->emplace_back(entry(key, variant()));
265 return _key_value->back().value();
266 }
267
269 {
270 return _key_value->size();
271 }
272
277
279 : _key_value(new std::vector<entry>())
280 {
281 _key_value->push_back(entry(fc::move(key), fc::move(val)));
282 }
283
285 : _key_value( new std::vector<entry>(*obj._key_value) )
286 {
287 }
288
290 : _key_value( new std::vector<entry>(*obj._key_value) )
291 {
292 }
293
295 : _key_value(fc::move(obj._key_value))
296 {
297 }
298
300 {
301 *_key_value = *obj._key_value;
302 return *this;
303 }
304
306 {
307 if (this != &obj)
308 {
309 _key_value = fc::move(obj._key_value);
310 }
311 return *this;
312 }
313
315 {
316 if (this != &obj)
317 {
318 *_key_value = *obj._key_value;
319 }
320 return *this;
321 }
322
324 {
325 _key_value->reserve(s);
326 }
327
328 void mutable_variant_object::erase( const string& key )
329 {
330 for( auto itr = begin(); itr != end(); ++itr )
331 {
332 if( itr->key() == key )
333 {
334 _key_value->erase(itr);
335 return;
336 }
337 }
338 }
339
342 {
343 auto itr = find( key.c_str() );
344 if( itr != end() )
345 {
346 itr->set( fc::move(var) );
347 }
348 else
349 {
350 _key_value->push_back( entry( fc::move(key), fc::move(var) ) );
351 }
352 return *this;
353 }
354
356 {
357 auto itr = find( key.c_str() );
358 if( itr != end() )
359 {
360 itr->set( fc::move(var) );
361 }
362 else
363 {
364 _key_value->push_back( entry( fc::move(key), fc::move(var) ) );
365 }
366 return std::move(*this);
367 }
368
373 {
374 _key_value->push_back( entry( fc::move(key), fc::move(var) ) );
375 return *this;
376 }
377
379 {
380 _key_value->push_back( entry( fc::move(key), fc::move(var) ) );
381 return std::move(*this);
382 }
383
385 {
386 for( const variant_object::entry& e : vo )
387 set( e.key(), e.value() );
388 return *this;
389 }
390
392 {
393 for( const variant_object::entry& e : vo )
394 set( e.key(), e.value() );
395 return std::move(*this);
396 }
397
399 {
400 if( &mvo == this ) // mvo(mvo) is no-op
401 return *this;
402 for( const mutable_variant_object::entry& e : mvo )
403 set( e.key(), e.value() );
404 return *this;
405 }
406
408 {
409 for( const mutable_variant_object::entry& e : mvo )
410 set( e.key(), e.value() );
411 return std::move(*this);
412 }
413
415 {
416 vo = variant(var);
417 }
418
420 {
421 vo = var.get_object();
422 }
423
424} // namesapce fc
An order-preserving dictionary of variants.
std::vector< entry >::iterator iterator
mutable_variant_object & set(string key, variant var) &
mutable_variant_object & operator=(mutable_variant_object &&)
variant_object::entry entry
a key/value pair
mutable_variant_object & operator()(string key, variant var) &
const variant & operator[](const string &key) const
void erase(const string &key)
iterator find(const string &key) const
const string & key() const
const variant & value() const
entry & operator=(const entry &)
An order-preserving dictionary of variants.
size_t size() const
const variant & operator[](const string &key) const
size_t estimated_size() const
std::vector< entry >::const_iterator iterator
iterator end() const
variant_object & operator=(variant_object &&)
iterator find(const string &key) const
iterator begin() const
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
variant_object & get_object()
Definition variant.cpp:554
Defines exception's used by fc.
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
namespace sysio::chain
Definition authority.cpp:3
std::string string
Definition string.hpp:10
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
Definition name.hpp:106
void fc_swap(T &a, T &b)
Definition utility.hpp:211
char * s
bool set