Path: blob/master/thirdparty/openxr/src/external/jsoncpp/include/json/value.h
9913 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#endif36#if !defined(JSONCPP_TEMPLATE_DELETE)37#define JSONCPP_TEMPLATE_DELETE = delete38#endif39#endif4041#include <array>42#include <exception>43#include <map>44#include <memory>45#include <string>46#include <vector>4748// Disable warning C4251: <data member>: <type> needs to have dll-interface to49// be used by...50#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)51#pragma warning(push)52#pragma warning(disable : 4251 4275)53#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)5455#pragma pack(push)56#pragma pack()5758/** \brief JSON (JavaScript Object Notation).59*/60namespace Json {6162#if JSON_USE_EXCEPTION63/** Base class for all exceptions we throw.64*65* We use nothing but these internally. Of course, STL can throw others.66*/67class JSON_API Exception : public std::exception {68public:69Exception(String msg);70~Exception() noexcept override;71char const* what() const noexcept override;7273protected:74String msg_;75};7677/** Exceptions which the user cannot easily avoid.78*79* E.g. out-of-memory (when we use malloc), stack-overflow, malicious input80*81* \remark derived from Json::Exception82*/83class JSON_API RuntimeError : public Exception {84public:85RuntimeError(String const& msg);86};8788/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.89*90* These are precondition-violations (user bugs) and internal errors (our bugs).91*92* \remark derived from Json::Exception93*/94class JSON_API LogicError : public Exception {95public:96LogicError(String const& msg);97};98#endif99100/// used internally101JSONCPP_NORETURN void throwRuntimeError(String const& msg);102/// used internally103JSONCPP_NORETURN void throwLogicError(String const& msg);104105/** \brief Type of the value held by a Value object.106*/107enum ValueType {108nullValue = 0, ///< 'null' value109intValue, ///< signed integer value110uintValue, ///< unsigned integer value111realValue, ///< double value112stringValue, ///< UTF-8 string value113booleanValue, ///< bool value114arrayValue, ///< array value (ordered list)115objectValue ///< object value (collection of name/value pairs).116};117118enum CommentPlacement {119commentBefore = 0, ///< a comment placed on the line before a value120commentAfterOnSameLine, ///< a comment just after a value on the same line121commentAfter, ///< a comment on the line after a value (only make sense for122/// root value)123numberOfCommentPlacement124};125126/** \brief Type of precision for formatting of real values.127*/128enum PrecisionType {129significantDigits = 0, ///< we set max number of significant digits in string130decimalPlaces ///< we set max number of digits after "." in string131};132133/** \brief Lightweight wrapper to tag static string.134*135* Value constructor and objectValue member assignment takes advantage of the136* StaticString and avoid the cost of string duplication when storing the137* string or the member name.138*139* Example of usage:140* \code141* Json::Value aValue( StaticString("some text") );142* Json::Value object;143* static const StaticString code("code");144* object[code] = 1234;145* \endcode146*/147class JSON_API StaticString {148public:149explicit StaticString(const char* czstring) : c_str_(czstring) {}150151operator const char*() const { return c_str_; }152153const char* c_str() const { return c_str_; }154155private:156const char* c_str_;157};158159/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.160*161* This class is a discriminated union wrapper that can represents a:162* - signed integer [range: Value::minInt - Value::maxInt]163* - unsigned integer (range: 0 - Value::maxUInt)164* - double165* - UTF-8 string166* - boolean167* - 'null'168* - an ordered list of Value169* - collection of name/value pairs (javascript object)170*171* The type of the held value is represented by a #ValueType and172* can be obtained using type().173*174* Values of an #objectValue or #arrayValue can be accessed using operator[]()175* methods.176* Non-const methods will automatically create the a #nullValue element177* if it does not exist.178* The sequence of an #arrayValue will be automatically resized and initialized179* with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.180*181* The get() methods can be used to obtain default value in the case the182* required element does not exist.183*184* It is possible to iterate over the list of member keys of an object using185* the getMemberNames() method.186*187* \note #Value string-length fit in size_t, but keys must be < 2^30.188* (The reason is an implementation detail.) A #CharReader will raise an189* exception if a bound is exceeded to avoid security holes in your app,190* but the Value API does *not* check bounds. That is the responsibility191* of the caller.192*/193class JSON_API Value {194friend class ValueIteratorBase;195196public:197using Members = std::vector<String>;198using iterator = ValueIterator;199using const_iterator = ValueConstIterator;200using UInt = Json::UInt;201using Int = Json::Int;202#if defined(JSON_HAS_INT64)203using UInt64 = Json::UInt64;204using Int64 = Json::Int64;205#endif // defined(JSON_HAS_INT64)206using LargestInt = Json::LargestInt;207using LargestUInt = Json::LargestUInt;208using ArrayIndex = Json::ArrayIndex;209210// Required for boost integration, e. g. BOOST_TEST211using value_type = std::string;212213#if JSON_USE_NULLREF214// Binary compatibility kludges, do not use.215static const Value& null;216static const Value& nullRef;217#endif218219// null and nullRef are deprecated, use this instead.220static Value const& nullSingleton();221222/// Minimum signed integer value that can be stored in a Json::Value.223static constexpr LargestInt minLargestInt =224LargestInt(~(LargestUInt(-1) / 2));225/// Maximum signed integer value that can be stored in a Json::Value.226static constexpr LargestInt maxLargestInt = LargestInt(LargestUInt(-1) / 2);227/// Maximum unsigned integer value that can be stored in a Json::Value.228static constexpr LargestUInt maxLargestUInt = LargestUInt(-1);229230/// Minimum signed int value that can be stored in a Json::Value.231static constexpr Int minInt = Int(~(UInt(-1) / 2));232/// Maximum signed int value that can be stored in a Json::Value.233static constexpr Int maxInt = Int(UInt(-1) / 2);234/// Maximum unsigned int value that can be stored in a Json::Value.235static constexpr UInt maxUInt = UInt(-1);236237#if defined(JSON_HAS_INT64)238/// Minimum signed 64 bits int value that can be stored in a Json::Value.239static constexpr Int64 minInt64 = Int64(~(UInt64(-1) / 2));240/// Maximum signed 64 bits int value that can be stored in a Json::Value.241static constexpr Int64 maxInt64 = Int64(UInt64(-1) / 2);242/// Maximum unsigned 64 bits int value that can be stored in a Json::Value.243static constexpr UInt64 maxUInt64 = UInt64(-1);244#endif // defined(JSON_HAS_INT64)245/// Default precision for real value for string representation.246static constexpr UInt defaultRealPrecision = 17;247// The constant is hard-coded because some compiler have trouble248// converting Value::maxUInt64 to a double correctly (AIX/xlC).249// Assumes that UInt64 is a 64 bits integer.250static constexpr double maxUInt64AsDouble = 18446744073709551615.0;251// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler252// when using gcc and clang backend compilers. CZString253// cannot be defined as private. See issue #486254#ifdef __NVCC__255public:256#else257private:258#endif259#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION260class CZString {261public:262enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };263CZString(ArrayIndex index);264CZString(char const* str, unsigned length, DuplicationPolicy allocate);265CZString(CZString const& other);266CZString(CZString&& other) noexcept;267~CZString();268CZString& operator=(const CZString& other);269CZString& operator=(CZString&& other) noexcept;270271bool operator<(CZString const& other) const;272bool operator==(CZString const& other) const;273ArrayIndex index() const;274// const char* c_str() const; ///< \deprecated275char const* data() const;276unsigned length() const;277bool isStaticString() const;278279private:280void swap(CZString& other);281282struct StringStorage {283unsigned policy_ : 2;284unsigned length_ : 30; // 1GB max285};286287char const* cstr_; // actually, a prefixed string, unless policy is noDup288union {289ArrayIndex index_;290StringStorage storage_;291};292};293294public:295typedef std::map<CZString, Value> ObjectValues;296#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION297298public:299/**300* \brief Create a default Value of the given type.301*302* This is a very useful constructor.303* To create an empty array, pass arrayValue.304* To create an empty object, pass objectValue.305* Another Value can then be set to this one by assignment.306* This is useful since clear() and resize() will not alter types.307*308* Examples:309* \code310* Json::Value null_value; // null311* Json::Value arr_value(Json::arrayValue); // []312* Json::Value obj_value(Json::objectValue); // {}313* \endcode314*/315Value(ValueType type = nullValue);316Value(Int value);317Value(UInt value);318#if defined(JSON_HAS_INT64)319Value(Int64 value);320Value(UInt64 value);321#endif // if defined(JSON_HAS_INT64)322Value(double value);323Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)324Value(const char* begin, const char* end); ///< Copy all, incl zeroes.325/**326* \brief Constructs a value from a static string.327*328* Like other value string constructor but do not duplicate the string for329* internal storage. The given string must remain alive after the call to330* this constructor.331*332* \note This works only for null-terminated strings. (We cannot change the333* size of this class, so we have nowhere to store the length, which might be334* computed later for various operations.)335*336* Example of usage:337* \code338* static StaticString foo("some text");339* Json::Value aValue(foo);340* \endcode341*/342Value(const StaticString& value);343Value(const String& value);344Value(bool value);345Value(std::nullptr_t ptr) = delete;346Value(const Value& other);347Value(Value&& other) noexcept;348~Value();349350/// \note Overwrite existing comments. To preserve comments, use351/// #swapPayload().352Value& operator=(const Value& other);353Value& operator=(Value&& other) noexcept;354355/// Swap everything.356void swap(Value& other);357/// Swap values but leave comments and source offsets in place.358void swapPayload(Value& other);359360/// copy everything.361void copy(const Value& other);362/// copy values but leave comments and source offsets in place.363void copyPayload(const Value& other);364365ValueType type() const;366367/// Compare payload only, not comments etc.368bool operator<(const Value& other) const;369bool operator<=(const Value& other) const;370bool operator>=(const Value& other) const;371bool operator>(const Value& other) const;372bool operator==(const Value& other) const;373bool operator!=(const Value& other) const;374int compare(const Value& other) const;375376const char* asCString() const; ///< Embedded zeroes could cause you trouble!377#if JSONCPP_USING_SECURE_MEMORY378unsigned getCStringLength() const; // Allows you to understand the length of379// the CString380#endif381String asString() const; ///< Embedded zeroes are possible.382/** Get raw char* of string-value.383* \return false if !string. (Seg-fault if str or end are NULL.)384*/385bool getString(char const** begin, char const** end) const;386Int asInt() const;387UInt asUInt() const;388#if defined(JSON_HAS_INT64)389Int64 asInt64() const;390UInt64 asUInt64() const;391#endif // if defined(JSON_HAS_INT64)392LargestInt asLargestInt() const;393LargestUInt asLargestUInt() const;394float asFloat() const;395double asDouble() const;396bool asBool() const;397398bool isNull() const;399bool isBool() const;400bool isInt() const;401bool isInt64() const;402bool isUInt() const;403bool isUInt64() const;404bool isIntegral() const;405bool isDouble() const;406bool isNumeric() const;407bool isString() const;408bool isArray() const;409bool isObject() const;410411/// The `as<T>` and `is<T>` member function templates and specializations.412template <typename T> T as() const JSONCPP_TEMPLATE_DELETE;413template <typename T> bool is() const JSONCPP_TEMPLATE_DELETE;414415bool isConvertibleTo(ValueType other) const;416417/// Number of values in array or object418ArrayIndex size() const;419420/// \brief Return true if empty array, empty object, or null;421/// otherwise, false.422bool empty() const;423424/// Return !isNull()425explicit operator bool() const;426427/// Remove all object members and array elements.428/// \pre type() is arrayValue, objectValue, or nullValue429/// \post type() is unchanged430void clear();431432/// Resize the array to newSize elements.433/// New elements are initialized to null.434/// May only be called on nullValue or arrayValue.435/// \pre type() is arrayValue or nullValue436/// \post type() is arrayValue437void resize(ArrayIndex newSize);438439///@{440/// Access an array element (zero based index). If the array contains less441/// than index element, then null value are inserted in the array so that442/// its size is index+1.443/// (You may need to say 'value[0u]' to get your compiler to distinguish444/// this from the operator[] which takes a string.)445Value& operator[](ArrayIndex index);446Value& operator[](int index);447///@}448449///@{450/// Access an array element (zero based index).451/// (You may need to say 'value[0u]' to get your compiler to distinguish452/// this from the operator[] which takes a string.)453const Value& operator[](ArrayIndex index) const;454const Value& operator[](int index) const;455///@}456457/// If the array contains at least index+1 elements, returns the element458/// value, otherwise returns defaultValue.459Value get(ArrayIndex index, const Value& defaultValue) const;460/// Return true if index < size().461bool isValidIndex(ArrayIndex index) const;462/// \brief Append value to array at the end.463///464/// Equivalent to jsonvalue[jsonvalue.size()] = value;465Value& append(const Value& value);466Value& append(Value&& value);467468/// \brief Insert value in array at specific index469bool insert(ArrayIndex index, const Value& newValue);470bool insert(ArrayIndex index, Value&& newValue);471472/// Access an object value by name, create a null member if it does not exist.473/// \note Because of our implementation, keys are limited to 2^30 -1 chars.474/// Exceeding that will cause an exception.475Value& operator[](const char* key);476/// Access an object value by name, returns null if there is no member with477/// that name.478const Value& operator[](const char* key) const;479/// Access an object value by name, create a null member if it does not exist.480/// \param key may contain embedded nulls.481Value& operator[](const String& key);482/// Access an object value by name, returns null if there is no member with483/// that name.484/// \param key may contain embedded nulls.485const Value& operator[](const String& key) const;486/** \brief Access an object value by name, create a null member if it does not487* exist.488*489* If the object has no entry for that name, then the member name used to490* store the new entry is not duplicated.491* Example of use:492* \code493* Json::Value object;494* static const StaticString code("code");495* object[code] = 1234;496* \endcode497*/498Value& operator[](const StaticString& key);499/// Return the member named key if it exist, defaultValue otherwise.500/// \note deep copy501Value get(const char* key, const Value& defaultValue) const;502/// Return the member named key if it exist, defaultValue otherwise.503/// \note deep copy504/// \note key may contain embedded nulls.505Value get(const char* begin, const char* end,506const Value& defaultValue) const;507/// Return the member named key if it exist, defaultValue otherwise.508/// \note deep copy509/// \param key may contain embedded nulls.510Value get(const String& key, const Value& defaultValue) const;511/// Most general and efficient version of isMember()const, get()const,512/// and operator[]const513/// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30514Value const* find(char const* begin, char const* end) const;515/// Most general and efficient version of isMember()const, get()const,516/// and operator[]const517Value const* find(const String& key) const;518/// Most general and efficient version of object-mutators.519/// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30520/// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.521Value* demand(char const* begin, char const* end);522/// \brief Remove and return the named member.523///524/// Do nothing if it did not exist.525/// \pre type() is objectValue or nullValue526/// \post type() is unchanged527void removeMember(const char* key);528/// Same as removeMember(const char*)529/// \param key may contain embedded nulls.530void removeMember(const String& key);531/// Same as removeMember(const char* begin, const char* end, Value* removed),532/// but 'key' is null-terminated.533bool removeMember(const char* key, Value* removed);534/** \brief Remove the named map member.535*536* Update 'removed' iff removed.537* \param key may contain embedded nulls.538* \return true iff removed (no exceptions)539*/540bool removeMember(String const& key, Value* removed);541/// Same as removeMember(String const& key, Value* removed)542bool removeMember(const char* begin, const char* end, Value* removed);543/** \brief Remove the indexed array element.544*545* O(n) expensive operations.546* Update 'removed' iff removed.547* \return true if removed (no exceptions)548*/549bool removeIndex(ArrayIndex index, Value* removed);550551/// Return true if the object has a member named key.552/// \note 'key' must be null-terminated.553bool isMember(const char* key) const;554/// Return true if the object has a member named key.555/// \param key may contain embedded nulls.556bool isMember(const String& key) const;557/// Same as isMember(String const& key)const558bool isMember(const char* begin, const char* end) const;559560/// \brief Return a list of the member names.561///562/// If null, return an empty list.563/// \pre type() is objectValue or nullValue564/// \post if type() was nullValue, it remains nullValue565Members getMemberNames() const;566567/// \deprecated Always pass len.568JSONCPP_DEPRECATED("Use setComment(String const&) instead.")569void setComment(const char* comment, CommentPlacement placement) {570setComment(String(comment, strlen(comment)), placement);571}572/// Comments must be //... or /* ... */573void setComment(const char* comment, size_t len, CommentPlacement placement) {574setComment(String(comment, len), placement);575}576/// Comments must be //... or /* ... */577void setComment(String comment, CommentPlacement placement);578bool hasComment(CommentPlacement placement) const;579/// Include delimiters and embedded newlines.580String getComment(CommentPlacement placement) const;581582String toStyledString() const;583584const_iterator begin() const;585const_iterator end() const;586587iterator begin();588iterator end();589590/// \brief Returns a reference to the first element in the `Value`.591/// Requires that this value holds an array or json object, with at least one592/// element.593const Value& front() const;594595/// \brief Returns a reference to the first element in the `Value`.596/// Requires that this value holds an array or json object, with at least one597/// element.598Value& front();599600/// \brief Returns a reference to the last element in the `Value`.601/// Requires that value holds an array or json object, with at least one602/// element.603const Value& back() const;604605/// \brief Returns a reference to the last element in the `Value`.606/// Requires that this value holds an array or json object, with at least one607/// element.608Value& back();609610// Accessors for the [start, limit) range of bytes within the JSON text from611// which this value was parsed, if any.612void setOffsetStart(ptrdiff_t start);613void setOffsetLimit(ptrdiff_t limit);614ptrdiff_t getOffsetStart() const;615ptrdiff_t getOffsetLimit() const;616617private:618void setType(ValueType v) {619bits_.value_type_ = static_cast<unsigned char>(v);620}621bool isAllocated() const { return bits_.allocated_; }622void setIsAllocated(bool v) { bits_.allocated_ = v; }623624void initBasic(ValueType type, bool allocated = false);625void dupPayload(const Value& other);626void releasePayload();627void dupMeta(const Value& other);628629Value& resolveReference(const char* key);630Value& resolveReference(const char* key, const char* end);631632// struct MemberNamesTransform633//{634// typedef const char *result_type;635// const char *operator()( const CZString &name ) const636// {637// return name.c_str();638// }639//};640641union ValueHolder {642LargestInt int_;643LargestUInt uint_;644double real_;645bool bool_;646char* string_; // if allocated_, ptr to { unsigned, char[] }.647ObjectValues* map_;648} value_;649650struct {651// Really a ValueType, but types should agree for bitfield packing.652unsigned int value_type_ : 8;653// Unless allocated_, string_ must be null-terminated.654unsigned int allocated_ : 1;655} bits_;656657class Comments {658public:659Comments() = default;660Comments(const Comments& that);661Comments(Comments&& that) noexcept;662Comments& operator=(const Comments& that);663Comments& operator=(Comments&& that) noexcept;664bool has(CommentPlacement slot) const;665String get(CommentPlacement slot) const;666void set(CommentPlacement slot, String comment);667668private:669using Array = std::array<String, numberOfCommentPlacement>;670std::unique_ptr<Array> ptr_;671};672Comments comments_;673674// [start, limit) byte offsets in the source JSON text from which this Value675// was extracted.676ptrdiff_t start_;677ptrdiff_t limit_;678};679680template <> inline bool Value::as<bool>() const { return asBool(); }681template <> inline bool Value::is<bool>() const { return isBool(); }682683template <> inline Int Value::as<Int>() const { return asInt(); }684template <> inline bool Value::is<Int>() const { return isInt(); }685686template <> inline UInt Value::as<UInt>() const { return asUInt(); }687template <> inline bool Value::is<UInt>() const { return isUInt(); }688689#if defined(JSON_HAS_INT64)690template <> inline Int64 Value::as<Int64>() const { return asInt64(); }691template <> inline bool Value::is<Int64>() const { return isInt64(); }692693template <> inline UInt64 Value::as<UInt64>() const { return asUInt64(); }694template <> inline bool Value::is<UInt64>() const { return isUInt64(); }695#endif696697template <> inline double Value::as<double>() const { return asDouble(); }698template <> inline bool Value::is<double>() const { return isDouble(); }699700template <> inline String Value::as<String>() const { return asString(); }701template <> inline bool Value::is<String>() const { return isString(); }702703/// These `as` specializations are type conversions, and do not have a704/// corresponding `is`.705template <> inline float Value::as<float>() const { return asFloat(); }706template <> inline const char* Value::as<const char*>() const {707return asCString();708}709710/** \brief Experimental and untested: represents an element of the "path" to711* access a node.712*/713class JSON_API PathArgument {714public:715friend class Path;716717PathArgument();718PathArgument(ArrayIndex index);719PathArgument(const char* key);720PathArgument(String key);721722private:723enum Kind { kindNone = 0, kindIndex, kindKey };724String key_;725ArrayIndex index_{};726Kind kind_{kindNone};727};728729/** \brief Experimental and untested: represents a "path" to access a node.730*731* Syntax:732* - "." => root node733* - ".[n]" => elements at index 'n' of root node (an array value)734* - ".name" => member named 'name' of root node (an object value)735* - ".name1.name2.name3"736* - ".[0][1][2].name1[3]"737* - ".%" => member name is provided as parameter738* - ".[%]" => index is provided as parameter739*/740class JSON_API Path {741public:742Path(const String& path, const PathArgument& a1 = PathArgument(),743const PathArgument& a2 = PathArgument(),744const PathArgument& a3 = PathArgument(),745const PathArgument& a4 = PathArgument(),746const PathArgument& a5 = PathArgument());747748const Value& resolve(const Value& root) const;749Value resolve(const Value& root, const Value& defaultValue) const;750/// Creates the "path" to access the specified node and returns a reference on751/// the node.752Value& make(Value& root) const;753754private:755using InArgs = std::vector<const PathArgument*>;756using Args = std::vector<PathArgument>;757758void makePath(const String& path, const InArgs& in);759void addPathInArg(const String& path, const InArgs& in,760InArgs::const_iterator& itInArg, PathArgument::Kind kind);761static void invalidPath(const String& path, int location);762763Args args_;764};765766/** \brief base class for Value iterators.767*768*/769class JSON_API ValueIteratorBase {770public:771using iterator_category = std::bidirectional_iterator_tag;772using size_t = unsigned int;773using difference_type = int;774using SelfType = ValueIteratorBase;775776bool operator==(const SelfType& other) const { return isEqual(other); }777778bool operator!=(const SelfType& other) const { return !isEqual(other); }779780difference_type operator-(const SelfType& other) const {781return other.computeDistance(*this);782}783784/// Return either the index or the member name of the referenced value as a785/// Value.786Value key() const;787788/// Return the index of the referenced Value, or -1 if it is not an789/// arrayValue.790UInt index() const;791792/// Return the member name of the referenced Value, or "" if it is not an793/// objectValue.794/// \note Avoid `c_str()` on result, as embedded zeroes are possible.795String name() const;796797/// Return the member name of the referenced Value. "" if it is not an798/// objectValue.799/// \deprecated This cannot be used for UTF-8 strings, since there can be800/// embedded nulls.801JSONCPP_DEPRECATED("Use `key = name();` instead.")802char const* memberName() const;803/// Return the member name of the referenced Value, or NULL if it is not an804/// objectValue.805/// \note Better version than memberName(). Allows embedded nulls.806char const* memberName(char const** end) const;807808protected:809/*! Internal utility functions to assist with implementing810* other iterator functions. The const and non-const versions811* of the "deref" protected methods expose the protected812* current_ member variable in a way that can often be813* optimized away by the compiler.814*/815const Value& deref() const;816Value& deref();817818void increment();819820void decrement();821822difference_type computeDistance(const SelfType& other) const;823824bool isEqual(const SelfType& other) const;825826void copy(const SelfType& other);827828private:829Value::ObjectValues::iterator current_;830// Indicates that iterator is for a null value.831bool isNull_{true};832833public:834// For some reason, BORLAND needs these at the end, rather835// than earlier. No idea why.836ValueIteratorBase();837explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);838};839840/** \brief const iterator for object and array value.841*842*/843class JSON_API ValueConstIterator : public ValueIteratorBase {844friend class Value;845846public:847using value_type = const Value;848// typedef unsigned int size_t;849// typedef int difference_type;850using reference = const Value&;851using pointer = const Value*;852using SelfType = ValueConstIterator;853854ValueConstIterator();855ValueConstIterator(ValueIterator const& other);856857private:858/*! \internal Use by Value to create an iterator.859*/860explicit ValueConstIterator(const Value::ObjectValues::iterator& current);861862public:863SelfType& operator=(const ValueIteratorBase& other);864865SelfType operator++(int) {866SelfType temp(*this);867++*this;868return temp;869}870871SelfType operator--(int) {872SelfType temp(*this);873--*this;874return temp;875}876877SelfType& operator--() {878decrement();879return *this;880}881882SelfType& operator++() {883increment();884return *this;885}886887reference operator*() const { return deref(); }888889pointer operator->() const { return &deref(); }890};891892/** \brief Iterator for object and array value.893*/894class JSON_API ValueIterator : public ValueIteratorBase {895friend class Value;896897public:898using value_type = Value;899using size_t = unsigned int;900using difference_type = int;901using reference = Value&;902using pointer = Value*;903using SelfType = ValueIterator;904905ValueIterator();906explicit ValueIterator(const ValueConstIterator& other);907ValueIterator(const ValueIterator& other);908909private:910/*! \internal Use by Value to create an iterator.911*/912explicit ValueIterator(const Value::ObjectValues::iterator& current);913914public:915SelfType& operator=(const SelfType& other);916917SelfType operator++(int) {918SelfType temp(*this);919++*this;920return temp;921}922923SelfType operator--(int) {924SelfType temp(*this);925--*this;926return temp;927}928929SelfType& operator--() {930decrement();931return *this;932}933934SelfType& operator++() {935increment();936return *this;937}938939/*! The return value of non-const iterators can be940* changed, so the these functions are not const941* because the returned references/pointers can be used942* to change state of the base class.943*/944reference operator*() const { return const_cast<reference>(deref()); }945pointer operator->() const { return const_cast<pointer>(&deref()); }946};947948inline void swap(Value& a, Value& b) { a.swap(b); }949950inline const Value& Value::front() const { return *begin(); }951952inline Value& Value::front() { return *begin(); }953954inline const Value& Value::back() const { return *(--end()); }955956inline Value& Value::back() { return *(--end()); }957958} // namespace Json959960#pragma pack(pop)961962#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)963#pragma warning(pop)964#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)965966#endif // JSON_H_INCLUDED967968969