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 19 matching lines...) Expand all Loading... |
30 #include "base/base_export.h" | 30 #include "base/base_export.h" |
31 #include "base/compiler_specific.h" | 31 #include "base/compiler_specific.h" |
32 #include "base/macros.h" | 32 #include "base/macros.h" |
33 #include "base/strings/string16.h" | 33 #include "base/strings/string16.h" |
34 #include "base/strings/string_piece.h" | 34 #include "base/strings/string_piece.h" |
35 | 35 |
36 namespace base { | 36 namespace base { |
37 | 37 |
38 class BinaryValue; | 38 class BinaryValue; |
39 class DictionaryValue; | 39 class DictionaryValue; |
40 class FundamentalValue; | |
41 class ListValue; | 40 class ListValue; |
42 class StringValue; | 41 class StringValue; |
43 class Value; | 42 class 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 enum Type { | 51 enum Type { |
53 TYPE_NULL = 0, | 52 TYPE_NULL = 0, |
54 TYPE_BOOLEAN, | 53 TYPE_BOOLEAN, |
55 TYPE_INTEGER, | 54 TYPE_INTEGER, |
56 TYPE_DOUBLE, | 55 TYPE_DOUBLE, |
57 TYPE_STRING, | 56 TYPE_STRING, |
58 TYPE_BINARY, | 57 TYPE_BINARY, |
59 TYPE_DICTIONARY, | 58 TYPE_DICTIONARY, |
60 TYPE_LIST | 59 TYPE_LIST |
61 // Note: Do not add more types. See the file-level comment above for why. | 60 // Note: Do not add more types. See the file-level comment above for why. |
62 }; | 61 }; |
63 | 62 |
64 Value(); // A null value. | 63 Value(); // A null value. |
| 64 Value(bool in_bool); |
| 65 Value(int in_int); |
| 66 Value(double in_double); |
65 | 67 |
66 virtual ~Value(); | 68 virtual ~Value(); |
67 | 69 |
68 static std::unique_ptr<Value> CreateNullValue(); | 70 static std::unique_ptr<Value> CreateNullValue(); |
69 | 71 |
70 // Returns the name for a given |type|. | 72 // Returns the name for a given |type|. |
71 static const char* GetTypeName(Type type); | 73 static const char* GetTypeName(Type type); |
72 | 74 |
73 // Returns the type of the value stored by the current Value object. | 75 // Returns the type of the value stored by the current Value object. |
74 // Each type will be implemented by only one subclass of Value, so it's | 76 // Each type will be implemented by only one subclass of Value, so it's |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 // Compares if two Value objects have equal contents. Can handle NULLs. | 129 // Compares if two Value objects have equal contents. Can handle NULLs. |
128 // NULLs are considered equal but different from Value::CreateNullValue(). | 130 // NULLs are considered equal but different from Value::CreateNullValue(). |
129 static bool Equals(const Value* a, const Value* b); | 131 static bool Equals(const Value* a, const Value* b); |
130 | 132 |
131 protected: | 133 protected: |
132 // These aren't safe for end-users, but they are useful for subclasses. | 134 // These aren't safe for end-users, but they are useful for subclasses. |
133 explicit Value(Type type); | 135 explicit Value(Type type); |
134 Value(const Value& that); | 136 Value(const Value& that); |
135 Value& operator=(const Value& that); | 137 Value& operator=(const Value& that); |
136 | 138 |
137 // TODO(brettw) move to public when FundamentalValue is gone. | |
138 Value(bool in_bool); | |
139 Value(int in_int); | |
140 Value(double in_double); | |
141 | |
142 private: | 139 private: |
143 Type type_; | 140 Type type_; |
144 | 141 |
145 union { | 142 union { |
146 bool bool_value_; | 143 bool bool_value_; |
147 int int_value_; | 144 int int_value_; |
148 double double_value_; | 145 double double_value_; |
149 }; | 146 }; |
150 }; | 147 }; |
151 | 148 |
152 // FundamentalValue represents the simple fundamental types of values. | |
153 // TODO(brettw) remove when callers are updated to use raw Value. | |
154 class BASE_EXPORT FundamentalValue : public Value { | |
155 public: | |
156 explicit FundamentalValue(bool in_value); | |
157 explicit FundamentalValue(int in_value); | |
158 explicit FundamentalValue(double in_value); | |
159 ~FundamentalValue() override; | |
160 }; | |
161 | |
162 class BASE_EXPORT StringValue : public Value { | 149 class BASE_EXPORT StringValue : public Value { |
163 public: | 150 public: |
164 // Initializes a StringValue with a UTF-8 narrow character string. | 151 // Initializes a StringValue with a UTF-8 narrow character string. |
165 explicit StringValue(StringPiece in_value); | 152 explicit StringValue(StringPiece in_value); |
166 | 153 |
167 // Initializes a StringValue with a string16. | 154 // Initializes a StringValue with a string16. |
168 explicit StringValue(const string16& in_value); | 155 explicit StringValue(const string16& in_value); |
169 | 156 |
170 ~StringValue() override; | 157 ~StringValue() override; |
171 | 158 |
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 std::string* error_str) = 0; | 534 std::string* error_str) = 0; |
548 }; | 535 }; |
549 | 536 |
550 // Stream operator so Values can be used in assertion statements. In order that | 537 // Stream operator so Values can be used in assertion statements. In order that |
551 // gtest uses this operator to print readable output on test failures, we must | 538 // gtest uses this operator to print readable output on test failures, we must |
552 // override each specific type. Otherwise, the default template implementation | 539 // override each specific type. Otherwise, the default template implementation |
553 // is preferred over an upcast. | 540 // is preferred over an upcast. |
554 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value); | 541 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value); |
555 | 542 |
556 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, | 543 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
557 const FundamentalValue& value) { | |
558 return out << static_cast<const Value&>(value); | |
559 } | |
560 | |
561 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, | |
562 const StringValue& value) { | 544 const StringValue& value) { |
563 return out << static_cast<const Value&>(value); | 545 return out << static_cast<const Value&>(value); |
564 } | 546 } |
565 | 547 |
566 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, | 548 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
567 const DictionaryValue& value) { | 549 const DictionaryValue& value) { |
568 return out << static_cast<const Value&>(value); | 550 return out << static_cast<const Value&>(value); |
569 } | 551 } |
570 | 552 |
571 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, | 553 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
572 const ListValue& value) { | 554 const ListValue& value) { |
573 return out << static_cast<const Value&>(value); | 555 return out << static_cast<const Value&>(value); |
574 } | 556 } |
575 | 557 |
576 } // namespace base | 558 } // namespace base |
577 | 559 |
578 #endif // BASE_VALUES_H_ | 560 #endif // BASE_VALUES_H_ |
OLD | NEW |