Path: blob/master/Utilities/cmjsoncpp/include/json/value.h
3156 views
// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors1// Distributed under MIT license, or public domain if desired and2// recognized in your jurisdiction.3// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE45#ifndef JSON_VALUE_H_INCLUDED6#define JSON_VALUE_H_INCLUDED78#if !defined(JSON_IS_AMALGAMATION)9#include "forwards.h"10#endif // if !defined(JSON_IS_AMALGAMATION)1112// Conditional NORETURN attribute on the throw functions would:13// a) suppress false positives from static code analysis14// b) possibly improve optimization opportunities.15#if !defined(JSONCPP_NORETURN)16#if defined(_MSC_VER) && _MSC_VER == 180017#define JSONCPP_NORETURN __declspec(noreturn)18#else19#define JSONCPP_NORETURN [[noreturn]]20#endif21#endif2223// Support for '= delete' with template declarations was a late addition24// to the c++11 standard and is rejected by clang 3.8 and Apple clang 8.225// even though these declare themselves to be c++11 compilers.26#if !defined(JSONCPP_TEMPLATE_DELETE)27#if defined(__clang__) && defined(__apple_build_version__)28#if __apple_build_version__ <= 800004229#define JSONCPP_TEMPLATE_DELETE30#endif31#elif defined(__clang__)32#if __clang_major__ == 3 && __clang_minor__ <= 833#define JSONCPP_TEMPLATE_DELETE34#endif35#elif defined(__EDG__) && defined(__LCC__)36#if __LCC__ < 12337#define JSONCPP_TEMPLATE_DELETE38#endif39#endif40#if !defined(JSONCPP_TEMPLATE_DELETE)41#define JSONCPP_TEMPLATE_DELETE = delete42#endif43#endif4445#include <array>46#include <exception>47#include <map>48#include <memory>49#include <string>50#include <vector>5152// Disable warning C4251: <data member>: <type> needs to have dll-interface to53// be used by...54#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)55#pragma warning(push)56#pragma warning(disable : 4251 4275)57#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)5859#if !defined(__SUNPRO_CC)60#pragma pack(push)61#pragma pack()62#endif6364/** \brief JSON (JavaScript Object Notation).65*/66namespace Json {6768#if JSON_USE_EXCEPTION69/** Base class for all exceptions we throw.70*71* We use nothing but these internally. Of course, STL can throw others.72*/73class JSON_API Exception : public std::exception {74public:75Exception(String msg);76~Exception() noexcept override;77char const* what() const noexcept override;7879protected:80String msg_;81};8283/** Exceptions which the user cannot easily avoid.84*85* E.g. out-of-memory (when we use malloc), stack-overflow, malicious input86*87* \remark derived from Json::Exception88*/89class JSON_API RuntimeError : public Exception {90public:91RuntimeError(String const& msg);92};9394/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.95*96* These are precondition-violations (user bugs) and internal errors (our bugs).97*98* \remark derived from Json::Exception99*/100class JSON_API LogicError : public Exception {101public:102LogicError(String const& msg);103};104#endif105106/// used internally107JSONCPP_NORETURN void throwRuntimeError(String const& msg);108/// used internally109JSONCPP_NORETURN void throwLogicError(String const& msg);110111/** \brief Type of the value held by a Value object.112*/113enum ValueType {114nullValue = 0, ///< 'null' value115intValue, ///< signed integer value116uintValue, ///< unsigned integer value117realValue, ///< double value118stringValue, ///< UTF-8 string value119booleanValue, ///< bool value120arrayValue, ///< array value (ordered list)121objectValue ///< object value (collection of name/value pairs).122};123124enum CommentPlacement {125commentBefore = 0, ///< a comment placed on the line before a value126commentAfterOnSameLine, ///< a comment just after a value on the same line127commentAfter, ///< a comment on the line after a value (only make sense for128/// root value)129numberOfCommentPlacement130};131132/** \brief Type of precision for formatting of real values.133*/134enum PrecisionType {135significantDigits = 0, ///< we set max number of significant digits in string136decimalPlaces ///< we set max number of digits after "." in string137};138139/** \brief Lightweight wrapper to tag static string.140*141* Value constructor and objectValue member assignment takes advantage of the142* StaticString and avoid the cost of string duplication when storing the143* string or the member name.144*145* Example of usage:146* \code147* Json::Value aValue( StaticString("some text") );148* Json::Value object;149* static const StaticString code("code");150* object[code] = 1234;151* \endcode152*/153class JSON_API StaticString {154public:155explicit StaticString(const char* czstring) : c_str_(czstring) {}156157operator const char*() const { return c_str_; }158159const char* c_str() const { return c_str_; }160161private:162const char* c_str_;163};164165/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.166*167* This class is a discriminated union wrapper that can represents a:168* - signed integer [range: Value::minInt - Value::maxInt]169* - unsigned integer (range: 0 - Value::maxUInt)170* - double171* - UTF-8 string172* - boolean173* - 'null'174* - an ordered list of Value175* - collection of name/value pairs (javascript object)176*177* The type of the held value is represented by a #ValueType and178* can be obtained using type().179*180* Values of an #objectValue or #arrayValue can be accessed using operator[]()181* methods.182* Non-const methods will automatically create the a #nullValue element183* if it does not exist.184* The sequence of an #arrayValue will be automatically resized and initialized185* with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.186*187* The get() methods can be used to obtain default value in the case the188* required element does not exist.189*190* It is possible to iterate over the list of member keys of an object using191* the getMemberNames() method.192*193* \note #Value string-length fit in size_t, but keys must be < 2^30.194* (The reason is an implementation detail.) A #CharReader will raise an195* exception if a bound is exceeded to avoid security holes in your app,196* but the Value API does *not* check bounds. That is the responsibility197* of the caller.198*/199class JSON_API Value {200friend class ValueIteratorBase;201202public:203using Members = std::vector<String>;204using iterator = ValueIterator;205using const_iterator = ValueConstIterator;206using UInt = Json::UInt;207using Int = Json::Int;208#if defined(JSON_HAS_INT64)209using UInt64 = Json::UInt64;210using Int64 = Json::Int64;211#endif // defined(JSON_HAS_INT64)212using LargestInt = Json::LargestInt;213using LargestUInt = Json::LargestUInt;214using ArrayIndex = Json::ArrayIndex;215216// Required for boost integration, e. g. BOOST_TEST217using value_type = std::string;218219#if JSON_USE_NULLREF220// Binary compatibility kludges, do not use.221static const Value& null;222static const Value& nullRef;223#endif224225// null and nullRef are deprecated, use this instead.226static Value const& nullSingleton();227228/// Minimum signed integer value that can be stored in a Json::Value.229static constexpr LargestInt minLargestInt =230LargestInt(~(LargestUInt(-1) / 2));231/// Maximum signed integer value that can be stored in a Json::Value.232static constexpr LargestInt maxLargestInt = LargestInt(LargestUInt(-1) / 2);233/// Maximum unsigned integer value that can be stored in a Json::Value.234static constexpr LargestUInt maxLargestUInt = LargestUInt(-1);235236/// Minimum signed int value that can be stored in a Json::Value.237static constexpr Int minInt = Int(~(UInt(-1) / 2));238/// Maximum signed int value that can be stored in a Json::Value.239static constexpr Int maxInt = Int(UInt(-1) / 2);240/// Maximum unsigned int value that can be stored in a Json::Value.241static constexpr UInt maxUInt = UInt(-1);242243#if defined(JSON_HAS_INT64)244/// Minimum signed 64 bits int value that can be stored in a Json::Value.245static constexpr Int64 minInt64 = Int64(~(UInt64(-1) / 2));246/// Maximum signed 64 bits int value that can be stored in a Json::Value.247static constexpr Int64 maxInt64 = Int64(UInt64(-1) / 2);248/// Maximum unsigned 64 bits int value that can be stored in a Json::Value.249static constexpr UInt64 maxUInt64 = UInt64(-1);250#endif // defined(JSON_HAS_INT64)251/// Default precision for real value for string representation.252static constexpr UInt defaultRealPrecision = 17;253// The constant is hard-coded because some compiler have trouble254// converting Value::maxUInt64 to a double correctly (AIX/xlC).255// Assumes that UInt64 is a 64 bits integer.256static constexpr double maxUInt64AsDouble = 18446744073709551615.0;257// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler258// when using gcc and clang backend compilers. CZString259// cannot be defined as private. See issue #486260#ifdef __NVCC__261public:262#else263private:264#endif265#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION266class CZString {267public:268enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };269CZString(ArrayIndex index);270CZString(char const* str, unsigned length, DuplicationPolicy allocate);271CZString(CZString const& other);272CZString(CZString&& other) noexcept;273~CZString();274CZString& operator=(const CZString& other);275CZString& operator=(CZString&& other) noexcept;276277bool operator<(CZString const& other) const;278bool operator==(CZString const& other) const;279ArrayIndex index() const;280// const char* c_str() const; ///< deprecated281char const* data() const;282unsigned length() const;283bool isStaticString() const;284285private:286void swap(CZString& other);287288struct StringStorage {289unsigned policy_ : 2;290unsigned length_ : 30; // 1GB max291};292293char const* cstr_; // actually, a prefixed string, unless policy is noDup294union {295ArrayIndex index_;296StringStorage storage_;297};298};299300public:301typedef std::map<CZString, Value> ObjectValues;302#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION303304public:305/**306* \brief Create a default Value of the given type.307*308* This is a very useful constructor.309* To create an empty array, pass arrayValue.310* To create an empty object, pass objectValue.311* Another Value can then be set to this one by assignment.312* This is useful since clear() and resize() will not alter types.313*314* Examples:315* \code316* Json::Value null_value; // null317* Json::Value arr_value(Json::arrayValue); // []318* Json::Value obj_value(Json::objectValue); // {}319* \endcode320*/321Value(ValueType type = nullValue);322Value(Int value);323Value(UInt value);324#if defined(JSON_HAS_INT64)325Value(Int64 value);326Value(UInt64 value);327#endif // if defined(JSON_HAS_INT64)328Value(double value);329Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)330Value(const char* begin, const char* end); ///< Copy all, incl zeroes.331/**332* \brief Constructs a value from a static string.333*334* Like other value string constructor but do not duplicate the string for335* internal storage. The given string must remain alive after the call to336* this constructor.337*338* \note This works only for null-terminated strings. (We cannot change the339* size of this class, so we have nowhere to store the length, which might be340* computed later for various operations.)341*342* Example of usage:343* \code344* static StaticString foo("some text");345* Json::Value aValue(foo);346* \endcode347*/348Value(const StaticString& value);349Value(const String& value);350Value(bool value);351Value(std::nullptr_t ptr) = delete;352Value(const Value& other);353Value(Value&& other) noexcept;354~Value();355356/// \note Overwrite existing comments. To preserve comments, use357/// #swapPayload().358Value& operator=(const Value& other);359Value& operator=(Value&& other) noexcept;360361/// Swap everything.362void swap(Value& other);363/// Swap values but leave comments and source offsets in place.364void swapPayload(Value& other);365366/// copy everything.367void copy(const Value& other);368/// copy values but leave comments and source offsets in place.369void copyPayload(const Value& other);370371ValueType type() const;372373/// Compare payload only, not comments etc.374bool operator<(const Value& other) const;375bool operator<=(const Value& other) const;376bool operator>=(const Value& other) const;377bool operator>(const Value& other) const;378bool operator==(const Value& other) const;379bool operator!=(const Value& other) const;380int compare(const Value& other) const;381382const char* asCString() const; ///< Embedded zeroes could cause you trouble!383#if JSONCPP_USING_SECURE_MEMORY384unsigned getCStringLength() const; // Allows you to understand the length of385// the CString386#endif387String asString() const; ///< Embedded zeroes are possible.388/** Get raw char* of string-value.389* \return false if !string. (Seg-fault if str or end are NULL.)390*/391bool getString(char const** begin, char const** end) const;392Int asInt() const;393UInt asUInt() const;394#if defined(JSON_HAS_INT64)395Int64 asInt64() const;396UInt64 asUInt64() const;397#endif // if defined(JSON_HAS_INT64)398LargestInt asLargestInt() const;399LargestUInt asLargestUInt() const;400float asFloat() const;401double asDouble() const;402bool asBool() const;403404bool isNull() const;405bool isBool() const;406bool isInt() const;407bool isInt64() const;408bool isUInt() const;409bool isUInt64() const;410bool isIntegral() const;411bool isDouble() const;412bool isNumeric() const;413bool isString() const;414bool isArray() const;415bool isObject() const;416417/// The `as<T>` and `is<T>` member function templates and specializations.418template <typename T> T as() const JSONCPP_TEMPLATE_DELETE;419template <typename T> bool is() const JSONCPP_TEMPLATE_DELETE;420421bool isConvertibleTo(ValueType other) const;422423/// Number of values in array or object424ArrayIndex size() const;425426/// \brief Return true if empty array, empty object, or null;427/// otherwise, false.428bool empty() const;429430/// Return !isNull()431explicit operator bool() const;432433/// Remove all object members and array elements.434/// \pre type() is arrayValue, objectValue, or nullValue435/// \post type() is unchanged436void clear();437438/// Resize the array to newSize elements.439/// New elements are initialized to null.440/// May only be called on nullValue or arrayValue.441/// \pre type() is arrayValue or nullValue442/// \post type() is arrayValue443void resize(ArrayIndex newSize);444445///@{446/// Access an array element (zero based index). If the array contains less447/// than index element, then null value are inserted in the array so that448/// its size is index+1.449/// (You may need to say 'value[0u]' to get your compiler to distinguish450/// this from the operator[] which takes a string.)451Value& operator[](ArrayIndex index);452Value& operator[](int index);453///@}454455///@{456/// Access an array element (zero based index).457/// (You may need to say 'value[0u]' to get your compiler to distinguish458/// this from the operator[] which takes a string.)459const Value& operator[](ArrayIndex index) const;460const Value& operator[](int index) const;461///@}462463/// If the array contains at least index+1 elements, returns the element464/// value, otherwise returns defaultValue.465Value get(ArrayIndex index, const Value& defaultValue) const;466/// Return true if index < size().467bool isValidIndex(ArrayIndex index) const;468/// \brief Append value to array at the end.469///470/// Equivalent to jsonvalue[jsonvalue.size()] = value;471Value& append(const Value& value);472Value& append(Value&& value);473474/// \brief Insert value in array at specific index475bool insert(ArrayIndex index, const Value& newValue);476bool insert(ArrayIndex index, Value&& newValue);477478/// Access an object value by name, create a null member if it does not exist.479/// \note Because of our implementation, keys are limited to 2^30 -1 chars.480/// Exceeding that will cause an exception.481Value& operator[](const char* key);482/// Access an object value by name, returns null if there is no member with483/// that name.484const Value& operator[](const char* key) const;485/// Access an object value by name, create a null member if it does not exist.486/// \param key may contain embedded nulls.487Value& operator[](const String& key);488/// Access an object value by name, returns null if there is no member with489/// that name.490/// \param key may contain embedded nulls.491const Value& operator[](const String& key) const;492/** \brief Access an object value by name, create a null member if it does not493* exist.494*495* If the object has no entry for that name, then the member name used to496* store the new entry is not duplicated.497* Example of use:498* \code499* Json::Value object;500* static const StaticString code("code");501* object[code] = 1234;502* \endcode503*/504Value& operator[](const StaticString& key);505/// Return the member named key if it exist, defaultValue otherwise.506/// \note deep copy507Value get(const char* key, const Value& defaultValue) const;508/// Return the member named key if it exist, defaultValue otherwise.509/// \note deep copy510/// \note key may contain embedded nulls.511Value get(const char* begin, const char* end,512const Value& defaultValue) const;513/// Return the member named key if it exist, defaultValue otherwise.514/// \note deep copy515/// \param key may contain embedded nulls.516Value get(const String& key, const Value& defaultValue) const;517/// Most general and efficient version of isMember()const, get()const,518/// and operator[]const519/// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30520Value const* find(char const* begin, char const* end) const;521/// Most general and efficient version of isMember()const, get()const,522/// and operator[]const523Value const* find(const String& key) const;524/// Most general and efficient version of object-mutators.525/// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30526/// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.527Value* demand(char const* begin, char const* end);528/// \brief Remove and return the named member.529///530/// Do nothing if it did not exist.531/// \pre type() is objectValue or nullValue532/// \post type() is unchanged533void removeMember(const char* key);534/// Same as removeMember(const char*)535/// \param key may contain embedded nulls.536void removeMember(const String& key);537/// Same as removeMember(const char* begin, const char* end, Value* removed),538/// but 'key' is null-terminated.539bool removeMember(const char* key, Value* removed);540/** \brief Remove the named map member.541*542* Update 'removed' iff removed.543* \param key may contain embedded nulls.544* \return true iff removed (no exceptions)545*/546bool removeMember(String const& key, Value* removed);547/// Same as removeMember(String const& key, Value* removed)548bool removeMember(const char* begin, const char* end, Value* removed);549/** \brief Remove the indexed array element.550*551* O(n) expensive operations.552* Update 'removed' iff removed.553* \return true if removed (no exceptions)554*/555bool removeIndex(ArrayIndex index, Value* removed);556557/// Return true if the object has a member named key.558/// \note 'key' must be null-terminated.559bool isMember(const char* key) const;560/// Return true if the object has a member named key.561/// \param key may contain embedded nulls.562bool isMember(const String& key) const;563/// Same as isMember(String const& key)const564bool isMember(const char* begin, const char* end) const;565566/// \brief Return a list of the member names.567///568/// If null, return an empty list.569/// \pre type() is objectValue or nullValue570/// \post if type() was nullValue, it remains nullValue571Members getMemberNames() const;572573/// deprecated Always pass len.574JSONCPP_DEPRECATED("Use setComment(String const&) instead.")575void setComment(const char* comment, CommentPlacement placement) {576setComment(String(comment, strlen(comment)), placement);577}578/// Comments must be //... or /* ... */579void setComment(const char* comment, size_t len, CommentPlacement placement) {580setComment(String(comment, len), placement);581}582/// Comments must be //... or /* ... */583void setComment(String comment, CommentPlacement placement);584bool hasComment(CommentPlacement placement) const;585/// Include delimiters and embedded newlines.586String getComment(CommentPlacement placement) const;587588String toStyledString() const;589590const_iterator begin() const;591const_iterator end() const;592593iterator begin();594iterator end();595596/// \brief Returns a reference to the first element in the `Value`.597/// Requires that this value holds an array or json object, with at least one598/// element.599const Value& front() const;600601/// \brief Returns a reference to the first element in the `Value`.602/// Requires that this value holds an array or json object, with at least one603/// element.604Value& front();605606/// \brief Returns a reference to the last element in the `Value`.607/// Requires that value holds an array or json object, with at least one608/// element.609const Value& back() const;610611/// \brief Returns a reference to the last element in the `Value`.612/// Requires that this value holds an array or json object, with at least one613/// element.614Value& back();615616// Accessors for the [start, limit) range of bytes within the JSON text from617// which this value was parsed, if any.618void setOffsetStart(ptrdiff_t start);619void setOffsetLimit(ptrdiff_t limit);620ptrdiff_t getOffsetStart() const;621ptrdiff_t getOffsetLimit() const;622623private:624void setType(ValueType v) {625bits_.value_type_ = static_cast<unsigned char>(v);626}627bool isAllocated() const { return bits_.allocated_; }628void setIsAllocated(bool v) { bits_.allocated_ = v; }629630void initBasic(ValueType type, bool allocated = false);631void dupPayload(const Value& other);632void releasePayload();633void dupMeta(const Value& other);634635Value& resolveReference(const char* key);636Value& resolveReference(const char* key, const char* end);637638// struct MemberNamesTransform639//{640// typedef const char *result_type;641// const char *operator()( const CZString &name ) const642// {643// return name.c_str();644// }645//};646647union ValueHolder {648LargestInt int_;649LargestUInt uint_;650double real_;651bool bool_;652char* string_; // if allocated_, ptr to { unsigned, char[] }.653ObjectValues* map_;654} value_;655656struct {657// Really a ValueType, but types should agree for bitfield packing.658unsigned int value_type_ : 8;659// Unless allocated_, string_ must be null-terminated.660unsigned int allocated_ : 1;661} bits_;662663class Comments {664public:665Comments() = default;666Comments(const Comments& that);667Comments(Comments&& that) noexcept;668Comments& operator=(const Comments& that);669Comments& operator=(Comments&& that) noexcept;670bool has(CommentPlacement slot) const;671String get(CommentPlacement slot) const;672void set(CommentPlacement slot, String comment);673674private:675using Array = std::array<String, numberOfCommentPlacement>;676std::unique_ptr<Array> ptr_;677};678Comments comments_;679680// [start, limit) byte offsets in the source JSON text from which this Value681// was extracted.682ptrdiff_t start_;683ptrdiff_t limit_;684};685686template <> inline bool Value::as<bool>() const { return asBool(); }687template <> inline bool Value::is<bool>() const { return isBool(); }688689template <> inline Int Value::as<Int>() const { return asInt(); }690template <> inline bool Value::is<Int>() const { return isInt(); }691692template <> inline UInt Value::as<UInt>() const { return asUInt(); }693template <> inline bool Value::is<UInt>() const { return isUInt(); }694695#if defined(JSON_HAS_INT64)696template <> inline Int64 Value::as<Int64>() const { return asInt64(); }697template <> inline bool Value::is<Int64>() const { return isInt64(); }698699template <> inline UInt64 Value::as<UInt64>() const { return asUInt64(); }700template <> inline bool Value::is<UInt64>() const { return isUInt64(); }701#endif702703template <> inline double Value::as<double>() const { return asDouble(); }704template <> inline bool Value::is<double>() const { return isDouble(); }705706template <> inline String Value::as<String>() const { return asString(); }707template <> inline bool Value::is<String>() const { return isString(); }708709/// These `as` specializations are type conversions, and do not have a710/// corresponding `is`.711template <> inline float Value::as<float>() const { return asFloat(); }712template <> inline const char* Value::as<const char*>() const {713return asCString();714}715716/** \brief Experimental and untested: represents an element of the "path" to717* access a node.718*/719class JSON_API PathArgument {720public:721friend class Path;722723PathArgument();724PathArgument(ArrayIndex index);725PathArgument(const char* key);726PathArgument(String key);727728private:729enum Kind { kindNone = 0, kindIndex, kindKey };730String key_;731ArrayIndex index_{};732Kind kind_{kindNone};733};734735/** \brief Experimental and untested: represents a "path" to access a node.736*737* Syntax:738* - "." => root node739* - ".[n]" => elements at index 'n' of root node (an array value)740* - ".name" => member named 'name' of root node (an object value)741* - ".name1.name2.name3"742* - ".[0][1][2].name1[3]"743* - ".%" => member name is provided as parameter744* - ".[%]" => index is provided as parameter745*/746class JSON_API Path {747public:748Path(const String& path, const PathArgument& a1 = PathArgument(),749const PathArgument& a2 = PathArgument(),750const PathArgument& a3 = PathArgument(),751const PathArgument& a4 = PathArgument(),752const PathArgument& a5 = PathArgument());753754const Value& resolve(const Value& root) const;755Value resolve(const Value& root, const Value& defaultValue) const;756/// Creates the "path" to access the specified node and returns a reference on757/// the node.758Value& make(Value& root) const;759760private:761using InArgs = std::vector<const PathArgument*>;762using Args = std::vector<PathArgument>;763764void makePath(const String& path, const InArgs& in);765void addPathInArg(const String& path, const InArgs& in,766InArgs::const_iterator& itInArg, PathArgument::Kind kind);767static void invalidPath(const String& path, int location);768769Args args_;770};771772/** \brief base class for Value iterators.773*774*/775class JSON_API ValueIteratorBase {776public:777using iterator_category = std::bidirectional_iterator_tag;778using size_t = unsigned int;779using difference_type = int;780using SelfType = ValueIteratorBase;781782bool operator==(const SelfType& other) const { return isEqual(other); }783784bool operator!=(const SelfType& other) const { return !isEqual(other); }785786difference_type operator-(const SelfType& other) const {787return other.computeDistance(*this);788}789790/// Return either the index or the member name of the referenced value as a791/// Value.792Value key() const;793794/// Return the index of the referenced Value, or -1 if it is not an795/// arrayValue.796UInt index() const;797798/// Return the member name of the referenced Value, or "" if it is not an799/// objectValue.800/// \note Avoid `c_str()` on result, as embedded zeroes are possible.801String name() const;802803/// Return the member name of the referenced Value. "" if it is not an804/// objectValue.805/// deprecated This cannot be used for UTF-8 strings, since there can be806/// embedded nulls.807JSONCPP_DEPRECATED("Use `key = name();` instead.")808char const* memberName() const;809/// Return the member name of the referenced Value, or NULL if it is not an810/// objectValue.811/// \note Better version than memberName(). Allows embedded nulls.812char const* memberName(char const** end) const;813814protected:815/*! Internal utility functions to assist with implementing816* other iterator functions. The const and non-const versions817* of the "deref" protected methods expose the protected818* current_ member variable in a way that can often be819* optimized away by the compiler.820*/821const Value& deref() const;822Value& deref();823824void increment();825826void decrement();827828difference_type computeDistance(const SelfType& other) const;829830bool isEqual(const SelfType& other) const;831832void copy(const SelfType& other);833834private:835Value::ObjectValues::iterator current_;836// Indicates that iterator is for a null value.837bool isNull_{true};838839public:840// For some reason, BORLAND needs these at the end, rather841// than earlier. No idea why.842ValueIteratorBase();843explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);844};845846/** \brief const iterator for object and array value.847*848*/849class JSON_API ValueConstIterator : public ValueIteratorBase {850friend class Value;851852public:853using value_type = const Value;854// typedef unsigned int size_t;855// typedef int difference_type;856using reference = const Value&;857using pointer = const Value*;858using SelfType = ValueConstIterator;859860ValueConstIterator();861ValueConstIterator(ValueIterator const& other);862863private:864/*! internal Use by Value to create an iterator.865*/866explicit ValueConstIterator(const Value::ObjectValues::iterator& current);867868public:869SelfType& operator=(const ValueIteratorBase& other);870871SelfType operator++(int) {872SelfType temp(*this);873++*this;874return temp;875}876877SelfType operator--(int) {878SelfType temp(*this);879--*this;880return temp;881}882883SelfType& operator--() {884decrement();885return *this;886}887888SelfType& operator++() {889increment();890return *this;891}892893reference operator*() const { return deref(); }894895pointer operator->() const { return &deref(); }896};897898/** \brief Iterator for object and array value.899*/900class JSON_API ValueIterator : public ValueIteratorBase {901friend class Value;902903public:904using value_type = Value;905using size_t = unsigned int;906using difference_type = int;907using reference = Value&;908using pointer = Value*;909using SelfType = ValueIterator;910911ValueIterator();912explicit ValueIterator(const ValueConstIterator& other);913ValueIterator(const ValueIterator& other);914915private:916/*! internal Use by Value to create an iterator.917*/918explicit ValueIterator(const Value::ObjectValues::iterator& current);919920public:921SelfType& operator=(const SelfType& other);922923SelfType operator++(int) {924SelfType temp(*this);925++*this;926return temp;927}928929SelfType operator--(int) {930SelfType temp(*this);931--*this;932return temp;933}934935SelfType& operator--() {936decrement();937return *this;938}939940SelfType& operator++() {941increment();942return *this;943}944945/*! The return value of non-const iterators can be946* changed, so the these functions are not const947* because the returned references/pointers can be used948* to change state of the base class.949*/950reference operator*() const { return const_cast<reference>(deref()); }951pointer operator->() const { return const_cast<pointer>(&deref()); }952};953954inline void swap(Value& a, Value& b) { a.swap(b); }955956inline const Value& Value::front() const { return *begin(); }957958inline Value& Value::front() { return *begin(); }959960inline const Value& Value::back() const { return *(--end()); }961962inline Value& Value::back() { return *(--end()); }963964} // namespace Json965966#if !defined(__SUNPRO_CC)967#pragma pack(pop)968#endif969970#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)971#pragma warning(pop)972#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)973974#endif // JSON_H_INCLUDED975976977