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

Side by Side Diff: base/values.h

Issue 2516363005: Inline StringValue into base::Value (Closed)
Patch Set: Rebase and Nits. Created 3 years, 10 months 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
« no previous file with comments | « base/value_conversions.h ('k') | base/values.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 ListValue; 41 class ListValue;
41 class StringValue;
42 class Value; 42 class Value;
43 using FundamentalValue = Value; 43 using FundamentalValue = Value;
44 using StringValue = Value;
44 45
45 // The Value class is the base class for Values. A Value can be instantiated 46 // 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 47 // via the Create*Value() factory methods, or by directly creating instances of
47 // the subclasses. 48 // the subclasses.
48 // 49 //
49 // See the file-level comment above for more information. 50 // See the file-level comment above for more information.
50 class BASE_EXPORT Value { 51 class BASE_EXPORT Value {
51 public: 52 public:
52 enum class Type { 53 enum class Type {
53 NONE = 0, 54 NONE = 0,
(...skipping 10 matching lines...) Expand all
64 static std::unique_ptr<Value> CreateNullValue(); 65 static std::unique_ptr<Value> CreateNullValue();
65 66
66 Value(const Value& that); 67 Value(const Value& that);
67 Value(Value&& that); 68 Value(Value&& that);
68 Value(); // A null value. 69 Value(); // A null value.
69 explicit Value(Type type); 70 explicit Value(Type type);
70 explicit Value(bool in_bool); 71 explicit Value(bool in_bool);
71 explicit Value(int in_int); 72 explicit Value(int in_int);
72 explicit Value(double in_double); 73 explicit Value(double in_double);
73 74
75 // Value(const char*) and Value(const char16*) are required despite
76 // Value(const std::string&) and Value(const string16&) because otherwise the
77 // compiler will choose the Value(bool) constructor for these arguments.
78 // Value(std::string&&) allow for efficient move construction.
79 // Value(StringPiece) exists due to many callsites passing StringPieces as
80 // arguments.
81 explicit Value(const char* in_string);
82 explicit Value(const std::string& in_string);
83 explicit Value(std::string&& in_string);
84 explicit Value(const char16* in_string);
85 explicit Value(const string16& in_string);
86 explicit Value(StringPiece in_string);
87
74 Value& operator=(const Value& that); 88 Value& operator=(const Value& that);
75 Value& operator=(Value&& that); 89 Value& operator=(Value&& that);
76 90
77 virtual ~Value(); 91 virtual ~Value();
78 92
79 // Returns the name for a given |type|. 93 // Returns the name for a given |type|.
80 static const char* GetTypeName(Type type); 94 static const char* GetTypeName(Type type);
81 95
82 // Returns the type of the value stored by the current Value object. 96 // Returns the type of the value stored by the current Value object.
83 // Each type will be implemented by only one subclass of Value, so it's 97 // Each type will be implemented by only one subclass of Value, so it's
(...skipping 10 matching lines...) Expand all
94 bool is_double() const { return type() == Type::DOUBLE; } 108 bool is_double() const { return type() == Type::DOUBLE; }
95 bool is_string() const { return type() == Type::STRING; } 109 bool is_string() const { return type() == Type::STRING; }
96 bool is_blob() const { return type() == Type::BINARY; } 110 bool is_blob() const { return type() == Type::BINARY; }
97 bool is_dict() const { return type() == Type::DICTIONARY; } 111 bool is_dict() const { return type() == Type::DICTIONARY; }
98 bool is_list() const { return type() == Type::LIST; } 112 bool is_list() const { return type() == Type::LIST; }
99 113
100 // These will all fatally assert if the type doesn't match. 114 // These will all fatally assert if the type doesn't match.
101 bool GetBool() const; 115 bool GetBool() const;
102 int GetInt() const; 116 int GetInt() const;
103 double GetDouble() const; // Implicitly converts from int if necessary. 117 double GetDouble() const; // Implicitly converts from int if necessary.
118 const std::string& GetString() const;
104 119
105 // These methods allow the convenient retrieval of the contents of the Value. 120 // These methods allow the convenient retrieval of the contents of the Value.
106 // If the current object can be converted into the given type, the value is 121 // If the current object can be converted into the given type, the value is
107 // returned through the |out_value| parameter and true is returned; 122 // returned through the |out_value| parameter and true is returned;
108 // otherwise, false is returned and |out_value| is unchanged. 123 // otherwise, false is returned and |out_value| is unchanged.
109 virtual bool GetAsBoolean(bool* out_value) const; 124 virtual bool GetAsBoolean(bool* out_value) const;
110 virtual bool GetAsInteger(int* out_value) const; 125 virtual bool GetAsInteger(int* out_value) const;
111 virtual bool GetAsDouble(double* out_value) const; 126 virtual bool GetAsDouble(double* out_value) const;
112 virtual bool GetAsString(std::string* out_value) const; 127 virtual bool GetAsString(std::string* out_value) const;
113 virtual bool GetAsString(string16* out_value) const; 128 virtual bool GetAsString(string16* out_value) const;
(...skipping 16 matching lines...) Expand all
130 // Preferred version of DeepCopy. TODO(estade): remove the above. 145 // Preferred version of DeepCopy. TODO(estade): remove the above.
131 std::unique_ptr<Value> CreateDeepCopy() const; 146 std::unique_ptr<Value> CreateDeepCopy() const;
132 147
133 // Compares if two Value objects have equal contents. 148 // Compares if two Value objects have equal contents.
134 virtual bool Equals(const Value* other) const; 149 virtual bool Equals(const Value* other) const;
135 150
136 // Compares if two Value objects have equal contents. Can handle NULLs. 151 // Compares if two Value objects have equal contents. Can handle NULLs.
137 // NULLs are considered equal but different from Value::CreateNullValue(). 152 // NULLs are considered equal but different from Value::CreateNullValue().
138 static bool Equals(const Value* a, const Value* b); 153 static bool Equals(const Value* a, const Value* b);
139 154
155 protected:
156 // TODO(crbug.com/646113): Make this private once JSONStringValue is removed.
157 Type type_;
158
140 private: 159 private:
141 void InternalCopyFrom(const Value& that); 160 void InternalCopyFundamentalValue(const Value& that);
142 161 void InternalCopyConstructFrom(const Value& that);
143 Type type_; 162 void InternalMoveConstructFrom(Value&& that);
163 void InternalCopyAssignFrom(const Value& that);
164 void InternalMoveAssignFrom(Value&& that);
165 void InternalCleanup();
144 166
145 union { 167 union {
146 bool bool_value_; 168 bool bool_value_;
147 int int_value_; 169 int int_value_;
148 double double_value_; 170 double double_value_;
171 ManualConstructor<std::string> string_value_;
149 }; 172 };
150 }; 173 };
151 174
152 class BASE_EXPORT StringValue : public Value {
153 public:
154 // Initializes a StringValue with a UTF-8 narrow character string.
155 explicit StringValue(StringPiece in_value);
156
157 // Initializes a StringValue with a string16.
158 explicit StringValue(const string16& in_value);
159
160 ~StringValue() override;
161
162 // Returns |value_| as a pointer or reference.
163 std::string* GetString();
164 const std::string& GetString() const;
165
166 // Overridden from Value:
167 bool GetAsString(std::string* out_value) const override;
168 bool GetAsString(string16* out_value) const override;
169 bool GetAsString(const StringValue** out_value) const override;
170 bool GetAsString(StringPiece* out_value) const override;
171 StringValue* DeepCopy() const override;
172 bool Equals(const Value* other) const override;
173
174 private:
175 std::string value_;
176 };
177
178 class BASE_EXPORT BinaryValue: public Value { 175 class BASE_EXPORT BinaryValue: public Value {
179 public: 176 public:
180 // Creates a BinaryValue with a null buffer and size of 0. 177 // Creates a BinaryValue with a null buffer and size of 0.
181 BinaryValue(); 178 BinaryValue();
182 179
183 // Creates a BinaryValue, taking ownership of the bytes pointed to by 180 // Creates a BinaryValue, taking ownership of the bytes pointed to by
184 // |buffer|. 181 // |buffer|.
185 BinaryValue(std::unique_ptr<char[]> buffer, size_t size); 182 BinaryValue(std::unique_ptr<char[]> buffer, size_t size);
186 183
187 ~BinaryValue() override; 184 ~BinaryValue() override;
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 std::string* error_str) = 0; 535 std::string* error_str) = 0;
539 }; 536 };
540 537
541 // Stream operator so Values can be used in assertion statements. In order that 538 // Stream operator so Values can be used in assertion statements. In order that
542 // gtest uses this operator to print readable output on test failures, we must 539 // gtest uses this operator to print readable output on test failures, we must
543 // override each specific type. Otherwise, the default template implementation 540 // override each specific type. Otherwise, the default template implementation
544 // is preferred over an upcast. 541 // is preferred over an upcast.
545 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value); 542 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
546 543
547 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, 544 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
548 const StringValue& value) {
549 return out << static_cast<const Value&>(value);
550 }
551
552 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
553 const DictionaryValue& value) { 545 const DictionaryValue& value) {
554 return out << static_cast<const Value&>(value); 546 return out << static_cast<const Value&>(value);
555 } 547 }
556 548
557 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, 549 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
558 const ListValue& value) { 550 const ListValue& value) {
559 return out << static_cast<const Value&>(value); 551 return out << static_cast<const Value&>(value);
560 } 552 }
561 553
562 // Stream operator so that enum class Types can be used in log statements. 554 // Stream operator so that enum class Types can be used in log statements.
563 BASE_EXPORT std::ostream& operator<<(std::ostream& out, 555 BASE_EXPORT std::ostream& operator<<(std::ostream& out,
564 const Value::Type& type); 556 const Value::Type& type);
565 557
566 } // namespace base 558 } // namespace base
567 559
568 #endif // BASE_VALUES_H_ 560 #endif // BASE_VALUES_H_
OLDNEW
« no previous file with comments | « base/value_conversions.h ('k') | base/values.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698