rt300@0: #ifndef CPPTL_JSON_H_INCLUDED rt300@0: # define CPPTL_JSON_H_INCLUDED rt300@0: rt300@0: # include "forwards.h" rt300@0: # include rt300@0: # include rt300@0: rt300@0: # ifndef JSON_USE_CPPTL_SMALLMAP rt300@0: # include rt300@0: # else rt300@0: # include rt300@0: # endif rt300@0: # ifdef JSON_USE_CPPTL rt300@0: # include rt300@0: # endif rt300@0: rt300@0: /** \brief JSON (JavaScript Object Notation). rt300@0: */ rt300@0: namespace Json { rt300@0: rt300@0: /** \brief Type of the value held by a Value object. rt300@0: */ rt300@0: enum ValueType rt300@0: { rt300@0: nullValue = 0, ///< 'null' value rt300@0: intValue, ///< signed integer value rt300@0: uintValue, ///< unsigned integer value rt300@0: realValue, ///< double value rt300@0: stringValue, ///< UTF-8 string value rt300@0: booleanValue, ///< bool value rt300@0: arrayValue, ///< array value (ordered list) rt300@0: objectValue ///< object value (collection of name/value pairs). rt300@0: }; rt300@0: rt300@0: enum CommentPlacement rt300@0: { rt300@0: commentBefore = 0, ///< a comment placed on the line before a value rt300@0: commentAfterOnSameLine, ///< a comment just after a value on the same line rt300@0: commentAfter, ///< a comment on the line after a value (only make sense for root value) rt300@0: numberOfCommentPlacement rt300@0: }; rt300@0: rt300@0: //# ifdef JSON_USE_CPPTL rt300@0: // typedef CppTL::AnyEnumerator EnumMemberNames; rt300@0: // typedef CppTL::AnyEnumerator EnumValues; rt300@0: //# endif rt300@0: rt300@0: /** \brief Lightweight wrapper to tag static string. rt300@0: * rt300@0: * Value constructor and objectValue member assignement takes advantage of the rt300@0: * StaticString and avoid the cost of string duplication when storing the rt300@0: * string or the member name. rt300@0: * rt300@0: * Example of usage: rt300@0: * \code rt300@0: * Json::Value aValue( StaticString("some text") ); rt300@0: * Json::Value object; rt300@0: * static const StaticString code("code"); rt300@0: * object[code] = 1234; rt300@0: * \endcode rt300@0: */ rt300@0: class JSON_API StaticString rt300@0: { rt300@0: public: rt300@0: explicit StaticString( const char *czstring ) rt300@0: : str_( czstring ) rt300@0: { rt300@0: } rt300@0: rt300@0: operator const char *() const rt300@0: { rt300@0: return str_; rt300@0: } rt300@0: rt300@0: const char *c_str() const rt300@0: { rt300@0: return str_; rt300@0: } rt300@0: rt300@0: private: rt300@0: const char *str_; rt300@0: }; rt300@0: rt300@0: /** \brief Represents a JSON value. rt300@0: * rt300@0: * This class is a discriminated union wrapper that can represents a: rt300@0: * - signed integer [range: Value::minInt - Value::maxInt] rt300@0: * - unsigned integer (range: 0 - Value::maxUInt) rt300@0: * - double rt300@0: * - UTF-8 string rt300@0: * - boolean rt300@0: * - 'null' rt300@0: * - an ordered list of Value rt300@0: * - collection of name/value pairs (javascript object) rt300@0: * rt300@0: * The type of the held value is represented by a #ValueType and rt300@0: * can be obtained using type(). rt300@0: * rt300@0: * values of an #objectValue or #arrayValue can be accessed using operator[]() methods. rt300@0: * Non const methods will automatically create the a #nullValue element rt300@0: * if it does not exist. rt300@0: * The sequence of an #arrayValue will be automatically resize and initialized rt300@0: * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue. rt300@0: * rt300@0: * The get() methods can be used to obtanis default value in the case the required element rt300@0: * does not exist. rt300@0: * rt300@0: * It is possible to iterate over the list of a #objectValue values using rt300@0: * the getMemberNames() method. rt300@0: */ rt300@0: class JSON_API Value rt300@0: { rt300@0: friend class ValueIteratorBase; rt300@0: # ifdef JSON_VALUE_USE_INTERNAL_MAP rt300@0: friend class ValueInternalLink; rt300@0: friend class ValueInternalMap; rt300@0: # endif rt300@0: public: rt300@0: typedef std::vector Members; rt300@0: typedef ValueIterator iterator; rt300@0: typedef ValueConstIterator const_iterator; rt300@0: typedef Json::UInt UInt; rt300@0: typedef Json::Int Int; rt300@0: typedef UInt ArrayIndex; rt300@0: rt300@0: static const Value null; rt300@0: static const Int minInt; rt300@0: static const Int maxInt; rt300@0: static const UInt maxUInt; rt300@0: rt300@0: private: rt300@0: #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION rt300@0: # ifndef JSON_VALUE_USE_INTERNAL_MAP rt300@0: class CZString rt300@0: { rt300@0: public: rt300@0: enum DuplicationPolicy rt300@0: { rt300@0: noDuplication = 0, rt300@0: duplicate, rt300@0: duplicateOnCopy rt300@0: }; rt300@0: CZString( int index ); rt300@0: CZString( const char *cstr, DuplicationPolicy allocate ); rt300@0: CZString( const CZString &other ); rt300@0: ~CZString(); rt300@0: CZString &operator =( const CZString &other ); rt300@0: bool operator<( const CZString &other ) const; rt300@0: bool operator==( const CZString &other ) const; rt300@0: int index() const; rt300@0: const char *c_str() const; rt300@0: bool isStaticString() const; rt300@0: private: rt300@0: void swap( CZString &other ); rt300@0: const char *cstr_; rt300@0: int index_; rt300@0: }; rt300@0: rt300@0: public: rt300@0: # ifndef JSON_USE_CPPTL_SMALLMAP rt300@0: typedef std::map ObjectValues; rt300@0: # else rt300@0: typedef CppTL::SmallMap ObjectValues; rt300@0: # endif // ifndef JSON_USE_CPPTL_SMALLMAP rt300@0: # endif // ifndef JSON_VALUE_USE_INTERNAL_MAP rt300@0: #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION rt300@0: rt300@0: public: rt300@0: /** \brief Create a default Value of the given type. rt300@0: rt300@0: This is a very useful constructor. rt300@0: To create an empty array, pass arrayValue. rt300@0: To create an empty object, pass objectValue. rt300@0: Another Value can then be set to this one by assignment. rt300@0: This is useful since clear() and resize() will not alter types. rt300@0: rt300@0: Examples: rt300@0: \code rt300@0: Json::Value null_value; // null rt300@0: Json::Value arr_value(Json::arrayValue); // [] rt300@0: Json::Value obj_value(Json::objectValue); // {} rt300@0: \endcode rt300@0: */ rt300@0: Value( ValueType type = nullValue ); rt300@0: Value( Int value ); rt300@0: Value( UInt value ); rt300@0: Value( double value ); rt300@0: Value( const char *value ); rt300@0: Value( const char *beginValue, const char *endValue ); rt300@0: /** \brief Constructs a value from a static string. rt300@0: rt300@0: * Like other value string constructor but do not duplicate the string for rt300@0: * internal storage. The given string must remain alive after the call to this rt300@0: * constructor. rt300@0: * Example of usage: rt300@0: * \code rt300@0: * Json::Value aValue( StaticString("some text") ); rt300@0: * \endcode rt300@0: */ rt300@0: Value( const StaticString &value ); rt300@0: Value( const std::string &value ); rt300@0: # ifdef JSON_USE_CPPTL rt300@0: Value( const CppTL::ConstString &value ); rt300@0: # endif rt300@0: Value( bool value ); rt300@0: Value( const Value &other ); rt300@0: ~Value(); rt300@0: rt300@0: Value &operator=( const Value &other ); rt300@0: /// Swap values. rt300@0: /// \note Currently, comments are intentionally not swapped, for rt300@0: /// both logic and efficiency. rt300@0: void swap( Value &other ); rt300@0: rt300@0: ValueType type() const; rt300@0: rt300@0: bool operator <( const Value &other ) const; rt300@0: bool operator <=( const Value &other ) const; rt300@0: bool operator >=( const Value &other ) const; rt300@0: bool operator >( const Value &other ) const; rt300@0: rt300@0: bool operator ==( const Value &other ) const; rt300@0: bool operator !=( const Value &other ) const; rt300@0: rt300@0: int compare( const Value &other ); rt300@0: rt300@0: const char *asCString() const; rt300@0: std::string asString() const; rt300@0: # ifdef JSON_USE_CPPTL rt300@0: CppTL::ConstString asConstString() const; rt300@0: # endif rt300@0: Int asInt() const; rt300@0: UInt asUInt() const; rt300@0: double asDouble() const; rt300@0: bool asBool() const; rt300@0: rt300@0: bool isNull() const; rt300@0: bool isBool() const; rt300@0: bool isInt() const; rt300@0: bool isUInt() const; rt300@0: bool isIntegral() const; rt300@0: bool isDouble() const; rt300@0: bool isNumeric() const; rt300@0: bool isString() const; rt300@0: bool isArray() const; rt300@0: bool isObject() const; rt300@0: rt300@0: bool isConvertibleTo( ValueType other ) const; rt300@0: rt300@0: /// Number of values in array or object rt300@0: UInt size() const; rt300@0: rt300@0: /// \brief Return true if empty array, empty object, or null; rt300@0: /// otherwise, false. rt300@0: bool empty() const; rt300@0: rt300@0: /// Return isNull() rt300@0: bool operator!() const; rt300@0: rt300@0: /// Remove all object members and array elements. rt300@0: /// \pre type() is arrayValue, objectValue, or nullValue rt300@0: /// \post type() is unchanged rt300@0: void clear(); rt300@0: rt300@0: /// Resize the array to size elements. rt300@0: /// New elements are initialized to null. rt300@0: /// May only be called on nullValue or arrayValue. rt300@0: /// \pre type() is arrayValue or nullValue rt300@0: /// \post type() is arrayValue rt300@0: void resize( UInt size ); rt300@0: rt300@0: /// Access an array element (zero based index ). rt300@0: /// If the array contains less than index element, then null value are inserted rt300@0: /// in the array so that its size is index+1. rt300@0: /// (You may need to say 'value[0u]' to get your compiler to distinguish rt300@0: /// this from the operator[] which takes a string.) rt300@0: Value &operator[]( UInt index ); rt300@0: /// Access an array element (zero based index ) rt300@0: /// (You may need to say 'value[0u]' to get your compiler to distinguish rt300@0: /// this from the operator[] which takes a string.) rt300@0: const Value &operator[]( UInt index ) const; rt300@0: /// If the array contains at least index+1 elements, returns the element value, rt300@0: /// otherwise returns defaultValue. rt300@0: Value get( UInt index, rt300@0: const Value &defaultValue ) const; rt300@0: /// Return true if index < size(). rt300@0: bool isValidIndex( UInt index ) const; rt300@0: /// \brief Append value to array at the end. rt300@0: /// rt300@0: /// Equivalent to jsonvalue[jsonvalue.size()] = value; rt300@0: Value &append( const Value &value ); rt300@0: rt300@0: /// Access an object value by name, create a null member if it does not exist. rt300@0: Value &operator[]( const char *key ); rt300@0: /// Access an object value by name, returns null if there is no member with that name. rt300@0: const Value &operator[]( const char *key ) const; rt300@0: /// Access an object value by name, create a null member if it does not exist. rt300@0: Value &operator[]( const std::string &key ); rt300@0: /// Access an object value by name, returns null if there is no member with that name. rt300@0: const Value &operator[]( const std::string &key ) const; rt300@0: /** \brief Access an object value by name, create a null member if it does not exist. rt300@0: rt300@0: * If the object as no entry for that name, then the member name used to store rt300@0: * the new entry is not duplicated. rt300@0: * Example of use: rt300@0: * \code rt300@0: * Json::Value object; rt300@0: * static const StaticString code("code"); rt300@0: * object[code] = 1234; rt300@0: * \endcode rt300@0: */ rt300@0: Value &operator[]( const StaticString &key ); rt300@0: # ifdef JSON_USE_CPPTL rt300@0: /// Access an object value by name, create a null member if it does not exist. rt300@0: Value &operator[]( const CppTL::ConstString &key ); rt300@0: /// Access an object value by name, returns null if there is no member with that name. rt300@0: const Value &operator[]( const CppTL::ConstString &key ) const; rt300@0: # endif rt300@0: /// Return the member named key if it exist, defaultValue otherwise. rt300@0: Value get( const char *key, rt300@0: const Value &defaultValue ) const; rt300@0: /// Return the member named key if it exist, defaultValue otherwise. rt300@0: Value get( const std::string &key, rt300@0: const Value &defaultValue ) const; rt300@0: # ifdef JSON_USE_CPPTL rt300@0: /// Return the member named key if it exist, defaultValue otherwise. rt300@0: Value get( const CppTL::ConstString &key, rt300@0: const Value &defaultValue ) const; rt300@0: # endif rt300@0: /// \brief Remove and return the named member. rt300@0: /// rt300@0: /// Do nothing if it did not exist. rt300@0: /// \return the removed Value, or null. rt300@0: /// \pre type() is objectValue or nullValue rt300@0: /// \post type() is unchanged rt300@0: Value removeMember( const char* key ); rt300@0: /// Same as removeMember(const char*) rt300@0: Value removeMember( const std::string &key ); rt300@0: rt300@0: /// Return true if the object has a member named key. rt300@0: bool isMember( const char *key ) const; rt300@0: /// Return true if the object has a member named key. rt300@0: bool isMember( const std::string &key ) const; rt300@0: # ifdef JSON_USE_CPPTL rt300@0: /// Return true if the object has a member named key. rt300@0: bool isMember( const CppTL::ConstString &key ) const; rt300@0: # endif rt300@0: rt300@0: /// \brief Return a list of the member names. rt300@0: /// rt300@0: /// If null, return an empty list. rt300@0: /// \pre type() is objectValue or nullValue rt300@0: /// \post if type() was nullValue, it remains nullValue rt300@0: Members getMemberNames() const; rt300@0: rt300@0: //# ifdef JSON_USE_CPPTL rt300@0: // EnumMemberNames enumMemberNames() const; rt300@0: // EnumValues enumValues() const; rt300@0: //# endif rt300@0: rt300@0: /// Comments must be //... or /* ... */ rt300@0: void setComment( const char *comment, rt300@0: CommentPlacement placement ); rt300@0: /// Comments must be //... or /* ... */ rt300@0: void setComment( const std::string &comment, rt300@0: CommentPlacement placement ); rt300@0: bool hasComment( CommentPlacement placement ) const; rt300@0: /// Include delimiters and embedded newlines. rt300@0: std::string getComment( CommentPlacement placement ) const; rt300@0: rt300@0: std::string toStyledString() const; rt300@0: rt300@0: const_iterator begin() const; rt300@0: const_iterator end() const; rt300@0: rt300@0: iterator begin(); rt300@0: iterator end(); rt300@0: rt300@0: private: rt300@0: Value &resolveReference( const char *key, rt300@0: bool isStatic ); rt300@0: rt300@0: # ifdef JSON_VALUE_USE_INTERNAL_MAP rt300@0: inline bool isItemAvailable() const rt300@0: { rt300@0: return itemIsUsed_ == 0; rt300@0: } rt300@0: rt300@0: inline void setItemUsed( bool isUsed = true ) rt300@0: { rt300@0: itemIsUsed_ = isUsed ? 1 : 0; rt300@0: } rt300@0: rt300@0: inline bool isMemberNameStatic() const rt300@0: { rt300@0: return memberNameIsStatic_ == 0; rt300@0: } rt300@0: rt300@0: inline void setMemberNameIsStatic( bool isStatic ) rt300@0: { rt300@0: memberNameIsStatic_ = isStatic ? 1 : 0; rt300@0: } rt300@0: # endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP rt300@0: rt300@0: private: rt300@0: struct CommentInfo rt300@0: { rt300@0: CommentInfo(); rt300@0: ~CommentInfo(); rt300@0: rt300@0: void setComment( const char *text ); rt300@0: rt300@0: char *comment_; rt300@0: }; rt300@0: rt300@0: //struct MemberNamesTransform rt300@0: //{ rt300@0: // typedef const char *result_type; rt300@0: // const char *operator()( const CZString &name ) const rt300@0: // { rt300@0: // return name.c_str(); rt300@0: // } rt300@0: //}; rt300@0: rt300@0: union ValueHolder rt300@0: { rt300@0: Int int_; rt300@0: UInt uint_; rt300@0: double real_; rt300@0: bool bool_; rt300@0: char *string_; rt300@0: # ifdef JSON_VALUE_USE_INTERNAL_MAP rt300@0: ValueInternalArray *array_; rt300@0: ValueInternalMap *map_; rt300@0: #else rt300@0: ObjectValues *map_; rt300@0: # endif rt300@0: } value_; rt300@0: ValueType type_ : 8; rt300@0: int allocated_ : 1; // Notes: if declared as bool, bitfield is useless. rt300@0: # ifdef JSON_VALUE_USE_INTERNAL_MAP rt300@0: unsigned int itemIsUsed_ : 1; // used by the ValueInternalMap container. rt300@0: int memberNameIsStatic_ : 1; // used by the ValueInternalMap container. rt300@0: # endif rt300@0: CommentInfo *comments_; rt300@0: }; rt300@0: rt300@0: rt300@0: /** \brief Experimental and untested: represents an element of the "path" to access a node. rt300@0: */ rt300@0: class PathArgument rt300@0: { rt300@0: public: rt300@0: friend class Path; rt300@0: rt300@0: PathArgument(); rt300@0: PathArgument( UInt index ); rt300@0: PathArgument( const char *key ); rt300@0: PathArgument( const std::string &key ); rt300@0: rt300@0: private: rt300@0: enum Kind rt300@0: { rt300@0: kindNone = 0, rt300@0: kindIndex, rt300@0: kindKey rt300@0: }; rt300@0: std::string key_; rt300@0: UInt index_; rt300@0: Kind kind_; rt300@0: }; rt300@0: rt300@0: /** \brief Experimental and untested: represents a "path" to access a node. rt300@0: * rt300@0: * Syntax: rt300@0: * - "." => root node rt300@0: * - ".[n]" => elements at index 'n' of root node (an array value) rt300@0: * - ".name" => member named 'name' of root node (an object value) rt300@0: * - ".name1.name2.name3" rt300@0: * - ".[0][1][2].name1[3]" rt300@0: * - ".%" => member name is provided as parameter rt300@0: * - ".[%]" => index is provied as parameter rt300@0: */ rt300@0: class Path rt300@0: { rt300@0: public: rt300@0: Path( const std::string &path, rt300@0: const PathArgument &a1 = PathArgument(), rt300@0: const PathArgument &a2 = PathArgument(), rt300@0: const PathArgument &a3 = PathArgument(), rt300@0: const PathArgument &a4 = PathArgument(), rt300@0: const PathArgument &a5 = PathArgument() ); rt300@0: rt300@0: const Value &resolve( const Value &root ) const; rt300@0: Value resolve( const Value &root, rt300@0: const Value &defaultValue ) const; rt300@0: /// Creates the "path" to access the specified node and returns a reference on the node. rt300@0: Value &make( Value &root ) const; rt300@0: rt300@0: private: rt300@0: typedef std::vector InArgs; rt300@0: typedef std::vector Args; rt300@0: rt300@0: void makePath( const std::string &path, rt300@0: const InArgs &in ); rt300@0: void addPathInArg( const std::string &path, rt300@0: const InArgs &in, rt300@0: InArgs::const_iterator &itInArg, rt300@0: PathArgument::Kind kind ); rt300@0: void invalidPath( const std::string &path, rt300@0: int location ); rt300@0: rt300@0: Args args_; rt300@0: }; rt300@0: rt300@0: /** \brief Experimental do not use: Allocator to customize member name and string value memory management done by Value. rt300@0: * rt300@0: * - makeMemberName() and releaseMemberName() are called to respectively duplicate and rt300@0: * free an Json::objectValue member name. rt300@0: * - duplicateStringValue() and releaseStringValue() are called similarly to rt300@0: * duplicate and free a Json::stringValue value. rt300@0: */ rt300@0: class ValueAllocator rt300@0: { rt300@0: public: rt300@0: enum { unknown = (unsigned)-1 }; rt300@0: rt300@0: virtual ~ValueAllocator(); rt300@0: rt300@0: virtual char *makeMemberName( const char *memberName ) = 0; rt300@0: virtual void releaseMemberName( char *memberName ) = 0; rt300@0: virtual char *duplicateStringValue( const char *value, rt300@0: unsigned int length = unknown ) = 0; rt300@0: virtual void releaseStringValue( char *value ) = 0; rt300@0: }; rt300@0: rt300@0: #ifdef JSON_VALUE_USE_INTERNAL_MAP rt300@0: /** \brief Allocator to customize Value internal map. rt300@0: * Below is an example of a simple implementation (default implementation actually rt300@0: * use memory pool for speed). rt300@0: * \code rt300@0: class DefaultValueMapAllocator : public ValueMapAllocator rt300@0: { rt300@0: public: // overridden from ValueMapAllocator rt300@0: virtual ValueInternalMap *newMap() rt300@0: { rt300@0: return new ValueInternalMap(); rt300@0: } rt300@0: rt300@0: virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) rt300@0: { rt300@0: return new ValueInternalMap( other ); rt300@0: } rt300@0: rt300@0: virtual void destructMap( ValueInternalMap *map ) rt300@0: { rt300@0: delete map; rt300@0: } rt300@0: rt300@0: virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) rt300@0: { rt300@0: return new ValueInternalLink[size]; rt300@0: } rt300@0: rt300@0: virtual void releaseMapBuckets( ValueInternalLink *links ) rt300@0: { rt300@0: delete [] links; rt300@0: } rt300@0: rt300@0: virtual ValueInternalLink *allocateMapLink() rt300@0: { rt300@0: return new ValueInternalLink(); rt300@0: } rt300@0: rt300@0: virtual void releaseMapLink( ValueInternalLink *link ) rt300@0: { rt300@0: delete link; rt300@0: } rt300@0: }; rt300@0: * \endcode rt300@0: */ rt300@0: class JSON_API ValueMapAllocator rt300@0: { rt300@0: public: rt300@0: virtual ~ValueMapAllocator(); rt300@0: virtual ValueInternalMap *newMap() = 0; rt300@0: virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) = 0; rt300@0: virtual void destructMap( ValueInternalMap *map ) = 0; rt300@0: virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) = 0; rt300@0: virtual void releaseMapBuckets( ValueInternalLink *links ) = 0; rt300@0: virtual ValueInternalLink *allocateMapLink() = 0; rt300@0: virtual void releaseMapLink( ValueInternalLink *link ) = 0; rt300@0: }; rt300@0: rt300@0: /** \brief ValueInternalMap hash-map bucket chain link (for internal use only). rt300@0: * \internal previous_ & next_ allows for bidirectional traversal. rt300@0: */ rt300@0: class JSON_API ValueInternalLink rt300@0: { rt300@0: public: rt300@0: enum { itemPerLink = 6 }; // sizeof(ValueInternalLink) = 128 on 32 bits architecture. rt300@0: enum InternalFlags { rt300@0: flagAvailable = 0, rt300@0: flagUsed = 1 rt300@0: }; rt300@0: rt300@0: ValueInternalLink(); rt300@0: rt300@0: ~ValueInternalLink(); rt300@0: rt300@0: Value items_[itemPerLink]; rt300@0: char *keys_[itemPerLink]; rt300@0: ValueInternalLink *previous_; rt300@0: ValueInternalLink *next_; rt300@0: }; rt300@0: rt300@0: rt300@0: /** \brief A linked page based hash-table implementation used internally by Value. rt300@0: * \internal ValueInternalMap is a tradional bucket based hash-table, with a linked rt300@0: * list in each bucket to handle collision. There is an addional twist in that rt300@0: * each node of the collision linked list is a page containing a fixed amount of rt300@0: * value. This provides a better compromise between memory usage and speed. rt300@0: * rt300@0: * Each bucket is made up of a chained list of ValueInternalLink. The last rt300@0: * link of a given bucket can be found in the 'previous_' field of the following bucket. rt300@0: * The last link of the last bucket is stored in tailLink_ as it has no following bucket. rt300@0: * Only the last link of a bucket may contains 'available' item. The last link always rt300@0: * contains at least one element unless is it the bucket one very first link. rt300@0: */ rt300@0: class JSON_API ValueInternalMap rt300@0: { rt300@0: friend class ValueIteratorBase; rt300@0: friend class Value; rt300@0: public: rt300@0: typedef unsigned int HashKey; rt300@0: typedef unsigned int BucketIndex; rt300@0: rt300@0: # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION rt300@0: struct IteratorState rt300@0: { rt300@0: IteratorState() rt300@0: : map_(0) rt300@0: , link_(0) rt300@0: , itemIndex_(0) rt300@0: , bucketIndex_(0) rt300@0: { rt300@0: } rt300@0: ValueInternalMap *map_; rt300@0: ValueInternalLink *link_; rt300@0: BucketIndex itemIndex_; rt300@0: BucketIndex bucketIndex_; rt300@0: }; rt300@0: # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION rt300@0: rt300@0: ValueInternalMap(); rt300@0: ValueInternalMap( const ValueInternalMap &other ); rt300@0: ValueInternalMap &operator =( const ValueInternalMap &other ); rt300@0: ~ValueInternalMap(); rt300@0: rt300@0: void swap( ValueInternalMap &other ); rt300@0: rt300@0: BucketIndex size() const; rt300@0: rt300@0: void clear(); rt300@0: rt300@0: bool reserveDelta( BucketIndex growth ); rt300@0: rt300@0: bool reserve( BucketIndex newItemCount ); rt300@0: rt300@0: const Value *find( const char *key ) const; rt300@0: rt300@0: Value *find( const char *key ); rt300@0: rt300@0: Value &resolveReference( const char *key, rt300@0: bool isStatic ); rt300@0: rt300@0: void remove( const char *key ); rt300@0: rt300@0: void doActualRemove( ValueInternalLink *link, rt300@0: BucketIndex index, rt300@0: BucketIndex bucketIndex ); rt300@0: rt300@0: ValueInternalLink *&getLastLinkInBucket( BucketIndex bucketIndex ); rt300@0: rt300@0: Value &setNewItem( const char *key, rt300@0: bool isStatic, rt300@0: ValueInternalLink *link, rt300@0: BucketIndex index ); rt300@0: rt300@0: Value &unsafeAdd( const char *key, rt300@0: bool isStatic, rt300@0: HashKey hashedKey ); rt300@0: rt300@0: HashKey hash( const char *key ) const; rt300@0: rt300@0: int compare( const ValueInternalMap &other ) const; rt300@0: rt300@0: private: rt300@0: void makeBeginIterator( IteratorState &it ) const; rt300@0: void makeEndIterator( IteratorState &it ) const; rt300@0: static bool equals( const IteratorState &x, const IteratorState &other ); rt300@0: static void increment( IteratorState &iterator ); rt300@0: static void incrementBucket( IteratorState &iterator ); rt300@0: static void decrement( IteratorState &iterator ); rt300@0: static const char *key( const IteratorState &iterator ); rt300@0: static const char *key( const IteratorState &iterator, bool &isStatic ); rt300@0: static Value &value( const IteratorState &iterator ); rt300@0: static int distance( const IteratorState &x, const IteratorState &y ); rt300@0: rt300@0: private: rt300@0: ValueInternalLink *buckets_; rt300@0: ValueInternalLink *tailLink_; rt300@0: BucketIndex bucketsSize_; rt300@0: BucketIndex itemCount_; rt300@0: }; rt300@0: rt300@0: /** \brief A simplified deque implementation used internally by Value. rt300@0: * \internal rt300@0: * It is based on a list of fixed "page", each page contains a fixed number of items. rt300@0: * Instead of using a linked-list, a array of pointer is used for fast item look-up. rt300@0: * Look-up for an element is as follow: rt300@0: * - compute page index: pageIndex = itemIndex / itemsPerPage rt300@0: * - look-up item in page: pages_[pageIndex][itemIndex % itemsPerPage] rt300@0: * rt300@0: * Insertion is amortized constant time (only the array containing the index of pointers rt300@0: * need to be reallocated when items are appended). rt300@0: */ rt300@0: class JSON_API ValueInternalArray rt300@0: { rt300@0: friend class Value; rt300@0: friend class ValueIteratorBase; rt300@0: public: rt300@0: enum { itemsPerPage = 8 }; // should be a power of 2 for fast divide and modulo. rt300@0: typedef Value::ArrayIndex ArrayIndex; rt300@0: typedef unsigned int PageIndex; rt300@0: rt300@0: # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION rt300@0: struct IteratorState // Must be a POD rt300@0: { rt300@0: IteratorState() rt300@0: : array_(0) rt300@0: , currentPageIndex_(0) rt300@0: , currentItemIndex_(0) rt300@0: { rt300@0: } rt300@0: ValueInternalArray *array_; rt300@0: Value **currentPageIndex_; rt300@0: unsigned int currentItemIndex_; rt300@0: }; rt300@0: # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION rt300@0: rt300@0: ValueInternalArray(); rt300@0: ValueInternalArray( const ValueInternalArray &other ); rt300@0: ValueInternalArray &operator =( const ValueInternalArray &other ); rt300@0: ~ValueInternalArray(); rt300@0: void swap( ValueInternalArray &other ); rt300@0: rt300@0: void clear(); rt300@0: void resize( ArrayIndex newSize ); rt300@0: rt300@0: Value &resolveReference( ArrayIndex index ); rt300@0: rt300@0: Value *find( ArrayIndex index ) const; rt300@0: rt300@0: ArrayIndex size() const; rt300@0: rt300@0: int compare( const ValueInternalArray &other ) const; rt300@0: rt300@0: private: rt300@0: static bool equals( const IteratorState &x, const IteratorState &other ); rt300@0: static void increment( IteratorState &iterator ); rt300@0: static void decrement( IteratorState &iterator ); rt300@0: static Value &dereference( const IteratorState &iterator ); rt300@0: static Value &unsafeDereference( const IteratorState &iterator ); rt300@0: static int distance( const IteratorState &x, const IteratorState &y ); rt300@0: static ArrayIndex indexOf( const IteratorState &iterator ); rt300@0: void makeBeginIterator( IteratorState &it ) const; rt300@0: void makeEndIterator( IteratorState &it ) const; rt300@0: void makeIterator( IteratorState &it, ArrayIndex index ) const; rt300@0: rt300@0: void makeIndexValid( ArrayIndex index ); rt300@0: rt300@0: Value **pages_; rt300@0: ArrayIndex size_; rt300@0: PageIndex pageCount_; rt300@0: }; rt300@0: rt300@0: /** \brief Experimental: do not use. Allocator to customize Value internal array. rt300@0: * Below is an example of a simple implementation (actual implementation use rt300@0: * memory pool). rt300@0: \code rt300@0: class DefaultValueArrayAllocator : public ValueArrayAllocator rt300@0: { rt300@0: public: // overridden from ValueArrayAllocator rt300@0: virtual ~DefaultValueArrayAllocator() rt300@0: { rt300@0: } rt300@0: rt300@0: virtual ValueInternalArray *newArray() rt300@0: { rt300@0: return new ValueInternalArray(); rt300@0: } rt300@0: rt300@0: virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) rt300@0: { rt300@0: return new ValueInternalArray( other ); rt300@0: } rt300@0: rt300@0: virtual void destruct( ValueInternalArray *array ) rt300@0: { rt300@0: delete array; rt300@0: } rt300@0: rt300@0: virtual void reallocateArrayPageIndex( Value **&indexes, rt300@0: ValueInternalArray::PageIndex &indexCount, rt300@0: ValueInternalArray::PageIndex minNewIndexCount ) rt300@0: { rt300@0: ValueInternalArray::PageIndex newIndexCount = (indexCount*3)/2 + 1; rt300@0: if ( minNewIndexCount > newIndexCount ) rt300@0: newIndexCount = minNewIndexCount; rt300@0: void *newIndexes = realloc( indexes, sizeof(Value*) * newIndexCount ); rt300@0: if ( !newIndexes ) rt300@0: throw std::bad_alloc(); rt300@0: indexCount = newIndexCount; rt300@0: indexes = static_cast( newIndexes ); rt300@0: } rt300@0: virtual void releaseArrayPageIndex( Value **indexes, rt300@0: ValueInternalArray::PageIndex indexCount ) rt300@0: { rt300@0: if ( indexes ) rt300@0: free( indexes ); rt300@0: } rt300@0: rt300@0: virtual Value *allocateArrayPage() rt300@0: { rt300@0: return static_cast( malloc( sizeof(Value) * ValueInternalArray::itemsPerPage ) ); rt300@0: } rt300@0: rt300@0: virtual void releaseArrayPage( Value *value ) rt300@0: { rt300@0: if ( value ) rt300@0: free( value ); rt300@0: } rt300@0: }; rt300@0: \endcode rt300@0: */ rt300@0: class JSON_API ValueArrayAllocator rt300@0: { rt300@0: public: rt300@0: virtual ~ValueArrayAllocator(); rt300@0: virtual ValueInternalArray *newArray() = 0; rt300@0: virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) = 0; rt300@0: virtual void destructArray( ValueInternalArray *array ) = 0; rt300@0: /** \brief Reallocate array page index. rt300@0: * Reallocates an array of pointer on each page. rt300@0: * \param indexes [input] pointer on the current index. May be \c NULL. rt300@0: * [output] pointer on the new index of at least rt300@0: * \a minNewIndexCount pages. rt300@0: * \param indexCount [input] current number of pages in the index. rt300@0: * [output] number of page the reallocated index can handle. rt300@0: * \b MUST be >= \a minNewIndexCount. rt300@0: * \param minNewIndexCount Minimum number of page the new index must be able to rt300@0: * handle. rt300@0: */ rt300@0: virtual void reallocateArrayPageIndex( Value **&indexes, rt300@0: ValueInternalArray::PageIndex &indexCount, rt300@0: ValueInternalArray::PageIndex minNewIndexCount ) = 0; rt300@0: virtual void releaseArrayPageIndex( Value **indexes, rt300@0: ValueInternalArray::PageIndex indexCount ) = 0; rt300@0: virtual Value *allocateArrayPage() = 0; rt300@0: virtual void releaseArrayPage( Value *value ) = 0; rt300@0: }; rt300@0: #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP rt300@0: rt300@0: rt300@0: /** \brief base class for Value iterators. rt300@0: * rt300@0: */ rt300@0: class ValueIteratorBase rt300@0: { rt300@0: public: rt300@0: typedef unsigned int size_t; rt300@0: typedef int difference_type; rt300@0: typedef ValueIteratorBase SelfType; rt300@0: rt300@0: ValueIteratorBase(); rt300@0: #ifndef JSON_VALUE_USE_INTERNAL_MAP rt300@0: explicit ValueIteratorBase( const Value::ObjectValues::iterator ¤t ); rt300@0: #else rt300@0: ValueIteratorBase( const ValueInternalArray::IteratorState &state ); rt300@0: ValueIteratorBase( const ValueInternalMap::IteratorState &state ); rt300@0: #endif rt300@0: rt300@0: bool operator ==( const SelfType &other ) const rt300@0: { rt300@0: return isEqual( other ); rt300@0: } rt300@0: rt300@0: bool operator !=( const SelfType &other ) const rt300@0: { rt300@0: return !isEqual( other ); rt300@0: } rt300@0: rt300@0: difference_type operator -( const SelfType &other ) const rt300@0: { rt300@0: return computeDistance( other ); rt300@0: } rt300@0: rt300@0: /// Return either the index or the member name of the referenced value as a Value. rt300@0: Value key() const; rt300@0: rt300@0: /// Return the index of the referenced Value. -1 if it is not an arrayValue. rt300@0: UInt index() const; rt300@0: rt300@0: /// Return the member name of the referenced Value. "" if it is not an objectValue. rt300@0: const char *memberName() const; rt300@0: rt300@0: protected: rt300@0: Value &deref() const; rt300@0: rt300@0: void increment(); rt300@0: rt300@0: void decrement(); rt300@0: rt300@0: difference_type computeDistance( const SelfType &other ) const; rt300@0: rt300@0: bool isEqual( const SelfType &other ) const; rt300@0: rt300@0: void copy( const SelfType &other ); rt300@0: rt300@0: private: rt300@0: #ifndef JSON_VALUE_USE_INTERNAL_MAP rt300@0: Value::ObjectValues::iterator current_; rt300@0: // Indicates that iterator is for a null value. rt300@0: bool isNull_; rt300@0: #else rt300@0: union rt300@0: { rt300@0: ValueInternalArray::IteratorState array_; rt300@0: ValueInternalMap::IteratorState map_; rt300@0: } iterator_; rt300@0: bool isArray_; rt300@0: #endif rt300@0: }; rt300@0: rt300@0: /** \brief const iterator for object and array value. rt300@0: * rt300@0: */ rt300@0: class ValueConstIterator : public ValueIteratorBase rt300@0: { rt300@0: friend class Value; rt300@0: public: rt300@0: typedef unsigned int size_t; rt300@0: typedef int difference_type; rt300@0: typedef const Value &reference; rt300@0: typedef const Value *pointer; rt300@0: typedef ValueConstIterator SelfType; rt300@0: rt300@0: ValueConstIterator(); rt300@0: private: rt300@0: /*! \internal Use by Value to create an iterator. rt300@0: */ rt300@0: #ifndef JSON_VALUE_USE_INTERNAL_MAP rt300@0: explicit ValueConstIterator( const Value::ObjectValues::iterator ¤t ); rt300@0: #else rt300@0: ValueConstIterator( const ValueInternalArray::IteratorState &state ); rt300@0: ValueConstIterator( const ValueInternalMap::IteratorState &state ); rt300@0: #endif rt300@0: public: rt300@0: SelfType &operator =( const ValueIteratorBase &other ); rt300@0: rt300@0: SelfType operator++( int ) rt300@0: { rt300@0: SelfType temp( *this ); rt300@0: ++*this; rt300@0: return temp; rt300@0: } rt300@0: rt300@0: SelfType operator--( int ) rt300@0: { rt300@0: SelfType temp( *this ); rt300@0: --*this; rt300@0: return temp; rt300@0: } rt300@0: rt300@0: SelfType &operator--() rt300@0: { rt300@0: decrement(); rt300@0: return *this; rt300@0: } rt300@0: rt300@0: SelfType &operator++() rt300@0: { rt300@0: increment(); rt300@0: return *this; rt300@0: } rt300@0: rt300@0: reference operator *() const rt300@0: { rt300@0: return deref(); rt300@0: } rt300@0: }; rt300@0: rt300@0: rt300@0: /** \brief Iterator for object and array value. rt300@0: */ rt300@0: class ValueIterator : public ValueIteratorBase rt300@0: { rt300@0: friend class Value; rt300@0: public: rt300@0: typedef unsigned int size_t; rt300@0: typedef int difference_type; rt300@0: typedef Value &reference; rt300@0: typedef Value *pointer; rt300@0: typedef ValueIterator SelfType; rt300@0: rt300@0: ValueIterator(); rt300@0: ValueIterator( const ValueConstIterator &other ); rt300@0: ValueIterator( const ValueIterator &other ); rt300@0: private: rt300@0: /*! \internal Use by Value to create an iterator. rt300@0: */ rt300@0: #ifndef JSON_VALUE_USE_INTERNAL_MAP rt300@0: explicit ValueIterator( const Value::ObjectValues::iterator ¤t ); rt300@0: #else rt300@0: ValueIterator( const ValueInternalArray::IteratorState &state ); rt300@0: ValueIterator( const ValueInternalMap::IteratorState &state ); rt300@0: #endif rt300@0: public: rt300@0: rt300@0: SelfType &operator =( const SelfType &other ); rt300@0: rt300@0: SelfType operator++( int ) rt300@0: { rt300@0: SelfType temp( *this ); rt300@0: ++*this; rt300@0: return temp; rt300@0: } rt300@0: rt300@0: SelfType operator--( int ) rt300@0: { rt300@0: SelfType temp( *this ); rt300@0: --*this; rt300@0: return temp; rt300@0: } rt300@0: rt300@0: SelfType &operator--() rt300@0: { rt300@0: decrement(); rt300@0: return *this; rt300@0: } rt300@0: rt300@0: SelfType &operator++() rt300@0: { rt300@0: increment(); rt300@0: return *this; rt300@0: } rt300@0: rt300@0: reference operator *() const rt300@0: { rt300@0: return deref(); rt300@0: } rt300@0: }; rt300@0: rt300@0: rt300@0: } // namespace Json rt300@0: rt300@0: rt300@0: #endif // CPPTL_JSON_H_INCLUDED