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 12 matching lines...) Expand all Loading... | |
23 #include <iosfwd> | 23 #include <iosfwd> |
24 #include <map> | 24 #include <map> |
25 #include <memory> | 25 #include <memory> |
26 #include <string> | 26 #include <string> |
27 #include <utility> | 27 #include <utility> |
28 #include <vector> | 28 #include <vector> |
29 | 29 |
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/memory/manual_constructor.h" | |
33 #include "base/strings/string16.h" | 34 #include "base/strings/string16.h" |
34 #include "base/strings/string_piece.h" | 35 #include "base/strings/string_piece.h" |
35 | 36 |
36 namespace base { | 37 namespace base { |
37 | 38 |
38 class BinaryValue; | 39 class BinaryValue; |
39 class DictionaryValue; | 40 class DictionaryValue; |
40 class FundamentalValue; | 41 class FundamentalValue; |
41 class ListValue; | 42 class ListValue; |
42 class StringValue; | 43 class StringValue; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 bool is_double() const { return type() == Type::DOUBLE; } | 86 bool is_double() const { return type() == Type::DOUBLE; } |
86 bool is_string() const { return type() == Type::STRING; } | 87 bool is_string() const { return type() == Type::STRING; } |
87 bool is_blob() const { return type() == Type::BINARY; } | 88 bool is_blob() const { return type() == Type::BINARY; } |
88 bool is_dict() const { return type() == Type::DICTIONARY; } | 89 bool is_dict() const { return type() == Type::DICTIONARY; } |
89 bool is_list() const { return type() == Type::LIST; } | 90 bool is_list() const { return type() == Type::LIST; } |
90 | 91 |
91 // These will all fatally assert if the type doesn't match. | 92 // These will all fatally assert if the type doesn't match. |
92 bool GetBool() const; | 93 bool GetBool() const; |
93 int GetInt() const; | 94 int GetInt() const; |
94 double GetDouble() const; // Implicitly converts from int if necessary. | 95 double GetDouble() const; // Implicitly converts from int if necessary. |
96 // Returns |string_value_| as a non-const or const reference. | |
97 std::string& GetString(); | |
jdoerrie
2016/12/16 09:54:37
For now I added the non-const ref version for simp
vabr (Chromium)
2016/12/16 17:00:56
Just for context: part of the discussion was also
| |
98 const std::string& GetString() const; | |
95 | 99 |
96 // These methods allow the convenient retrieval of the contents of the Value. | 100 // These methods allow the convenient retrieval of the contents of the Value. |
97 // If the current object can be converted into the given type, the value is | 101 // If the current object can be converted into the given type, the value is |
98 // returned through the |out_value| parameter and true is returned; | 102 // returned through the |out_value| parameter and true is returned; |
99 // otherwise, false is returned and |out_value| is unchanged. | 103 // otherwise, false is returned and |out_value| is unchanged. |
100 virtual bool GetAsBoolean(bool* out_value) const; | 104 virtual bool GetAsBoolean(bool* out_value) const; |
101 virtual bool GetAsInteger(int* out_value) const; | 105 virtual bool GetAsInteger(int* out_value) const; |
102 virtual bool GetAsDouble(double* out_value) const; | 106 virtual bool GetAsDouble(double* out_value) const; |
103 virtual bool GetAsString(std::string* out_value) const; | 107 virtual bool GetAsString(std::string* out_value) const; |
104 virtual bool GetAsString(string16* out_value) const; | 108 virtual bool GetAsString(string16* out_value) const; |
(...skipping 29 matching lines...) Expand all Loading... | |
134 Value(const Value& that); | 138 Value(const Value& that); |
135 Value(Value&& that); | 139 Value(Value&& that); |
136 Value& operator=(const Value& that); | 140 Value& operator=(const Value& that); |
137 Value& operator=(Value&& that); | 141 Value& operator=(Value&& that); |
138 | 142 |
139 // TODO(brettw) move to public when FundamentalValue is gone. | 143 // TODO(brettw) move to public when FundamentalValue is gone. |
140 explicit Value(bool in_bool); | 144 explicit Value(bool in_bool); |
141 explicit Value(int in_int); | 145 explicit Value(int in_int); |
142 explicit Value(double in_double); | 146 explicit Value(double in_double); |
143 | 147 |
148 // TODO(crbug.com/646113) move to public when StringValue is gone. | |
149 explicit Value(StringPiece in_string); | |
jdoerrie
2016/12/16 09:54:37
Given that we need an explicit Value::Value(const
vabr (Chromium)
2016/12/16 17:00:56
Definitely in favour of replacing by string-based
| |
150 explicit Value(const string16& in_string); | |
151 | |
152 // Required despite StringPiece because otherwise the compiler will | |
153 // choose the Value(bool) constructor. | |
154 explicit Value(const char* in_string); | |
vabr (Chromium)
2016/12/16 17:00:55
As a TODO to be done in a separate CL, could we tr
jdoerrie
2016/12/19 11:54:09
If it is your goal to stop being able to do |Value
vabr (Chromium)
2016/12/19 13:00:56
Good to know about the error. However, because it
| |
155 | |
156 // TODO(crbug.com/646113): Make this private once JSONStringValue is removed. | |
157 Type type_; | |
158 | |
144 private: | 159 private: |
145 void InternalCopyFrom(const Value& that); | 160 void InternalCopyFrom(const Value& that); |
146 | 161 void InternalMoveFrom(Value&& that); |
147 Type type_; | 162 void InternalCleanup(); |
148 | 163 |
149 union { | 164 union { |
150 bool bool_value_; | 165 bool bool_value_; |
151 int int_value_; | 166 int int_value_; |
152 double double_value_; | 167 double double_value_; |
168 ManualConstructor<std::string> string_value_; | |
153 }; | 169 }; |
154 }; | 170 }; |
155 | 171 |
156 // FundamentalValue represents the simple fundamental types of values. | 172 // FundamentalValue represents the simple fundamental types of values. |
157 // TODO(brettw) remove when callers are updated to use raw Value. | 173 // TODO(brettw) remove when callers are updated to use raw Value. |
158 class BASE_EXPORT FundamentalValue : public Value { | 174 class BASE_EXPORT FundamentalValue : public Value { |
159 public: | 175 public: |
160 explicit FundamentalValue(bool in_value); | 176 explicit FundamentalValue(bool in_value); |
161 explicit FundamentalValue(int in_value); | 177 explicit FundamentalValue(int in_value); |
162 explicit FundamentalValue(double in_value); | 178 explicit FundamentalValue(double in_value); |
163 ~FundamentalValue() override; | 179 ~FundamentalValue() override; |
164 }; | 180 }; |
165 | 181 |
182 // TODO(crbug.com/64113) remove when callers are updated to use raw Value. | |
166 class BASE_EXPORT StringValue : public Value { | 183 class BASE_EXPORT StringValue : public Value { |
vabr (Chromium)
2016/12/16 17:00:56
Could we actually make this a typedef instead? (Fo
jdoerrie
2016/12/19 11:54:09
This is true, but it probably also would require m
vabr (Chromium)
2016/12/19 13:00:56
I'm not sure the comment about safety applies to a
| |
167 public: | 184 public: |
168 // Initializes a StringValue with a UTF-8 narrow character string. | 185 // Initializes a StringValue with a UTF-8 narrow character string. |
169 explicit StringValue(StringPiece in_value); | 186 explicit StringValue(StringPiece in_value); |
170 | 187 |
171 // Initializes a StringValue with a string16. | 188 // Initializes a StringValue with a string16. |
172 explicit StringValue(const string16& in_value); | 189 explicit StringValue(const string16& in_value); |
173 | |
174 ~StringValue() override; | |
175 | |
176 // Returns |value_| as a pointer or reference. | |
177 std::string* GetString(); | |
178 const std::string& GetString() const; | |
179 | |
180 // Overridden from Value: | |
181 bool GetAsString(std::string* out_value) const override; | |
182 bool GetAsString(string16* out_value) const override; | |
183 bool GetAsString(const StringValue** out_value) const override; | |
184 StringValue* DeepCopy() const override; | |
185 bool Equals(const Value* other) const override; | |
186 | |
187 private: | |
188 std::string value_; | |
189 }; | 190 }; |
190 | 191 |
191 class BASE_EXPORT BinaryValue: public Value { | 192 class BASE_EXPORT BinaryValue: public Value { |
192 public: | 193 public: |
193 // Creates a BinaryValue with a null buffer and size of 0. | 194 // Creates a BinaryValue with a null buffer and size of 0. |
194 BinaryValue(); | 195 BinaryValue(); |
195 | 196 |
196 // Creates a BinaryValue, taking ownership of the bytes pointed to by | 197 // Creates a BinaryValue, taking ownership of the bytes pointed to by |
197 // |buffer|. | 198 // |buffer|. |
198 BinaryValue(std::unique_ptr<char[]> buffer, size_t size); | 199 BinaryValue(std::unique_ptr<char[]> buffer, size_t size); |
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
577 return out << static_cast<const Value&>(value); | 578 return out << static_cast<const Value&>(value); |
578 } | 579 } |
579 | 580 |
580 // Stream operator so that enum class Types can be used in log statements. | 581 // Stream operator so that enum class Types can be used in log statements. |
581 BASE_EXPORT std::ostream& operator<<(std::ostream& out, | 582 BASE_EXPORT std::ostream& operator<<(std::ostream& out, |
582 const Value::Type& type); | 583 const Value::Type& type); |
583 | 584 |
584 } // namespace base | 585 } // namespace base |
585 | 586 |
586 #endif // BASE_VALUES_H_ | 587 #endif // BASE_VALUES_H_ |
OLD | NEW |