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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 TYPE_BOOLEAN, | 54 TYPE_BOOLEAN, |
55 TYPE_INTEGER, | 55 TYPE_INTEGER, |
56 TYPE_DOUBLE, | 56 TYPE_DOUBLE, |
57 TYPE_STRING, | 57 TYPE_STRING, |
58 TYPE_BINARY, | 58 TYPE_BINARY, |
59 TYPE_DICTIONARY, | 59 TYPE_DICTIONARY, |
60 TYPE_LIST | 60 TYPE_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 Value(); // A null value. | |
65 | |
64 virtual ~Value(); | 66 virtual ~Value(); |
65 | 67 |
66 static std::unique_ptr<Value> CreateNullValue(); | 68 static std::unique_ptr<Value> CreateNullValue(); |
67 | 69 |
68 // Returns the name for a given |type|. | 70 // Returns the name for a given |type|. |
69 static const char* GetTypeName(Type type); | 71 static const char* GetTypeName(Type type); |
70 | 72 |
71 // Returns the type of the value stored by the current Value object. | 73 // Returns the type of the value stored by the current Value object. |
72 // Each type will be implemented by only one subclass of Value, so it's | 74 // Each type will be implemented by only one subclass of Value, so it's |
73 // safe to use the Type to determine whether you can cast from | 75 // safe to use the Type to determine whether you can cast from |
74 // Value* to (Implementing Class)*. Also, a Value object never changes | 76 // Value* to (Implementing Class)*. Also, a Value object never changes |
75 // its type after construction. | 77 // its type after construction. |
76 Type GetType() const { return type_; } | 78 Type GetType() const { return type_; } // DEPRECATED, use type(). |
79 Type type() const { return type_; } | |
77 | 80 |
78 // Returns true if the current object represents a given type. | 81 // Returns true if the current object represents a given type. |
79 bool IsType(Type type) const { return type == type_; } | 82 bool IsType(Type type) const { return type == type_; } |
83 bool is_bool() const { return type() == TYPE_BOOLEAN; } | |
84 bool is_int() const { return type() == TYPE_INTEGER; } | |
85 bool is_double() const { return type() == TYPE_DOUBLE; } | |
86 bool is_string() const { return type() == TYPE_STRING; } | |
87 bool is_blob() const { return type() == TYPE_BINARY; } | |
88 bool is_dict() const { return type() == TYPE_DICTIONARY; } | |
89 bool is_list() const { return type() == TYPE_LIST; } | |
90 | |
91 // These will all fatally assert if the type doesn't match. | |
92 bool GetBool() const; | |
93 int GetInt() const; | |
94 double GetDouble() const; // Implicitly converts from int if necessary. | |
80 | 95 |
81 // These methods allow the convenient retrieval of the contents of the Value. | 96 // These methods allow the convenient retrieval of the contents of the Value. |
82 // If the current object can be converted into the given type, the value is | 97 // If the current object can be converted into the given type, the value is |
83 // returned through the |out_value| parameter and true is returned; | 98 // returned through the |out_value| parameter and true is returned; |
84 // otherwise, false is returned and |out_value| is unchanged. | 99 // otherwise, false is returned and |out_value| is unchanged. |
85 virtual bool GetAsBoolean(bool* out_value) const; | 100 virtual bool GetAsBoolean(bool* out_value) const; |
86 virtual bool GetAsInteger(int* out_value) const; | 101 virtual bool GetAsInteger(int* out_value) const; |
87 virtual bool GetAsDouble(double* out_value) const; | 102 virtual bool GetAsDouble(double* out_value) const; |
88 virtual bool GetAsString(std::string* out_value) const; | 103 virtual bool GetAsString(std::string* out_value) const; |
89 virtual bool GetAsString(string16* out_value) const; | 104 virtual bool GetAsString(string16* out_value) const; |
(...skipping 22 matching lines...) Expand all Loading... | |
112 // Compares if two Value objects have equal contents. Can handle NULLs. | 127 // Compares if two Value objects have equal contents. Can handle NULLs. |
113 // NULLs are considered equal but different from Value::CreateNullValue(). | 128 // NULLs are considered equal but different from Value::CreateNullValue(). |
114 static bool Equals(const Value* a, const Value* b); | 129 static bool Equals(const Value* a, const Value* b); |
115 | 130 |
116 protected: | 131 protected: |
117 // These aren't safe for end-users, but they are useful for subclasses. | 132 // These aren't safe for end-users, but they are useful for subclasses. |
118 explicit Value(Type type); | 133 explicit Value(Type type); |
119 Value(const Value& that); | 134 Value(const Value& that); |
120 Value& operator=(const Value& that); | 135 Value& operator=(const Value& that); |
121 | 136 |
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 | |
122 private: | 142 private: |
123 Type type_; | 143 Type type_; |
144 | |
145 union { | |
146 bool bool_value_; | |
147 int int_value_; | |
148 double double_value_; | |
149 }; | |
124 }; | 150 }; |
125 | 151 |
126 // FundamentalValue represents the simple fundamental types of values. | 152 // FundamentalValue represents the simple fundamental types of values. |
153 // TODO(brettw) remove when callers are updated to use raw Value. | |
127 class BASE_EXPORT FundamentalValue : public Value { | 154 class BASE_EXPORT FundamentalValue : public Value { |
128 public: | 155 public: |
129 explicit FundamentalValue(bool in_value); | 156 explicit FundamentalValue(bool in_value); |
130 explicit FundamentalValue(int in_value); | 157 explicit FundamentalValue(int in_value); |
131 explicit FundamentalValue(double in_value); | 158 explicit FundamentalValue(double in_value); |
132 ~FundamentalValue() override; | 159 ~FundamentalValue() override; |
133 | |
134 // Overridden from Value: | |
135 bool GetAsBoolean(bool* 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 | |
138 // doubles. | |
139 bool GetAsDouble(double* out_value) const override; | |
140 FundamentalValue* DeepCopy() const override; | |
141 bool Equals(const Value* other) const override; | |
142 | |
143 private: | |
144 union { | |
vabr (Chromium)
2016/12/07 14:31:11
As a result of moving this union up to base::Value
| |
145 bool boolean_value_; | |
146 int integer_value_; | |
147 double double_value_; | |
148 }; | |
149 }; | 160 }; |
150 | 161 |
151 class BASE_EXPORT StringValue : public Value { | 162 class BASE_EXPORT StringValue : public Value { |
152 public: | 163 public: |
153 // Initializes a StringValue with a UTF-8 narrow character string. | 164 // Initializes a StringValue with a UTF-8 narrow character string. |
154 explicit StringValue(StringPiece in_value); | 165 explicit StringValue(StringPiece in_value); |
155 | 166 |
156 // Initializes a StringValue with a string16. | 167 // Initializes a StringValue with a string16. |
157 explicit StringValue(const string16& in_value); | 168 explicit StringValue(const string16& in_value); |
158 | 169 |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
558 } | 569 } |
559 | 570 |
560 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, | 571 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
561 const ListValue& value) { | 572 const ListValue& value) { |
562 return out << static_cast<const Value&>(value); | 573 return out << static_cast<const Value&>(value); |
563 } | 574 } |
564 | 575 |
565 } // namespace base | 576 } // namespace base |
566 | 577 |
567 #endif // BASE_VALUES_H_ | 578 #endif // BASE_VALUES_H_ |
OLD | NEW |