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