Baptiste Lepilleur | 7469f1d | 2010-04-20 21:35:19 +0000 | [diff] [blame] | 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 |
| 5 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 6 | #ifndef CPPTL_JSON_H_INCLUDED |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 7 | #define CPPTL_JSON_H_INCLUDED |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 8 | |
Baptiste Lepilleur | eadc478 | 2011-05-02 21:09:30 +0000 | [diff] [blame] | 9 | #if !defined(JSON_IS_AMALGAMATION) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 10 | #include "forwards.h" |
Baptiste Lepilleur | eadc478 | 2011-05-02 21:09:30 +0000 | [diff] [blame] | 11 | #endif // if !defined(JSON_IS_AMALGAMATION) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 14 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 15 | #ifndef JSON_USE_CPPTL_SMALLMAP |
| 16 | #include <map> |
| 17 | #else |
| 18 | #include <cpptl/smallmap.h> |
| 19 | #endif |
| 20 | #ifdef JSON_USE_CPPTL |
| 21 | #include <cpptl/forwards.h> |
| 22 | #endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 23 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 24 | // Disable warning C4251: <data member>: <type> needs to have dll-interface to |
| 25 | // be used by... |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 26 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 27 | #pragma warning(push) |
| 28 | #pragma warning(disable : 4251) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 29 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 30 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 31 | /** \brief JSON (JavaScript Object Notation). |
| 32 | */ |
| 33 | namespace Json { |
| 34 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 35 | /** \brief Type of the value held by a Value object. |
| 36 | */ |
| 37 | enum ValueType { |
| 38 | nullValue = 0, ///< 'null' value |
| 39 | intValue, ///< signed integer value |
| 40 | uintValue, ///< unsigned integer value |
| 41 | realValue, ///< double value |
| 42 | stringValue, ///< UTF-8 string value |
| 43 | booleanValue, ///< bool value |
| 44 | arrayValue, ///< array value (ordered list) |
| 45 | objectValue ///< object value (collection of name/value pairs). |
| 46 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 47 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 48 | enum CommentPlacement { |
| 49 | commentBefore = 0, ///< a comment placed on the line before a value |
| 50 | commentAfterOnSameLine, ///< a comment just after a value on the same line |
| 51 | commentAfter, ///< a comment on the line after a value (only make sense for |
Aaron Jacobs | 3a0c4fc | 2014-07-01 09:20:48 +1000 | [diff] [blame] | 52 | /// root value) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 53 | numberOfCommentPlacement |
| 54 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 55 | |
| 56 | //# ifdef JSON_USE_CPPTL |
| 57 | // typedef CppTL::AnyEnumerator<const char *> EnumMemberNames; |
| 58 | // typedef CppTL::AnyEnumerator<const Value &> EnumValues; |
| 59 | //# endif |
| 60 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 61 | /** \brief Lightweight wrapper to tag static string. |
| 62 | * |
| 63 | * Value constructor and objectValue member assignement takes advantage of the |
| 64 | * StaticString and avoid the cost of string duplication when storing the |
| 65 | * string or the member name. |
| 66 | * |
| 67 | * Example of usage: |
| 68 | * \code |
| 69 | * Json::Value aValue( StaticString("some text") ); |
| 70 | * Json::Value object; |
| 71 | * static const StaticString code("code"); |
| 72 | * object[code] = 1234; |
| 73 | * \endcode |
| 74 | */ |
| 75 | class JSON_API StaticString { |
| 76 | public: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 77 | explicit StaticString(const char* czstring) : str_(czstring) {} |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 78 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 79 | operator const char*() const { return str_; } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 80 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 81 | const char* c_str() const { return str_; } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 82 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 83 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 84 | const char* str_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 85 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 86 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 87 | /** \brief Represents a <a HREF="http://www.json.org">JSON</a> value. |
| 88 | * |
| 89 | * This class is a discriminated union wrapper that can represents a: |
| 90 | * - signed integer [range: Value::minInt - Value::maxInt] |
| 91 | * - unsigned integer (range: 0 - Value::maxUInt) |
| 92 | * - double |
| 93 | * - UTF-8 string |
| 94 | * - boolean |
| 95 | * - 'null' |
| 96 | * - an ordered list of Value |
| 97 | * - collection of name/value pairs (javascript object) |
| 98 | * |
| 99 | * The type of the held value is represented by a #ValueType and |
| 100 | * can be obtained using type(). |
| 101 | * |
| 102 | * values of an #objectValue or #arrayValue can be accessed using operator[]() |
| 103 | *methods. |
| 104 | * Non const methods will automatically create the a #nullValue element |
| 105 | * if it does not exist. |
| 106 | * The sequence of an #arrayValue will be automatically resize and initialized |
| 107 | * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue. |
| 108 | * |
| 109 | * The get() methods can be used to obtanis default value in the case the |
| 110 | *required element |
| 111 | * does not exist. |
| 112 | * |
| 113 | * It is possible to iterate over the list of a #objectValue values using |
| 114 | * the getMemberNames() method. |
| 115 | */ |
| 116 | class JSON_API Value { |
| 117 | friend class ValueIteratorBase; |
| 118 | #ifdef JSON_VALUE_USE_INTERNAL_MAP |
| 119 | friend class ValueInternalLink; |
| 120 | friend class ValueInternalMap; |
| 121 | #endif |
| 122 | public: |
| 123 | typedef std::vector<std::string> Members; |
| 124 | typedef ValueIterator iterator; |
| 125 | typedef ValueConstIterator const_iterator; |
| 126 | typedef Json::UInt UInt; |
| 127 | typedef Json::Int Int; |
| 128 | #if defined(JSON_HAS_INT64) |
| 129 | typedef Json::UInt64 UInt64; |
| 130 | typedef Json::Int64 Int64; |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 131 | #endif // defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 132 | typedef Json::LargestInt LargestInt; |
| 133 | typedef Json::LargestUInt LargestUInt; |
| 134 | typedef Json::ArrayIndex ArrayIndex; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 135 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 136 | static const Value& null; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 137 | /// Minimum signed integer value that can be stored in a Json::Value. |
| 138 | static const LargestInt minLargestInt; |
| 139 | /// Maximum signed integer value that can be stored in a Json::Value. |
| 140 | static const LargestInt maxLargestInt; |
| 141 | /// Maximum unsigned integer value that can be stored in a Json::Value. |
| 142 | static const LargestUInt maxLargestUInt; |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 143 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 144 | /// Minimum signed int value that can be stored in a Json::Value. |
| 145 | static const Int minInt; |
| 146 | /// Maximum signed int value that can be stored in a Json::Value. |
| 147 | static const Int maxInt; |
| 148 | /// Maximum unsigned int value that can be stored in a Json::Value. |
| 149 | static const UInt maxUInt; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 150 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 151 | #if defined(JSON_HAS_INT64) |
| 152 | /// Minimum signed 64 bits int value that can be stored in a Json::Value. |
| 153 | static const Int64 minInt64; |
| 154 | /// Maximum signed 64 bits int value that can be stored in a Json::Value. |
| 155 | static const Int64 maxInt64; |
| 156 | /// Maximum unsigned 64 bits int value that can be stored in a Json::Value. |
| 157 | static const UInt64 maxUInt64; |
Aaron Jacobs | f1053e7 | 2011-05-24 03:18:02 +0000 | [diff] [blame] | 158 | #endif // defined(JSON_HAS_INT64) |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 159 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 160 | private: |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 161 | #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 162 | #ifndef JSON_VALUE_USE_INTERNAL_MAP |
| 163 | class CZString { |
| 164 | public: |
| 165 | enum DuplicationPolicy { |
| 166 | noDuplication = 0, |
| 167 | duplicate, |
| 168 | duplicateOnCopy |
| 169 | }; |
| 170 | CZString(ArrayIndex index); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 171 | CZString(const char* cstr, DuplicationPolicy allocate); |
| 172 | CZString(const CZString& other); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 173 | ~CZString(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 174 | CZString& operator=(CZString other); |
| 175 | bool operator<(const CZString& other) const; |
| 176 | bool operator==(const CZString& other) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 177 | ArrayIndex index() const; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 178 | const char* c_str() const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 179 | bool isStaticString() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 180 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 181 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 182 | void swap(CZString& other); |
| 183 | const char* cstr_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 184 | ArrayIndex index_; |
| 185 | }; |
| 186 | |
| 187 | public: |
| 188 | #ifndef JSON_USE_CPPTL_SMALLMAP |
| 189 | typedef std::map<CZString, Value> ObjectValues; |
| 190 | #else |
| 191 | typedef CppTL::SmallMap<CZString, Value> ObjectValues; |
| 192 | #endif // ifndef JSON_USE_CPPTL_SMALLMAP |
| 193 | #endif // ifndef JSON_VALUE_USE_INTERNAL_MAP |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 194 | #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
| 195 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 196 | public: |
| 197 | /** \brief Create a default Value of the given type. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 198 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 199 | This is a very useful constructor. |
| 200 | To create an empty array, pass arrayValue. |
| 201 | To create an empty object, pass objectValue. |
| 202 | Another Value can then be set to this one by assignment. |
| 203 | This is useful since clear() and resize() will not alter types. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 204 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 205 | Examples: |
| 206 | \code |
| 207 | Json::Value null_value; // null |
| 208 | Json::Value arr_value(Json::arrayValue); // [] |
| 209 | Json::Value obj_value(Json::objectValue); // {} |
| 210 | \endcode |
| 211 | */ |
| 212 | Value(ValueType type = nullValue); |
| 213 | Value(Int value); |
| 214 | Value(UInt value); |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 215 | #if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 216 | Value(Int64 value); |
| 217 | Value(UInt64 value); |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 218 | #endif // if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 219 | Value(double value); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 220 | Value(const char* value); |
| 221 | Value(const char* beginValue, const char* endValue); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 222 | /** \brief Constructs a value from a static string. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 223 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 224 | * Like other value string constructor but do not duplicate the string for |
| 225 | * internal storage. The given string must remain alive after the call to this |
| 226 | * constructor. |
| 227 | * Example of usage: |
| 228 | * \code |
| 229 | * Json::Value aValue( StaticString("some text") ); |
| 230 | * \endcode |
| 231 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 232 | Value(const StaticString& value); |
| 233 | Value(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 234 | #ifdef JSON_USE_CPPTL |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 235 | Value(const CppTL::ConstString& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 236 | #endif |
| 237 | Value(bool value); |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 238 | /// Deep copy. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 239 | Value(const Value& other); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 240 | ~Value(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 241 | |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 242 | // Deep copy, then swap(other). |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 243 | Value& operator=(Value other); |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 244 | /// Swap everything. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 245 | void swap(Value& other); |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 246 | /// Swap values but leave comments and source offsets in place. |
| 247 | void swapPayload(Value& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 248 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 249 | ValueType type() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 250 | |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 251 | /// Compare payload only, not comments etc. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 252 | bool operator<(const Value& other) const; |
| 253 | bool operator<=(const Value& other) const; |
| 254 | bool operator>=(const Value& other) const; |
| 255 | bool operator>(const Value& other) const; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 256 | bool operator==(const Value& other) const; |
| 257 | bool operator!=(const Value& other) const; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 258 | int compare(const Value& other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 259 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 260 | const char* asCString() const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 261 | std::string asString() const; |
| 262 | #ifdef JSON_USE_CPPTL |
| 263 | CppTL::ConstString asConstString() const; |
| 264 | #endif |
| 265 | Int asInt() const; |
| 266 | UInt asUInt() const; |
Aaron Jacobs | f1053e7 | 2011-05-24 03:18:02 +0000 | [diff] [blame] | 267 | #if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 268 | Int64 asInt64() const; |
| 269 | UInt64 asUInt64() const; |
Aaron Jacobs | f1053e7 | 2011-05-24 03:18:02 +0000 | [diff] [blame] | 270 | #endif // if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 271 | LargestInt asLargestInt() const; |
| 272 | LargestUInt asLargestUInt() const; |
| 273 | float asFloat() const; |
| 274 | double asDouble() const; |
| 275 | bool asBool() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 276 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 277 | bool isNull() const; |
| 278 | bool isBool() const; |
| 279 | bool isInt() const; |
| 280 | bool isInt64() const; |
| 281 | bool isUInt() const; |
| 282 | bool isUInt64() const; |
| 283 | bool isIntegral() const; |
| 284 | bool isDouble() const; |
| 285 | bool isNumeric() const; |
| 286 | bool isString() const; |
| 287 | bool isArray() const; |
| 288 | bool isObject() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 289 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 290 | bool isConvertibleTo(ValueType other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 291 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 292 | /// Number of values in array or object |
| 293 | ArrayIndex size() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 294 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 295 | /// \brief Return true if empty array, empty object, or null; |
| 296 | /// otherwise, false. |
| 297 | bool empty() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 298 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 299 | /// Return isNull() |
| 300 | bool operator!() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 301 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 302 | /// Remove all object members and array elements. |
| 303 | /// \pre type() is arrayValue, objectValue, or nullValue |
| 304 | /// \post type() is unchanged |
| 305 | void clear(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 306 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 307 | /// Resize the array to size elements. |
| 308 | /// New elements are initialized to null. |
| 309 | /// May only be called on nullValue or arrayValue. |
| 310 | /// \pre type() is arrayValue or nullValue |
| 311 | /// \post type() is arrayValue |
| 312 | void resize(ArrayIndex size); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 313 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 314 | /// Access an array element (zero based index ). |
| 315 | /// If the array contains less than index element, then null value are |
| 316 | /// inserted |
| 317 | /// in the array so that its size is index+1. |
| 318 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 319 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 320 | Value& operator[](ArrayIndex index); |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 321 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 322 | /// Access an array element (zero based index ). |
| 323 | /// If the array contains less than index element, then null value are |
| 324 | /// inserted |
| 325 | /// in the array so that its size is index+1. |
| 326 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 327 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 328 | Value& operator[](int index); |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 329 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 330 | /// Access an array element (zero based index ) |
| 331 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 332 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 333 | const Value& operator[](ArrayIndex index) const; |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 334 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 335 | /// Access an array element (zero based index ) |
| 336 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 337 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 338 | const Value& operator[](int index) const; |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 339 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 340 | /// If the array contains at least index+1 elements, returns the element |
| 341 | /// value, |
| 342 | /// otherwise returns defaultValue. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 343 | Value get(ArrayIndex index, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 344 | /// Return true if index < size(). |
| 345 | bool isValidIndex(ArrayIndex index) const; |
| 346 | /// \brief Append value to array at the end. |
| 347 | /// |
| 348 | /// Equivalent to jsonvalue[jsonvalue.size()] = value; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 349 | Value& append(const Value& value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 350 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 351 | /// Access an object value by name, create a null member if it does not exist. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 352 | Value& operator[](const char* key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 353 | /// Access an object value by name, returns null if there is no member with |
| 354 | /// that name. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 355 | const Value& operator[](const char* key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 356 | /// Access an object value by name, create a null member if it does not exist. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 357 | Value& operator[](const std::string& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 358 | /// Access an object value by name, returns null if there is no member with |
| 359 | /// that name. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 360 | const Value& operator[](const std::string& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 361 | /** \brief Access an object value by name, create a null member if it does not |
| 362 | exist. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 363 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 364 | * If the object as no entry for that name, then the member name used to store |
| 365 | * the new entry is not duplicated. |
| 366 | * Example of use: |
| 367 | * \code |
| 368 | * Json::Value object; |
| 369 | * static const StaticString code("code"); |
| 370 | * object[code] = 1234; |
| 371 | * \endcode |
| 372 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 373 | Value& operator[](const StaticString& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 374 | #ifdef JSON_USE_CPPTL |
| 375 | /// Access an object value by name, create a null member if it does not exist. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 376 | Value& operator[](const CppTL::ConstString& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 377 | /// Access an object value by name, returns null if there is no member with |
| 378 | /// that name. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 379 | const Value& operator[](const CppTL::ConstString& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 380 | #endif |
| 381 | /// Return the member named key if it exist, defaultValue otherwise. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 382 | Value get(const char* key, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 383 | /// Return the member named key if it exist, defaultValue otherwise. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 384 | Value get(const std::string& key, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 385 | #ifdef JSON_USE_CPPTL |
| 386 | /// Return the member named key if it exist, defaultValue otherwise. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 387 | Value get(const CppTL::ConstString& key, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 388 | #endif |
| 389 | /// \brief Remove and return the named member. |
| 390 | /// |
| 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 |
Christopher Dunn | 76746b0 | 2015-01-21 16:01:30 -0600 | [diff] [blame] | 395 | /// \deprecated |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 396 | Value removeMember(const char* key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 397 | /// Same as removeMember(const char*) |
Christopher Dunn | 76746b0 | 2015-01-21 16:01:30 -0600 | [diff] [blame] | 398 | /// \deprecated |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 399 | Value removeMember(const std::string& key); |
Christopher Dunn | 76746b0 | 2015-01-21 16:01:30 -0600 | [diff] [blame] | 400 | /** \brief Remove the named map member. |
| 401 | |
| 402 | Update 'removed' iff removed. |
| 403 | \return true iff removed (no exceptions) |
| 404 | */ |
| 405 | bool removeMember(const char* key, Value* removed); |
Christopher Dunn | 9de2c2d | 2015-01-20 16:15:40 -0600 | [diff] [blame] | 406 | /** \brief Remove the indexed array element. |
| 407 | |
| 408 | O(n) expensive operations. |
| 409 | Update 'removed' iff removed. |
Christopher Dunn | e87e41c | 2015-01-20 16:24:11 -0600 | [diff] [blame] | 410 | \return true iff removed (no exceptions) |
Christopher Dunn | 9de2c2d | 2015-01-20 16:15:40 -0600 | [diff] [blame] | 411 | */ |
| 412 | bool removeIndex(ArrayIndex i, Value* removed); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 413 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 414 | /// Return true if the object has a member named key. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 415 | bool isMember(const char* key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 416 | /// Return true if the object has a member named key. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 417 | bool isMember(const std::string& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 418 | #ifdef JSON_USE_CPPTL |
| 419 | /// Return true if the object has a member named key. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 420 | bool isMember(const CppTL::ConstString& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 421 | #endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 422 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 423 | /// \brief Return a list of the member names. |
| 424 | /// |
| 425 | /// If null, return an empty list. |
| 426 | /// \pre type() is objectValue or nullValue |
| 427 | /// \post if type() was nullValue, it remains nullValue |
| 428 | Members getMemberNames() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 429 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 430 | //# ifdef JSON_USE_CPPTL |
| 431 | // EnumMemberNames enumMemberNames() const; |
| 432 | // EnumValues enumValues() const; |
| 433 | //# endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 434 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 435 | /// Comments must be //... or /* ... */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 436 | void setComment(const char* comment, CommentPlacement placement); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 437 | /// Comments must be //... or /* ... */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 438 | void setComment(const std::string& comment, CommentPlacement placement); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 439 | bool hasComment(CommentPlacement placement) const; |
| 440 | /// Include delimiters and embedded newlines. |
| 441 | std::string getComment(CommentPlacement placement) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 442 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 443 | std::string toStyledString() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 444 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 445 | const_iterator begin() const; |
| 446 | const_iterator end() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 447 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 448 | iterator begin(); |
| 449 | iterator end(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 450 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 451 | // Accessors for the [start, limit) range of bytes within the JSON text from |
| 452 | // which this value was parsed, if any. |
| 453 | void setOffsetStart(size_t start); |
| 454 | void setOffsetLimit(size_t limit); |
| 455 | size_t getOffsetStart() const; |
| 456 | size_t getOffsetLimit() const; |
Aaron Jacobs | 68db655 | 2014-04-23 23:41:12 +0000 | [diff] [blame] | 457 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 458 | private: |
Billy Donahue | 8eb5d89 | 2014-11-10 01:35:42 -0500 | [diff] [blame] | 459 | void initBasic(ValueType type, bool allocated = false); |
| 460 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 461 | Value& resolveReference(const char* key, bool isStatic); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 462 | |
| 463 | #ifdef JSON_VALUE_USE_INTERNAL_MAP |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 464 | inline bool isItemAvailable() const { return itemIsUsed_ == 0; } |
| 465 | |
| 466 | inline void setItemUsed(bool isUsed = true) { itemIsUsed_ = isUsed ? 1 : 0; } |
| 467 | |
| 468 | inline bool isMemberNameStatic() const { return memberNameIsStatic_ == 0; } |
| 469 | |
| 470 | inline void setMemberNameIsStatic(bool isStatic) { |
| 471 | memberNameIsStatic_ = isStatic ? 1 : 0; |
| 472 | } |
| 473 | #endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP |
| 474 | |
| 475 | private: |
| 476 | struct CommentInfo { |
| 477 | CommentInfo(); |
| 478 | ~CommentInfo(); |
| 479 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 480 | void setComment(const char* text); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 481 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 482 | char* comment_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 483 | }; |
| 484 | |
| 485 | // struct MemberNamesTransform |
| 486 | //{ |
| 487 | // typedef const char *result_type; |
| 488 | // const char *operator()( const CZString &name ) const |
| 489 | // { |
| 490 | // return name.c_str(); |
| 491 | // } |
| 492 | //}; |
| 493 | |
| 494 | union ValueHolder { |
| 495 | LargestInt int_; |
| 496 | LargestUInt uint_; |
| 497 | double real_; |
| 498 | bool bool_; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 499 | char* string_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 500 | #ifdef JSON_VALUE_USE_INTERNAL_MAP |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 501 | ValueInternalArray* array_; |
| 502 | ValueInternalMap* map_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 503 | #else |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 504 | ObjectValues* map_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 505 | #endif |
| 506 | } value_; |
| 507 | ValueType type_ : 8; |
| 508 | int allocated_ : 1; // Notes: if declared as bool, bitfield is useless. |
| 509 | #ifdef JSON_VALUE_USE_INTERNAL_MAP |
| 510 | unsigned int itemIsUsed_ : 1; // used by the ValueInternalMap container. |
| 511 | int memberNameIsStatic_ : 1; // used by the ValueInternalMap container. |
| 512 | #endif |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 513 | CommentInfo* comments_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 514 | |
| 515 | // [start, limit) byte offsets in the source JSON text from which this Value |
| 516 | // was extracted. |
| 517 | size_t start_; |
| 518 | size_t limit_; |
| 519 | }; |
| 520 | |
| 521 | /** \brief Experimental and untested: represents an element of the "path" to |
| 522 | * access a node. |
| 523 | */ |
| 524 | class JSON_API PathArgument { |
| 525 | public: |
| 526 | friend class Path; |
| 527 | |
| 528 | PathArgument(); |
| 529 | PathArgument(ArrayIndex index); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 530 | PathArgument(const char* key); |
| 531 | PathArgument(const std::string& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 532 | |
| 533 | private: |
| 534 | enum Kind { |
| 535 | kindNone = 0, |
| 536 | kindIndex, |
| 537 | kindKey |
| 538 | }; |
| 539 | std::string key_; |
| 540 | ArrayIndex index_; |
| 541 | Kind kind_; |
| 542 | }; |
| 543 | |
| 544 | /** \brief Experimental and untested: represents a "path" to access a node. |
| 545 | * |
| 546 | * Syntax: |
| 547 | * - "." => root node |
| 548 | * - ".[n]" => elements at index 'n' of root node (an array value) |
| 549 | * - ".name" => member named 'name' of root node (an object value) |
| 550 | * - ".name1.name2.name3" |
| 551 | * - ".[0][1][2].name1[3]" |
| 552 | * - ".%" => member name is provided as parameter |
| 553 | * - ".[%]" => index is provied as parameter |
| 554 | */ |
| 555 | class JSON_API Path { |
| 556 | public: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 557 | Path(const std::string& path, |
| 558 | const PathArgument& a1 = PathArgument(), |
| 559 | const PathArgument& a2 = PathArgument(), |
| 560 | const PathArgument& a3 = PathArgument(), |
| 561 | const PathArgument& a4 = PathArgument(), |
| 562 | const PathArgument& a5 = PathArgument()); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 563 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 564 | const Value& resolve(const Value& root) const; |
| 565 | Value resolve(const Value& root, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 566 | /// Creates the "path" to access the specified node and returns a reference on |
| 567 | /// the node. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 568 | Value& make(Value& root) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 569 | |
| 570 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 571 | typedef std::vector<const PathArgument*> InArgs; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 572 | typedef std::vector<PathArgument> Args; |
| 573 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 574 | void makePath(const std::string& path, const InArgs& in); |
| 575 | void addPathInArg(const std::string& path, |
| 576 | const InArgs& in, |
| 577 | InArgs::const_iterator& itInArg, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 578 | PathArgument::Kind kind); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 579 | void invalidPath(const std::string& path, int location); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 580 | |
| 581 | Args args_; |
| 582 | }; |
| 583 | |
| 584 | #ifdef JSON_VALUE_USE_INTERNAL_MAP |
| 585 | /** \brief Allocator to customize Value internal map. |
| 586 | * Below is an example of a simple implementation (default implementation |
| 587 | actually |
| 588 | * use memory pool for speed). |
| 589 | * \code |
| 590 | class DefaultValueMapAllocator : public ValueMapAllocator |
| 591 | { |
| 592 | public: // overridden from ValueMapAllocator |
| 593 | virtual ValueInternalMap *newMap() |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 594 | { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 595 | return new ValueInternalMap(); |
| 596 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 597 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 598 | virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 599 | { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 600 | return new ValueInternalMap( other ); |
| 601 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 602 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 603 | virtual void destructMap( ValueInternalMap *map ) |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 604 | { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 605 | delete map; |
| 606 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 607 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 608 | virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) |
| 609 | { |
| 610 | return new ValueInternalLink[size]; |
| 611 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 612 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 613 | virtual void releaseMapBuckets( ValueInternalLink *links ) |
| 614 | { |
| 615 | delete [] links; |
| 616 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 617 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 618 | virtual ValueInternalLink *allocateMapLink() |
| 619 | { |
| 620 | return new ValueInternalLink(); |
| 621 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 622 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 623 | virtual void releaseMapLink( ValueInternalLink *link ) |
| 624 | { |
| 625 | delete link; |
| 626 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 627 | }; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 628 | * \endcode |
| 629 | */ |
| 630 | class JSON_API ValueMapAllocator { |
| 631 | public: |
| 632 | virtual ~ValueMapAllocator(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 633 | virtual ValueInternalMap* newMap() = 0; |
| 634 | virtual ValueInternalMap* newMapCopy(const ValueInternalMap& other) = 0; |
| 635 | virtual void destructMap(ValueInternalMap* map) = 0; |
| 636 | virtual ValueInternalLink* allocateMapBuckets(unsigned int size) = 0; |
| 637 | virtual void releaseMapBuckets(ValueInternalLink* links) = 0; |
| 638 | virtual ValueInternalLink* allocateMapLink() = 0; |
| 639 | virtual void releaseMapLink(ValueInternalLink* link) = 0; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 640 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 641 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 642 | /** \brief ValueInternalMap hash-map bucket chain link (for internal use only). |
| 643 | * \internal previous_ & next_ allows for bidirectional traversal. |
| 644 | */ |
| 645 | class JSON_API ValueInternalLink { |
| 646 | public: |
| 647 | enum { |
| 648 | itemPerLink = 6 |
| 649 | }; // sizeof(ValueInternalLink) = 128 on 32 bits architecture. |
| 650 | enum InternalFlags { |
| 651 | flagAvailable = 0, |
| 652 | flagUsed = 1 |
| 653 | }; |
| 654 | |
| 655 | ValueInternalLink(); |
| 656 | |
| 657 | ~ValueInternalLink(); |
| 658 | |
| 659 | Value items_[itemPerLink]; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 660 | char* keys_[itemPerLink]; |
| 661 | ValueInternalLink* previous_; |
| 662 | ValueInternalLink* next_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 663 | }; |
| 664 | |
| 665 | /** \brief A linked page based hash-table implementation used internally by |
| 666 | *Value. |
| 667 | * \internal ValueInternalMap is a tradional bucket based hash-table, with a |
| 668 | *linked |
| 669 | * list in each bucket to handle collision. There is an addional twist in that |
| 670 | * each node of the collision linked list is a page containing a fixed amount of |
| 671 | * value. This provides a better compromise between memory usage and speed. |
| 672 | * |
| 673 | * Each bucket is made up of a chained list of ValueInternalLink. The last |
| 674 | * link of a given bucket can be found in the 'previous_' field of the following |
| 675 | *bucket. |
| 676 | * The last link of the last bucket is stored in tailLink_ as it has no |
| 677 | *following bucket. |
| 678 | * Only the last link of a bucket may contains 'available' item. The last link |
| 679 | *always |
| 680 | * contains at least one element unless is it the bucket one very first link. |
| 681 | */ |
| 682 | class JSON_API ValueInternalMap { |
| 683 | friend class ValueIteratorBase; |
| 684 | friend class Value; |
| 685 | |
| 686 | public: |
| 687 | typedef unsigned int HashKey; |
| 688 | typedef unsigned int BucketIndex; |
| 689 | |
| 690 | #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
| 691 | struct IteratorState { |
| 692 | IteratorState() : map_(0), link_(0), itemIndex_(0), bucketIndex_(0) {} |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 693 | ValueInternalMap* map_; |
| 694 | ValueInternalLink* link_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 695 | BucketIndex itemIndex_; |
| 696 | BucketIndex bucketIndex_; |
| 697 | }; |
| 698 | #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
| 699 | |
| 700 | ValueInternalMap(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 701 | ValueInternalMap(const ValueInternalMap& other); |
| 702 | ValueInternalMap& operator=(ValueInternalMap other); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 703 | ~ValueInternalMap(); |
| 704 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 705 | void swap(ValueInternalMap& other); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 706 | |
| 707 | BucketIndex size() const; |
| 708 | |
| 709 | void clear(); |
| 710 | |
| 711 | bool reserveDelta(BucketIndex growth); |
| 712 | |
| 713 | bool reserve(BucketIndex newItemCount); |
| 714 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 715 | const Value* find(const char* key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 716 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 717 | Value* find(const char* key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 718 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 719 | Value& resolveReference(const char* key, bool isStatic); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 720 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 721 | void remove(const char* key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 722 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 723 | void doActualRemove(ValueInternalLink* link, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 724 | BucketIndex index, |
| 725 | BucketIndex bucketIndex); |
| 726 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 727 | ValueInternalLink*& getLastLinkInBucket(BucketIndex bucketIndex); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 728 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 729 | Value& setNewItem(const char* key, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 730 | bool isStatic, |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 731 | ValueInternalLink* link, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 732 | BucketIndex index); |
| 733 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 734 | Value& unsafeAdd(const char* key, bool isStatic, HashKey hashedKey); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 735 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 736 | HashKey hash(const char* key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 737 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 738 | int compare(const ValueInternalMap& other) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 739 | |
| 740 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 741 | void makeBeginIterator(IteratorState& it) const; |
| 742 | void makeEndIterator(IteratorState& it) const; |
| 743 | static bool equals(const IteratorState& x, const IteratorState& other); |
| 744 | static void increment(IteratorState& iterator); |
| 745 | static void incrementBucket(IteratorState& iterator); |
| 746 | static void decrement(IteratorState& iterator); |
| 747 | static const char* key(const IteratorState& iterator); |
| 748 | static const char* key(const IteratorState& iterator, bool& isStatic); |
| 749 | static Value& value(const IteratorState& iterator); |
| 750 | static int distance(const IteratorState& x, const IteratorState& y); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 751 | |
| 752 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 753 | ValueInternalLink* buckets_; |
| 754 | ValueInternalLink* tailLink_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 755 | BucketIndex bucketsSize_; |
| 756 | BucketIndex itemCount_; |
| 757 | }; |
| 758 | |
| 759 | /** \brief A simplified deque implementation used internally by Value. |
| 760 | * \internal |
| 761 | * It is based on a list of fixed "page", each page contains a fixed number of |
| 762 | *items. |
| 763 | * Instead of using a linked-list, a array of pointer is used for fast item |
| 764 | *look-up. |
| 765 | * Look-up for an element is as follow: |
| 766 | * - compute page index: pageIndex = itemIndex / itemsPerPage |
| 767 | * - look-up item in page: pages_[pageIndex][itemIndex % itemsPerPage] |
| 768 | * |
| 769 | * Insertion is amortized constant time (only the array containing the index of |
| 770 | *pointers |
| 771 | * need to be reallocated when items are appended). |
| 772 | */ |
| 773 | class JSON_API ValueInternalArray { |
| 774 | friend class Value; |
| 775 | friend class ValueIteratorBase; |
| 776 | |
| 777 | public: |
| 778 | enum { |
| 779 | itemsPerPage = 8 |
| 780 | }; // should be a power of 2 for fast divide and modulo. |
| 781 | typedef Value::ArrayIndex ArrayIndex; |
| 782 | typedef unsigned int PageIndex; |
| 783 | |
| 784 | #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
| 785 | struct IteratorState // Must be a POD |
| 786 | { |
| 787 | IteratorState() : array_(0), currentPageIndex_(0), currentItemIndex_(0) {} |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 788 | ValueInternalArray* array_; |
| 789 | Value** currentPageIndex_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 790 | unsigned int currentItemIndex_; |
| 791 | }; |
| 792 | #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
| 793 | |
| 794 | ValueInternalArray(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 795 | ValueInternalArray(const ValueInternalArray& other); |
| 796 | ValueInternalArray& operator=(ValueInternalArray other); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 797 | ~ValueInternalArray(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 798 | void swap(ValueInternalArray& other); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 799 | |
| 800 | void clear(); |
| 801 | void resize(ArrayIndex newSize); |
| 802 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 803 | Value& resolveReference(ArrayIndex index); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 804 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 805 | Value* find(ArrayIndex index) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 806 | |
| 807 | ArrayIndex size() const; |
| 808 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 809 | int compare(const ValueInternalArray& other) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 810 | |
| 811 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 812 | static bool equals(const IteratorState& x, const IteratorState& other); |
| 813 | static void increment(IteratorState& iterator); |
| 814 | static void decrement(IteratorState& iterator); |
| 815 | static Value& dereference(const IteratorState& iterator); |
| 816 | static Value& unsafeDereference(const IteratorState& iterator); |
| 817 | static int distance(const IteratorState& x, const IteratorState& y); |
| 818 | static ArrayIndex indexOf(const IteratorState& iterator); |
| 819 | void makeBeginIterator(IteratorState& it) const; |
| 820 | void makeEndIterator(IteratorState& it) const; |
| 821 | void makeIterator(IteratorState& it, ArrayIndex index) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 822 | |
| 823 | void makeIndexValid(ArrayIndex index); |
| 824 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 825 | Value** pages_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 826 | ArrayIndex size_; |
| 827 | PageIndex pageCount_; |
| 828 | }; |
| 829 | |
| 830 | /** \brief Experimental: do not use. Allocator to customize Value internal |
| 831 | array. |
| 832 | * Below is an example of a simple implementation (actual implementation use |
| 833 | * memory pool). |
| 834 | \code |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 835 | class DefaultValueArrayAllocator : public ValueArrayAllocator |
| 836 | { |
| 837 | public: // overridden from ValueArrayAllocator |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 838 | virtual ~DefaultValueArrayAllocator() |
| 839 | { |
| 840 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 841 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 842 | virtual ValueInternalArray *newArray() |
| 843 | { |
| 844 | return new ValueInternalArray(); |
| 845 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 846 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 847 | virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) |
| 848 | { |
| 849 | return new ValueInternalArray( other ); |
| 850 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 851 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 852 | virtual void destruct( ValueInternalArray *array ) |
| 853 | { |
| 854 | delete array; |
| 855 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 856 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 857 | virtual void reallocateArrayPageIndex( Value **&indexes, |
| 858 | ValueInternalArray::PageIndex |
| 859 | &indexCount, |
| 860 | ValueInternalArray::PageIndex |
| 861 | minNewIndexCount ) |
| 862 | { |
| 863 | ValueInternalArray::PageIndex newIndexCount = (indexCount*3)/2 + 1; |
| 864 | if ( minNewIndexCount > newIndexCount ) |
| 865 | newIndexCount = minNewIndexCount; |
| 866 | void *newIndexes = realloc( indexes, sizeof(Value*) * newIndexCount ); |
| 867 | if ( !newIndexes ) |
| 868 | throw std::bad_alloc(); |
| 869 | indexCount = newIndexCount; |
| 870 | indexes = static_cast<Value **>( newIndexes ); |
| 871 | } |
| 872 | virtual void releaseArrayPageIndex( Value **indexes, |
| 873 | ValueInternalArray::PageIndex indexCount ) |
| 874 | { |
| 875 | if ( indexes ) |
| 876 | free( indexes ); |
| 877 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 878 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 879 | virtual Value *allocateArrayPage() |
| 880 | { |
| 881 | return static_cast<Value *>( malloc( sizeof(Value) * |
| 882 | ValueInternalArray::itemsPerPage ) ); |
| 883 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 884 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 885 | virtual void releaseArrayPage( Value *value ) |
| 886 | { |
| 887 | if ( value ) |
| 888 | free( value ); |
| 889 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 890 | }; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 891 | \endcode |
| 892 | */ |
| 893 | class JSON_API ValueArrayAllocator { |
| 894 | public: |
| 895 | virtual ~ValueArrayAllocator(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 896 | virtual ValueInternalArray* newArray() = 0; |
| 897 | virtual ValueInternalArray* newArrayCopy(const ValueInternalArray& other) = 0; |
| 898 | virtual void destructArray(ValueInternalArray* array) = 0; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 899 | /** \brief Reallocate array page index. |
| 900 | * Reallocates an array of pointer on each page. |
| 901 | * \param indexes [input] pointer on the current index. May be \c NULL. |
| 902 | * [output] pointer on the new index of at least |
| 903 | * \a minNewIndexCount pages. |
| 904 | * \param indexCount [input] current number of pages in the index. |
| 905 | * [output] number of page the reallocated index can handle. |
| 906 | * \b MUST be >= \a minNewIndexCount. |
| 907 | * \param minNewIndexCount Minimum number of page the new index must be able |
| 908 | * to |
| 909 | * handle. |
| 910 | */ |
| 911 | virtual void |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 912 | reallocateArrayPageIndex(Value**& indexes, |
| 913 | ValueInternalArray::PageIndex& indexCount, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 914 | ValueInternalArray::PageIndex minNewIndexCount) = 0; |
| 915 | virtual void |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 916 | releaseArrayPageIndex(Value** indexes, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 917 | ValueInternalArray::PageIndex indexCount) = 0; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 918 | virtual Value* allocateArrayPage() = 0; |
| 919 | virtual void releaseArrayPage(Value* value) = 0; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 920 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 921 | #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP |
| 922 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 923 | /** \brief base class for Value iterators. |
| 924 | * |
| 925 | */ |
| 926 | class JSON_API ValueIteratorBase { |
| 927 | public: |
| 928 | typedef std::bidirectional_iterator_tag iterator_category; |
| 929 | typedef unsigned int size_t; |
| 930 | typedef int difference_type; |
| 931 | typedef ValueIteratorBase SelfType; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 932 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 933 | ValueIteratorBase(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 934 | #ifndef JSON_VALUE_USE_INTERNAL_MAP |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 935 | explicit ValueIteratorBase(const Value::ObjectValues::iterator& current); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 936 | #else |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 937 | ValueIteratorBase(const ValueInternalArray::IteratorState& state); |
| 938 | ValueIteratorBase(const ValueInternalMap::IteratorState& state); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 939 | #endif |
| 940 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 941 | bool operator==(const SelfType& other) const { return isEqual(other); } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 942 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 943 | bool operator!=(const SelfType& other) const { return !isEqual(other); } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 944 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 945 | difference_type operator-(const SelfType& other) const { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 946 | return computeDistance(other); |
| 947 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 948 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 949 | /// Return either the index or the member name of the referenced value as a |
| 950 | /// Value. |
| 951 | Value key() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 952 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 953 | /// Return the index of the referenced Value. -1 if it is not an arrayValue. |
| 954 | UInt index() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 955 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 956 | /// Return the member name of the referenced Value. "" if it is not an |
| 957 | /// objectValue. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 958 | const char* memberName() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 959 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 960 | protected: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 961 | Value& deref() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 962 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 963 | void increment(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 964 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 965 | void decrement(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 966 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 967 | difference_type computeDistance(const SelfType& other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 968 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 969 | bool isEqual(const SelfType& other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 970 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 971 | void copy(const SelfType& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 972 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 973 | private: |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 974 | #ifndef JSON_VALUE_USE_INTERNAL_MAP |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 975 | Value::ObjectValues::iterator current_; |
| 976 | // Indicates that iterator is for a null value. |
| 977 | bool isNull_; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 978 | #else |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 979 | union { |
| 980 | ValueInternalArray::IteratorState array_; |
| 981 | ValueInternalMap::IteratorState map_; |
| 982 | } iterator_; |
| 983 | bool isArray_; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 984 | #endif |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 985 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 986 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 987 | /** \brief const iterator for object and array value. |
| 988 | * |
| 989 | */ |
| 990 | class JSON_API ValueConstIterator : public ValueIteratorBase { |
| 991 | friend class Value; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 992 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 993 | public: |
| 994 | typedef const Value value_type; |
| 995 | typedef unsigned int size_t; |
| 996 | typedef int difference_type; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 997 | typedef const Value& reference; |
| 998 | typedef const Value* pointer; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 999 | typedef ValueConstIterator SelfType; |
| 1000 | |
| 1001 | ValueConstIterator(); |
| 1002 | |
| 1003 | private: |
| 1004 | /*! \internal Use by Value to create an iterator. |
| 1005 | */ |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1006 | #ifndef JSON_VALUE_USE_INTERNAL_MAP |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 1007 | explicit ValueConstIterator(const Value::ObjectValues::iterator& current); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1008 | #else |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 1009 | ValueConstIterator(const ValueInternalArray::IteratorState& state); |
| 1010 | ValueConstIterator(const ValueInternalMap::IteratorState& state); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1011 | #endif |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1012 | public: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 1013 | SelfType& operator=(const ValueIteratorBase& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1014 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1015 | SelfType operator++(int) { |
| 1016 | SelfType temp(*this); |
| 1017 | ++*this; |
| 1018 | return temp; |
| 1019 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1020 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1021 | SelfType operator--(int) { |
| 1022 | SelfType temp(*this); |
| 1023 | --*this; |
| 1024 | return temp; |
| 1025 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1026 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 1027 | SelfType& operator--() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1028 | decrement(); |
| 1029 | return *this; |
| 1030 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1031 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 1032 | SelfType& operator++() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1033 | increment(); |
| 1034 | return *this; |
| 1035 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1036 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1037 | reference operator*() const { return deref(); } |
Braden McDorman | 540db3b | 2014-09-14 02:31:23 -0500 | [diff] [blame] | 1038 | |
| 1039 | pointer operator->() const { return &deref(); } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1040 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1041 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1042 | /** \brief Iterator for object and array value. |
| 1043 | */ |
| 1044 | class JSON_API ValueIterator : public ValueIteratorBase { |
| 1045 | friend class Value; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1046 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1047 | public: |
| 1048 | typedef Value value_type; |
| 1049 | typedef unsigned int size_t; |
| 1050 | typedef int difference_type; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 1051 | typedef Value& reference; |
| 1052 | typedef Value* pointer; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1053 | typedef ValueIterator SelfType; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1054 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1055 | ValueIterator(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 1056 | ValueIterator(const ValueConstIterator& other); |
| 1057 | ValueIterator(const ValueIterator& other); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1058 | |
| 1059 | private: |
| 1060 | /*! \internal Use by Value to create an iterator. |
| 1061 | */ |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1062 | #ifndef JSON_VALUE_USE_INTERNAL_MAP |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 1063 | explicit ValueIterator(const Value::ObjectValues::iterator& current); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1064 | #else |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 1065 | ValueIterator(const ValueInternalArray::IteratorState& state); |
| 1066 | ValueIterator(const ValueInternalMap::IteratorState& state); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1067 | #endif |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1068 | public: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 1069 | SelfType& operator=(const SelfType& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1070 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1071 | SelfType operator++(int) { |
| 1072 | SelfType temp(*this); |
| 1073 | ++*this; |
| 1074 | return temp; |
| 1075 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1076 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1077 | SelfType operator--(int) { |
| 1078 | SelfType temp(*this); |
| 1079 | --*this; |
| 1080 | return temp; |
| 1081 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1082 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 1083 | SelfType& operator--() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1084 | decrement(); |
| 1085 | return *this; |
| 1086 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1087 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 1088 | SelfType& operator++() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1089 | increment(); |
| 1090 | return *this; |
| 1091 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1092 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1093 | reference operator*() const { return deref(); } |
Braden McDorman | 540db3b | 2014-09-14 02:31:23 -0500 | [diff] [blame] | 1094 | |
| 1095 | pointer operator->() const { return &deref(); } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1096 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1097 | |
| 1098 | } // namespace Json |
| 1099 | |
datadiode | 9454e68 | 2015-01-20 15:25:04 -0600 | [diff] [blame] | 1100 | |
| 1101 | namespace std { |
| 1102 | /// Specialize std::swap() for Json::Value. |
| 1103 | template<> |
| 1104 | inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); } |
| 1105 | } |
| 1106 | |
| 1107 | |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 1108 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1109 | #pragma warning(pop) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 1110 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 1111 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1112 | #endif // CPPTL_JSON_H_INCLUDED |