1 // Copyright 2007-2010 Baptiste Lepilleur
2 // Distributed under MIT license, or public domain if desired and
3 // recognized in your jurisdiction.
4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
6 #ifndef CPPTL_JSON_H_INCLUDED
7 # define CPPTL_JSON_H_INCLUDED
9 #if !defined(JSON_IS_AMALGAMATION)
10 # include "json/forwards.h"
11 #endif // if !defined(JSON_IS_AMALGAMATION)
15 # ifndef JSON_USE_CPPTL_SMALLMAP
18 # include <cpptl/smallmap.h>
20 # ifdef JSON_USE_CPPTL
21 # include <cpptl/forwards.h>
24 /** \brief JSON (JavaScript Object Notation).
28 /** \brief Type of the value held by a Value object.
32 nullValue
= 0, ///< 'null' value
33 intValue
, ///< signed integer value
34 uintValue
, ///< unsigned integer value
35 realValue
, ///< double value
36 stringValue
, ///< UTF-8 string value
37 booleanValue
, ///< bool value
38 arrayValue
, ///< array value (ordered list)
39 objectValue
///< object value (collection of name/value pairs).
44 commentBefore
= 0, ///< a comment placed on the line before a value
45 commentAfterOnSameLine
, ///< a comment just after a value on the same line
46 commentAfter
, ///< a comment on the line after a value (only make sense for root value)
47 numberOfCommentPlacement
50 //# ifdef JSON_USE_CPPTL
51 // typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
52 // typedef CppTL::AnyEnumerator<const Value &> EnumValues;
55 /** \brief Lightweight wrapper to tag static string.
57 * Value constructor and objectValue member assignement takes advantage of the
58 * StaticString and avoid the cost of string duplication when storing the
59 * string or the member name.
63 * Json::Value aValue( StaticString("some text") );
65 * static const StaticString code("code");
66 * object[code] = 1234;
69 class JSON_API StaticString
72 explicit StaticString( const char *czstring
)
77 operator const char *() const
82 const char *c_str() const
91 /** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
93 * This class is a discriminated union wrapper that can represents a:
94 * - signed integer [range: Value::minInt - Value::maxInt]
95 * - unsigned integer (range: 0 - Value::maxUInt)
100 * - an ordered list of Value
101 * - collection of name/value pairs (javascript object)
103 * The type of the held value is represented by a #ValueType and
104 * can be obtained using type().
106 * values of an #objectValue or #arrayValue can be accessed using operator[]() methods.
107 * Non const methods will automatically create the a #nullValue element
108 * if it does not exist.
109 * The sequence of an #arrayValue will be automatically resize and initialized
110 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
112 * The get() methods can be used to obtanis default value in the case the required element
115 * It is possible to iterate over the list of a #objectValue values using
116 * the getMemberNames() method.
120 friend class ValueIteratorBase
;
121 # ifdef JSON_VALUE_USE_INTERNAL_MAP
122 friend class ValueInternalLink
;
123 friend class ValueInternalMap
;
126 typedef std::vector
<std::string
> Members
;
127 typedef ValueIterator iterator
;
128 typedef ValueConstIterator const_iterator
;
129 typedef Json::UInt UInt
;
130 typedef Json::Int Int
;
131 # if defined(JSON_HAS_INT64)
132 typedef Json::UInt64 UInt64
;
133 typedef Json::Int64 Int64
;
134 #endif // defined(JSON_HAS_INT64)
135 typedef Json::LargestInt LargestInt
;
136 typedef Json::LargestUInt LargestUInt
;
137 typedef Json::ArrayIndex ArrayIndex
;
139 static const Value
& null
;
140 /// Minimum signed integer value that can be stored in a Json::Value.
141 static const LargestInt minLargestInt
;
142 /// Maximum signed integer value that can be stored in a Json::Value.
143 static const LargestInt maxLargestInt
;
144 /// Maximum unsigned integer value that can be stored in a Json::Value.
145 static const LargestUInt maxLargestUInt
;
147 /// Minimum signed int value that can be stored in a Json::Value.
148 static const Int minInt
;
149 /// Maximum signed int value that can be stored in a Json::Value.
150 static const Int maxInt
;
151 /// Maximum unsigned int value that can be stored in a Json::Value.
152 static const UInt maxUInt
;
154 # if defined(JSON_HAS_INT64)
155 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
156 static const Int64 minInt64
;
157 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
158 static const Int64 maxInt64
;
159 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
160 static const UInt64 maxUInt64
;
161 #endif // defined(JSON_HAS_INT64)
164 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
165 # ifndef JSON_VALUE_USE_INTERNAL_MAP
169 enum DuplicationPolicy
175 CZString( ArrayIndex index
);
176 CZString( const char *cstr
, DuplicationPolicy allocate
);
177 CZString( const CZString
&other
);
179 CZString
&operator =( const CZString
&other
);
180 bool operator<( const CZString
&other
) const;
181 bool operator==( const CZString
&other
) const;
182 ArrayIndex
index() const;
183 const char *c_str() const;
184 bool isStaticString() const;
186 void swap( CZString
&other
);
192 # ifndef JSON_USE_CPPTL_SMALLMAP
193 typedef std::map
<CZString
, Value
> ObjectValues
;
195 typedef CppTL::SmallMap
<CZString
, Value
> ObjectValues
;
196 # endif // ifndef JSON_USE_CPPTL_SMALLMAP
197 # endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
198 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
201 /** \brief Create a default Value of the given type.
203 This is a very useful constructor.
204 To create an empty array, pass arrayValue.
205 To create an empty object, pass objectValue.
206 Another Value can then be set to this one by assignment.
207 This is useful since clear() and resize() will not alter types.
211 Json::Value null_value; // null
212 Json::Value arr_value(Json::arrayValue); // []
213 Json::Value obj_value(Json::objectValue); // {}
216 Value( ValueType type
= nullValue
);
219 #if defined(JSON_HAS_INT64)
220 Value( Int64 value
);
221 Value( UInt64 value
);
222 #endif // if defined(JSON_HAS_INT64)
223 Value( double value
);
224 Value( const char *value
);
225 Value( const char *beginValue
, const char *endValue
);
226 /** \brief Constructs a value from a static string.
228 * Like other value string constructor but do not duplicate the string for
229 * internal storage. The given string must remain alive after the call to this
233 * Json::Value aValue( StaticString("some text") );
236 Value( const StaticString
&value
);
237 Value( const std::string
&value
);
238 # ifdef JSON_USE_CPPTL
239 Value( const CppTL::ConstString
&value
);
242 Value( const Value
&other
);
245 Value
&operator=( const Value
&other
);
247 /// \note Currently, comments are intentionally not swapped, for
248 /// both logic and efficiency.
249 void swap( Value
&other
);
251 ValueType
type() const;
253 bool operator <( const Value
&other
) const;
254 bool operator <=( const Value
&other
) const;
255 bool operator >=( const Value
&other
) const;
256 bool operator >( const Value
&other
) const;
258 bool operator ==( const Value
&other
) const;
259 bool operator !=( const Value
&other
) const;
261 int compare( const Value
&other
) const;
263 const char *asCString() const;
264 std::string
asString() const;
265 # ifdef JSON_USE_CPPTL
266 CppTL::ConstString
asConstString() const;
270 #if defined(JSON_HAS_INT64)
271 Int64
asInt64() const;
272 UInt64
asUInt64() const;
273 #endif // if defined(JSON_HAS_INT64)
274 LargestInt
asLargestInt() const;
275 LargestUInt
asLargestUInt() const;
276 float asFloat() const;
277 double asDouble() const;
283 bool isInt64() const;
285 bool isUInt64() const;
286 bool isIntegral() const;
287 bool isDouble() const;
288 bool isNumeric() const;
289 bool isString() const;
290 bool isArray() const;
291 bool isObject() const;
293 bool isConvertibleTo( ValueType other
) const;
295 /// Number of values in array or object
296 ArrayIndex
size() const;
298 /// \brief Return true if empty array, empty object, or null;
299 /// otherwise, false.
303 bool operator!() const;
305 /// Remove all object members and array elements.
306 /// \pre type() is arrayValue, objectValue, or nullValue
307 /// \post type() is unchanged
310 /// Resize the array to size elements.
311 /// New elements are initialized to null.
312 /// May only be called on nullValue or arrayValue.
313 /// \pre type() is arrayValue or nullValue
314 /// \post type() is arrayValue
315 void resize( ArrayIndex size
);
317 /// Access an array element (zero based index ).
318 /// If the array contains less than index element, then null value are inserted
319 /// in the array so that its size is index+1.
320 /// (You may need to say 'value[0u]' to get your compiler to distinguish
321 /// this from the operator[] which takes a string.)
322 Value
&operator[]( ArrayIndex index
);
324 /// Access an array element (zero based index ).
325 /// If the array contains less than index element, then null value are inserted
326 /// in the array so that its size is index+1.
327 /// (You may need to say 'value[0u]' to get your compiler to distinguish
328 /// this from the operator[] which takes a string.)
329 Value
&operator[]( int index
);
331 /// Access an array element (zero based index )
332 /// (You may need to say 'value[0u]' to get your compiler to distinguish
333 /// this from the operator[] which takes a string.)
334 const Value
&operator[]( ArrayIndex index
) const;
336 /// Access an array element (zero based index )
337 /// (You may need to say 'value[0u]' to get your compiler to distinguish
338 /// this from the operator[] which takes a string.)
339 const Value
&operator[]( int index
) const;
341 /// If the array contains at least index+1 elements, returns the element value,
342 /// otherwise returns defaultValue.
343 Value
get( ArrayIndex index
,
344 const Value
&defaultValue
) const;
345 /// Return true if index < size().
346 bool isValidIndex( ArrayIndex index
) const;
347 /// \brief Append value to array at the end.
349 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
350 Value
&append( const Value
&value
);
352 /// Access an object value by name, create a null member if it does not exist.
353 Value
&operator[]( const char *key
);
354 /// Access an object value by name, returns null if there is no member with that name.
355 const Value
&operator[]( const char *key
) const;
356 /// Access an object value by name, create a null member if it does not exist.
357 Value
&operator[]( const std::string
&key
);
358 /// Access an object value by name, returns null if there is no member with that name.
359 const Value
&operator[]( const std::string
&key
) const;
360 /** \brief Access an object value by name, create a null member if it does not exist.
362 * If the object as no entry for that name, then the member name used to store
363 * the new entry is not duplicated.
366 * Json::Value object;
367 * static const StaticString code("code");
368 * object[code] = 1234;
371 Value
&operator[]( const StaticString
&key
);
372 # ifdef JSON_USE_CPPTL
373 /// Access an object value by name, create a null member if it does not exist.
374 Value
&operator[]( const CppTL::ConstString
&key
);
375 /// Access an object value by name, returns null if there is no member with that name.
376 const Value
&operator[]( const CppTL::ConstString
&key
) const;
378 /// Return the member named key if it exist, defaultValue otherwise.
379 Value
get( const char *key
,
380 const Value
&defaultValue
) const;
381 /// Return the member named key if it exist, defaultValue otherwise.
382 Value
get( const std::string
&key
,
383 const Value
&defaultValue
) const;
384 # ifdef JSON_USE_CPPTL
385 /// Return the member named key if it exist, defaultValue otherwise.
386 Value
get( const CppTL::ConstString
&key
,
387 const Value
&defaultValue
) const;
389 /// \brief Remove and return the named member.
391 /// Do nothing if it did not exist.
392 /// \return the removed Value, or null.
393 /// \pre type() is objectValue or nullValue
394 /// \post type() is unchanged
395 Value
removeMember( const char* key
);
396 /// Same as removeMember(const char*)
397 Value
removeMember( const std::string
&key
);
399 /// Return true if the object has a member named key.
400 bool isMember( const char *key
) const;
401 /// Return true if the object has a member named key.
402 bool isMember( const std::string
&key
) const;
403 # ifdef JSON_USE_CPPTL
404 /// Return true if the object has a member named key.
405 bool isMember( const CppTL::ConstString
&key
) const;
408 /// \brief Return a list of the member names.
410 /// If null, return an empty list.
411 /// \pre type() is objectValue or nullValue
412 /// \post if type() was nullValue, it remains nullValue
413 Members
getMemberNames() const;
415 //# ifdef JSON_USE_CPPTL
416 // EnumMemberNames enumMemberNames() const;
417 // EnumValues enumValues() const;
420 /// Comments must be //... or /* ... */
421 void setComment( const char *comment
,
422 CommentPlacement placement
);
423 /// Comments must be //... or /* ... */
424 void setComment( const std::string
&comment
,
425 CommentPlacement placement
);
426 bool hasComment( CommentPlacement placement
) const;
427 /// Include delimiters and embedded newlines.
428 std::string
getComment( CommentPlacement placement
) const;
430 std::string
toStyledString() const;
432 const_iterator
begin() const;
433 const_iterator
end() const;
439 Value
&resolveReference( const char *key
,
442 # ifdef JSON_VALUE_USE_INTERNAL_MAP
443 inline bool isItemAvailable() const
445 return itemIsUsed_
== 0;
448 inline void setItemUsed( bool isUsed
= true )
450 itemIsUsed_
= isUsed
? 1 : 0;
453 inline bool isMemberNameStatic() const
455 return memberNameIsStatic_
== 0;
458 inline void setMemberNameIsStatic( bool isStatic
)
460 memberNameIsStatic_
= isStatic
? 1 : 0;
462 # endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP
470 void setComment( const char *text
);
475 //struct MemberNamesTransform
477 // typedef const char *result_type;
478 // const char *operator()( const CZString &name ) const
480 // return name.c_str();
491 # ifdef JSON_VALUE_USE_INTERNAL_MAP
492 ValueInternalArray
*array_
;
493 ValueInternalMap
*map_
;
499 int allocated_
: 1; // Notes: if declared as bool, bitfield is useless.
500 # ifdef JSON_VALUE_USE_INTERNAL_MAP
501 unsigned int itemIsUsed_
: 1; // used by the ValueInternalMap container.
502 int memberNameIsStatic_
: 1; // used by the ValueInternalMap container.
504 CommentInfo
*comments_
;
508 /** \brief Experimental and untested: represents an element of the "path" to access a node.
516 PathArgument( ArrayIndex index
);
517 PathArgument( const char *key
);
518 PathArgument( const std::string
&key
);
532 /** \brief Experimental and untested: represents a "path" to access a node.
536 * - ".[n]" => elements at index 'n' of root node (an array value)
537 * - ".name" => member named 'name' of root node (an object value)
538 * - ".name1.name2.name3"
539 * - ".[0][1][2].name1[3]"
540 * - ".%" => member name is provided as parameter
541 * - ".[%]" => index is provied as parameter
546 Path( const std::string
&path
,
547 const PathArgument
&a1
= PathArgument(),
548 const PathArgument
&a2
= PathArgument(),
549 const PathArgument
&a3
= PathArgument(),
550 const PathArgument
&a4
= PathArgument(),
551 const PathArgument
&a5
= PathArgument() );
553 const Value
&resolve( const Value
&root
) const;
554 Value
resolve( const Value
&root
,
555 const Value
&defaultValue
) const;
556 /// Creates the "path" to access the specified node and returns a reference on the node.
557 Value
&make( Value
&root
) const;
560 typedef std::vector
<const PathArgument
*> InArgs
;
561 typedef std::vector
<PathArgument
> Args
;
563 void makePath( const std::string
&path
,
565 void addPathInArg( const std::string
&path
,
567 InArgs::const_iterator
&itInArg
,
568 PathArgument::Kind kind
);
569 void invalidPath( const std::string
&path
,
577 #ifdef JSON_VALUE_USE_INTERNAL_MAP
578 /** \brief Allocator to customize Value internal map.
579 * Below is an example of a simple implementation (default implementation actually
580 * use memory pool for speed).
582 class DefaultValueMapAllocator : public ValueMapAllocator
584 public: // overridden from ValueMapAllocator
585 virtual ValueInternalMap *newMap()
587 return new ValueInternalMap();
590 virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other )
592 return new ValueInternalMap( other );
595 virtual void destructMap( ValueInternalMap *map )
600 virtual ValueInternalLink *allocateMapBuckets( unsigned int size )
602 return new ValueInternalLink[size];
605 virtual void releaseMapBuckets( ValueInternalLink *links )
610 virtual ValueInternalLink *allocateMapLink()
612 return new ValueInternalLink();
615 virtual void releaseMapLink( ValueInternalLink *link )
622 class JSON_API ValueMapAllocator
625 virtual ~ValueMapAllocator();
626 virtual ValueInternalMap
*newMap() = 0;
627 virtual ValueInternalMap
*newMapCopy( const ValueInternalMap
&other
) = 0;
628 virtual void destructMap( ValueInternalMap
*map
) = 0;
629 virtual ValueInternalLink
*allocateMapBuckets( unsigned int size
) = 0;
630 virtual void releaseMapBuckets( ValueInternalLink
*links
) = 0;
631 virtual ValueInternalLink
*allocateMapLink() = 0;
632 virtual void releaseMapLink( ValueInternalLink
*link
) = 0;
635 /** \brief ValueInternalMap hash-map bucket chain link (for internal use only).
636 * \internal previous_ & next_ allows for bidirectional traversal.
638 class JSON_API ValueInternalLink
641 enum { itemPerLink
= 6 }; // sizeof(ValueInternalLink) = 128 on 32 bits architecture.
649 ~ValueInternalLink();
651 Value items_
[itemPerLink
];
652 char *keys_
[itemPerLink
];
653 ValueInternalLink
*previous_
;
654 ValueInternalLink
*next_
;
658 /** \brief A linked page based hash-table implementation used internally by Value.
659 * \internal ValueInternalMap is a tradional bucket based hash-table, with a linked
660 * list in each bucket to handle collision. There is an addional twist in that
661 * each node of the collision linked list is a page containing a fixed amount of
662 * value. This provides a better compromise between memory usage and speed.
664 * Each bucket is made up of a chained list of ValueInternalLink. The last
665 * link of a given bucket can be found in the 'previous_' field of the following bucket.
666 * The last link of the last bucket is stored in tailLink_ as it has no following bucket.
667 * Only the last link of a bucket may contains 'available' item. The last link always
668 * contains at least one element unless is it the bucket one very first link.
670 class JSON_API ValueInternalMap
672 friend class ValueIteratorBase
;
675 typedef unsigned int HashKey
;
676 typedef unsigned int BucketIndex
;
678 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
688 ValueInternalMap
*map_
;
689 ValueInternalLink
*link_
;
690 BucketIndex itemIndex_
;
691 BucketIndex bucketIndex_
;
693 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
696 ValueInternalMap( const ValueInternalMap
&other
);
697 ValueInternalMap
&operator =( const ValueInternalMap
&other
);
700 void swap( ValueInternalMap
&other
);
702 BucketIndex
size() const;
706 bool reserveDelta( BucketIndex growth
);
708 bool reserve( BucketIndex newItemCount
);
710 const Value
*find( const char *key
) const;
712 Value
*find( const char *key
);
714 Value
&resolveReference( const char *key
,
717 void remove( const char *key
);
719 void doActualRemove( ValueInternalLink
*link
,
721 BucketIndex bucketIndex
);
723 ValueInternalLink
*&getLastLinkInBucket( BucketIndex bucketIndex
);
725 Value
&setNewItem( const char *key
,
727 ValueInternalLink
*link
,
730 Value
&unsafeAdd( const char *key
,
734 HashKey
hash( const char *key
) const;
736 int compare( const ValueInternalMap
&other
) const;
739 void makeBeginIterator( IteratorState
&it
) const;
740 void makeEndIterator( IteratorState
&it
) const;
741 static bool equals( const IteratorState
&x
, const IteratorState
&other
);
742 static void increment( IteratorState
&iterator
);
743 static void incrementBucket( IteratorState
&iterator
);
744 static void decrement( IteratorState
&iterator
);
745 static const char *key( const IteratorState
&iterator
);
746 static const char *key( const IteratorState
&iterator
, bool &isStatic
);
747 static Value
&value( const IteratorState
&iterator
);
748 static int distance( const IteratorState
&x
, const IteratorState
&y
);
751 ValueInternalLink
*buckets_
;
752 ValueInternalLink
*tailLink_
;
753 BucketIndex bucketsSize_
;
754 BucketIndex itemCount_
;
757 /** \brief A simplified deque implementation used internally by Value.
759 * It is based on a list of fixed "page", each page contains a fixed number of items.
760 * Instead of using a linked-list, a array of pointer is used for fast item look-up.
761 * Look-up for an element is as follow:
762 * - compute page index: pageIndex = itemIndex / itemsPerPage
763 * - look-up item in page: pages_[pageIndex][itemIndex % itemsPerPage]
765 * Insertion is amortized constant time (only the array containing the index of pointers
766 * need to be reallocated when items are appended).
768 class JSON_API ValueInternalArray
771 friend class ValueIteratorBase
;
773 enum { itemsPerPage
= 8 }; // should be a power of 2 for fast divide and modulo.
774 typedef Value::ArrayIndex ArrayIndex
;
775 typedef unsigned int PageIndex
;
777 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
778 struct IteratorState
// Must be a POD
782 , currentPageIndex_(0)
783 , currentItemIndex_(0)
786 ValueInternalArray
*array_
;
787 Value
**currentPageIndex_
;
788 unsigned int currentItemIndex_
;
790 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
792 ValueInternalArray();
793 ValueInternalArray( const ValueInternalArray
&other
);
794 ValueInternalArray
&operator =( const ValueInternalArray
&other
);
795 ~ValueInternalArray();
796 void swap( ValueInternalArray
&other
);
799 void resize( ArrayIndex newSize
);
801 Value
&resolveReference( ArrayIndex index
);
803 Value
*find( ArrayIndex index
) const;
805 ArrayIndex
size() const;
807 int compare( const ValueInternalArray
&other
) const;
810 static bool equals( const IteratorState
&x
, const IteratorState
&other
);
811 static void increment( IteratorState
&iterator
);
812 static void decrement( IteratorState
&iterator
);
813 static Value
&dereference( const IteratorState
&iterator
);
814 static Value
&unsafeDereference( const IteratorState
&iterator
);
815 static int distance( const IteratorState
&x
, const IteratorState
&y
);
816 static ArrayIndex
indexOf( const IteratorState
&iterator
);
817 void makeBeginIterator( IteratorState
&it
) const;
818 void makeEndIterator( IteratorState
&it
) const;
819 void makeIterator( IteratorState
&it
, ArrayIndex index
) const;
821 void makeIndexValid( ArrayIndex index
);
825 PageIndex pageCount_
;
828 /** \brief Experimental: do not use. Allocator to customize Value internal array.
829 * Below is an example of a simple implementation (actual implementation use
832 class DefaultValueArrayAllocator : public ValueArrayAllocator
834 public: // overridden from ValueArrayAllocator
835 virtual ~DefaultValueArrayAllocator()
839 virtual ValueInternalArray *newArray()
841 return new ValueInternalArray();
844 virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other )
846 return new ValueInternalArray( other );
849 virtual void destruct( ValueInternalArray *array )
854 virtual void reallocateArrayPageIndex( Value **&indexes,
855 ValueInternalArray::PageIndex &indexCount,
856 ValueInternalArray::PageIndex minNewIndexCount )
858 ValueInternalArray::PageIndex newIndexCount = (indexCount*3)/2 + 1;
859 if ( minNewIndexCount > newIndexCount )
860 newIndexCount = minNewIndexCount;
861 void *newIndexes = realloc( indexes, sizeof(Value*) * newIndexCount );
863 throw std::bad_alloc();
864 indexCount = newIndexCount;
865 indexes = static_cast<Value **>( newIndexes );
867 virtual void releaseArrayPageIndex( Value **indexes,
868 ValueInternalArray::PageIndex indexCount )
874 virtual Value *allocateArrayPage()
876 return static_cast<Value *>( malloc( sizeof(Value) * ValueInternalArray::itemsPerPage ) );
879 virtual void releaseArrayPage( Value *value )
887 class JSON_API ValueArrayAllocator
890 virtual ~ValueArrayAllocator();
891 virtual ValueInternalArray
*newArray() = 0;
892 virtual ValueInternalArray
*newArrayCopy( const ValueInternalArray
&other
) = 0;
893 virtual void destructArray( ValueInternalArray
*array
) = 0;
894 /** \brief Reallocate array page index.
895 * Reallocates an array of pointer on each page.
896 * \param indexes [input] pointer on the current index. May be \c NULL.
897 * [output] pointer on the new index of at least
898 * \a minNewIndexCount pages.
899 * \param indexCount [input] current number of pages in the index.
900 * [output] number of page the reallocated index can handle.
901 * \b MUST be >= \a minNewIndexCount.
902 * \param minNewIndexCount Minimum number of page the new index must be able to
905 virtual void reallocateArrayPageIndex( Value
**&indexes
,
906 ValueInternalArray::PageIndex
&indexCount
,
907 ValueInternalArray::PageIndex minNewIndexCount
) = 0;
908 virtual void releaseArrayPageIndex( Value
**indexes
,
909 ValueInternalArray::PageIndex indexCount
) = 0;
910 virtual Value
*allocateArrayPage() = 0;
911 virtual void releaseArrayPage( Value
*value
) = 0;
913 #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP
916 /** \brief base class for Value iterators.
919 class ValueIteratorBase
922 typedef unsigned int size_t;
923 typedef int difference_type
;
924 typedef ValueIteratorBase SelfType
;
927 #ifndef JSON_VALUE_USE_INTERNAL_MAP
928 explicit ValueIteratorBase( const Value::ObjectValues::iterator
¤t
);
930 ValueIteratorBase( const ValueInternalArray::IteratorState
&state
);
931 ValueIteratorBase( const ValueInternalMap::IteratorState
&state
);
934 bool operator ==( const SelfType
&other
) const
936 return isEqual( other
);
939 bool operator !=( const SelfType
&other
) const
941 return !isEqual( other
);
944 difference_type
operator -( const SelfType
&other
) const
946 return computeDistance( other
);
949 /// Return either the index or the member name of the referenced value as a Value.
952 /// Return the index of the referenced Value. -1 if it is not an arrayValue.
955 /// Return the member name of the referenced Value. "" if it is not an objectValue.
956 const char *memberName() const;
959 Value
&deref() const;
965 difference_type
computeDistance( const SelfType
&other
) const;
967 bool isEqual( const SelfType
&other
) const;
969 void copy( const SelfType
&other
);
972 #ifndef JSON_VALUE_USE_INTERNAL_MAP
973 Value::ObjectValues::iterator current_
;
974 // Indicates that iterator is for a null value.
979 ValueInternalArray::IteratorState array_
;
980 ValueInternalMap::IteratorState map_
;
986 /** \brief const iterator for object and array value.
989 class ValueConstIterator
: public ValueIteratorBase
993 typedef unsigned int size_t;
994 typedef int difference_type
;
995 typedef const Value
&reference
;
996 typedef const Value
*pointer
;
997 typedef ValueConstIterator SelfType
;
999 ValueConstIterator();
1001 /*! \internal Use by Value to create an iterator.
1003 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1004 explicit ValueConstIterator( const Value::ObjectValues::iterator
¤t
);
1006 ValueConstIterator( const ValueInternalArray::IteratorState
&state
);
1007 ValueConstIterator( const ValueInternalMap::IteratorState
&state
);
1010 SelfType
&operator =( const ValueIteratorBase
&other
);
1012 SelfType
operator++( int )
1014 SelfType
temp( *this );
1019 SelfType
operator--( int )
1021 SelfType
temp( *this );
1026 SelfType
&operator--()
1032 SelfType
&operator++()
1038 reference
operator *() const
1045 /** \brief Iterator for object and array value.
1047 class ValueIterator
: public ValueIteratorBase
1051 typedef unsigned int size_t;
1052 typedef int difference_type
;
1053 typedef Value
&reference
;
1054 typedef Value
*pointer
;
1055 typedef ValueIterator SelfType
;
1058 ValueIterator( const ValueConstIterator
&other
);
1059 ValueIterator( const ValueIterator
&other
);
1061 /*! \internal Use by Value to create an iterator.
1063 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1064 explicit ValueIterator( const Value::ObjectValues::iterator
¤t
);
1066 ValueIterator( const ValueInternalArray::IteratorState
&state
);
1067 ValueIterator( const ValueInternalMap::IteratorState
&state
);
1071 SelfType
&operator =( const SelfType
&other
);
1073 SelfType
operator++( int )
1075 SelfType
temp( *this );
1080 SelfType
operator--( int )
1082 SelfType
temp( *this );
1087 SelfType
&operator--()
1093 SelfType
&operator++()
1099 reference
operator *() const
1109 #endif // CPPTL_JSON_H_INCLUDED