Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(777)

Side by Side Diff: base/values.h

Issue 2516363005: Inline StringValue into base::Value (Closed)
Patch Set: Reimplement old base::Value::GetAsString behavior in json_parser. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 pointer or reference.
97 std::string* GetString();
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 26 matching lines...) Expand all
131 protected: 135 protected:
132 // These aren't safe for end-users, but they are useful for subclasses. 136 // These aren't safe for end-users, but they are useful for subclasses.
133 explicit Value(Type type); 137 explicit Value(Type type);
134 Value(const Value& that); 138 Value(const Value& that);
135 Value& operator=(const Value& that); 139 Value& operator=(const Value& that);
136 140
137 // TODO(brettw) move to public when FundamentalValue is gone. 141 // TODO(brettw) move to public when FundamentalValue is gone.
138 Value(bool in_bool); 142 Value(bool in_bool);
139 Value(int in_int); 143 Value(int in_int);
140 Value(double in_double); 144 Value(double in_double);
145 // TODO(jdoerrie) move to public when StringValue is gone.
146 Value(const char* in_string); // Value(bool in_bool) shadows otherwise
147 Value(StringPiece in_string);
148 Value(const string16& in_string);
141 149
142 private: 150 private:
143 Type type_; 151 Type type_;
144 152
145 union { 153 union {
146 bool bool_value_; 154 bool bool_value_;
147 int int_value_; 155 int int_value_;
148 double double_value_; 156 double double_value_;
157 ManualConstructor<std::string> string_value_;
149 }; 158 };
150 }; 159 };
151 160
152 // FundamentalValue represents the simple fundamental types of values. 161 // FundamentalValue represents the simple fundamental types of values.
153 // TODO(brettw) remove when callers are updated to use raw Value. 162 // TODO(brettw) remove when callers are updated to use raw Value.
154 class BASE_EXPORT FundamentalValue : public Value { 163 class BASE_EXPORT FundamentalValue : public Value {
155 public: 164 public:
156 explicit FundamentalValue(bool in_value); 165 explicit FundamentalValue(bool in_value);
157 explicit FundamentalValue(int in_value); 166 explicit FundamentalValue(int in_value);
158 explicit FundamentalValue(double in_value); 167 explicit FundamentalValue(double in_value);
159 ~FundamentalValue() override; 168 ~FundamentalValue() override;
160 }; 169 };
161 170
171 // TODO(jdoerrie) remove when callers are updated to use raw Value.
162 class BASE_EXPORT StringValue : public Value { 172 class BASE_EXPORT StringValue : public Value {
163 public: 173 public:
164 // Initializes a StringValue with a UTF-8 narrow character string. 174 // Initializes a StringValue with a UTF-8 narrow character string.
165 explicit StringValue(StringPiece in_value); 175 explicit StringValue(StringPiece in_value);
166 176
167 // Initializes a StringValue with a string16. 177 // Initializes a StringValue with a string16.
168 explicit StringValue(const string16& in_value); 178 explicit StringValue(const string16& in_value);
169 179
170 ~StringValue() override; 180 ~StringValue() override;
171
172 // Returns |value_| as a pointer or reference.
173 std::string* GetString();
174 const std::string& GetString() const;
175
176 // Overridden from Value:
177 bool GetAsString(std::string* out_value) const override;
178 bool GetAsString(string16* out_value) const override;
179 bool GetAsString(const StringValue** out_value) const override;
180 StringValue* DeepCopy() const override;
181 bool Equals(const Value* other) const override;
182
183 private:
184 std::string value_;
185 }; 181 };
186 182
187 class BASE_EXPORT BinaryValue: public Value { 183 class BASE_EXPORT BinaryValue: public Value {
188 public: 184 public:
189 // Creates a BinaryValue with a null buffer and size of 0. 185 // Creates a BinaryValue with a null buffer and size of 0.
190 BinaryValue(); 186 BinaryValue();
191 187
192 // Creates a BinaryValue, taking ownership of the bytes pointed to by 188 // Creates a BinaryValue, taking ownership of the bytes pointed to by
193 // |buffer|. 189 // |buffer|.
194 BinaryValue(std::unique_ptr<char[]> buffer, size_t size); 190 BinaryValue(std::unique_ptr<char[]> buffer, size_t size);
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 } 565 }
570 566
571 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, 567 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
572 const ListValue& value) { 568 const ListValue& value) {
573 return out << static_cast<const Value&>(value); 569 return out << static_cast<const Value&>(value);
574 } 570 }
575 571
576 } // namespace base 572 } // namespace base
577 573
578 #endif // BASE_VALUES_H_ 574 #endif // BASE_VALUES_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698