39 #ifndef CPPTL_JSON_H_INCLUDED
40 # define CPPTL_JSON_H_INCLUDED
42 # include "forwards.h"
46 # ifndef JSON_USE_CPPTL_SMALLMAP
49 # include <cpptl/smallmap.h>
51 # ifdef JSON_USE_CPPTL
52 # include <cpptl/forwards.h>
78 numberOfCommentPlacement
108 operator const char *()
const
113 const char *c_str()
const
152 # ifdef JSON_VALUE_USE_INTERNAL_MAP
153 friend class ValueInternalLink;
154 friend class ValueInternalMap;
157 typedef std::vector<std::string> Members;
160 typedef Json::UInt UInt;
161 typedef Json::Int Int;
162 typedef UInt ArrayIndex;
164 static const Value null;
165 static const Int minInt;
166 static const Int maxInt;
167 static const UInt maxUInt;
170 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
171 # ifndef JSON_VALUE_USE_INTERNAL_MAP
175 enum DuplicationPolicy
182 CZString(
const char *cstr, DuplicationPolicy allocate );
186 bool operator<(
const CZString &other )
const;
187 bool operator==(
const CZString &other )
const;
189 const char *c_str()
const;
190 bool isStaticString()
const;
198 # ifndef JSON_USE_CPPTL_SMALLMAP
199 typedef std::map<CZString, Value> ObjectValues;
201 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
202 # endif // ifndef JSON_USE_CPPTL_SMALLMAP
203 # endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
204 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
225 Value(
double value );
226 Value(
const char *value );
227 Value(
const char *beginValue,
const char *endValue );
239 Value(
const std::string &value );
240 # ifdef JSON_USE_CPPTL
241 Value(
const CppTL::ConstString &value );
251 void swap(
Value &other );
255 bool operator <(
const Value &other )
const;
256 bool operator <=(
const Value &other )
const;
257 bool operator >=(
const Value &other )
const;
258 bool operator >(
const Value &other )
const;
260 bool operator ==(
const Value &other )
const;
261 bool operator !=(
const Value &other )
const;
263 int compare(
const Value &other );
265 const char *asCString()
const;
266 std::string asString()
const;
267 # ifdef JSON_USE_CPPTL
268 CppTL::ConstString asConstString()
const;
272 double asDouble()
const;
279 bool isIntegral()
const;
280 bool isDouble()
const;
281 bool isNumeric()
const;
282 bool isString()
const;
283 bool isArray()
const;
284 bool isObject()
const;
286 bool isConvertibleTo(
ValueType other )
const;
296 bool operator!()
const;
308 void resize( UInt size );
315 Value &operator[]( UInt index );
319 const Value &operator[]( UInt index )
const;
322 Value get( UInt index,
323 const Value &defaultValue )
const;
325 bool isValidIndex( UInt index )
const;
332 Value &operator[](
const char *key );
334 const Value &operator[](
const char *key )
const;
336 Value &operator[](
const std::string &key );
338 const Value &operator[](
const std::string &key )
const;
351 # ifdef JSON_USE_CPPTL
352 Value &operator[](
const CppTL::ConstString &key );
355 const Value &operator[](
const CppTL::ConstString &key )
const;
357 Value get(
const char *key,
359 const Value &defaultValue )
const;
361 Value get(
const std::string &key,
362 const Value &defaultValue )
const;
363 # ifdef JSON_USE_CPPTL
364 Value get(
const CppTL::ConstString &key,
366 const Value &defaultValue )
const;
368 Value removeMember(
const char* key );
376 Value removeMember(
const std::string &key );
379 bool isMember(
const char *key )
const;
381 bool isMember(
const std::string &key )
const;
382 # ifdef JSON_USE_CPPTL
383 bool isMember(
const CppTL::ConstString &key )
const;
392 Members getMemberNames()
const;
400 void setComment(
const char *comment,
403 void setComment(
const std::string &comment,
409 std::string toStyledString()
const;
418 Value &resolveReference(
const char *key,
421 # ifdef JSON_VALUE_USE_INTERNAL_MAP
422 inline bool isItemAvailable()
const
424 return itemIsUsed_ == 0;
427 inline void setItemUsed(
bool isUsed =
true )
429 itemIsUsed_ = isUsed ? 1 : 0;
432 inline bool isMemberNameStatic()
const
434 return memberNameIsStatic_ == 0;
437 inline void setMemberNameIsStatic(
bool isStatic )
439 memberNameIsStatic_ = isStatic ? 1 : 0;
441 # endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP
449 void setComment(
const char *text );
470 # ifdef JSON_VALUE_USE_INTERNAL_MAP
471 ValueInternalArray *array_;
472 ValueInternalMap *map_;
479 # ifdef JSON_VALUE_USE_INTERNAL_MAP
480 unsigned int itemIsUsed_ : 1;
481 int memberNameIsStatic_ : 1;
525 Path(
const std::string &path,
532 const Value &resolve(
const Value &root )
const;
534 const Value &defaultValue )
const;
539 typedef std::vector<const PathArgument *> InArgs;
540 typedef std::vector<PathArgument> Args;
542 void makePath(
const std::string &path,
544 void addPathInArg(
const std::string &path,
546 InArgs::const_iterator &itInArg,
547 PathArgument::Kind kind );
548 void invalidPath(
const std::string &path,
549 int location )
const;
564 enum { unknown = (unsigned)-1 };
568 virtual char *makeMemberName(
const char *memberName ) = 0;
569 virtual void releaseMemberName(
char *memberName ) = 0;
570 virtual char *duplicateStringValue(
const char *value,
571 unsigned int length = unknown ) = 0;
572 virtual void releaseStringValue(
char *value ) = 0;
575 #ifdef JSON_VALUE_USE_INTERNAL_MAP
620 class JSON_API ValueMapAllocator
623 virtual ~ValueMapAllocator();
624 virtual ValueInternalMap *newMap() = 0;
625 virtual ValueInternalMap *newMapCopy(
const ValueInternalMap &other ) = 0;
626 virtual void destructMap( ValueInternalMap *map ) = 0;
627 virtual ValueInternalLink *allocateMapBuckets(
unsigned int size ) = 0;
628 virtual void releaseMapBuckets( ValueInternalLink *links ) = 0;
629 virtual ValueInternalLink *allocateMapLink() = 0;
630 virtual void releaseMapLink( ValueInternalLink *link ) = 0;
636 class JSON_API ValueInternalLink
639 enum { itemPerLink = 6 };
647 ~ValueInternalLink();
649 Value items_[itemPerLink];
650 char *keys_[itemPerLink];
651 ValueInternalLink *previous_;
652 ValueInternalLink *next_;
668 class JSON_API ValueInternalMap
673 typedef unsigned int HashKey;
674 typedef unsigned int BucketIndex;
676 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
686 ValueInternalMap *map_;
687 ValueInternalLink *link_;
688 BucketIndex itemIndex_;
689 BucketIndex bucketIndex_;
691 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
694 ValueInternalMap(
const ValueInternalMap &other );
695 ValueInternalMap &operator =(
const ValueInternalMap &other );
698 void swap( ValueInternalMap &other );
700 BucketIndex size()
const;
704 bool reserveDelta( BucketIndex growth );
706 bool reserve( BucketIndex newItemCount );
708 const Value *find(
const char *key )
const;
710 Value *find(
const char *key );
712 Value &resolveReference(
const char *key,
715 void remove(
const char *key );
717 void doActualRemove( ValueInternalLink *link,
719 BucketIndex bucketIndex );
721 ValueInternalLink *&getLastLinkInBucket( BucketIndex bucketIndex );
723 Value &setNewItem(
const char *key,
725 ValueInternalLink *link,
728 Value &unsafeAdd(
const char *key,
732 HashKey hash(
const char *key )
const;
734 int compare(
const ValueInternalMap &other )
const;
737 void makeBeginIterator( IteratorState &it )
const;
738 void makeEndIterator( IteratorState &it )
const;
739 static bool equals(
const IteratorState &x,
const IteratorState &other );
740 static void increment( IteratorState &iterator );
741 static void incrementBucket( IteratorState &iterator );
742 static void decrement( IteratorState &iterator );
743 static const char *key(
const IteratorState &iterator );
744 static const char *key(
const IteratorState &iterator,
bool &isStatic );
745 static Value &value(
const IteratorState &iterator );
746 static int distance(
const IteratorState &x,
const IteratorState &y );
749 ValueInternalLink *buckets_;
750 ValueInternalLink *tailLink_;
751 BucketIndex bucketsSize_;
752 BucketIndex itemCount_;
766 class JSON_API ValueInternalArray
771 enum { itemsPerPage = 8 };
772 typedef Value::ArrayIndex ArrayIndex;
773 typedef unsigned int PageIndex;
775 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
780 , currentPageIndex_(0)
781 , currentItemIndex_(0)
784 ValueInternalArray *array_;
785 Value **currentPageIndex_;
786 unsigned int currentItemIndex_;
788 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
790 ValueInternalArray();
791 ValueInternalArray(
const ValueInternalArray &other );
792 ValueInternalArray &operator =(
const ValueInternalArray &other );
793 ~ValueInternalArray();
794 void swap( ValueInternalArray &other );
797 void resize( ArrayIndex newSize );
799 Value &resolveReference( ArrayIndex index );
801 Value *find( ArrayIndex index )
const;
803 ArrayIndex size()
const;
805 int compare(
const ValueInternalArray &other )
const;
808 static bool equals(
const IteratorState &x,
const IteratorState &other );
809 static void increment( IteratorState &iterator );
810 static void decrement( IteratorState &iterator );
811 static Value &dereference(
const IteratorState &iterator );
812 static Value &unsafeDereference(
const IteratorState &iterator );
813 static int distance(
const IteratorState &x,
const IteratorState &y );
814 static ArrayIndex indexOf(
const IteratorState &iterator );
815 void makeBeginIterator( IteratorState &it )
const;
816 void makeEndIterator( IteratorState &it )
const;
817 void makeIterator( IteratorState &it, ArrayIndex index )
const;
819 void makeIndexValid( ArrayIndex index );
823 PageIndex pageCount_;
885 class JSON_API ValueArrayAllocator
888 virtual ~ValueArrayAllocator();
889 virtual ValueInternalArray *newArray() = 0;
890 virtual ValueInternalArray *newArrayCopy(
const ValueInternalArray &other ) = 0;
891 virtual void destructArray( ValueInternalArray *array ) = 0;
903 virtual void reallocateArrayPageIndex(
Value **&indexes,
904 ValueInternalArray::PageIndex &indexCount,
905 ValueInternalArray::PageIndex minNewIndexCount ) = 0;
906 virtual void releaseArrayPageIndex(
Value **indexes,
907 ValueInternalArray::PageIndex indexCount ) = 0;
908 virtual Value *allocateArrayPage() = 0;
909 virtual void releaseArrayPage(
Value *value ) = 0;
911 #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP
920 typedef unsigned int size_t;
921 typedef int difference_type;
925 #ifndef JSON_VALUE_USE_INTERNAL_MAP
932 bool operator ==(
const SelfType &other )
const
934 return isEqual( other );
937 bool operator !=(
const SelfType &other )
const
939 return !isEqual( other );
942 difference_type operator -(
const SelfType &other )
const
944 return computeDistance( other );
954 const char *memberName()
const;
957 Value &deref()
const;
963 difference_type computeDistance(
const SelfType &other )
const;
965 bool isEqual(
const SelfType &other )
const;
970 #ifndef JSON_VALUE_USE_INTERNAL_MAP
971 Value::ObjectValues::iterator current_;
977 ValueInternalArray::IteratorState array_;
978 ValueInternalMap::IteratorState map_;
991 typedef unsigned int size_t;
992 typedef int difference_type;
1001 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1049 typedef unsigned int size_t;
1050 typedef int difference_type;
1061 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1062 explicit ValueIterator(
const Value::ObjectValues::iterator ¤t );
1064 ValueIterator(
const ValueInternalArray::IteratorState &state );
1065 ValueIterator(
const ValueInternalMap::IteratorState &state );
1107 #endif // CPPTL_JSON_H_INCLUDED