]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #ifndef JSON_SPIRIT_VALUE\r |
2 | #define JSON_SPIRIT_VALUE\r | |
3 | \r | |
4 | // Copyright John W. Wilkinson 2007 - 2011\r | |
5 | // Distributed under the MIT License, see accompanying file LICENSE.txt\r | |
6 | \r | |
7 | // json spirit version 4.05\r | |
8 | \r | |
9 | #if defined(_MSC_VER) && (_MSC_VER >= 1020)\r | |
10 | # pragma once\r | |
11 | #endif\r | |
12 | \r | |
13 | #include <vector>\r | |
14 | #include <map>\r | |
15 | #include <string>\r | |
7c673cae FG |
16 | #include <sstream>\r |
17 | #include <stdexcept>\r | |
18 | #include <boost/config.hpp> \r | |
19 | #include <boost/cstdint.hpp> \r | |
20 | #include <boost/shared_ptr.hpp> \r | |
21 | #include <boost/variant.hpp> \r | |
22 | \r | |
23 | // comment out the value types you don't need to reduce build times and intermediate file sizes\r | |
24 | #define JSON_SPIRIT_VALUE_ENABLED\r | |
25 | //#define JSON_SPIRIT_WVALUE_ENABLED\r | |
26 | #define JSON_SPIRIT_MVALUE_ENABLED\r | |
27 | //#define JSON_SPIRIT_WMVALUE_ENABLED\r | |
28 | \r | |
29 | namespace json_spirit\r | |
30 | {\r | |
31 | enum Value_type{ obj_type, array_type, str_type, bool_type, int_type, real_type, null_type };\r | |
32 | \r | |
33 | struct Null{};\r | |
34 | \r | |
35 | template< class Config > // Config determines whether the value uses std::string or std::wstring and\r | |
36 | // whether JSON Objects are represented as vectors or maps\r | |
37 | class Value_impl\r | |
38 | {\r | |
39 | public:\r | |
40 | \r | |
41 | typedef Config Config_type;\r | |
42 | typedef typename Config::String_type String_type;\r | |
43 | typedef typename Config::Object_type Object;\r | |
44 | typedef typename Config::Array_type Array;\r | |
45 | typedef typename String_type::const_pointer Const_str_ptr; // eg const char*\r | |
46 | \r | |
47 | Value_impl(); // creates null value\r | |
48 | Value_impl( Const_str_ptr value ); \r | |
49 | Value_impl( const String_type& value );\r | |
50 | Value_impl( const Object& value );\r | |
51 | Value_impl( const Array& value );\r | |
52 | Value_impl( bool value );\r | |
53 | Value_impl( int value );\r | |
54 | Value_impl( boost::int64_t value );\r | |
55 | Value_impl( boost::uint64_t value );\r | |
56 | Value_impl( double value );\r | |
57 | \r | |
58 | template< class Iter >\r | |
59 | Value_impl( Iter first, Iter last ); // constructor from containers, e.g. std::vector or std::list\r | |
60 | \r | |
61 | template< BOOST_VARIANT_ENUM_PARAMS( typename T ) >\r | |
62 | Value_impl( const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& variant ); // constructor for compatible variant types\r | |
63 | \r | |
64 | Value_impl( const Value_impl& other );\r | |
65 | \r | |
66 | bool operator==( const Value_impl& lhs ) const;\r | |
67 | \r | |
68 | Value_impl& operator=( const Value_impl& lhs );\r | |
69 | \r | |
70 | Value_type type() const;\r | |
71 | \r | |
72 | bool is_uint64() const;\r | |
73 | bool is_null() const;\r | |
74 | \r | |
75 | const String_type& get_str() const;\r | |
76 | const Object& get_obj() const;\r | |
77 | const Array& get_array() const;\r | |
78 | bool get_bool() const;\r | |
79 | int get_int() const;\r | |
80 | boost::int64_t get_int64() const;\r | |
81 | boost::uint64_t get_uint64() const;\r | |
82 | double get_real() const;\r | |
83 | \r | |
84 | Object& get_obj();\r | |
85 | Array& get_array();\r | |
86 | \r | |
87 | template< typename T > T get_value() const; // example usage: int i = value.get_value< int >();\r | |
88 | // or double d = value.get_value< double >();\r | |
89 | \r | |
90 | static const Value_impl null;\r | |
91 | \r | |
92 | private:\r | |
93 | \r | |
94 | void check_type( const Value_type vtype ) const;\r | |
95 | \r | |
96 | typedef boost::variant< boost::recursive_wrapper< Object >, boost::recursive_wrapper< Array >, \r | |
97 | String_type, bool, boost::int64_t, double, Null, boost::uint64_t > Variant;\r | |
98 | \r | |
99 | Variant v_;\r | |
100 | \r | |
101 | class Variant_converter_visitor : public boost::static_visitor< Variant > \r | |
102 | {\r | |
103 | public:\r | |
104 | \r | |
105 | template< typename T, typename A, template< typename, typename > class Cont >\r | |
106 | Variant operator()( const Cont< T, A >& cont ) const \r | |
107 | {\r | |
108 | return Array( cont.begin(), cont.end() );\r | |
109 | }\r | |
110 | \r | |
111 | Variant operator()( int i ) const \r | |
112 | {\r | |
113 | return static_cast< boost::int64_t >( i );\r | |
114 | }\r | |
115 | \r | |
116 | template<class T>\r | |
117 | Variant operator()( const T& t ) const \r | |
118 | {\r | |
119 | return t;\r | |
120 | }\r | |
121 | };\r | |
122 | };\r | |
123 | \r | |
124 | // vector objects\r | |
125 | \r | |
126 | template< class Config >\r | |
127 | struct Pair_impl\r | |
128 | {\r | |
129 | typedef typename Config::String_type String_type;\r | |
130 | typedef typename Config::Value_type Value_type;\r | |
131 | \r | |
132 | Pair_impl()\r | |
133 | {\r | |
134 | }\r | |
135 | \r | |
136 | Pair_impl( const String_type& name, const Value_type& value );\r | |
137 | \r | |
138 | bool operator==( const Pair_impl& lhs ) const;\r | |
139 | \r | |
140 | String_type name_;\r | |
141 | Value_type value_;\r | |
142 | };\r | |
143 | \r | |
144 | #if defined( JSON_SPIRIT_VALUE_ENABLED ) || defined( JSON_SPIRIT_WVALUE_ENABLED )\r | |
145 | template< class String >\r | |
146 | struct Config_vector\r | |
147 | {\r | |
148 | typedef String String_type;\r | |
149 | typedef Value_impl< Config_vector > Value_type;\r | |
150 | typedef Pair_impl < Config_vector > Pair_type;\r | |
151 | typedef std::vector< Value_type > Array_type;\r | |
152 | typedef std::vector< Pair_type > Object_type;\r | |
153 | \r | |
154 | static Value_type& add( Object_type& obj, const String_type& name, const Value_type& value )\r | |
155 | {\r | |
156 | obj.push_back( Pair_type( name , value ) );\r | |
157 | \r | |
158 | return obj.back().value_;\r | |
159 | }\r | |
160 | \r | |
161 | static String_type get_name( const Pair_type& pair )\r | |
162 | {\r | |
163 | return pair.name_;\r | |
164 | }\r | |
165 | \r | |
166 | static Value_type get_value( const Pair_type& pair )\r | |
167 | {\r | |
168 | return pair.value_;\r | |
169 | }\r | |
170 | };\r | |
171 | #endif\r | |
172 | \r | |
173 | // typedefs for ASCII\r | |
174 | \r | |
175 | #ifdef JSON_SPIRIT_VALUE_ENABLED\r | |
176 | typedef Config_vector< std::string > Config;\r | |
177 | \r | |
178 | typedef Config::Value_type Value;\r | |
179 | typedef Config::Pair_type Pair;\r | |
180 | typedef Config::Object_type Object;\r | |
181 | typedef Config::Array_type Array;\r | |
182 | #endif\r | |
183 | \r | |
184 | // typedefs for Unicode\r | |
185 | \r | |
186 | #if defined( JSON_SPIRIT_WVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )\r | |
187 | typedef Config_vector< std::wstring > wConfig;\r | |
188 | \r | |
189 | typedef wConfig::Value_type wValue;\r | |
190 | typedef wConfig::Pair_type wPair;\r | |
191 | typedef wConfig::Object_type wObject;\r | |
192 | typedef wConfig::Array_type wArray;\r | |
193 | #endif\r | |
194 | \r | |
195 | // map objects\r | |
196 | \r | |
197 | #if defined( JSON_SPIRIT_MVALUE_ENABLED ) || defined( JSON_SPIRIT_WMVALUE_ENABLED )\r | |
198 | template< class String >\r | |
199 | struct Config_map\r | |
200 | {\r | |
201 | typedef String String_type;\r | |
202 | typedef Value_impl< Config_map > Value_type;\r | |
203 | typedef std::vector< Value_type > Array_type;\r | |
204 | typedef std::map< String_type, Value_type > Object_type;\r | |
205 | typedef std::pair< String_type, Value_type > Pair_type;\r | |
206 | \r | |
207 | static Value_type& add( Object_type& obj, const String_type& name, const Value_type& value )\r | |
208 | {\r | |
209 | return obj[ name ] = value;\r | |
210 | }\r | |
211 | \r | |
212 | static String_type get_name( const Pair_type& pair )\r | |
213 | {\r | |
214 | return pair.first;\r | |
215 | }\r | |
216 | \r | |
217 | static Value_type get_value( const Pair_type& pair )\r | |
218 | {\r | |
219 | return pair.second;\r | |
220 | }\r | |
221 | };\r | |
222 | #endif\r | |
223 | \r | |
224 | // typedefs for ASCII\r | |
225 | \r | |
226 | #ifdef JSON_SPIRIT_MVALUE_ENABLED\r | |
227 | typedef Config_map< std::string > mConfig;\r | |
228 | \r | |
229 | typedef mConfig::Value_type mValue;\r | |
230 | typedef mConfig::Object_type mObject;\r | |
231 | typedef mConfig::Array_type mArray;\r | |
232 | #endif\r | |
233 | \r | |
234 | // typedefs for Unicode\r | |
235 | \r | |
236 | #if defined( JSON_SPIRIT_WMVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )\r | |
237 | typedef Config_map< std::wstring > wmConfig;\r | |
238 | \r | |
239 | typedef wmConfig::Value_type wmValue;\r | |
240 | typedef wmConfig::Object_type wmObject;\r | |
241 | typedef wmConfig::Array_type wmArray;\r | |
242 | #endif\r | |
243 | \r | |
244 | ///////////////////////////////////////////////////////////////////////////////////////////////\r | |
245 | //\r | |
246 | // implementation\r | |
247 | \r | |
248 | inline bool operator==( const Null&, const Null& )\r | |
249 | {\r | |
250 | return true;\r | |
251 | }\r | |
252 | \r | |
253 | template< class Config >\r | |
254 | const Value_impl< Config > Value_impl< Config >::null;\r | |
255 | \r | |
256 | template< class Config >\r | |
257 | Value_impl< Config >::Value_impl()\r | |
258 | : v_( Null() )\r | |
259 | {\r | |
260 | }\r | |
261 | \r | |
262 | template< class Config >\r | |
263 | Value_impl< Config >::Value_impl( const Const_str_ptr value )\r | |
264 | : v_( String_type( value ) )\r | |
265 | {\r | |
266 | }\r | |
267 | \r | |
268 | template< class Config >\r | |
269 | Value_impl< Config >::Value_impl( const String_type& value )\r | |
270 | : v_( value )\r | |
271 | {\r | |
272 | }\r | |
273 | \r | |
274 | template< class Config >\r | |
275 | Value_impl< Config >::Value_impl( const Object& value )\r | |
276 | : v_( value )\r | |
277 | {\r | |
278 | }\r | |
279 | \r | |
280 | template< class Config >\r | |
281 | Value_impl< Config >::Value_impl( const Array& value )\r | |
282 | : v_( value )\r | |
283 | {\r | |
284 | }\r | |
285 | \r | |
286 | template< class Config >\r | |
287 | Value_impl< Config >::Value_impl( bool value )\r | |
288 | : v_( value )\r | |
289 | {\r | |
290 | }\r | |
291 | \r | |
292 | template< class Config >\r | |
293 | Value_impl< Config >::Value_impl( int value )\r | |
294 | : v_( static_cast< boost::int64_t >( value ) )\r | |
295 | {\r | |
296 | }\r | |
297 | \r | |
298 | template< class Config >\r | |
299 | Value_impl< Config >::Value_impl( boost::int64_t value )\r | |
300 | : v_( value )\r | |
301 | {\r | |
302 | }\r | |
303 | \r | |
304 | template< class Config >\r | |
305 | Value_impl< Config >::Value_impl( boost::uint64_t value )\r | |
306 | : v_( value )\r | |
307 | {\r | |
308 | }\r | |
309 | \r | |
310 | template< class Config >\r | |
311 | Value_impl< Config >::Value_impl( double value )\r | |
312 | : v_( value )\r | |
313 | {\r | |
314 | }\r | |
315 | \r | |
316 | template< class Config >\r | |
317 | Value_impl< Config >::Value_impl( const Value_impl< Config >& other )\r | |
318 | : v_( other.v_ )\r | |
319 | {\r | |
320 | }\r | |
321 | \r | |
322 | template< class Config >\r | |
323 | template< class Iter >\r | |
324 | Value_impl< Config >::Value_impl( Iter first, Iter last )\r | |
325 | : v_( Array( first, last ) )\r | |
326 | {\r | |
327 | }\r | |
328 | \r | |
329 | template< class Config >\r | |
330 | template< BOOST_VARIANT_ENUM_PARAMS( typename T ) >\r | |
331 | Value_impl< Config >::Value_impl( const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& variant )\r | |
332 | : v_( boost::apply_visitor( Variant_converter_visitor(), variant) )\r | |
333 | {\r | |
334 | }\r | |
335 | \r | |
336 | template< class Config >\r | |
337 | Value_impl< Config >& Value_impl< Config >::operator=( const Value_impl& lhs )\r | |
338 | {\r | |
339 | Value_impl tmp( lhs );\r | |
340 | \r | |
341 | std::swap( v_, tmp.v_ );\r | |
342 | \r | |
343 | return *this;\r | |
344 | }\r | |
345 | \r | |
346 | template< class Config >\r | |
347 | bool Value_impl< Config >::operator==( const Value_impl& lhs ) const\r | |
348 | {\r | |
349 | if( this == &lhs ) return true;\r | |
350 | \r | |
351 | if( type() != lhs.type() ) return false;\r | |
352 | \r | |
353 | return v_ == lhs.v_; \r | |
354 | }\r | |
355 | \r | |
356 | template< class Config >\r | |
357 | Value_type Value_impl< Config >::type() const\r | |
358 | {\r | |
359 | if( is_uint64() )\r | |
360 | {\r | |
361 | return int_type;\r | |
362 | }\r | |
363 | \r | |
364 | return static_cast< Value_type >( v_.which() );\r | |
365 | }\r | |
366 | \r | |
367 | template< class Config >\r | |
368 | bool Value_impl< Config >::is_uint64() const\r | |
369 | {\r | |
370 | return v_.which() == null_type + 1;\r | |
371 | }\r | |
372 | \r | |
373 | template< class Config >\r | |
374 | bool Value_impl< Config >::is_null() const\r | |
375 | {\r | |
376 | return type() == null_type;\r | |
377 | }\r | |
378 | \r | |
379 | template< class Config >\r | |
380 | void Value_impl< Config >::check_type( const Value_type vtype ) const\r | |
381 | {\r | |
382 | if( type() != vtype ) \r | |
383 | {\r | |
384 | std::ostringstream os;\r | |
385 | \r | |
386 | os << "value type is " << type() << " not " << vtype;\r | |
387 | \r | |
388 | throw std::runtime_error( os.str() );\r | |
389 | }\r | |
390 | }\r | |
391 | \r | |
392 | template< class Config >\r | |
393 | const typename Config::String_type& Value_impl< Config >::get_str() const\r | |
394 | {\r | |
395 | check_type( str_type );\r | |
396 | \r | |
397 | return *boost::get< String_type >( &v_ );\r | |
398 | }\r | |
399 | \r | |
400 | template< class Config >\r | |
401 | const typename Value_impl< Config >::Object& Value_impl< Config >::get_obj() const\r | |
402 | {\r | |
403 | check_type( obj_type );\r | |
404 | \r | |
405 | return *boost::get< Object >( &v_ );\r | |
406 | }\r | |
407 | \r | |
408 | template< class Config >\r | |
409 | const typename Value_impl< Config >::Array& Value_impl< Config >::get_array() const\r | |
410 | {\r | |
411 | check_type( array_type );\r | |
412 | \r | |
413 | return *boost::get< Array >( &v_ );\r | |
414 | }\r | |
415 | \r | |
416 | template< class Config >\r | |
417 | bool Value_impl< Config >::get_bool() const\r | |
418 | {\r | |
419 | check_type( bool_type );\r | |
420 | \r | |
421 | return boost::get< bool >( v_ );\r | |
422 | }\r | |
423 | \r | |
424 | template< class Config >\r | |
425 | int Value_impl< Config >::get_int() const\r | |
426 | {\r | |
427 | check_type( int_type );\r | |
428 | \r | |
429 | return static_cast< int >( get_int64() );\r | |
430 | }\r | |
431 | \r | |
432 | template< class Config >\r | |
433 | boost::int64_t Value_impl< Config >::get_int64() const\r | |
434 | {\r | |
435 | check_type( int_type );\r | |
436 | \r | |
437 | if( is_uint64() )\r | |
438 | {\r | |
439 | return static_cast< boost::int64_t >( get_uint64() );\r | |
440 | }\r | |
441 | \r | |
442 | return boost::get< boost::int64_t >( v_ );\r | |
443 | }\r | |
444 | \r | |
445 | template< class Config >\r | |
446 | boost::uint64_t Value_impl< Config >::get_uint64() const\r | |
447 | {\r | |
448 | check_type( int_type );\r | |
449 | \r | |
450 | if( !is_uint64() )\r | |
451 | {\r | |
452 | return static_cast< boost::uint64_t >( get_int64() );\r | |
453 | }\r | |
454 | \r | |
455 | return boost::get< boost::uint64_t >( v_ );\r | |
456 | }\r | |
457 | \r | |
458 | template< class Config >\r | |
459 | double Value_impl< Config >::get_real() const\r | |
460 | {\r | |
461 | if( type() == int_type )\r | |
462 | {\r | |
463 | return is_uint64() ? static_cast< double >( get_uint64() )\r | |
464 | : static_cast< double >( get_int64() );\r | |
465 | }\r | |
466 | \r | |
467 | check_type( real_type );\r | |
468 | \r | |
469 | return boost::get< double >( v_ );\r | |
470 | }\r | |
471 | \r | |
472 | template< class Config >\r | |
473 | typename Value_impl< Config >::Object& Value_impl< Config >::get_obj()\r | |
474 | {\r | |
475 | check_type( obj_type );\r | |
476 | \r | |
477 | return *boost::get< Object >( &v_ );\r | |
478 | }\r | |
479 | \r | |
480 | template< class Config >\r | |
481 | typename Value_impl< Config >::Array& Value_impl< Config >::get_array()\r | |
482 | {\r | |
483 | check_type( array_type );\r | |
484 | \r | |
485 | return *boost::get< Array >( &v_ );\r | |
486 | }\r | |
487 | \r | |
488 | template< class Config >\r | |
489 | Pair_impl< Config >::Pair_impl( const String_type& name, const Value_type& value )\r | |
490 | : name_( name )\r | |
491 | , value_( value )\r | |
492 | {\r | |
493 | }\r | |
494 | \r | |
495 | template< class Config >\r | |
496 | bool Pair_impl< Config >::operator==( const Pair_impl< Config >& lhs ) const\r | |
497 | {\r | |
498 | if( this == &lhs ) return true;\r | |
499 | \r | |
500 | return ( name_ == lhs.name_ ) && ( value_ == lhs.value_ );\r | |
501 | }\r | |
502 | \r | |
503 | // converts a C string, ie. 8 bit char array, to a string object\r | |
504 | //\r | |
505 | template < class String_type >\r | |
506 | String_type to_str( const char* c_str )\r | |
507 | {\r | |
508 | String_type result;\r | |
509 | \r | |
510 | for( const char* p = c_str; *p != 0; ++p )\r | |
511 | {\r | |
512 | result += *p;\r | |
513 | }\r | |
514 | \r | |
515 | return result;\r | |
516 | }\r | |
517 | \r | |
518 | //\r | |
519 | \r | |
520 | namespace internal_\r | |
521 | {\r | |
522 | template< typename T >\r | |
523 | struct Type_to_type\r | |
524 | {\r | |
525 | };\r | |
526 | \r | |
527 | template< class Value > \r | |
528 | int get_value( const Value& value, Type_to_type< int > )\r | |
529 | {\r | |
530 | return value.get_int();\r | |
531 | }\r | |
532 | \r | |
533 | template< class Value > \r | |
534 | boost::int64_t get_value( const Value& value, Type_to_type< boost::int64_t > )\r | |
535 | {\r | |
536 | return value.get_int64();\r | |
537 | }\r | |
538 | \r | |
539 | template< class Value > \r | |
540 | boost::uint64_t get_value( const Value& value, Type_to_type< boost::uint64_t > )\r | |
541 | {\r | |
542 | return value.get_uint64();\r | |
543 | }\r | |
544 | \r | |
545 | template< class Value > \r | |
546 | double get_value( const Value& value, Type_to_type< double > )\r | |
547 | {\r | |
548 | return value.get_real();\r | |
549 | }\r | |
550 | \r | |
551 | template< class Value > \r | |
552 | typename Value::String_type get_value( const Value& value, Type_to_type< typename Value::String_type > )\r | |
553 | {\r | |
554 | return value.get_str();\r | |
555 | }\r | |
556 | \r | |
557 | template< class Value > \r | |
558 | typename Value::Array get_value( const Value& value, Type_to_type< typename Value::Array > )\r | |
559 | {\r | |
560 | return value.get_array();\r | |
561 | }\r | |
562 | \r | |
563 | template< class Value > \r | |
564 | typename Value::Object get_value( const Value& value, Type_to_type< typename Value::Object > )\r | |
565 | {\r | |
566 | return value.get_obj();\r | |
567 | }\r | |
568 | \r | |
569 | template< class Value > \r | |
570 | bool get_value( const Value& value, Type_to_type< bool > )\r | |
571 | {\r | |
572 | return value.get_bool();\r | |
573 | }\r | |
574 | }\r | |
575 | \r | |
576 | template< class Config >\r | |
577 | template< typename T > \r | |
578 | T Value_impl< Config >::get_value() const\r | |
579 | {\r | |
580 | return internal_::get_value( *this, internal_::Type_to_type< T >() );\r | |
581 | }\r | |
582 | }\r | |
583 | \r | |
584 | #endif\r |