-
+ 97D6C0AC1B46AAB429504060682017A993DB5D1C75ECAF35D001343C78B8A2DF62B51F830DE272A589C400033DC27B32824944248923F670D46D65DC32778A16
bitcoin/src/json/json_spirit_value.h
(0 . 0)(1 . 565)
8432 // /****************************\
8433 // * EXPERIMENTAL BRANCH. *
8434 // * FOR LABORATORY USE ONLY. *
8435 // ********************************
8436 // ************
8437 // **************
8438 // ****************
8439 // **** **** ****
8440 // *** *** ***
8441 // *** *** ***
8442 // *** * * **
8443 // ******** ********
8444 // ******* ******
8445 // *** **
8446 // * ******* **
8447 // ** * * * * *
8448 // ** * * ***
8449 // **** * * * * ****
8450 // **** *** * * ** ***
8451 // **** ********* ******
8452 // ******* ***** *******
8453 // ********* ****** **
8454 // ** ****** ******
8455 // ** ******* **
8456 // ** ******* ***
8457 // **** ******** ************
8458 // ************ ************
8459 // ******** *******
8460 // ****** ****
8461 // *** ***
8462 // ********************************
8463 #ifndef JSON_SPIRIT_VALUE
8464 #define JSON_SPIRIT_VALUE
8465
8466 // Copyright John W. Wilkinson 2007 - 2009.
8467 // Distributed under the MIT License, see accompanying file LICENSE.txt
8468
8469 // json spirit version 4.03
8470
8471 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
8472 # pragma once
8473 #endif
8474
8475 #include <vector>
8476 #include <map>
8477 #include <string>
8478 #include <cassert>
8479 #include <sstream>
8480 #include <stdexcept>
8481 #include <boost/config.hpp>
8482 #include <boost/cstdint.hpp>
8483 #include <boost/shared_ptr.hpp>
8484 #include <boost/variant.hpp>
8485
8486 namespace json_spirit
8487 {
8488 enum Value_type{ obj_type, array_type, str_type, bool_type, int_type, real_type, null_type };
8489 static const char* Value_type_name[]={"obj", "array", "str", "bool", "int", "real", "null"};
8490
8491 template< class Config > // Config determines whether the value uses std::string or std::wstring and
8492 // whether JSON Objects are represented as vectors or maps
8493 class Value_impl
8494 {
8495 public:
8496
8497 typedef Config Config_type;
8498 typedef typename Config::String_type String_type;
8499 typedef typename Config::Object_type Object;
8500 typedef typename Config::Array_type Array;
8501 typedef typename String_type::const_pointer Const_str_ptr; // eg const char*
8502
8503 Value_impl(); // creates null value
8504 Value_impl( Const_str_ptr value );
8505 Value_impl( const String_type& value );
8506 Value_impl( const Object& value );
8507 Value_impl( const Array& value );
8508 Value_impl( bool value );
8509 Value_impl( int value );
8510 Value_impl( boost::int64_t value );
8511 Value_impl( boost::uint64_t value );
8512 Value_impl( double value );
8513
8514 Value_impl( const Value_impl& other );
8515
8516 bool operator==( const Value_impl& lhs ) const;
8517
8518 Value_impl& operator=( const Value_impl& lhs );
8519
8520 Value_type type() const;
8521
8522 bool is_uint64() const;
8523 bool is_null() const;
8524
8525 const String_type& get_str() const;
8526 const Object& get_obj() const;
8527 const Array& get_array() const;
8528 bool get_bool() const;
8529 int get_int() const;
8530 boost::int64_t get_int64() const;
8531 boost::uint64_t get_uint64() const;
8532 double get_real() const;
8533
8534 Object& get_obj();
8535 Array& get_array();
8536
8537 template< typename T > T get_value() const; // example usage: int i = value.get_value< int >();
8538 // or double d = value.get_value< double >();
8539
8540 static const Value_impl null;
8541
8542 private:
8543
8544 void check_type( const Value_type vtype ) const;
8545
8546 typedef boost::variant< String_type,
8547 boost::recursive_wrapper< Object >, boost::recursive_wrapper< Array >,
8548 bool, boost::int64_t, double > Variant;
8549
8550 Value_type type_;
8551 Variant v_;
8552 bool is_uint64_;
8553 };
8554
8555 // vector objects
8556
8557 template< class Config >
8558 struct Pair_impl
8559 {
8560 typedef typename Config::String_type String_type;
8561 typedef typename Config::Value_type Value_type;
8562
8563 Pair_impl( const String_type& name, const Value_type& value );
8564
8565 bool operator==( const Pair_impl& lhs ) const;
8566
8567 String_type name_;
8568 Value_type value_;
8569 };
8570
8571 template< class String >
8572 struct Config_vector
8573 {
8574 typedef String String_type;
8575 typedef Value_impl< Config_vector > Value_type;
8576 typedef Pair_impl < Config_vector > Pair_type;
8577 typedef std::vector< Value_type > Array_type;
8578 typedef std::vector< Pair_type > Object_type;
8579
8580 static Value_type& add( Object_type& obj, const String_type& name, const Value_type& value )
8581 {
8582 obj.push_back( Pair_type( name , value ) );
8583
8584 return obj.back().value_;
8585 }
8586
8587 static String_type get_name( const Pair_type& pair )
8588 {
8589 return pair.name_;
8590 }
8591
8592 static Value_type get_value( const Pair_type& pair )
8593 {
8594 return pair.value_;
8595 }
8596 };
8597
8598 // typedefs for ASCII
8599
8600 typedef Config_vector< std::string > Config;
8601
8602 typedef Config::Value_type Value;
8603 typedef Config::Pair_type Pair;
8604 typedef Config::Object_type Object;
8605 typedef Config::Array_type Array;
8606
8607 // typedefs for Unicode
8608
8609 #ifndef BOOST_NO_STD_WSTRING
8610
8611 typedef Config_vector< std::wstring > wConfig;
8612
8613 typedef wConfig::Value_type wValue;
8614 typedef wConfig::Pair_type wPair;
8615 typedef wConfig::Object_type wObject;
8616 typedef wConfig::Array_type wArray;
8617 #endif
8618
8619 // map objects
8620
8621 template< class String >
8622 struct Config_map
8623 {
8624 typedef String String_type;
8625 typedef Value_impl< Config_map > Value_type;
8626 typedef std::vector< Value_type > Array_type;
8627 typedef std::map< String_type, Value_type > Object_type;
8628 typedef typename Object_type::value_type Pair_type;
8629
8630 static Value_type& add( Object_type& obj, const String_type& name, const Value_type& value )
8631 {
8632 return obj[ name ] = value;
8633 }
8634
8635 static String_type get_name( const Pair_type& pair )
8636 {
8637 return pair.first;
8638 }
8639
8640 static Value_type get_value( const Pair_type& pair )
8641 {
8642 return pair.second;
8643 }
8644 };
8645
8646 // typedefs for ASCII
8647
8648 typedef Config_map< std::string > mConfig;
8649
8650 typedef mConfig::Value_type mValue;
8651 typedef mConfig::Object_type mObject;
8652 typedef mConfig::Array_type mArray;
8653
8654 // typedefs for Unicode
8655
8656 #ifndef BOOST_NO_STD_WSTRING
8657
8658 typedef Config_map< std::wstring > wmConfig;
8659
8660 typedef wmConfig::Value_type wmValue;
8661 typedef wmConfig::Object_type wmObject;
8662 typedef wmConfig::Array_type wmArray;
8663
8664 #endif
8665
8666 ///////////////////////////////////////////////////////////////////////////////////////////////
8667 //
8668 // implementation
8669
8670 template< class Config >
8671 const Value_impl< Config > Value_impl< Config >::null;
8672
8673 template< class Config >
8674 Value_impl< Config >::Value_impl()
8675 : type_( null_type )
8676 , is_uint64_( false )
8677 {
8678 }
8679
8680 template< class Config >
8681 Value_impl< Config >::Value_impl( const Const_str_ptr value )
8682 : type_( str_type )
8683 , v_( String_type( value ) )
8684 , is_uint64_( false )
8685 {
8686 }
8687
8688 template< class Config >
8689 Value_impl< Config >::Value_impl( const String_type& value )
8690 : type_( str_type )
8691 , v_( value )
8692 , is_uint64_( false )
8693 {
8694 }
8695
8696 template< class Config >
8697 Value_impl< Config >::Value_impl( const Object& value )
8698 : type_( obj_type )
8699 , v_( value )
8700 , is_uint64_( false )
8701 {
8702 }
8703
8704 template< class Config >
8705 Value_impl< Config >::Value_impl( const Array& value )
8706 : type_( array_type )
8707 , v_( value )
8708 , is_uint64_( false )
8709 {
8710 }
8711
8712 template< class Config >
8713 Value_impl< Config >::Value_impl( bool value )
8714 : type_( bool_type )
8715 , v_( value )
8716 , is_uint64_( false )
8717 {
8718 }
8719
8720 template< class Config >
8721 Value_impl< Config >::Value_impl( int value )
8722 : type_( int_type )
8723 , v_( static_cast< boost::int64_t >( value ) )
8724 , is_uint64_( false )
8725 {
8726 }
8727
8728 template< class Config >
8729 Value_impl< Config >::Value_impl( boost::int64_t value )
8730 : type_( int_type )
8731 , v_( value )
8732 , is_uint64_( false )
8733 {
8734 }
8735
8736 template< class Config >
8737 Value_impl< Config >::Value_impl( boost::uint64_t value )
8738 : type_( int_type )
8739 , v_( static_cast< boost::int64_t >( value ) )
8740 , is_uint64_( true )
8741 {
8742 }
8743
8744 template< class Config >
8745 Value_impl< Config >::Value_impl( double value )
8746 : type_( real_type )
8747 , v_( value )
8748 , is_uint64_( false )
8749 {
8750 }
8751
8752 template< class Config >
8753 Value_impl< Config >::Value_impl( const Value_impl< Config >& other )
8754 : type_( other.type() )
8755 , v_( other.v_ )
8756 , is_uint64_( other.is_uint64_ )
8757 {
8758 }
8759
8760 template< class Config >
8761 Value_impl< Config >& Value_impl< Config >::operator=( const Value_impl& lhs )
8762 {
8763 Value_impl tmp( lhs );
8764
8765 std::swap( type_, tmp.type_ );
8766 std::swap( v_, tmp.v_ );
8767 std::swap( is_uint64_, tmp.is_uint64_ );
8768
8769 return *this;
8770 }
8771
8772 template< class Config >
8773 bool Value_impl< Config >::operator==( const Value_impl& lhs ) const
8774 {
8775 if( this == &lhs ) return true;
8776
8777 if( type() != lhs.type() ) return false;
8778
8779 return v_ == lhs.v_;
8780 }
8781
8782 template< class Config >
8783 Value_type Value_impl< Config >::type() const
8784 {
8785 return type_;
8786 }
8787
8788 template< class Config >
8789 bool Value_impl< Config >::is_uint64() const
8790 {
8791 return is_uint64_;
8792 }
8793
8794 template< class Config >
8795 bool Value_impl< Config >::is_null() const
8796 {
8797 return type() == null_type;
8798 }
8799
8800 template< class Config >
8801 void Value_impl< Config >::check_type( const Value_type vtype ) const
8802 {
8803 if( type() != vtype )
8804 {
8805 std::ostringstream os;
8806
8807 ///// Bitcoin: Tell the types by name instead of by number
8808 os << "value is type " << Value_type_name[type()] << ", expected " << Value_type_name[vtype];
8809
8810 throw std::runtime_error( os.str() );
8811 }
8812 }
8813
8814 template< class Config >
8815 const typename Config::String_type& Value_impl< Config >::get_str() const
8816 {
8817 check_type( str_type );
8818
8819 return *boost::get< String_type >( &v_ );
8820 }
8821
8822 template< class Config >
8823 const typename Value_impl< Config >::Object& Value_impl< Config >::get_obj() const
8824 {
8825 check_type( obj_type );
8826
8827 return *boost::get< Object >( &v_ );
8828 }
8829
8830 template< class Config >
8831 const typename Value_impl< Config >::Array& Value_impl< Config >::get_array() const
8832 {
8833 check_type( array_type );
8834
8835 return *boost::get< Array >( &v_ );
8836 }
8837
8838 template< class Config >
8839 bool Value_impl< Config >::get_bool() const
8840 {
8841 check_type( bool_type );
8842
8843 return boost::get< bool >( v_ );
8844 }
8845
8846 template< class Config >
8847 int Value_impl< Config >::get_int() const
8848 {
8849 check_type( int_type );
8850
8851 return static_cast< int >( get_int64() );
8852 }
8853
8854 template< class Config >
8855 boost::int64_t Value_impl< Config >::get_int64() const
8856 {
8857 check_type( int_type );
8858
8859 return boost::get< boost::int64_t >( v_ );
8860 }
8861
8862 template< class Config >
8863 boost::uint64_t Value_impl< Config >::get_uint64() const
8864 {
8865 check_type( int_type );
8866
8867 return static_cast< boost::uint64_t >( get_int64() );
8868 }
8869
8870 template< class Config >
8871 double Value_impl< Config >::get_real() const
8872 {
8873 if( type() == int_type )
8874 {
8875 return is_uint64() ? static_cast< double >( get_uint64() )
8876 : static_cast< double >( get_int64() );
8877 }
8878
8879 check_type( real_type );
8880
8881 return boost::get< double >( v_ );
8882 }
8883
8884 template< class Config >
8885 typename Value_impl< Config >::Object& Value_impl< Config >::get_obj()
8886 {
8887 check_type( obj_type );
8888
8889 return *boost::get< Object >( &v_ );
8890 }
8891
8892 template< class Config >
8893 typename Value_impl< Config >::Array& Value_impl< Config >::get_array()
8894 {
8895 check_type( array_type );
8896
8897 return *boost::get< Array >( &v_ );
8898 }
8899
8900 template< class Config >
8901 Pair_impl< Config >::Pair_impl( const String_type& name, const Value_type& value )
8902 : name_( name )
8903 , value_( value )
8904 {
8905 }
8906
8907 template< class Config >
8908 bool Pair_impl< Config >::operator==( const Pair_impl< Config >& lhs ) const
8909 {
8910 if( this == &lhs ) return true;
8911
8912 return ( name_ == lhs.name_ ) && ( value_ == lhs.value_ );
8913 }
8914
8915 // converts a C string, ie. 8 bit char array, to a string object
8916 //
8917 template < class String_type >
8918 String_type to_str( const char* c_str )
8919 {
8920 String_type result;
8921
8922 for( const char* p = c_str; *p != 0; ++p )
8923 {
8924 result += *p;
8925 }
8926
8927 return result;
8928 }
8929
8930 //
8931
8932 namespace internal_
8933 {
8934 template< typename T >
8935 struct Type_to_type
8936 {
8937 };
8938
8939 template< class Value >
8940 int get_value( const Value& value, Type_to_type< int > )
8941 {
8942 return value.get_int();
8943 }
8944
8945 template< class Value >
8946 boost::int64_t get_value( const Value& value, Type_to_type< boost::int64_t > )
8947 {
8948 return value.get_int64();
8949 }
8950
8951 template< class Value >
8952 boost::uint64_t get_value( const Value& value, Type_to_type< boost::uint64_t > )
8953 {
8954 return value.get_uint64();
8955 }
8956
8957 template< class Value >
8958 double get_value( const Value& value, Type_to_type< double > )
8959 {
8960 return value.get_real();
8961 }
8962
8963 template< class Value >
8964 typename Value::String_type get_value( const Value& value, Type_to_type< typename Value::String_type > )
8965 {
8966 return value.get_str();
8967 }
8968
8969 template< class Value >
8970 typename Value::Array get_value( const Value& value, Type_to_type< typename Value::Array > )
8971 {
8972 return value.get_array();
8973 }
8974
8975 template< class Value >
8976 typename Value::Object get_value( const Value& value, Type_to_type< typename Value::Object > )
8977 {
8978 return value.get_obj();
8979 }
8980
8981 template< class Value >
8982 bool get_value( const Value& value, Type_to_type< bool > )
8983 {
8984 return value.get_bool();
8985 }
8986 }
8987
8988 template< class Config >
8989 template< typename T >
8990 T Value_impl< Config >::get_value() const
8991 {
8992 return internal_::get_value( *this, internal_::Type_to_type< T >() );
8993 }
8994 }
8995
8996 #endif