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: |
| 77 | explicit StaticString(const char *czstring) : str_(czstring) {} |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 78 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +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 | 9fa4e84 | 2014-07-01 08:48:54 +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: |
| 84 | const char *str_; |
| 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 | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 136 | static const Value null; |
| 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); |
| 171 | CZString(const char *cstr, DuplicationPolicy allocate); |
| 172 | CZString(const CZString &other); |
| 173 | ~CZString(); |
| 174 | CZString &operator=(const CZString &other); |
| 175 | bool operator<(const CZString &other) const; |
| 176 | bool operator==(const CZString &other) const; |
| 177 | ArrayIndex index() const; |
| 178 | const char *c_str() const; |
| 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: |
| 182 | void swap(CZString &other); |
| 183 | const char *cstr_; |
| 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); |
| 220 | Value(const char *value); |
| 221 | Value(const char *beginValue, const char *endValue); |
| 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 | */ |
| 232 | Value(const StaticString &value); |
| 233 | Value(const std::string &value); |
| 234 | #ifdef JSON_USE_CPPTL |
| 235 | Value(const CppTL::ConstString &value); |
| 236 | #endif |
| 237 | Value(bool value); |
| 238 | Value(const Value &other); |
| 239 | ~Value(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 240 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 241 | Value &operator=(const Value &other); |
| 242 | /// Swap values. |
| 243 | /// \note Currently, comments are intentionally not swapped, for |
| 244 | /// both logic and efficiency. |
| 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 | 9fa4e84 | 2014-07-01 08:48:54 +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 | 9fa4e84 | 2014-07-01 08:48:54 +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 | 9fa4e84 | 2014-07-01 08:48:54 +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 | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 259 | const char *asCString() const; |
| 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.) |
| 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.) |
| 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.) |
| 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.) |
| 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. |
| 342 | Value get(ArrayIndex index, const Value &defaultValue) const; |
| 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; |
| 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. |
| 351 | Value &operator[](const char *key); |
| 352 | /// Access an object value by name, returns null if there is no member with |
| 353 | /// that name. |
| 354 | const Value &operator[](const char *key) const; |
| 355 | /// Access an object value by name, create a null member if it does not exist. |
| 356 | Value &operator[](const std::string &key); |
| 357 | /// Access an object value by name, returns null if there is no member with |
| 358 | /// that name. |
| 359 | const Value &operator[](const std::string &key) const; |
| 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 | */ |
| 372 | Value &operator[](const StaticString &key); |
| 373 | #ifdef JSON_USE_CPPTL |
| 374 | /// Access an object value by name, create a null member if it does not exist. |
| 375 | Value &operator[](const CppTL::ConstString &key); |
| 376 | /// Access an object value by name, returns null if there is no member with |
| 377 | /// that name. |
| 378 | const Value &operator[](const CppTL::ConstString &key) const; |
| 379 | #endif |
| 380 | /// Return the member named key if it exist, defaultValue otherwise. |
| 381 | Value get(const char *key, const Value &defaultValue) const; |
| 382 | /// Return the member named key if it exist, defaultValue otherwise. |
| 383 | Value get(const std::string &key, const Value &defaultValue) const; |
| 384 | #ifdef JSON_USE_CPPTL |
| 385 | /// Return the member named key if it exist, defaultValue otherwise. |
| 386 | Value get(const CppTL::ConstString &key, const Value &defaultValue) const; |
| 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 |
| 394 | Value removeMember(const char *key); |
| 395 | /// Same as removeMember(const char*) |
| 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. |
| 399 | bool isMember(const char *key) const; |
| 400 | /// Return true if the object has a member named key. |
| 401 | bool isMember(const std::string &key) const; |
| 402 | #ifdef JSON_USE_CPPTL |
| 403 | /// Return true if the object has a member named key. |
| 404 | bool isMember(const CppTL::ConstString &key) const; |
| 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 /* ... */ |
| 420 | void setComment(const char *comment, CommentPlacement placement); |
| 421 | /// Comments must be //... or /* ... */ |
| 422 | void setComment(const std::string &comment, CommentPlacement placement); |
| 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: |
| 443 | Value &resolveReference(const char *key, bool isStatic); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 444 | |
| 445 | #ifdef JSON_VALUE_USE_INTERNAL_MAP |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 446 | inline bool isItemAvailable() const { return itemIsUsed_ == 0; } |
| 447 | |
| 448 | inline void setItemUsed(bool isUsed = true) { itemIsUsed_ = isUsed ? 1 : 0; } |
| 449 | |
| 450 | inline bool isMemberNameStatic() const { return memberNameIsStatic_ == 0; } |
| 451 | |
| 452 | inline void setMemberNameIsStatic(bool isStatic) { |
| 453 | memberNameIsStatic_ = isStatic ? 1 : 0; |
| 454 | } |
| 455 | #endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP |
| 456 | |
| 457 | private: |
| 458 | struct CommentInfo { |
| 459 | CommentInfo(); |
| 460 | ~CommentInfo(); |
| 461 | |
| 462 | void setComment(const char *text); |
| 463 | |
| 464 | char *comment_; |
| 465 | }; |
| 466 | |
| 467 | // struct MemberNamesTransform |
| 468 | //{ |
| 469 | // typedef const char *result_type; |
| 470 | // const char *operator()( const CZString &name ) const |
| 471 | // { |
| 472 | // return name.c_str(); |
| 473 | // } |
| 474 | //}; |
| 475 | |
| 476 | union ValueHolder { |
| 477 | LargestInt int_; |
| 478 | LargestUInt uint_; |
| 479 | double real_; |
| 480 | bool bool_; |
| 481 | char *string_; |
| 482 | #ifdef JSON_VALUE_USE_INTERNAL_MAP |
| 483 | ValueInternalArray *array_; |
| 484 | ValueInternalMap *map_; |
| 485 | #else |
| 486 | ObjectValues *map_; |
| 487 | #endif |
| 488 | } value_; |
| 489 | ValueType type_ : 8; |
| 490 | int allocated_ : 1; // Notes: if declared as bool, bitfield is useless. |
| 491 | #ifdef JSON_VALUE_USE_INTERNAL_MAP |
| 492 | unsigned int itemIsUsed_ : 1; // used by the ValueInternalMap container. |
| 493 | int memberNameIsStatic_ : 1; // used by the ValueInternalMap container. |
| 494 | #endif |
| 495 | CommentInfo *comments_; |
| 496 | |
| 497 | // [start, limit) byte offsets in the source JSON text from which this Value |
| 498 | // was extracted. |
| 499 | size_t start_; |
| 500 | size_t limit_; |
| 501 | }; |
| 502 | |
| 503 | /** \brief Experimental and untested: represents an element of the "path" to |
| 504 | * access a node. |
| 505 | */ |
| 506 | class JSON_API PathArgument { |
| 507 | public: |
| 508 | friend class Path; |
| 509 | |
| 510 | PathArgument(); |
| 511 | PathArgument(ArrayIndex index); |
| 512 | PathArgument(const char *key); |
| 513 | PathArgument(const std::string &key); |
| 514 | |
| 515 | private: |
| 516 | enum Kind { |
| 517 | kindNone = 0, |
| 518 | kindIndex, |
| 519 | kindKey |
| 520 | }; |
| 521 | std::string key_; |
| 522 | ArrayIndex index_; |
| 523 | Kind kind_; |
| 524 | }; |
| 525 | |
| 526 | /** \brief Experimental and untested: represents a "path" to access a node. |
| 527 | * |
| 528 | * Syntax: |
| 529 | * - "." => root node |
| 530 | * - ".[n]" => elements at index 'n' of root node (an array value) |
| 531 | * - ".name" => member named 'name' of root node (an object value) |
| 532 | * - ".name1.name2.name3" |
| 533 | * - ".[0][1][2].name1[3]" |
| 534 | * - ".%" => member name is provided as parameter |
| 535 | * - ".[%]" => index is provied as parameter |
| 536 | */ |
| 537 | class JSON_API Path { |
| 538 | public: |
| 539 | Path(const std::string &path, |
| 540 | const PathArgument &a1 = PathArgument(), |
| 541 | const PathArgument &a2 = PathArgument(), |
| 542 | const PathArgument &a3 = PathArgument(), |
| 543 | const PathArgument &a4 = PathArgument(), |
| 544 | const PathArgument &a5 = PathArgument()); |
| 545 | |
| 546 | const Value &resolve(const Value &root) const; |
| 547 | Value resolve(const Value &root, const Value &defaultValue) const; |
| 548 | /// Creates the "path" to access the specified node and returns a reference on |
| 549 | /// the node. |
| 550 | Value &make(Value &root) const; |
| 551 | |
| 552 | private: |
| 553 | typedef std::vector<const PathArgument *> InArgs; |
| 554 | typedef std::vector<PathArgument> Args; |
| 555 | |
| 556 | void makePath(const std::string &path, const InArgs &in); |
| 557 | void addPathInArg(const std::string &path, |
| 558 | const InArgs &in, |
| 559 | InArgs::const_iterator &itInArg, |
| 560 | PathArgument::Kind kind); |
| 561 | void invalidPath(const std::string &path, int location); |
| 562 | |
| 563 | Args args_; |
| 564 | }; |
| 565 | |
| 566 | #ifdef JSON_VALUE_USE_INTERNAL_MAP |
| 567 | /** \brief Allocator to customize Value internal map. |
| 568 | * Below is an example of a simple implementation (default implementation |
| 569 | actually |
| 570 | * use memory pool for speed). |
| 571 | * \code |
| 572 | class DefaultValueMapAllocator : public ValueMapAllocator |
| 573 | { |
| 574 | public: // overridden from ValueMapAllocator |
| 575 | virtual ValueInternalMap *newMap() |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 576 | { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 577 | return new ValueInternalMap(); |
| 578 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 579 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 580 | virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) |
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 | return new ValueInternalMap( other ); |
| 583 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 584 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 585 | virtual void destructMap( ValueInternalMap *map ) |
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 | delete map; |
| 588 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 589 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 590 | virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) |
| 591 | { |
| 592 | return new ValueInternalLink[size]; |
| 593 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 594 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 595 | virtual void releaseMapBuckets( ValueInternalLink *links ) |
| 596 | { |
| 597 | delete [] links; |
| 598 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 599 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 600 | virtual ValueInternalLink *allocateMapLink() |
| 601 | { |
| 602 | return new ValueInternalLink(); |
| 603 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 604 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 605 | virtual void releaseMapLink( ValueInternalLink *link ) |
| 606 | { |
| 607 | delete link; |
| 608 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 609 | }; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 610 | * \endcode |
| 611 | */ |
| 612 | class JSON_API ValueMapAllocator { |
| 613 | public: |
| 614 | virtual ~ValueMapAllocator(); |
| 615 | virtual ValueInternalMap *newMap() = 0; |
| 616 | virtual ValueInternalMap *newMapCopy(const ValueInternalMap &other) = 0; |
| 617 | virtual void destructMap(ValueInternalMap *map) = 0; |
| 618 | virtual ValueInternalLink *allocateMapBuckets(unsigned int size) = 0; |
| 619 | virtual void releaseMapBuckets(ValueInternalLink *links) = 0; |
| 620 | virtual ValueInternalLink *allocateMapLink() = 0; |
| 621 | virtual void releaseMapLink(ValueInternalLink *link) = 0; |
| 622 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 623 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 624 | /** \brief ValueInternalMap hash-map bucket chain link (for internal use only). |
| 625 | * \internal previous_ & next_ allows for bidirectional traversal. |
| 626 | */ |
| 627 | class JSON_API ValueInternalLink { |
| 628 | public: |
| 629 | enum { |
| 630 | itemPerLink = 6 |
| 631 | }; // sizeof(ValueInternalLink) = 128 on 32 bits architecture. |
| 632 | enum InternalFlags { |
| 633 | flagAvailable = 0, |
| 634 | flagUsed = 1 |
| 635 | }; |
| 636 | |
| 637 | ValueInternalLink(); |
| 638 | |
| 639 | ~ValueInternalLink(); |
| 640 | |
| 641 | Value items_[itemPerLink]; |
| 642 | char *keys_[itemPerLink]; |
| 643 | ValueInternalLink *previous_; |
| 644 | ValueInternalLink *next_; |
| 645 | }; |
| 646 | |
| 647 | /** \brief A linked page based hash-table implementation used internally by |
| 648 | *Value. |
| 649 | * \internal ValueInternalMap is a tradional bucket based hash-table, with a |
| 650 | *linked |
| 651 | * list in each bucket to handle collision. There is an addional twist in that |
| 652 | * each node of the collision linked list is a page containing a fixed amount of |
| 653 | * value. This provides a better compromise between memory usage and speed. |
| 654 | * |
| 655 | * Each bucket is made up of a chained list of ValueInternalLink. The last |
| 656 | * link of a given bucket can be found in the 'previous_' field of the following |
| 657 | *bucket. |
| 658 | * The last link of the last bucket is stored in tailLink_ as it has no |
| 659 | *following bucket. |
| 660 | * Only the last link of a bucket may contains 'available' item. The last link |
| 661 | *always |
| 662 | * contains at least one element unless is it the bucket one very first link. |
| 663 | */ |
| 664 | class JSON_API ValueInternalMap { |
| 665 | friend class ValueIteratorBase; |
| 666 | friend class Value; |
| 667 | |
| 668 | public: |
| 669 | typedef unsigned int HashKey; |
| 670 | typedef unsigned int BucketIndex; |
| 671 | |
| 672 | #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
| 673 | struct IteratorState { |
| 674 | IteratorState() : map_(0), link_(0), itemIndex_(0), bucketIndex_(0) {} |
| 675 | ValueInternalMap *map_; |
| 676 | ValueInternalLink *link_; |
| 677 | BucketIndex itemIndex_; |
| 678 | BucketIndex bucketIndex_; |
| 679 | }; |
| 680 | #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
| 681 | |
| 682 | ValueInternalMap(); |
| 683 | ValueInternalMap(const ValueInternalMap &other); |
| 684 | ValueInternalMap &operator=(const ValueInternalMap &other); |
| 685 | ~ValueInternalMap(); |
| 686 | |
| 687 | void swap(ValueInternalMap &other); |
| 688 | |
| 689 | BucketIndex size() const; |
| 690 | |
| 691 | void clear(); |
| 692 | |
| 693 | bool reserveDelta(BucketIndex growth); |
| 694 | |
| 695 | bool reserve(BucketIndex newItemCount); |
| 696 | |
| 697 | const Value *find(const char *key) const; |
| 698 | |
| 699 | Value *find(const char *key); |
| 700 | |
| 701 | Value &resolveReference(const char *key, bool isStatic); |
| 702 | |
| 703 | void remove(const char *key); |
| 704 | |
| 705 | void doActualRemove(ValueInternalLink *link, |
| 706 | BucketIndex index, |
| 707 | BucketIndex bucketIndex); |
| 708 | |
| 709 | ValueInternalLink *&getLastLinkInBucket(BucketIndex bucketIndex); |
| 710 | |
| 711 | Value &setNewItem(const char *key, |
| 712 | bool isStatic, |
| 713 | ValueInternalLink *link, |
| 714 | BucketIndex index); |
| 715 | |
| 716 | Value &unsafeAdd(const char *key, bool isStatic, HashKey hashedKey); |
| 717 | |
| 718 | HashKey hash(const char *key) const; |
| 719 | |
| 720 | int compare(const ValueInternalMap &other) const; |
| 721 | |
| 722 | private: |
| 723 | void makeBeginIterator(IteratorState &it) const; |
| 724 | void makeEndIterator(IteratorState &it) const; |
| 725 | static bool equals(const IteratorState &x, const IteratorState &other); |
| 726 | static void increment(IteratorState &iterator); |
| 727 | static void incrementBucket(IteratorState &iterator); |
| 728 | static void decrement(IteratorState &iterator); |
| 729 | static const char *key(const IteratorState &iterator); |
| 730 | static const char *key(const IteratorState &iterator, bool &isStatic); |
| 731 | static Value &value(const IteratorState &iterator); |
| 732 | static int distance(const IteratorState &x, const IteratorState &y); |
| 733 | |
| 734 | private: |
| 735 | ValueInternalLink *buckets_; |
| 736 | ValueInternalLink *tailLink_; |
| 737 | BucketIndex bucketsSize_; |
| 738 | BucketIndex itemCount_; |
| 739 | }; |
| 740 | |
| 741 | /** \brief A simplified deque implementation used internally by Value. |
| 742 | * \internal |
| 743 | * It is based on a list of fixed "page", each page contains a fixed number of |
| 744 | *items. |
| 745 | * Instead of using a linked-list, a array of pointer is used for fast item |
| 746 | *look-up. |
| 747 | * Look-up for an element is as follow: |
| 748 | * - compute page index: pageIndex = itemIndex / itemsPerPage |
| 749 | * - look-up item in page: pages_[pageIndex][itemIndex % itemsPerPage] |
| 750 | * |
| 751 | * Insertion is amortized constant time (only the array containing the index of |
| 752 | *pointers |
| 753 | * need to be reallocated when items are appended). |
| 754 | */ |
| 755 | class JSON_API ValueInternalArray { |
| 756 | friend class Value; |
| 757 | friend class ValueIteratorBase; |
| 758 | |
| 759 | public: |
| 760 | enum { |
| 761 | itemsPerPage = 8 |
| 762 | }; // should be a power of 2 for fast divide and modulo. |
| 763 | typedef Value::ArrayIndex ArrayIndex; |
| 764 | typedef unsigned int PageIndex; |
| 765 | |
| 766 | #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
| 767 | struct IteratorState // Must be a POD |
| 768 | { |
| 769 | IteratorState() : array_(0), currentPageIndex_(0), currentItemIndex_(0) {} |
| 770 | ValueInternalArray *array_; |
| 771 | Value **currentPageIndex_; |
| 772 | unsigned int currentItemIndex_; |
| 773 | }; |
| 774 | #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
| 775 | |
| 776 | ValueInternalArray(); |
| 777 | ValueInternalArray(const ValueInternalArray &other); |
| 778 | ValueInternalArray &operator=(const ValueInternalArray &other); |
| 779 | ~ValueInternalArray(); |
| 780 | void swap(ValueInternalArray &other); |
| 781 | |
| 782 | void clear(); |
| 783 | void resize(ArrayIndex newSize); |
| 784 | |
| 785 | Value &resolveReference(ArrayIndex index); |
| 786 | |
| 787 | Value *find(ArrayIndex index) const; |
| 788 | |
| 789 | ArrayIndex size() const; |
| 790 | |
| 791 | int compare(const ValueInternalArray &other) const; |
| 792 | |
| 793 | private: |
| 794 | static bool equals(const IteratorState &x, const IteratorState &other); |
| 795 | static void increment(IteratorState &iterator); |
| 796 | static void decrement(IteratorState &iterator); |
| 797 | static Value &dereference(const IteratorState &iterator); |
| 798 | static Value &unsafeDereference(const IteratorState &iterator); |
| 799 | static int distance(const IteratorState &x, const IteratorState &y); |
| 800 | static ArrayIndex indexOf(const IteratorState &iterator); |
| 801 | void makeBeginIterator(IteratorState &it) const; |
| 802 | void makeEndIterator(IteratorState &it) const; |
| 803 | void makeIterator(IteratorState &it, ArrayIndex index) const; |
| 804 | |
| 805 | void makeIndexValid(ArrayIndex index); |
| 806 | |
| 807 | Value **pages_; |
| 808 | ArrayIndex size_; |
| 809 | PageIndex pageCount_; |
| 810 | }; |
| 811 | |
| 812 | /** \brief Experimental: do not use. Allocator to customize Value internal |
| 813 | array. |
| 814 | * Below is an example of a simple implementation (actual implementation use |
| 815 | * memory pool). |
| 816 | \code |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 817 | class DefaultValueArrayAllocator : public ValueArrayAllocator |
| 818 | { |
| 819 | public: // overridden from ValueArrayAllocator |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 820 | virtual ~DefaultValueArrayAllocator() |
| 821 | { |
| 822 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 823 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 824 | virtual ValueInternalArray *newArray() |
| 825 | { |
| 826 | return new ValueInternalArray(); |
| 827 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 828 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 829 | virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) |
| 830 | { |
| 831 | return new ValueInternalArray( other ); |
| 832 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 833 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 834 | virtual void destruct( ValueInternalArray *array ) |
| 835 | { |
| 836 | delete array; |
| 837 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 838 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 839 | virtual void reallocateArrayPageIndex( Value **&indexes, |
| 840 | ValueInternalArray::PageIndex |
| 841 | &indexCount, |
| 842 | ValueInternalArray::PageIndex |
| 843 | minNewIndexCount ) |
| 844 | { |
| 845 | ValueInternalArray::PageIndex newIndexCount = (indexCount*3)/2 + 1; |
| 846 | if ( minNewIndexCount > newIndexCount ) |
| 847 | newIndexCount = minNewIndexCount; |
| 848 | void *newIndexes = realloc( indexes, sizeof(Value*) * newIndexCount ); |
| 849 | if ( !newIndexes ) |
| 850 | throw std::bad_alloc(); |
| 851 | indexCount = newIndexCount; |
| 852 | indexes = static_cast<Value **>( newIndexes ); |
| 853 | } |
| 854 | virtual void releaseArrayPageIndex( Value **indexes, |
| 855 | ValueInternalArray::PageIndex indexCount ) |
| 856 | { |
| 857 | if ( indexes ) |
| 858 | free( indexes ); |
| 859 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 860 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 861 | virtual Value *allocateArrayPage() |
| 862 | { |
| 863 | return static_cast<Value *>( malloc( sizeof(Value) * |
| 864 | ValueInternalArray::itemsPerPage ) ); |
| 865 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 866 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 867 | virtual void releaseArrayPage( Value *value ) |
| 868 | { |
| 869 | if ( value ) |
| 870 | free( value ); |
| 871 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 872 | }; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 873 | \endcode |
| 874 | */ |
| 875 | class JSON_API ValueArrayAllocator { |
| 876 | public: |
| 877 | virtual ~ValueArrayAllocator(); |
| 878 | virtual ValueInternalArray *newArray() = 0; |
| 879 | virtual ValueInternalArray *newArrayCopy(const ValueInternalArray &other) = 0; |
| 880 | virtual void destructArray(ValueInternalArray *array) = 0; |
| 881 | /** \brief Reallocate array page index. |
| 882 | * Reallocates an array of pointer on each page. |
| 883 | * \param indexes [input] pointer on the current index. May be \c NULL. |
| 884 | * [output] pointer on the new index of at least |
| 885 | * \a minNewIndexCount pages. |
| 886 | * \param indexCount [input] current number of pages in the index. |
| 887 | * [output] number of page the reallocated index can handle. |
| 888 | * \b MUST be >= \a minNewIndexCount. |
| 889 | * \param minNewIndexCount Minimum number of page the new index must be able |
| 890 | * to |
| 891 | * handle. |
| 892 | */ |
| 893 | virtual void |
| 894 | reallocateArrayPageIndex(Value **&indexes, |
| 895 | ValueInternalArray::PageIndex &indexCount, |
| 896 | ValueInternalArray::PageIndex minNewIndexCount) = 0; |
| 897 | virtual void |
| 898 | releaseArrayPageIndex(Value **indexes, |
| 899 | ValueInternalArray::PageIndex indexCount) = 0; |
| 900 | virtual Value *allocateArrayPage() = 0; |
| 901 | virtual void releaseArrayPage(Value *value) = 0; |
| 902 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 903 | #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP |
| 904 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 905 | /** \brief base class for Value iterators. |
| 906 | * |
| 907 | */ |
| 908 | class JSON_API ValueIteratorBase { |
| 909 | public: |
| 910 | typedef std::bidirectional_iterator_tag iterator_category; |
| 911 | typedef unsigned int size_t; |
| 912 | typedef int difference_type; |
| 913 | typedef ValueIteratorBase SelfType; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 914 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 915 | ValueIteratorBase(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 916 | #ifndef JSON_VALUE_USE_INTERNAL_MAP |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 917 | explicit ValueIteratorBase(const Value::ObjectValues::iterator ¤t); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 918 | #else |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 919 | ValueIteratorBase(const ValueInternalArray::IteratorState &state); |
| 920 | ValueIteratorBase(const ValueInternalMap::IteratorState &state); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 921 | #endif |
| 922 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 923 | bool operator==(const SelfType &other) const { return isEqual(other); } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 924 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +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 | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 927 | difference_type operator-(const SelfType &other) const { |
| 928 | return computeDistance(other); |
| 929 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 930 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 931 | /// Return either the index or the member name of the referenced value as a |
| 932 | /// Value. |
| 933 | Value key() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 934 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 935 | /// Return the index of the referenced Value. -1 if it is not an arrayValue. |
| 936 | UInt index() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 937 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 938 | /// Return the member name of the referenced Value. "" if it is not an |
| 939 | /// objectValue. |
| 940 | const char *memberName() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 941 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 942 | protected: |
| 943 | Value &deref() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 944 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 945 | void increment(); |
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 decrement(); |
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 | difference_type computeDistance(const SelfType &other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 950 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 951 | bool isEqual(const SelfType &other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 952 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 953 | void copy(const SelfType &other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 954 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 955 | private: |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 956 | #ifndef JSON_VALUE_USE_INTERNAL_MAP |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 957 | Value::ObjectValues::iterator current_; |
| 958 | // Indicates that iterator is for a null value. |
| 959 | bool isNull_; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 960 | #else |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 961 | union { |
| 962 | ValueInternalArray::IteratorState array_; |
| 963 | ValueInternalMap::IteratorState map_; |
| 964 | } iterator_; |
| 965 | bool isArray_; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 966 | #endif |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 967 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 968 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 969 | /** \brief const iterator for object and array value. |
| 970 | * |
| 971 | */ |
| 972 | class JSON_API ValueConstIterator : public ValueIteratorBase { |
| 973 | friend class Value; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 974 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 975 | public: |
| 976 | typedef const Value value_type; |
| 977 | typedef unsigned int size_t; |
| 978 | typedef int difference_type; |
| 979 | typedef const Value &reference; |
| 980 | typedef const Value *pointer; |
| 981 | typedef ValueConstIterator SelfType; |
| 982 | |
| 983 | ValueConstIterator(); |
| 984 | |
| 985 | private: |
| 986 | /*! \internal Use by Value to create an iterator. |
| 987 | */ |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 988 | #ifndef JSON_VALUE_USE_INTERNAL_MAP |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 989 | explicit ValueConstIterator(const Value::ObjectValues::iterator ¤t); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 990 | #else |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 991 | ValueConstIterator(const ValueInternalArray::IteratorState &state); |
| 992 | ValueConstIterator(const ValueInternalMap::IteratorState &state); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 993 | #endif |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 994 | public: |
| 995 | SelfType &operator=(const ValueIteratorBase &other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 996 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 997 | SelfType operator++(int) { |
| 998 | SelfType temp(*this); |
| 999 | ++*this; |
| 1000 | return temp; |
| 1001 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1002 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1003 | SelfType operator--(int) { |
| 1004 | SelfType temp(*this); |
| 1005 | --*this; |
| 1006 | return temp; |
| 1007 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1008 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1009 | SelfType &operator--() { |
| 1010 | decrement(); |
| 1011 | return *this; |
| 1012 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1013 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1014 | SelfType &operator++() { |
| 1015 | increment(); |
| 1016 | return *this; |
| 1017 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1018 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1019 | reference operator*() const { return deref(); } |
| 1020 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1021 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1022 | /** \brief Iterator for object and array value. |
| 1023 | */ |
| 1024 | class JSON_API ValueIterator : public ValueIteratorBase { |
| 1025 | friend class Value; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1026 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1027 | public: |
| 1028 | typedef Value value_type; |
| 1029 | typedef unsigned int size_t; |
| 1030 | typedef int difference_type; |
| 1031 | typedef Value &reference; |
| 1032 | typedef Value *pointer; |
| 1033 | typedef ValueIterator SelfType; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1034 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1035 | ValueIterator(); |
| 1036 | ValueIterator(const ValueConstIterator &other); |
| 1037 | ValueIterator(const ValueIterator &other); |
| 1038 | |
| 1039 | private: |
| 1040 | /*! \internal Use by Value to create an iterator. |
| 1041 | */ |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1042 | #ifndef JSON_VALUE_USE_INTERNAL_MAP |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1043 | explicit ValueIterator(const Value::ObjectValues::iterator ¤t); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1044 | #else |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1045 | ValueIterator(const ValueInternalArray::IteratorState &state); |
| 1046 | ValueIterator(const ValueInternalMap::IteratorState &state); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1047 | #endif |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1048 | public: |
| 1049 | SelfType &operator=(const SelfType &other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1050 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1051 | SelfType operator++(int) { |
| 1052 | SelfType temp(*this); |
| 1053 | ++*this; |
| 1054 | return temp; |
| 1055 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1056 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1057 | SelfType operator--(int) { |
| 1058 | SelfType temp(*this); |
| 1059 | --*this; |
| 1060 | return temp; |
| 1061 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1062 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1063 | SelfType &operator--() { |
| 1064 | decrement(); |
| 1065 | return *this; |
| 1066 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1067 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1068 | SelfType &operator++() { |
| 1069 | increment(); |
| 1070 | return *this; |
| 1071 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1072 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1073 | reference operator*() const { return deref(); } |
| 1074 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1075 | |
| 1076 | } // namespace Json |
| 1077 | |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 1078 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 1079 | #pragma warning(pop) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 1080 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 1081 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1082 | #endif // CPPTL_JSON_H_INCLUDED |