initial code repo
[stor4nfv.git] / src / ceph / src / json_spirit / json_spirit_value.h
diff --git a/src/ceph/src/json_spirit/json_spirit_value.h b/src/ceph/src/json_spirit/json_spirit_value.h
new file mode 100644 (file)
index 0000000..a412636
--- /dev/null
@@ -0,0 +1,584 @@
+#ifndef JSON_SPIRIT_VALUE\r
+#define JSON_SPIRIT_VALUE\r
+\r
+//          Copyright John W. Wilkinson 2007 - 2011\r
+// Distributed under the MIT License, see accompanying file LICENSE.txt\r
+\r
+// json spirit version 4.05\r
+\r
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)\r
+# pragma once\r
+#endif\r
+\r
+#include <vector>\r
+#include <map>\r
+#include <string>\r
+#include <sstream>\r
+#include <stdexcept>\r
+#include <boost/config.hpp> \r
+#include <boost/cstdint.hpp> \r
+#include <boost/shared_ptr.hpp> \r
+#include <boost/variant.hpp> \r
+\r
+// comment out the value types you don't need to reduce build times and intermediate file sizes\r
+#define JSON_SPIRIT_VALUE_ENABLED\r
+//#define JSON_SPIRIT_WVALUE_ENABLED\r
+#define JSON_SPIRIT_MVALUE_ENABLED\r
+//#define JSON_SPIRIT_WMVALUE_ENABLED\r
+\r
+namespace json_spirit\r
+{\r
+    enum Value_type{ obj_type, array_type, str_type, bool_type, int_type, real_type, null_type };\r
+\r
+    struct Null{};\r
+    \r
+    template< class Config >    // Config determines whether the value uses std::string or std::wstring and\r
+                                // whether JSON Objects are represented as vectors or maps\r
+    class Value_impl\r
+    {\r
+    public:\r
+\r
+        typedef Config Config_type;\r
+        typedef typename Config::String_type String_type;\r
+        typedef typename Config::Object_type Object;\r
+        typedef typename Config::Array_type Array;\r
+        typedef typename String_type::const_pointer Const_str_ptr;  // eg const char*\r
+\r
+        Value_impl();  // creates null value\r
+        Value_impl( Const_str_ptr      value ); \r
+        Value_impl( const String_type& value );\r
+        Value_impl( const Object&      value );\r
+        Value_impl( const Array&       value );\r
+        Value_impl( bool               value );\r
+        Value_impl( int                value );\r
+        Value_impl( boost::int64_t     value );\r
+        Value_impl( boost::uint64_t    value );\r
+        Value_impl( double             value );\r
+\r
+        template< class Iter >\r
+        Value_impl( Iter first, Iter last );    // constructor from containers, e.g. std::vector or std::list\r
+\r
+        template< BOOST_VARIANT_ENUM_PARAMS( typename T ) >\r
+        Value_impl( const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& variant ); // constructor for compatible variant types\r
+\r
+        Value_impl( const Value_impl& other );\r
+\r
+        bool operator==( const Value_impl& lhs ) const;\r
+\r
+        Value_impl& operator=( const Value_impl& lhs );\r
+\r
+        Value_type type() const;\r
+\r
+        bool is_uint64() const;\r
+        bool is_null() const;\r
+\r
+        const String_type& get_str()    const;\r
+        const Object&      get_obj()    const;\r
+        const Array&       get_array()  const;\r
+        bool               get_bool()   const;\r
+        int                get_int()    const;\r
+        boost::int64_t     get_int64()  const;\r
+        boost::uint64_t    get_uint64() const;\r
+        double             get_real()   const;\r
+\r
+        Object& get_obj();\r
+        Array&  get_array();\r
+\r
+        template< typename T > T get_value() const;  // example usage: int    i = value.get_value< int >();\r
+                                                     // or             double d = value.get_value< double >();\r
+\r
+        static const Value_impl null;\r
+\r
+    private:\r
+\r
+        void check_type( const Value_type vtype ) const;\r
+\r
+        typedef boost::variant< boost::recursive_wrapper< Object >, boost::recursive_wrapper< Array >, \r
+                                String_type, bool, boost::int64_t, double, Null, boost::uint64_t > Variant;\r
+\r
+        Variant v_;\r
+\r
+        class Variant_converter_visitor : public boost::static_visitor< Variant > \r
+        {\r
+        public:\r
+         \r
+              template< typename T, typename A, template< typename, typename > class Cont >\r
+              Variant operator()( const Cont< T, A >& cont ) const \r
+              {\r
+                  return Array( cont.begin(), cont.end() );\r
+              }\r
+             \r
+              Variant operator()( int i ) const \r
+              {\r
+                  return static_cast< boost::int64_t >( i );\r
+              }\r
+           \r
+              template<class T>\r
+              Variant operator()( const T& t ) const \r
+              {\r
+                  return t;\r
+              }\r
+        };\r
+    };\r
+\r
+    // vector objects\r
+\r
+    template< class Config >\r
+    struct Pair_impl\r
+    {\r
+        typedef typename Config::String_type String_type;\r
+        typedef typename Config::Value_type Value_type;\r
+\r
+        Pair_impl()\r
+        {\r
+        }\r
+\r
+        Pair_impl( const String_type& name, const Value_type& value );\r
+\r
+        bool operator==( const Pair_impl& lhs ) const;\r
+\r
+        String_type name_;\r
+        Value_type value_;\r
+    };\r
+\r
+#if defined( JSON_SPIRIT_VALUE_ENABLED ) || defined( JSON_SPIRIT_WVALUE_ENABLED )\r
+    template< class String >\r
+    struct Config_vector\r
+    {\r
+        typedef String String_type;\r
+        typedef Value_impl< Config_vector > Value_type;\r
+        typedef Pair_impl < Config_vector > Pair_type;\r
+        typedef std::vector< Value_type > Array_type;\r
+        typedef std::vector< Pair_type > Object_type;\r
+\r
+        static Value_type& add( Object_type& obj, const String_type& name, const Value_type& value )\r
+        {\r
+            obj.push_back( Pair_type( name , value ) );\r
+\r
+            return obj.back().value_;\r
+        }\r
+                \r
+        static String_type get_name( const Pair_type& pair )\r
+        {\r
+            return pair.name_;\r
+        }\r
+                \r
+        static Value_type get_value( const Pair_type& pair )\r
+        {\r
+            return pair.value_;\r
+        }\r
+    };\r
+#endif\r
+\r
+    // typedefs for ASCII\r
+\r
+#ifdef JSON_SPIRIT_VALUE_ENABLED\r
+    typedef Config_vector< std::string > Config;\r
+\r
+    typedef Config::Value_type  Value;\r
+    typedef Config::Pair_type   Pair;\r
+    typedef Config::Object_type Object;\r
+    typedef Config::Array_type  Array;\r
+#endif\r
+\r
+    // typedefs for Unicode\r
+\r
+#if defined( JSON_SPIRIT_WVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )\r
+    typedef Config_vector< std::wstring > wConfig;\r
+\r
+    typedef wConfig::Value_type  wValue;\r
+    typedef wConfig::Pair_type   wPair;\r
+    typedef wConfig::Object_type wObject;\r
+    typedef wConfig::Array_type  wArray;\r
+#endif\r
+\r
+    // map objects\r
+\r
+#if defined( JSON_SPIRIT_MVALUE_ENABLED ) || defined( JSON_SPIRIT_WMVALUE_ENABLED )\r
+    template< class String >\r
+    struct Config_map\r
+    {\r
+        typedef String String_type;\r
+        typedef Value_impl< Config_map > Value_type;\r
+        typedef std::vector< Value_type > Array_type;\r
+        typedef std::map< String_type, Value_type > Object_type;\r
+        typedef std::pair< String_type, Value_type > Pair_type;\r
+\r
+        static Value_type& add( Object_type& obj, const String_type& name, const Value_type& value )\r
+        {\r
+            return obj[ name ] = value;\r
+        }\r
+                \r
+        static String_type get_name( const Pair_type& pair )\r
+        {\r
+            return pair.first;\r
+        }\r
+                \r
+        static Value_type get_value( const Pair_type& pair )\r
+        {\r
+            return pair.second;\r
+        }\r
+    };\r
+#endif\r
+\r
+    // typedefs for ASCII\r
+\r
+#ifdef JSON_SPIRIT_MVALUE_ENABLED\r
+    typedef Config_map< std::string > mConfig;\r
+\r
+    typedef mConfig::Value_type  mValue;\r
+    typedef mConfig::Object_type mObject;\r
+    typedef mConfig::Array_type  mArray;\r
+#endif\r
+\r
+    // typedefs for Unicode\r
+\r
+#if defined( JSON_SPIRIT_WMVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )\r
+    typedef Config_map< std::wstring > wmConfig;\r
+\r
+    typedef wmConfig::Value_type  wmValue;\r
+    typedef wmConfig::Object_type wmObject;\r
+    typedef wmConfig::Array_type  wmArray;\r
+#endif\r
+\r
+    ///////////////////////////////////////////////////////////////////////////////////////////////\r
+    //\r
+    // implementation\r
+\r
+    inline bool operator==( const Null&, const Null& )\r
+    {\r
+        return true;\r
+    }\r
+\r
+    template< class Config >\r
+    const Value_impl< Config > Value_impl< Config >::null;\r
+\r
+    template< class Config >\r
+    Value_impl< Config >::Value_impl()\r
+    :   v_( Null() )\r
+    {\r
+    }\r
+\r
+    template< class Config >\r
+    Value_impl< Config >::Value_impl( const Const_str_ptr value )\r
+    :  v_( String_type( value ) )\r
+    {\r
+    }\r
+\r
+    template< class Config >\r
+    Value_impl< Config >::Value_impl( const String_type& value )\r
+    :   v_( value )\r
+    {\r
+    }\r
+\r
+    template< class Config >\r
+    Value_impl< Config >::Value_impl( const Object& value )\r
+    :   v_( value )\r
+    {\r
+    }\r
+\r
+    template< class Config >\r
+    Value_impl< Config >::Value_impl( const Array& value )\r
+    :   v_( value )\r
+    {\r
+    }\r
+\r
+    template< class Config >\r
+    Value_impl< Config >::Value_impl( bool value )\r
+    :   v_( value )\r
+    {\r
+    }\r
+\r
+    template< class Config >\r
+    Value_impl< Config >::Value_impl( int value )\r
+    :   v_( static_cast< boost::int64_t >( value ) )\r
+    {\r
+    }\r
+\r
+    template< class Config >\r
+    Value_impl< Config >::Value_impl( boost::int64_t value )\r
+    :   v_( value )\r
+    {\r
+    }\r
+\r
+    template< class Config >\r
+    Value_impl< Config >::Value_impl( boost::uint64_t value )\r
+    :   v_( value )\r
+    {\r
+    }\r
+\r
+    template< class Config >\r
+    Value_impl< Config >::Value_impl( double value )\r
+    :   v_( value )\r
+    {\r
+    }\r
+\r
+    template< class Config >\r
+    Value_impl< Config >::Value_impl( const Value_impl< Config >& other )\r
+    :   v_( other.v_ )\r
+    {\r
+    }\r
+\r
+    template< class Config >\r
+    template< class Iter >\r
+    Value_impl< Config >::Value_impl( Iter first, Iter last )\r
+    :   v_( Array( first, last ) )\r
+    {\r
+    }\r
+\r
+    template< class Config >\r
+    template< BOOST_VARIANT_ENUM_PARAMS( typename T ) >\r
+    Value_impl< Config >::Value_impl( const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& variant )\r
+    :   v_( boost::apply_visitor( Variant_converter_visitor(), variant) )\r
+    {\r
+    }\r
+\r
+    template< class Config >\r
+    Value_impl< Config >& Value_impl< Config >::operator=( const Value_impl& lhs )\r
+    {\r
+        Value_impl tmp( lhs );\r
+\r
+        std::swap( v_, tmp.v_ );\r
+\r
+        return *this;\r
+    }\r
+\r
+    template< class Config >\r
+    bool Value_impl< Config >::operator==( const Value_impl& lhs ) const\r
+    {\r
+        if( this == &lhs ) return true;\r
+\r
+        if( type() != lhs.type() ) return false;\r
+\r
+        return v_ == lhs.v_; \r
+    }\r
+\r
+    template< class Config >\r
+    Value_type Value_impl< Config >::type() const\r
+    {\r
+        if( is_uint64() )\r
+        {\r
+            return int_type;\r
+        }\r
+\r
+        return static_cast< Value_type >( v_.which() );\r
+    }\r
+\r
+    template< class Config >\r
+    bool Value_impl< Config >::is_uint64() const\r
+    {\r
+        return v_.which() == null_type + 1;\r
+    }\r
+\r
+    template< class Config >\r
+    bool Value_impl< Config >::is_null() const\r
+    {\r
+        return type() == null_type;\r
+    }\r
+\r
+    template< class Config >\r
+    void Value_impl< Config >::check_type( const Value_type vtype ) const\r
+    {\r
+        if( type() != vtype ) \r
+        {\r
+            std::ostringstream os;\r
+\r
+            os << "value type is " << type() << " not " << vtype;\r
+\r
+            throw std::runtime_error( os.str() );\r
+        }\r
+    }\r
+\r
+    template< class Config >\r
+    const typename Config::String_type& Value_impl< Config >::get_str() const\r
+    {\r
+        check_type( str_type );\r
+\r
+        return *boost::get< String_type >( &v_ );\r
+    }\r
+\r
+    template< class Config >\r
+    const typename Value_impl< Config >::Object& Value_impl< Config >::get_obj() const\r
+    {\r
+        check_type( obj_type );\r
+\r
+        return *boost::get< Object >( &v_ );\r
+    }\r
+     \r
+    template< class Config >\r
+    const typename Value_impl< Config >::Array& Value_impl< Config >::get_array() const\r
+    {\r
+        check_type( array_type );\r
+\r
+        return *boost::get< Array >( &v_ );\r
+    }\r
+     \r
+    template< class Config >\r
+    bool Value_impl< Config >::get_bool() const\r
+    {\r
+        check_type( bool_type );\r
+\r
+        return boost::get< bool >( v_ );\r
+    }\r
+     \r
+    template< class Config >\r
+    int Value_impl< Config >::get_int() const\r
+    {\r
+        check_type( int_type );\r
+\r
+        return static_cast< int >( get_int64() );\r
+    }\r
+    \r
+    template< class Config >\r
+    boost::int64_t Value_impl< Config >::get_int64() const\r
+    {\r
+        check_type( int_type );\r
+\r
+        if( is_uint64() )\r
+        {\r
+            return static_cast< boost::int64_t >( get_uint64() );\r
+        }\r
+\r
+        return boost::get< boost::int64_t >( v_ );\r
+    }\r
+    \r
+    template< class Config >\r
+    boost::uint64_t Value_impl< Config >::get_uint64() const\r
+    {\r
+        check_type( int_type );\r
+\r
+        if( !is_uint64() )\r
+        {\r
+            return static_cast< boost::uint64_t >( get_int64() );\r
+        }\r
+\r
+        return boost::get< boost::uint64_t >( v_ );\r
+    }\r
+\r
+    template< class Config >\r
+    double Value_impl< Config >::get_real() const\r
+    {\r
+        if( type() == int_type )\r
+        {\r
+            return is_uint64() ? static_cast< double >( get_uint64() )\r
+                               : static_cast< double >( get_int64() );\r
+        }\r
+\r
+        check_type( real_type );\r
+\r
+        return boost::get< double >( v_ );\r
+    }\r
+\r
+    template< class Config >\r
+    typename Value_impl< Config >::Object& Value_impl< Config >::get_obj()\r
+    {\r
+        check_type( obj_type );\r
+\r
+        return *boost::get< Object >( &v_ );\r
+    }\r
+\r
+    template< class Config >\r
+    typename Value_impl< Config >::Array& Value_impl< Config >::get_array()\r
+    {\r
+        check_type( array_type );\r
+\r
+        return *boost::get< Array >( &v_ );\r
+    }\r
+\r
+    template< class Config >\r
+    Pair_impl< Config >::Pair_impl( const String_type& name, const Value_type& value )\r
+    :   name_( name )\r
+    ,   value_( value )\r
+    {\r
+    }\r
+\r
+    template< class Config >\r
+    bool Pair_impl< Config >::operator==( const Pair_impl< Config >& lhs ) const\r
+    {\r
+        if( this == &lhs ) return true;\r
+\r
+        return ( name_ == lhs.name_ ) && ( value_ == lhs.value_ );\r
+    }\r
+\r
+    // converts a C string, ie. 8 bit char array, to a string object\r
+    //\r
+    template < class String_type >\r
+    String_type to_str( const char* c_str )\r
+    {\r
+        String_type result;\r
+\r
+        for( const char* p = c_str; *p != 0; ++p )\r
+        {\r
+            result += *p;\r
+        }\r
+\r
+        return result;\r
+    }\r
+\r
+    //\r
+\r
+    namespace internal_\r
+    {\r
+        template< typename T >\r
+        struct Type_to_type\r
+        {\r
+        };\r
+\r
+        template< class Value > \r
+        int get_value( const Value& value, Type_to_type< int > )\r
+        {\r
+            return value.get_int();\r
+        }\r
+       \r
+        template< class Value > \r
+        boost::int64_t get_value( const Value& value, Type_to_type< boost::int64_t > )\r
+        {\r
+            return value.get_int64();\r
+        }\r
+       \r
+        template< class Value > \r
+        boost::uint64_t get_value( const Value& value, Type_to_type< boost::uint64_t > )\r
+        {\r
+            return value.get_uint64();\r
+        }\r
+       \r
+        template< class Value > \r
+        double get_value( const Value& value, Type_to_type< double > )\r
+        {\r
+            return value.get_real();\r
+        }\r
+       \r
+        template< class Value > \r
+        typename Value::String_type get_value( const Value& value, Type_to_type< typename Value::String_type > )\r
+        {\r
+            return value.get_str();\r
+        }\r
+       \r
+        template< class Value > \r
+        typename Value::Array get_value( const Value& value, Type_to_type< typename Value::Array > )\r
+        {\r
+            return value.get_array();\r
+        }\r
+       \r
+        template< class Value > \r
+        typename Value::Object get_value( const Value& value, Type_to_type< typename Value::Object > )\r
+        {\r
+            return value.get_obj();\r
+        }\r
+       \r
+        template< class Value > \r
+        bool get_value( const Value& value, Type_to_type< bool > )\r
+        {\r
+            return value.get_bool();\r
+        }\r
+    }\r
+\r
+    template< class Config >\r
+    template< typename T > \r
+    T Value_impl< Config >::get_value() const\r
+    {\r
+        return internal_::get_value( *this, internal_::Type_to_type< T >() );\r
+    }\r
+}\r
+\r
+#endif\r