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