forked from EOSIO/fc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variant_object.hpp
241 lines (209 loc) · 7.52 KB
/
variant_object.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#pragma once
#include <fc/variant.hpp>
#include <fc/shared_ptr.hpp>
#include <fc/unique_ptr.hpp>
namespace fc
{
class mutable_variant_object;
/**
* @ingroup Serializable
*
* @brief An order-perserving dictionary of variant's.
*
* Keys are kept in the order they are inserted.
* This dictionary implements copy-on-write
*
* @note This class is not optimized for random-access on large
* sets of key-value pairs.
*/
class variant_object
{
public:
/** @brief a key/value pair */
class entry
{
public:
entry();
entry( string k, variant v );
entry( entry&& e );
entry( const entry& e);
entry& operator=(const entry&);
entry& operator=(entry&&);
const string& key()const;
const variant& value()const;
void set( variant v );
variant& value();
friend bool operator == (const entry& a, const entry& b) {
return a._key == b._key && a._value == b._value;
}
friend bool operator != (const entry& a, const entry& b) {
return !(a == b);
}
private:
string _key;
variant _value;
};
typedef std::vector< entry >::const_iterator iterator;
/**
* @name Immutable Interface
*
* Calling these methods will not result in copies of the
* underlying type.
*/
///@{
iterator begin()const;
iterator end()const;
iterator find( const string& key )const;
iterator find( const char* key )const;
const variant& operator[]( const string& key )const;
const variant& operator[]( const char* key )const;
size_t size()const;
bool contains( const char* key ) const { return find(key) != end(); }
///@}
variant_object();
/** initializes the first key/value pair in the object */
variant_object( string key, variant val );
template<typename T>
variant_object( string key, T&& val )
:_key_value( std::make_shared<std::vector<entry> >() )
{
*this = variant_object( std::move(key), variant(forward<T>(val)) );
}
variant_object( const variant_object& );
variant_object( variant_object&& );
variant_object( const mutable_variant_object& );
variant_object( mutable_variant_object&& );
variant_object& operator=( variant_object&& );
variant_object& operator=( const variant_object& );
variant_object& operator=( mutable_variant_object&& );
variant_object& operator=( const mutable_variant_object& );
bool operator==(const variant_object&) const;
bool operator==(const mutable_variant_object&) const;
bool has_value(const variant_object&) const;
private:
std::shared_ptr< std::vector< entry > > _key_value;
friend class mutable_variant_object;
};
/** @ingroup Serializable */
void to_variant( const variant_object& var, variant& vo );
/** @ingroup Serializable */
void from_variant( const variant& var, variant_object& vo );
/**
* @ingroup Serializable
*
* @brief An order-perserving dictionary of variant's.
*
* Keys are kept in the order they are inserted.
* This dictionary implements copy-on-write
*
* @note This class is not optimized for random-access on large
* sets of key-value pairs.
*/
class mutable_variant_object
{
public:
/** @brief a key/value pair */
typedef variant_object::entry entry;
typedef std::vector< entry >::iterator iterator;
typedef std::vector< entry >::const_iterator const_iterator;
/**
* @name Immutable Interface
*
* Calling these methods will not result in copies of the
* underlying type.
*/
///@{
iterator begin()const;
iterator end()const;
iterator find( const string& key )const;
iterator find( const char* key )const;
const variant& operator[]( const string& key )const;
const variant& operator[]( const char* key )const;
size_t size()const;
///@}
variant& operator[]( const string& key );
variant& operator[]( const char* key );
/**
* @name mutable Interface
*
* Calling these methods will result in a copy of the underlying type
* being created if there is more than one reference to this object.
*/
///@{
void reserve( size_t s);
iterator begin();
iterator end();
void erase( const string& key );
/**
*
* @return end() if key is not found
*/
iterator find( const string& key );
iterator find( const char* key );
/** replaces the value at \a key with \a var or insert's \a key if not found */
mutable_variant_object& set( string key, variant var );
/** Appends \a key and \a var without checking for duplicates, designed to
* simplify construction of dictionaries using (key,val)(key2,val2) syntax
*/
/**
* Convenience method to simplify the manual construction of
* variant_object's
*
* Instead of:
* <code>mutable_variant_object("c",c).set("a",a).set("b",b);</code>
*
* You can use:
* <code>mutable_variant_object( "c", c )( "b", b)( "c",c )</code>
*
* @return *this;
*/
mutable_variant_object& operator()( string key, variant var );
template<typename T>
mutable_variant_object& operator()( string key, T&& var )
{
set(std::move(key), variant( fc::forward<T>(var) ) );
return *this;
}
/**
* Copy a variant_object into this mutable_variant_object.
*/
mutable_variant_object& operator()( const variant_object& vo );
/**
* Copy another mutable_variant_object into this mutable_variant_object.
*/
mutable_variant_object& operator()( const mutable_variant_object& mvo );
///@}
template<typename T>
explicit mutable_variant_object( T&& v )
:_key_value( new std::vector<entry>() )
{
*this = variant(fc::forward<T>(v)).get_object();
}
mutable_variant_object();
/** initializes the first key/value pair in the object */
mutable_variant_object( string key, variant val );
template<typename T>
mutable_variant_object( string key, T&& val )
:_key_value( new std::vector<entry>() )
{
reserve(100);
_key_value->emplace_back(fc::move(key), variant(fc::forward<T>(val)));
}
mutable_variant_object( mutable_variant_object&& );
mutable_variant_object( variant );
mutable_variant_object( const mutable_variant_object& );
mutable_variant_object( const variant_object& );
mutable_variant_object& operator=( mutable_variant_object&& );
mutable_variant_object& operator=( variant );
mutable_variant_object& operator=( const mutable_variant_object& );
mutable_variant_object& operator=( const variant_object& );
bool operator==(const variant_object&) const;
private:
std::unique_ptr< std::vector< entry > > _key_value;
friend class variant_object;
};
/** @ingroup Serializable */
void to_variant( const mutable_variant_object& var, variant& vo );
/** @ingroup Serializable */
void from_variant( const variant& var, mutable_variant_object& vo );
} // namespace fc