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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 class StringValue; | 42 class StringValue; |
43 class Value; | 43 class Value; |
44 | 44 |
45 // The Value class is the base class for Values. A Value can be instantiated | 45 // 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 | 46 // via the Create*Value() factory methods, or by directly creating instances of |
47 // the subclasses. | 47 // the subclasses. |
48 // | 48 // |
49 // See the file-level comment above for more information. | 49 // See the file-level comment above for more information. |
50 class BASE_EXPORT Value { | 50 class BASE_EXPORT Value { |
51 public: | 51 public: |
52 enum Type { | 52 enum class Type { |
53 TYPE_NULL = 0, | 53 NONE = 0, |
54 TYPE_BOOLEAN, | 54 BOOLEAN, |
55 TYPE_INTEGER, | 55 INTEGER, |
56 TYPE_DOUBLE, | 56 DOUBLE, |
57 TYPE_STRING, | 57 STRING, |
58 TYPE_BINARY, | 58 BINARY, |
59 TYPE_DICTIONARY, | 59 DICTIONARY, |
60 TYPE_LIST | 60 LIST |
61 // Note: Do not add more types. See the file-level comment above for why. | 61 // Note: Do not add more types. See the file-level comment above for why. |
62 }; | 62 }; |
63 | 63 |
64 virtual ~Value(); | 64 virtual ~Value(); |
65 | 65 |
66 static std::unique_ptr<Value> CreateNullValue(); | 66 static std::unique_ptr<Value> CreateNullValue(); |
67 | 67 |
68 // Returns the name for a given |type|. | 68 // Returns the name for a given |type|. |
69 static const char* GetTypeName(Type type); | 69 static const char* GetTypeName(Type type); |
70 | 70 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 class BASE_EXPORT FundamentalValue : public Value { | 127 class BASE_EXPORT FundamentalValue : public Value { |
128 public: | 128 public: |
129 explicit FundamentalValue(bool in_value); | 129 explicit FundamentalValue(bool in_value); |
130 explicit FundamentalValue(int in_value); | 130 explicit FundamentalValue(int in_value); |
131 explicit FundamentalValue(double in_value); | 131 explicit FundamentalValue(double in_value); |
132 ~FundamentalValue() override; | 132 ~FundamentalValue() override; |
133 | 133 |
134 // Overridden from Value: | 134 // Overridden from Value: |
135 bool GetAsBoolean(bool* out_value) const override; | 135 bool GetAsBoolean(bool* out_value) const override; |
136 bool GetAsInteger(int* out_value) const override; | 136 bool GetAsInteger(int* out_value) const override; |
137 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as | 137 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as |
138 // doubles. | 138 // doubles. |
139 bool GetAsDouble(double* out_value) const override; | 139 bool GetAsDouble(double* out_value) const override; |
140 FundamentalValue* DeepCopy() const override; | 140 FundamentalValue* DeepCopy() const override; |
141 bool Equals(const Value* other) const override; | 141 bool Equals(const Value* other) const override; |
142 | 142 |
143 private: | 143 private: |
144 union { | 144 union { |
145 bool boolean_value_; | 145 bool boolean_value_; |
146 int integer_value_; | 146 int integer_value_; |
147 double double_value_; | 147 double double_value_; |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 // |out_value| is optional and will only be set if non-NULL. | 280 // |out_value| is optional and will only be set if non-NULL. |
281 bool Get(StringPiece path, const Value** out_value) const; | 281 bool Get(StringPiece path, const Value** out_value) const; |
282 bool Get(StringPiece path, Value** out_value); | 282 bool Get(StringPiece path, Value** out_value); |
283 | 283 |
284 // These are convenience forms of Get(). The value will be retrieved | 284 // These are convenience forms of Get(). The value will be retrieved |
285 // and the return value will be true if the path is valid and the value at | 285 // and the return value will be true if the path is valid and the value at |
286 // the end of the path can be returned in the form specified. | 286 // the end of the path can be returned in the form specified. |
287 // |out_value| is optional and will only be set if non-NULL. | 287 // |out_value| is optional and will only be set if non-NULL. |
288 bool GetBoolean(StringPiece path, bool* out_value) const; | 288 bool GetBoolean(StringPiece path, bool* out_value) const; |
289 bool GetInteger(StringPiece path, int* out_value) const; | 289 bool GetInteger(StringPiece path, int* out_value) const; |
290 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as | 290 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as |
291 // doubles. | 291 // doubles. |
292 bool GetDouble(StringPiece path, double* out_value) const; | 292 bool GetDouble(StringPiece path, double* out_value) const; |
293 bool GetString(StringPiece path, std::string* out_value) const; | 293 bool GetString(StringPiece path, std::string* out_value) const; |
294 bool GetString(StringPiece path, string16* out_value) const; | 294 bool GetString(StringPiece path, string16* out_value) const; |
295 bool GetStringASCII(StringPiece path, std::string* out_value) const; | 295 bool GetStringASCII(StringPiece path, std::string* out_value) const; |
296 bool GetBinary(StringPiece path, const BinaryValue** out_value) const; | 296 bool GetBinary(StringPiece path, const BinaryValue** out_value) const; |
297 bool GetBinary(StringPiece path, BinaryValue** out_value); | 297 bool GetBinary(StringPiece path, BinaryValue** out_value); |
298 bool GetDictionary(StringPiece path, | 298 bool GetDictionary(StringPiece path, |
299 const DictionaryValue** out_value) const; | 299 const DictionaryValue** out_value) const; |
300 bool GetDictionary(StringPiece path, DictionaryValue** out_value); | 300 bool GetDictionary(StringPiece path, DictionaryValue** out_value); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 // |out_value| is optional and will only be set if non-NULL. | 420 // |out_value| is optional and will only be set if non-NULL. |
421 bool Get(size_t index, const Value** out_value) const; | 421 bool Get(size_t index, const Value** out_value) const; |
422 bool Get(size_t index, Value** out_value); | 422 bool Get(size_t index, Value** out_value); |
423 | 423 |
424 // Convenience forms of Get(). Modifies |out_value| (and returns true) | 424 // Convenience forms of Get(). Modifies |out_value| (and returns true) |
425 // only if the index is valid and the Value at that index can be returned | 425 // only if the index is valid and the Value at that index can be returned |
426 // in the specified form. | 426 // in the specified form. |
427 // |out_value| is optional and will only be set if non-NULL. | 427 // |out_value| is optional and will only be set if non-NULL. |
428 bool GetBoolean(size_t index, bool* out_value) const; | 428 bool GetBoolean(size_t index, bool* out_value) const; |
429 bool GetInteger(size_t index, int* out_value) const; | 429 bool GetInteger(size_t index, int* out_value) const; |
430 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as | 430 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as |
431 // doubles. | 431 // doubles. |
432 bool GetDouble(size_t index, double* out_value) const; | 432 bool GetDouble(size_t index, double* out_value) const; |
433 bool GetString(size_t index, std::string* out_value) const; | 433 bool GetString(size_t index, std::string* out_value) const; |
434 bool GetString(size_t index, string16* out_value) const; | 434 bool GetString(size_t index, string16* out_value) const; |
435 bool GetBinary(size_t index, const BinaryValue** out_value) const; | 435 bool GetBinary(size_t index, const BinaryValue** out_value) const; |
436 bool GetBinary(size_t index, BinaryValue** out_value); | 436 bool GetBinary(size_t index, BinaryValue** out_value); |
437 bool GetDictionary(size_t index, const DictionaryValue** out_value) const; | 437 bool GetDictionary(size_t index, const DictionaryValue** out_value) const; |
438 bool GetDictionary(size_t index, DictionaryValue** out_value); | 438 bool GetDictionary(size_t index, DictionaryValue** out_value); |
439 bool GetList(size_t index, const ListValue** out_value) const; | 439 bool GetList(size_t index, const ListValue** out_value) const; |
440 bool GetList(size_t index, ListValue** out_value); | 440 bool GetList(size_t index, ListValue** out_value); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, | 555 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
556 const DictionaryValue& value) { | 556 const DictionaryValue& value) { |
557 return out << static_cast<const Value&>(value); | 557 return out << static_cast<const Value&>(value); |
558 } | 558 } |
559 | 559 |
560 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, | 560 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
561 const ListValue& value) { | 561 const ListValue& value) { |
562 return out << static_cast<const Value&>(value); | 562 return out << static_cast<const Value&>(value); |
563 } | 563 } |
564 | 564 |
| 565 // Stream operator so that enum class Types can be used in log statements. |
| 566 BASE_EXPORT std::ostream& operator<<(std::ostream& out, |
| 567 const Value::Type& type); |
| 568 |
565 } // namespace base | 569 } // namespace base |
566 | 570 |
567 #endif // BASE_VALUES_H_ | 571 #endif // BASE_VALUES_H_ |
OLD | NEW |