OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file specifies a recursive data storage class called Value intended for | 5 // This file specifies a recursive data storage class called Value intended for |
6 // storing settings and other persistable data. | 6 // storing settings and other persistable data. |
7 // | 7 // |
8 // A Value represents something that can be stored in JSON or passed to/from | 8 // A Value represents something that can be stored in JSON or passed to/from |
9 // JavaScript. As such, it is NOT a generalized variant type, since only the | 9 // JavaScript. As such, it is NOT a generalized variant type, since only the |
10 // types supported by JavaScript/JSON are supported. | 10 // types supported by JavaScript/JSON are supported. |
(...skipping 22 matching lines...) Expand all Loading... |
33 #include "base/macros.h" | 33 #include "base/macros.h" |
34 #include "base/memory/manual_constructor.h" | 34 #include "base/memory/manual_constructor.h" |
35 #include "base/strings/string16.h" | 35 #include "base/strings/string16.h" |
36 #include "base/strings/string_piece.h" | 36 #include "base/strings/string_piece.h" |
37 | 37 |
38 namespace base { | 38 namespace base { |
39 | 39 |
40 class DictionaryValue; | 40 class DictionaryValue; |
41 class ListValue; | 41 class ListValue; |
42 class Value; | 42 class Value; |
43 using BinaryValue = Value; | |
44 | 43 |
45 // The Value class is the base class for Values. A Value can be instantiated | 44 // The Value class is the base class for Values. A Value can be instantiated |
46 // via the Create*Value() factory methods, or by directly creating instances of | 45 // via the Create*Value() factory methods, or by directly creating instances of |
47 // the subclasses. | 46 // the subclasses. |
48 // | 47 // |
49 // See the file-level comment above for more information. | 48 // See the file-level comment above for more information. |
50 class BASE_EXPORT Value { | 49 class BASE_EXPORT Value { |
51 public: | 50 public: |
52 using DictStorage = base::flat_map<std::string, std::unique_ptr<Value>>; | 51 using DictStorage = base::flat_map<std::string, std::unique_ptr<Value>>; |
53 using ListStorage = std::vector<std::unique_ptr<Value>>; | 52 using ListStorage = std::vector<std::unique_ptr<Value>>; |
54 | 53 |
55 enum class Type { | 54 enum class Type { |
56 NONE = 0, | 55 NONE = 0, |
57 BOOLEAN, | 56 BOOLEAN, |
58 INTEGER, | 57 INTEGER, |
59 DOUBLE, | 58 DOUBLE, |
60 STRING, | 59 STRING, |
61 BINARY, | 60 BINARY, |
62 DICTIONARY, | 61 DICTIONARY, |
63 LIST | 62 LIST |
64 // Note: Do not add more types. See the file-level comment above for why. | 63 // Note: Do not add more types. See the file-level comment above for why. |
65 }; | 64 }; |
66 | 65 |
67 // For situations where you want to keep ownership of your buffer, this | 66 // For situations where you want to keep ownership of your buffer, this |
68 // factory method creates a new BinaryValue by copying the contents of the | 67 // factory method creates a new BinaryValue by copying the contents of the |
69 // buffer that's passed in. | 68 // buffer that's passed in. |
70 // DEPRECATED, use MakeUnique<Value>(const std::vector<char>&) instead. | 69 // DEPRECATED, use MakeUnique<Value>(const std::vector<char>&) instead. |
71 // TODO(crbug.com/646113): Delete this and migrate callsites. | 70 // TODO(crbug.com/646113): Delete this and migrate callsites. |
72 static std::unique_ptr<BinaryValue> CreateWithCopiedBuffer(const char* buffer, | 71 static std::unique_ptr<Value> CreateWithCopiedBuffer(const char* buffer, |
73 size_t size); | 72 size_t size); |
74 | 73 |
75 Value(const Value& that); | 74 Value(const Value& that); |
76 Value(Value&& that) noexcept; | 75 Value(Value&& that) noexcept; |
77 Value() noexcept; // A null value. | 76 Value() noexcept; // A null value. |
78 explicit Value(Type type); | 77 explicit Value(Type type); |
79 explicit Value(bool in_bool); | 78 explicit Value(bool in_bool); |
80 explicit Value(int in_int); | 79 explicit Value(int in_int); |
81 explicit Value(double in_double); | 80 explicit Value(double in_double); |
82 | 81 |
83 // Value(const char*) and Value(const char16*) are required despite | 82 // Value(const char*) and Value(const char16*) are required despite |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 // If the current object can be converted into the given type, the value is | 137 // If the current object can be converted into the given type, the value is |
139 // returned through the |out_value| parameter and true is returned; | 138 // returned through the |out_value| parameter and true is returned; |
140 // otherwise, false is returned and |out_value| is unchanged. | 139 // otherwise, false is returned and |out_value| is unchanged. |
141 bool GetAsBoolean(bool* out_value) const; | 140 bool GetAsBoolean(bool* out_value) const; |
142 bool GetAsInteger(int* out_value) const; | 141 bool GetAsInteger(int* out_value) const; |
143 bool GetAsDouble(double* out_value) const; | 142 bool GetAsDouble(double* out_value) const; |
144 bool GetAsString(std::string* out_value) const; | 143 bool GetAsString(std::string* out_value) const; |
145 bool GetAsString(string16* out_value) const; | 144 bool GetAsString(string16* out_value) const; |
146 bool GetAsString(const Value** out_value) const; | 145 bool GetAsString(const Value** out_value) const; |
147 bool GetAsString(StringPiece* out_value) const; | 146 bool GetAsString(StringPiece* out_value) const; |
148 bool GetAsBinary(const BinaryValue** out_value) const; | 147 bool GetAsBinary(const Value** out_value) const; |
149 // ListValue::From is the equivalent for std::unique_ptr conversions. | 148 // ListValue::From is the equivalent for std::unique_ptr conversions. |
150 bool GetAsList(ListValue** out_value); | 149 bool GetAsList(ListValue** out_value); |
151 bool GetAsList(const ListValue** out_value) const; | 150 bool GetAsList(const ListValue** out_value) const; |
152 // DictionaryValue::From is the equivalent for std::unique_ptr conversions. | 151 // DictionaryValue::From is the equivalent for std::unique_ptr conversions. |
153 bool GetAsDictionary(DictionaryValue** out_value); | 152 bool GetAsDictionary(DictionaryValue** out_value); |
154 bool GetAsDictionary(const DictionaryValue** out_value) const; | 153 bool GetAsDictionary(const DictionaryValue** out_value) const; |
155 // Note: Do not add more types. See the file-level comment above for why. | 154 // Note: Do not add more types. See the file-level comment above for why. |
156 | 155 |
157 // This creates a deep copy of the entire Value tree, and returns a pointer | 156 // This creates a deep copy of the entire Value tree, and returns a pointer |
158 // to the copy. The caller gets ownership of the copy, of course. | 157 // to the copy. The caller gets ownership of the copy, of course. |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 // the end of the path can be returned in the form specified. | 281 // the end of the path can be returned in the form specified. |
283 // |out_value| is optional and will only be set if non-NULL. | 282 // |out_value| is optional and will only be set if non-NULL. |
284 bool GetBoolean(StringPiece path, bool* out_value) const; | 283 bool GetBoolean(StringPiece path, bool* out_value) const; |
285 bool GetInteger(StringPiece path, int* out_value) const; | 284 bool GetInteger(StringPiece path, int* out_value) const; |
286 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as | 285 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as |
287 // doubles. | 286 // doubles. |
288 bool GetDouble(StringPiece path, double* out_value) const; | 287 bool GetDouble(StringPiece path, double* out_value) const; |
289 bool GetString(StringPiece path, std::string* out_value) const; | 288 bool GetString(StringPiece path, std::string* out_value) const; |
290 bool GetString(StringPiece path, string16* out_value) const; | 289 bool GetString(StringPiece path, string16* out_value) const; |
291 bool GetStringASCII(StringPiece path, std::string* out_value) const; | 290 bool GetStringASCII(StringPiece path, std::string* out_value) const; |
292 bool GetBinary(StringPiece path, const BinaryValue** out_value) const; | 291 bool GetBinary(StringPiece path, const Value** out_value) const; |
293 bool GetBinary(StringPiece path, BinaryValue** out_value); | 292 bool GetBinary(StringPiece path, Value** out_value); |
294 bool GetDictionary(StringPiece path, | 293 bool GetDictionary(StringPiece path, |
295 const DictionaryValue** out_value) const; | 294 const DictionaryValue** out_value) const; |
296 bool GetDictionary(StringPiece path, DictionaryValue** out_value); | 295 bool GetDictionary(StringPiece path, DictionaryValue** out_value); |
297 bool GetList(StringPiece path, const ListValue** out_value) const; | 296 bool GetList(StringPiece path, const ListValue** out_value) const; |
298 bool GetList(StringPiece path, ListValue** out_value); | 297 bool GetList(StringPiece path, ListValue** out_value); |
299 | 298 |
300 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to | 299 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
301 // be used as paths. | 300 // be used as paths. |
302 bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const; | 301 bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const; |
303 bool GetWithoutPathExpansion(StringPiece key, Value** out_value); | 302 bool GetWithoutPathExpansion(StringPiece key, Value** out_value); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 // only if the index is valid and the Value at that index can be returned | 413 // only if the index is valid and the Value at that index can be returned |
415 // in the specified form. | 414 // in the specified form. |
416 // |out_value| is optional and will only be set if non-NULL. | 415 // |out_value| is optional and will only be set if non-NULL. |
417 bool GetBoolean(size_t index, bool* out_value) const; | 416 bool GetBoolean(size_t index, bool* out_value) const; |
418 bool GetInteger(size_t index, int* out_value) const; | 417 bool GetInteger(size_t index, int* out_value) const; |
419 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as | 418 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as |
420 // doubles. | 419 // doubles. |
421 bool GetDouble(size_t index, double* out_value) const; | 420 bool GetDouble(size_t index, double* out_value) const; |
422 bool GetString(size_t index, std::string* out_value) const; | 421 bool GetString(size_t index, std::string* out_value) const; |
423 bool GetString(size_t index, string16* out_value) const; | 422 bool GetString(size_t index, string16* out_value) const; |
424 bool GetBinary(size_t index, const BinaryValue** out_value) const; | 423 bool GetBinary(size_t index, const Value** out_value) const; |
425 bool GetBinary(size_t index, BinaryValue** out_value); | 424 bool GetBinary(size_t index, Value** out_value); |
426 bool GetDictionary(size_t index, const DictionaryValue** out_value) const; | 425 bool GetDictionary(size_t index, const DictionaryValue** out_value) const; |
427 bool GetDictionary(size_t index, DictionaryValue** out_value); | 426 bool GetDictionary(size_t index, DictionaryValue** out_value); |
428 bool GetList(size_t index, const ListValue** out_value) const; | 427 bool GetList(size_t index, const ListValue** out_value) const; |
429 bool GetList(size_t index, ListValue** out_value); | 428 bool GetList(size_t index, ListValue** out_value); |
430 | 429 |
431 // Removes the Value with the specified index from this list. | 430 // Removes the Value with the specified index from this list. |
432 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be | 431 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
433 // passed out via |out_value|. If |out_value| is NULL, the removed value will | 432 // passed out via |out_value|. If |out_value| is NULL, the removed value will |
434 // be deleted. This method returns true if |index| is valid; otherwise | 433 // be deleted. This method returns true if |index| is valid; otherwise |
435 // it will return false and the ListValue object will be unchanged. | 434 // it will return false and the ListValue object will be unchanged. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 return out << static_cast<const Value&>(value); | 532 return out << static_cast<const Value&>(value); |
534 } | 533 } |
535 | 534 |
536 // Stream operator so that enum class Types can be used in log statements. | 535 // Stream operator so that enum class Types can be used in log statements. |
537 BASE_EXPORT std::ostream& operator<<(std::ostream& out, | 536 BASE_EXPORT std::ostream& operator<<(std::ostream& out, |
538 const Value::Type& type); | 537 const Value::Type& type); |
539 | 538 |
540 } // namespace base | 539 } // namespace base |
541 | 540 |
542 #endif // BASE_VALUES_H_ | 541 #endif // BASE_VALUES_H_ |
OLD | NEW |