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

Side by Side Diff: base/values.h

Issue 2689673002: Inline base::BinaryValue into base::Value (Closed)
Patch Set: Rebase 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 | « no previous file | 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 18 matching lines...) Expand all
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/memory/manual_constructor.h"
34 #include "base/strings/string16.h" 34 #include "base/strings/string16.h"
35 #include "base/strings/string_piece.h" 35 #include "base/strings/string_piece.h"
36 36
37 namespace base { 37 namespace base {
38 38
39 class BinaryValue;
40 class DictionaryValue; 39 class DictionaryValue;
41 class ListValue; 40 class ListValue;
42 class Value; 41 class Value;
43 using FundamentalValue = Value; 42 using FundamentalValue = Value;
44 using StringValue = Value; 43 using StringValue = Value;
44 using BinaryValue = Value;
45 45
46 // 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
47 // 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
48 // the subclasses. 48 // the subclasses.
49 // 49 //
50 // See the file-level comment above for more information. 50 // See the file-level comment above for more information.
51 class BASE_EXPORT Value { 51 class BASE_EXPORT Value {
52 public: 52 public:
53 enum class Type { 53 enum class Type {
54 NONE = 0, 54 NONE = 0,
55 BOOLEAN, 55 BOOLEAN,
56 INTEGER, 56 INTEGER,
57 DOUBLE, 57 DOUBLE,
58 STRING, 58 STRING,
59 BINARY, 59 BINARY,
60 DICTIONARY, 60 DICTIONARY,
61 LIST 61 LIST
62 // Note: Do not add more types. See the file-level comment above for why. 62 // Note: Do not add more types. See the file-level comment above for why.
63 }; 63 };
64 64
65 static std::unique_ptr<Value> CreateNullValue(); 65 static std::unique_ptr<Value> CreateNullValue();
66 66
67 // For situations where you want to keep ownership of your buffer, this
68 // factory method creates a new BinaryValue by copying the contents of the
69 // buffer that's passed in.
70 // DEPRECATED, use MakeUnique<Value>(const std::vector<char>&) instead.
71 // TODO(crbug.com/646113): Delete this and migrate callsites.
72 static std::unique_ptr<BinaryValue> CreateWithCopiedBuffer(const char* buffer,
73 size_t size);
74
67 Value(const Value& that); 75 Value(const Value& that);
68 Value(Value&& that); 76 Value(Value&& that);
69 Value(); // A null value. 77 Value(); // A null value.
70 explicit Value(Type type); 78 explicit Value(Type type);
71 explicit Value(bool in_bool); 79 explicit Value(bool in_bool);
72 explicit Value(int in_int); 80 explicit Value(int in_int);
73 explicit Value(double in_double); 81 explicit Value(double in_double);
74 82
75 // Value(const char*) and Value(const char16*) are required despite 83 // Value(const char*) and Value(const char16*) are required despite
76 // Value(const std::string&) and Value(const string16&) because otherwise the 84 // Value(const std::string&) and Value(const string16&) because otherwise the
77 // compiler will choose the Value(bool) constructor for these arguments. 85 // compiler will choose the Value(bool) constructor for these arguments.
78 // Value(std::string&&) allow for efficient move construction. 86 // Value(std::string&&) allow for efficient move construction.
79 // Value(StringPiece) exists due to many callsites passing StringPieces as 87 // Value(StringPiece) exists due to many callsites passing StringPieces as
80 // arguments. 88 // arguments.
81 explicit Value(const char* in_string); 89 explicit Value(const char* in_string);
82 explicit Value(const std::string& in_string); 90 explicit Value(const std::string& in_string);
83 explicit Value(std::string&& in_string); 91 explicit Value(std::string&& in_string);
84 explicit Value(const char16* in_string); 92 explicit Value(const char16* in_string);
85 explicit Value(const string16& in_string); 93 explicit Value(const string16& in_string);
86 explicit Value(StringPiece in_string); 94 explicit Value(StringPiece in_string);
87 95
96 explicit Value(const std::vector<char>& in_blob);
97 explicit Value(std::vector<char>&& in_blob);
98
88 Value& operator=(const Value& that); 99 Value& operator=(const Value& that);
89 Value& operator=(Value&& that); 100 Value& operator=(Value&& that);
90 101
91 virtual ~Value(); 102 virtual ~Value();
92 103
93 // Returns the name for a given |type|. 104 // Returns the name for a given |type|.
94 static const char* GetTypeName(Type type); 105 static const char* GetTypeName(Type type);
95 106
96 // Returns the type of the value stored by the current Value object. 107 // Returns the type of the value stored by the current Value object.
97 // Each type will be implemented by only one subclass of Value, so it's 108 // Each type will be implemented by only one subclass of Value, so it's
(...skipping 11 matching lines...) Expand all
109 bool is_string() const { return type() == Type::STRING; } 120 bool is_string() const { return type() == Type::STRING; }
110 bool is_blob() const { return type() == Type::BINARY; } 121 bool is_blob() const { return type() == Type::BINARY; }
111 bool is_dict() const { return type() == Type::DICTIONARY; } 122 bool is_dict() const { return type() == Type::DICTIONARY; }
112 bool is_list() const { return type() == Type::LIST; } 123 bool is_list() const { return type() == Type::LIST; }
113 124
114 // These will all fatally assert if the type doesn't match. 125 // These will all fatally assert if the type doesn't match.
115 bool GetBool() const; 126 bool GetBool() const;
116 int GetInt() const; 127 int GetInt() const;
117 double GetDouble() const; // Implicitly converts from int if necessary. 128 double GetDouble() const; // Implicitly converts from int if necessary.
118 const std::string& GetString() const; 129 const std::string& GetString() const;
130 const std::vector<char>& GetBlob() const;
131
132 size_t GetSize() const; // DEPRECATED, use GetBlob().size() instead.
133 const char* GetBuffer() const; // DEPRECATED, use GetBlob().data() instead.
119 134
120 // These methods allow the convenient retrieval of the contents of the Value. 135 // These methods allow the convenient retrieval of the contents of the Value.
121 // If the current object can be converted into the given type, the value is 136 // If the current object can be converted into the given type, the value is
122 // returned through the |out_value| parameter and true is returned; 137 // returned through the |out_value| parameter and true is returned;
123 // otherwise, false is returned and |out_value| is unchanged. 138 // otherwise, false is returned and |out_value| is unchanged.
124 virtual bool GetAsBoolean(bool* out_value) const; 139 virtual bool GetAsBoolean(bool* out_value) const;
125 virtual bool GetAsInteger(int* out_value) const; 140 virtual bool GetAsInteger(int* out_value) const;
126 virtual bool GetAsDouble(double* out_value) const; 141 virtual bool GetAsDouble(double* out_value) const;
127 virtual bool GetAsString(std::string* out_value) const; 142 virtual bool GetAsString(std::string* out_value) const;
128 virtual bool GetAsString(string16* out_value) const; 143 virtual bool GetAsString(string16* out_value) const;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 void InternalMoveAssignFrom(Value&& that); 175 void InternalMoveAssignFrom(Value&& that);
161 void InternalCleanup(); 176 void InternalCleanup();
162 177
163 Type type_; 178 Type type_;
164 179
165 union { 180 union {
166 bool bool_value_; 181 bool bool_value_;
167 int int_value_; 182 int int_value_;
168 double double_value_; 183 double double_value_;
169 ManualConstructor<std::string> string_value_; 184 ManualConstructor<std::string> string_value_;
185 ManualConstructor<std::vector<char>> binary_value_;
170 }; 186 };
171 }; 187 };
172 188
173 class BASE_EXPORT BinaryValue: public Value {
174 public:
175 // Creates a BinaryValue with a null buffer and size of 0.
176 BinaryValue();
177
178 // Creates a BinaryValue, taking ownership of the bytes pointed to by
179 // |buffer|.
180 BinaryValue(std::unique_ptr<char[]> buffer, size_t size);
181
182 ~BinaryValue() override;
183
184 // For situations where you want to keep ownership of your buffer, this
185 // factory method creates a new BinaryValue by copying the contents of the
186 // buffer that's passed in.
187 static std::unique_ptr<BinaryValue> CreateWithCopiedBuffer(const char* buffer,
188 size_t size);
189
190 size_t GetSize() const { return size_; }
191
192 // May return NULL.
193 char* GetBuffer() { return buffer_.get(); }
194 const char* GetBuffer() const { return buffer_.get(); }
195
196 // Overridden from Value:
197 bool GetAsBinary(const BinaryValue** out_value) const override;
198 BinaryValue* DeepCopy() const override;
199 bool Equals(const Value* other) const override;
200
201 private:
202 std::unique_ptr<char[]> buffer_;
203 size_t size_;
204
205 DISALLOW_COPY_AND_ASSIGN(BinaryValue);
206 };
207
208 // DictionaryValue provides a key-value dictionary with (optional) "path" 189 // DictionaryValue provides a key-value dictionary with (optional) "path"
209 // parsing for recursive access; see the comment at the top of the file. Keys 190 // parsing for recursive access; see the comment at the top of the file. Keys
210 // are |std::string|s and should be UTF-8 encoded. 191 // are |std::string|s and should be UTF-8 encoded.
211 class BASE_EXPORT DictionaryValue : public Value { 192 class BASE_EXPORT DictionaryValue : public Value {
212 public: 193 public:
213 using Storage = std::map<std::string, std::unique_ptr<Value>>; 194 using Storage = std::map<std::string, std::unique_ptr<Value>>;
214 // Returns |value| if it is a dictionary, nullptr otherwise. 195 // Returns |value| if it is a dictionary, nullptr otherwise.
215 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value); 196 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
216 197
217 DictionaryValue(); 198 DictionaryValue();
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 return out << static_cast<const Value&>(value); 530 return out << static_cast<const Value&>(value);
550 } 531 }
551 532
552 // Stream operator so that enum class Types can be used in log statements. 533 // Stream operator so that enum class Types can be used in log statements.
553 BASE_EXPORT std::ostream& operator<<(std::ostream& out, 534 BASE_EXPORT std::ostream& operator<<(std::ostream& out,
554 const Value::Type& type); 535 const Value::Type& type);
555 536
556 } // namespace base 537 } // namespace base
557 538
558 #endif // BASE_VALUES_H_ 539 #endif // BASE_VALUES_H_
OLDNEW
« no previous file with comments | « no previous file | base/values.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698