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

Side by Side Diff: base/values.h

Issue 6324004: Made return types of various Value::DeepCopy() implementations more specific (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add test for covariant return types Created 9 years, 11 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 | Annotate | Revision Log
« 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 setting and other persistable data. It includes the ability to 6 // storing setting and other persistable data. It includes the ability to
7 // specify (recursive) lists and dictionaries, so it's fairly expressive. 7 // specify (recursive) lists and dictionaries, so it's fairly expressive.
8 // However, the API is optimized for the common case, namely storing a 8 // However, the API is optimized for the common case, namely storing a
9 // hierarchical tree of simple values. Given a DictionaryValue root, you can 9 // hierarchical tree of simple values. Given a DictionaryValue root, you can
10 // easily do things like: 10 // easily do things like:
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 TYPE_DICTIONARY, 56 TYPE_DICTIONARY,
57 TYPE_LIST 57 TYPE_LIST
58 }; 58 };
59 59
60 virtual ~Value(); 60 virtual ~Value();
61 61
62 // Convenience methods for creating Value objects for various 62 // Convenience methods for creating Value objects for various
63 // kinds of values without thinking about which class implements them. 63 // kinds of values without thinking about which class implements them.
64 // These can always be expected to return a valid Value*. 64 // These can always be expected to return a valid Value*.
65 static Value* CreateNullValue(); 65 static Value* CreateNullValue();
66 static Value* CreateBooleanValue(bool in_value); 66 static FundamentalValue* CreateBooleanValue(bool in_value);
67 static Value* CreateIntegerValue(int in_value); 67 static FundamentalValue* CreateIntegerValue(int in_value);
68 static Value* CreateRealValue(double in_value); 68 static FundamentalValue* CreateRealValue(double in_value);
69 static Value* CreateStringValue(const std::string& in_value); 69 static StringValue* CreateStringValue(const std::string& in_value);
70 static Value* CreateStringValue(const string16& in_value); 70 static StringValue* CreateStringValue(const string16& in_value);
71 71
72 // This one can return NULL if the input isn't valid. If the return value 72 // This one can return NULL if the input isn't valid. If the return value
73 // is non-null, the new object has taken ownership of the buffer pointer. 73 // is non-null, the new object has taken ownership of the buffer pointer.
74 static BinaryValue* CreateBinaryValue(char* buffer, size_t size); 74 static BinaryValue* CreateBinaryValue(char* buffer, size_t size);
75 75
76 // Returns the type of the value stored by the current Value object. 76 // Returns the type of the value stored by the current Value object.
77 // Each type will be implemented by only one subclass of Value, so it's 77 // Each type will be implemented by only one subclass of Value, so it's
78 // safe to use the ValueType to determine whether you can cast from 78 // safe to use the ValueType to determine whether you can cast from
79 // Value* to (Implementing Class)*. Also, a Value object never changes 79 // Value* to (Implementing Class)*. Also, a Value object never changes
80 // its type after construction. 80 // its type after construction.
81 ValueType GetType() const { return type_; } 81 ValueType GetType() const { return type_; }
82 82
83 // Returns true if the current object represents a given type. 83 // Returns true if the current object represents a given type.
84 bool IsType(ValueType type) const { return type == type_; } 84 bool IsType(ValueType type) const { return type == type_; }
85 85
86 // These methods allow the convenient retrieval of settings. 86 // These methods allow the convenient retrieval of settings.
87 // If the current setting object can be converted into the given type, 87 // If the current setting object can be converted into the given type,
88 // the value is returned through the |out_value| parameter and true is 88 // the value is returned through the |out_value| parameter and true is
89 // returned; otherwise, false is returned and |out_value| is unchanged. 89 // returned; otherwise, false is returned and |out_value| is unchanged.
90 virtual bool GetAsBoolean(bool* out_value) const; 90 virtual bool GetAsBoolean(bool* out_value) const;
91 virtual bool GetAsInteger(int* out_value) const; 91 virtual bool GetAsInteger(int* out_value) const;
92 virtual bool GetAsReal(double* out_value) const; 92 virtual bool GetAsReal(double* out_value) const;
93 virtual bool GetAsString(std::string* out_value) const; 93 virtual bool GetAsString(std::string* out_value) const;
94 virtual bool GetAsString(string16* out_value) const; 94 virtual bool GetAsString(string16* out_value) const;
95 virtual bool GetAsList(ListValue** out_value); 95 virtual bool GetAsList(ListValue** out_value);
96 96
97 // This creates a deep copy of the entire Value tree, and returns a pointer 97 // This creates a deep copy of the entire Value tree, and returns a pointer
98 // to the copy. The caller gets ownership of the copy, of course. 98 // to the copy. The caller gets ownership of the copy, of course.
99 //
100 // Subclasses return their own type directly in their overrides;
101 // this works because C++ supports covariant return types.
99 virtual Value* DeepCopy() const; 102 virtual Value* DeepCopy() const;
100 103
101 // Compares if two Value objects have equal contents. 104 // Compares if two Value objects have equal contents.
102 virtual bool Equals(const Value* other) const; 105 virtual bool Equals(const Value* other) const;
103 106
104 // Compares if two Value objects have equal contents. Can handle NULLs. 107 // Compares if two Value objects have equal contents. Can handle NULLs.
105 // NULLs are considered equal but different from Value::CreateNullValue(). 108 // NULLs are considered equal but different from Value::CreateNullValue().
106 static bool Equals(const Value* a, const Value* b); 109 static bool Equals(const Value* a, const Value* b);
107 110
108 protected: 111 protected:
(...skipping 14 matching lines...) Expand all
123 public: 126 public:
124 explicit FundamentalValue(bool in_value); 127 explicit FundamentalValue(bool in_value);
125 explicit FundamentalValue(int in_value); 128 explicit FundamentalValue(int in_value);
126 explicit FundamentalValue(double in_value); 129 explicit FundamentalValue(double in_value);
127 virtual ~FundamentalValue(); 130 virtual ~FundamentalValue();
128 131
129 // Subclassed methods 132 // Subclassed methods
130 virtual bool GetAsBoolean(bool* out_value) const; 133 virtual bool GetAsBoolean(bool* out_value) const;
131 virtual bool GetAsInteger(int* out_value) const; 134 virtual bool GetAsInteger(int* out_value) const;
132 virtual bool GetAsReal(double* out_value) const; 135 virtual bool GetAsReal(double* out_value) const;
133 virtual Value* DeepCopy() const; 136 virtual FundamentalValue* DeepCopy() const;
134 virtual bool Equals(const Value* other) const; 137 virtual bool Equals(const Value* other) const;
135 138
136 private: 139 private:
137 union { 140 union {
138 bool boolean_value_; 141 bool boolean_value_;
139 int integer_value_; 142 int integer_value_;
140 double real_value_; 143 double real_value_;
141 }; 144 };
142 145
143 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); 146 DISALLOW_COPY_AND_ASSIGN(FundamentalValue);
144 }; 147 };
145 148
146 class StringValue : public Value { 149 class StringValue : public Value {
147 public: 150 public:
148 // Initializes a StringValue with a UTF-8 narrow character string. 151 // Initializes a StringValue with a UTF-8 narrow character string.
149 explicit StringValue(const std::string& in_value); 152 explicit StringValue(const std::string& in_value);
150 153
151 // Initializes a StringValue with a string16. 154 // Initializes a StringValue with a string16.
152 explicit StringValue(const string16& in_value); 155 explicit StringValue(const string16& in_value);
153 156
154 virtual ~StringValue(); 157 virtual ~StringValue();
155 158
156 // Subclassed methods 159 // Subclassed methods
157 virtual bool GetAsString(std::string* out_value) const; 160 virtual bool GetAsString(std::string* out_value) const;
158 virtual bool GetAsString(string16* out_value) const; 161 virtual bool GetAsString(string16* out_value) const;
159 virtual Value* DeepCopy() const; 162 virtual StringValue* DeepCopy() const;
160 virtual bool Equals(const Value* other) const; 163 virtual bool Equals(const Value* other) const;
161 164
162 private: 165 private:
163 std::string value_; 166 std::string value_;
164 167
165 DISALLOW_COPY_AND_ASSIGN(StringValue); 168 DISALLOW_COPY_AND_ASSIGN(StringValue);
166 }; 169 };
167 170
168 class BinaryValue: public Value { 171 class BinaryValue: public Value {
169 public: 172 public:
170 virtual ~BinaryValue(); 173 virtual ~BinaryValue();
171 174
172 // Creates a Value to represent a binary buffer. The new object takes 175 // Creates a Value to represent a binary buffer. The new object takes
173 // ownership of the pointer passed in, if successful. 176 // ownership of the pointer passed in, if successful.
174 // Returns NULL if buffer is NULL. 177 // Returns NULL if buffer is NULL.
175 static BinaryValue* Create(char* buffer, size_t size); 178 static BinaryValue* Create(char* buffer, size_t size);
176 179
177 // For situations where you want to keep ownership of your buffer, this 180 // For situations where you want to keep ownership of your buffer, this
178 // factory method creates a new BinaryValue by copying the contents of the 181 // factory method creates a new BinaryValue by copying the contents of the
179 // buffer that's passed in. 182 // buffer that's passed in.
180 // Returns NULL if buffer is NULL. 183 // Returns NULL if buffer is NULL.
181 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); 184 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size);
182 185
183 size_t GetSize() const { return size_; } 186 size_t GetSize() const { return size_; }
184 char* GetBuffer() { return buffer_; } 187 char* GetBuffer() { return buffer_; }
185 const char* GetBuffer() const { return buffer_; } 188 const char* GetBuffer() const { return buffer_; }
186 189
187 // Overridden from Value: 190 // Overridden from Value:
188 virtual Value* DeepCopy() const; 191 virtual BinaryValue* DeepCopy() const;
189 virtual bool Equals(const Value* other) const; 192 virtual bool Equals(const Value* other) const;
190 193
191 private: 194 private:
192 // Constructor is private so that only objects with valid buffer pointers 195 // Constructor is private so that only objects with valid buffer pointers
193 // and size values can be created. 196 // and size values can be created.
194 BinaryValue(char* buffer, size_t size); 197 BinaryValue(char* buffer, size_t size);
195 198
196 char* buffer_; 199 char* buffer_;
197 size_t size_; 200 size_t size_;
198 201
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } 326 bool operator==(const key_iterator& other) { return itr_ == other.itr_; }
324 327
325 private: 328 private:
326 ValueMap::const_iterator itr_; 329 ValueMap::const_iterator itr_;
327 }; 330 };
328 331
329 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } 332 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); }
330 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } 333 key_iterator end_keys() const { return key_iterator(dictionary_.end()); }
331 334
332 // Overridden from Value: 335 // Overridden from Value:
333 virtual Value* DeepCopy() const; 336 virtual DictionaryValue* DeepCopy() const;
334 virtual bool Equals(const Value* other) const; 337 virtual bool Equals(const Value* other) const;
335 338
336 private: 339 private:
337 ValueMap dictionary_; 340 ValueMap dictionary_;
338 341
339 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); 342 DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
340 }; 343 };
341 344
342 // This type of Value represents a list of other Value values. 345 // This type of Value represents a list of other Value values.
343 class ListValue : public Value { 346 class ListValue : public Value {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 413
411 // Iteration 414 // Iteration
412 ListValue::iterator begin() { return list_.begin(); } 415 ListValue::iterator begin() { return list_.begin(); }
413 ListValue::iterator end() { return list_.end(); } 416 ListValue::iterator end() { return list_.end(); }
414 417
415 ListValue::const_iterator begin() const { return list_.begin(); } 418 ListValue::const_iterator begin() const { return list_.begin(); }
416 ListValue::const_iterator end() const { return list_.end(); } 419 ListValue::const_iterator end() const { return list_.end(); }
417 420
418 // Overridden from Value: 421 // Overridden from Value:
419 virtual bool GetAsList(ListValue** out_value); 422 virtual bool GetAsList(ListValue** out_value);
420 virtual Value* DeepCopy() const; 423 virtual ListValue* DeepCopy() const;
421 virtual bool Equals(const Value* other) const; 424 virtual bool Equals(const Value* other) const;
422 425
423 private: 426 private:
424 ValueVector list_; 427 ValueVector list_;
425 428
426 DISALLOW_COPY_AND_ASSIGN(ListValue); 429 DISALLOW_COPY_AND_ASSIGN(ListValue);
427 }; 430 };
428 431
429 // This interface is implemented by classes that know how to serialize and 432 // This interface is implemented by classes that know how to serialize and
430 // deserialize Value objects. 433 // deserialize Value objects.
431 class ValueSerializer { 434 class ValueSerializer {
432 public: 435 public:
433 virtual ~ValueSerializer(); 436 virtual ~ValueSerializer();
434 437
435 virtual bool Serialize(const Value& root) = 0; 438 virtual bool Serialize(const Value& root) = 0;
436 439
437 // This method deserializes the subclass-specific format into a Value object. 440 // This method deserializes the subclass-specific format into a Value object.
438 // If the return value is non-NULL, the caller takes ownership of returned 441 // If the return value is non-NULL, the caller takes ownership of returned
439 // Value. If the return value is NULL, and if error_code is non-NULL, 442 // Value. If the return value is NULL, and if error_code is non-NULL,
440 // error_code will be set with the underlying error. 443 // error_code will be set with the underlying error.
441 // If |error_message| is non-null, it will be filled in with a formatted 444 // If |error_message| is non-null, it will be filled in with a formatted
442 // error message including the location of the error if appropriate. 445 // error message including the location of the error if appropriate.
443 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; 446 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0;
444 }; 447 };
445 448
446 #endif // BASE_VALUES_H_ 449 #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