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
3 // found in the LICENSE file.
5 // This file specifies a recursive data storage class called Value intended for
6 // storing settings and other persistable data.
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
10 // types supported by JavaScript/JSON are supported.
12 // IN PARTICULAR this means that there is no support for int64 or unsigned
13 // numbers. Writing JSON with such types would violate the spec. If you need
14 // something like this, either use a double or make a string value containing
15 // the number you want.
17 #ifndef BASE_VALUES_H_
18 #define BASE_VALUES_H_
28 #include "base/base_export.h"
29 #include "base/basictypes.h"
30 #include "base/compiler_specific.h"
31 #include "base/memory/scoped_ptr.h"
32 #include "base/strings/string16.h"
36 class DictionaryValue
;
37 class FundamentalValue
;
42 typedef std::vector
<Value
*> ValueVector
;
43 typedef std::map
<std::string
, Value
*> ValueMap
;
45 // 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
49 // See the file-level comment above for more information.
50 class BASE_EXPORT Value
{
61 // Note: Do not add more types. See the file-level comment above for why.
66 static Value
* CreateNullValue();
67 // DEPRECATED: Do not use the following 5 functions. Instead, use
68 // new FundamentalValue or new StringValue.
69 static FundamentalValue
* CreateBooleanValue(bool in_value
);
70 static FundamentalValue
* CreateIntegerValue(int in_value
);
71 static FundamentalValue
* CreateDoubleValue(double in_value
);
72 static StringValue
* CreateStringValue(const std::string
& in_value
);
73 static StringValue
* CreateStringValue(const string16
& in_value
);
75 // Returns the type of the value stored by the current Value object.
76 // Each type will be implemented by only one subclass of Value, so it's
77 // safe to use the Type to determine whether you can cast from
78 // Value* to (Implementing Class)*. Also, a Value object never changes
79 // its type after construction.
80 Type
GetType() const { return type_
; }
82 // Returns true if the current object represents a given type.
83 bool IsType(Type type
) const { return type
== type_
; }
85 // These methods allow the convenient retrieval of the contents of the Value.
86 // If the current object can be converted into the given type, the value is
87 // returned through the |out_value| parameter and true is returned;
88 // otherwise, false is returned and |out_value| is unchanged.
89 virtual bool GetAsBoolean(bool* out_value
) const;
90 virtual bool GetAsInteger(int* out_value
) const;
91 virtual bool GetAsDouble(double* out_value
) const;
92 virtual bool GetAsString(std::string
* out_value
) const;
93 virtual bool GetAsString(string16
* out_value
) const;
94 virtual bool GetAsList(ListValue
** out_value
);
95 virtual bool GetAsList(const ListValue
** out_value
) const;
96 virtual bool GetAsDictionary(DictionaryValue
** out_value
);
97 virtual bool GetAsDictionary(const DictionaryValue
** out_value
) const;
98 // Note: Do not add more types. See the file-level comment above for why.
100 // This creates a deep copy of the entire Value tree, and returns a pointer
101 // to the copy. The caller gets ownership of the copy, of course.
103 // Subclasses return their own type directly in their overrides;
104 // this works because C++ supports covariant return types.
105 virtual Value
* DeepCopy() const;
107 // Compares if two Value objects have equal contents.
108 virtual bool Equals(const Value
* other
) const;
110 // Compares if two Value objects have equal contents. Can handle NULLs.
111 // NULLs are considered equal but different from Value::CreateNullValue().
112 static bool Equals(const Value
* a
, const Value
* b
);
115 // These aren't safe for end-users, but they are useful for subclasses.
116 explicit Value(Type type
);
117 Value(const Value
& that
);
118 Value
& operator=(const Value
& that
);
124 // FundamentalValue represents the simple fundamental types of values.
125 class BASE_EXPORT FundamentalValue
: public Value
{
127 explicit FundamentalValue(bool in_value
);
128 explicit FundamentalValue(int in_value
);
129 explicit FundamentalValue(double in_value
);
130 virtual ~FundamentalValue();
132 // Overridden from Value:
133 virtual bool GetAsBoolean(bool* out_value
) const OVERRIDE
;
134 virtual bool GetAsInteger(int* out_value
) const OVERRIDE
;
135 virtual bool GetAsDouble(double* out_value
) const OVERRIDE
;
136 virtual FundamentalValue
* DeepCopy() const OVERRIDE
;
137 virtual bool Equals(const Value
* other
) const OVERRIDE
;
143 double double_value_
;
147 class BASE_EXPORT StringValue
: public Value
{
149 // Initializes a StringValue with a UTF-8 narrow character string.
150 explicit StringValue(const std::string
& in_value
);
152 // Initializes a StringValue with a string16.
153 explicit StringValue(const string16
& in_value
);
155 virtual ~StringValue();
157 // Overridden from Value:
158 virtual bool GetAsString(std::string
* out_value
) const OVERRIDE
;
159 virtual bool GetAsString(string16
* out_value
) const OVERRIDE
;
160 virtual StringValue
* DeepCopy() const OVERRIDE
;
161 virtual bool Equals(const Value
* other
) const OVERRIDE
;
167 class BASE_EXPORT BinaryValue
: public Value
{
169 // Creates a BinaryValue with a null buffer and size of 0.
172 // Creates a BinaryValue, taking ownership of the bytes pointed to by
174 BinaryValue(scoped_ptr
<char[]> buffer
, size_t size
);
176 virtual ~BinaryValue();
178 // For situations where you want to keep ownership of your buffer, this
179 // factory method creates a new BinaryValue by copying the contents of the
180 // buffer that's passed in.
181 static BinaryValue
* CreateWithCopiedBuffer(const char* buffer
, size_t size
);
183 size_t GetSize() const { return size_
; }
186 char* GetBuffer() { return buffer_
.get(); }
187 const char* GetBuffer() const { return buffer_
.get(); }
189 // Overridden from Value:
190 virtual BinaryValue
* DeepCopy() const OVERRIDE
;
191 virtual bool Equals(const Value
* other
) const OVERRIDE
;
194 scoped_ptr
<char[]> buffer_
;
197 DISALLOW_COPY_AND_ASSIGN(BinaryValue
);
200 // DictionaryValue provides a key-value dictionary with (optional) "path"
201 // parsing for recursive access; see the comment at the top of the file. Keys
202 // are |std::string|s and should be UTF-8 encoded.
203 class BASE_EXPORT DictionaryValue
: public Value
{
206 virtual ~DictionaryValue();
208 // Overridden from Value:
209 virtual bool GetAsDictionary(DictionaryValue
** out_value
) OVERRIDE
;
210 virtual bool GetAsDictionary(
211 const DictionaryValue
** out_value
) const OVERRIDE
;
213 // Returns true if the current dictionary has a value for the given key.
214 bool HasKey(const std::string
& key
) const;
216 // Returns the number of Values in this dictionary.
217 size_t size() const { return dictionary_
.size(); }
219 // Returns whether the dictionary is empty.
220 bool empty() const { return dictionary_
.empty(); }
222 // Clears any current contents of this dictionary.
225 // Sets the Value associated with the given path starting from this object.
226 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
227 // into the next DictionaryValue down. Obviously, "." can't be used
228 // within a key, but there are no other restrictions on keys.
229 // If the key at any step of the way doesn't exist, or exists but isn't
230 // a DictionaryValue, a new DictionaryValue will be created and attached
231 // to the path in that location.
232 // Note that the dictionary takes ownership of the value referenced by
233 // |in_value|, and therefore |in_value| must be non-NULL.
234 void Set(const std::string
& path
, Value
* in_value
);
236 // Convenience forms of Set(). These methods will replace any existing
237 // value at that path, even if it has a different type.
238 void SetBoolean(const std::string
& path
, bool in_value
);
239 void SetInteger(const std::string
& path
, int in_value
);
240 void SetDouble(const std::string
& path
, double in_value
);
241 void SetString(const std::string
& path
, const std::string
& in_value
);
242 void SetString(const std::string
& path
, const string16
& in_value
);
244 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
246 void SetWithoutPathExpansion(const std::string
& key
, Value
* in_value
);
248 // Convenience forms of SetWithoutPathExpansion().
249 void SetBooleanWithoutPathExpansion(const std::string
& path
, bool in_value
);
250 void SetIntegerWithoutPathExpansion(const std::string
& path
, int in_value
);
251 void SetDoubleWithoutPathExpansion(const std::string
& path
, double in_value
);
252 void SetStringWithoutPathExpansion(const std::string
& path
,
253 const std::string
& in_value
);
254 void SetStringWithoutPathExpansion(const std::string
& path
,
255 const string16
& in_value
);
257 // Gets the Value associated with the given path starting from this object.
258 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
259 // into the next DictionaryValue down. If the path can be resolved
260 // successfully, the value for the last key in the path will be returned
261 // through the |out_value| parameter, and the function will return true.
262 // Otherwise, it will return false and |out_value| will be untouched.
263 // Note that the dictionary always owns the value that's returned.
264 bool Get(const std::string
& path
, const Value
** out_value
) const;
265 bool Get(const std::string
& path
, Value
** out_value
);
267 // These are convenience forms of Get(). The value will be retrieved
268 // and the return value will be true if the path is valid and the value at
269 // the end of the path can be returned in the form specified.
270 bool GetBoolean(const std::string
& path
, bool* out_value
) const;
271 bool GetInteger(const std::string
& path
, int* out_value
) const;
272 bool GetDouble(const std::string
& path
, double* out_value
) const;
273 bool GetString(const std::string
& path
, std::string
* out_value
) const;
274 bool GetString(const std::string
& path
, string16
* out_value
) const;
275 bool GetStringASCII(const std::string
& path
, std::string
* out_value
) const;
276 bool GetBinary(const std::string
& path
, const BinaryValue
** out_value
) const;
277 bool GetBinary(const std::string
& path
, BinaryValue
** out_value
);
278 bool GetDictionary(const std::string
& path
,
279 const DictionaryValue
** out_value
) const;
280 bool GetDictionary(const std::string
& path
, DictionaryValue
** out_value
);
281 bool GetList(const std::string
& path
, const ListValue
** out_value
) const;
282 bool GetList(const std::string
& path
, ListValue
** out_value
);
284 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
286 bool GetWithoutPathExpansion(const std::string
& key
,
287 const Value
** out_value
) const;
288 bool GetWithoutPathExpansion(const std::string
& key
, Value
** out_value
);
289 bool GetBooleanWithoutPathExpansion(const std::string
& key
,
290 bool* out_value
) const;
291 bool GetIntegerWithoutPathExpansion(const std::string
& key
,
292 int* out_value
) const;
293 bool GetDoubleWithoutPathExpansion(const std::string
& key
,
294 double* out_value
) const;
295 bool GetStringWithoutPathExpansion(const std::string
& key
,
296 std::string
* out_value
) const;
297 bool GetStringWithoutPathExpansion(const std::string
& key
,
298 string16
* out_value
) const;
299 bool GetDictionaryWithoutPathExpansion(
300 const std::string
& key
,
301 const DictionaryValue
** out_value
) const;
302 bool GetDictionaryWithoutPathExpansion(const std::string
& key
,
303 DictionaryValue
** out_value
);
304 bool GetListWithoutPathExpansion(const std::string
& key
,
305 const ListValue
** out_value
) const;
306 bool GetListWithoutPathExpansion(const std::string
& key
,
307 ListValue
** out_value
);
309 // Removes the Value with the specified path from this dictionary (or one
310 // of its child dictionaries, if the path is more than just a local key).
311 // If |out_value| is non-NULL, the removed Value will be passed out via
312 // |out_value|. If |out_value| is NULL, the removed value will be deleted.
313 // This method returns true if |path| is a valid path; otherwise it will
314 // return false and the DictionaryValue object will be unchanged.
315 virtual bool Remove(const std::string
& path
, scoped_ptr
<Value
>* out_value
);
317 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
318 // to be used as paths.
319 virtual bool RemoveWithoutPathExpansion(const std::string
& key
,
320 scoped_ptr
<Value
>* out_value
);
322 // Removes a path, clearing out all dictionaries on |path| that remain empty
323 // after removing the value at |path|.
324 virtual bool RemovePath(const std::string
& path
,
325 scoped_ptr
<Value
>* out_value
);
327 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
328 // the copy. This never returns NULL, even if |this| itself is empty.
329 DictionaryValue
* DeepCopyWithoutEmptyChildren() const;
331 // Merge |dictionary| into this dictionary. This is done recursively, i.e. any
332 // sub-dictionaries will be merged as well. In case of key collisions, the
333 // passed in dictionary takes precedence and data already present will be
334 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
335 // be freed any time after this call.
336 void MergeDictionary(const DictionaryValue
* dictionary
);
338 // Swaps contents with the |other| dictionary.
339 virtual void Swap(DictionaryValue
* other
);
341 // This class provides an iterator over both keys and values in the
342 // dictionary. It can't be used to modify the dictionary.
343 class BASE_EXPORT Iterator
{
345 explicit Iterator(const DictionaryValue
& target
);
348 bool IsAtEnd() const { return it_
== target_
.dictionary_
.end(); }
349 void Advance() { ++it_
; }
351 const std::string
& key() const { return it_
->first
; }
352 const Value
& value() const { return *it_
->second
; }
355 const DictionaryValue
& target_
;
356 ValueMap::const_iterator it_
;
359 // Overridden from Value:
360 virtual DictionaryValue
* DeepCopy() const OVERRIDE
;
361 virtual bool Equals(const Value
* other
) const OVERRIDE
;
364 ValueMap dictionary_
;
366 DISALLOW_COPY_AND_ASSIGN(DictionaryValue
);
369 // This type of Value represents a list of other Value values.
370 class BASE_EXPORT ListValue
: public Value
{
372 typedef ValueVector::iterator iterator
;
373 typedef ValueVector::const_iterator const_iterator
;
376 virtual ~ListValue();
378 // Clears the contents of this ListValue
381 // Returns the number of Values in this list.
382 size_t GetSize() const { return list_
.size(); }
384 // Returns whether the list is empty.
385 bool empty() const { return list_
.empty(); }
387 // Sets the list item at the given index to be the Value specified by
388 // the value given. If the index beyond the current end of the list, null
389 // Values will be used to pad out the list.
390 // Returns true if successful, or false if the index was negative or
391 // the value is a null pointer.
392 bool Set(size_t index
, Value
* in_value
);
394 // Gets the Value at the given index. Modifies |out_value| (and returns true)
395 // only if the index falls within the current list range.
396 // Note that the list always owns the Value passed out via |out_value|.
397 bool Get(size_t index
, const Value
** out_value
) const;
398 bool Get(size_t index
, Value
** out_value
);
400 // Convenience forms of Get(). Modifies |out_value| (and returns true)
401 // only if the index is valid and the Value at that index can be returned
402 // in the specified form.
403 bool GetBoolean(size_t index
, bool* out_value
) const;
404 bool GetInteger(size_t index
, int* out_value
) const;
405 bool GetDouble(size_t index
, double* out_value
) const;
406 bool GetString(size_t index
, std::string
* out_value
) const;
407 bool GetString(size_t index
, string16
* out_value
) const;
408 bool GetBinary(size_t index
, const BinaryValue
** out_value
) const;
409 bool GetBinary(size_t index
, BinaryValue
** out_value
);
410 bool GetDictionary(size_t index
, const DictionaryValue
** out_value
) const;
411 bool GetDictionary(size_t index
, DictionaryValue
** out_value
);
412 bool GetList(size_t index
, const ListValue
** out_value
) const;
413 bool GetList(size_t index
, ListValue
** out_value
);
415 // Removes the Value with the specified index from this list.
416 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
417 // passed out via |out_value|. If |out_value| is NULL, the removed value will
418 // be deleted. This method returns true if |index| is valid; otherwise
419 // it will return false and the ListValue object will be unchanged.
420 virtual bool Remove(size_t index
, scoped_ptr
<Value
>* out_value
);
422 // Removes the first instance of |value| found in the list, if any, and
423 // deletes it. |index| is the location where |value| was found. Returns false
425 bool Remove(const Value
& value
, size_t* index
);
427 // Removes the element at |iter|. If |out_value| is NULL, the value will be
428 // deleted, otherwise ownership of the value is passed back to the caller.
429 // Returns an iterator pointing to the location of the element that
430 // followed the erased element.
431 iterator
Erase(iterator iter
, scoped_ptr
<Value
>* out_value
);
433 // Appends a Value to the end of the list.
434 void Append(Value
* in_value
);
436 // Convenience forms of Append.
437 void AppendBoolean(bool in_value
);
438 void AppendInteger(int in_value
);
439 void AppendDouble(double in_value
);
440 void AppendString(const std::string
& in_value
);
441 void AppendString(const string16
& in_value
);
442 void AppendStrings(const std::vector
<std::string
>& in_values
);
443 void AppendStrings(const std::vector
<string16
>& in_values
);
445 // Appends a Value if it's not already present. Takes ownership of the
446 // |in_value|. Returns true if successful, or false if the value was already
447 // present. If the value was already present the |in_value| is deleted.
448 bool AppendIfNotPresent(Value
* in_value
);
450 // Insert a Value at index.
451 // Returns true if successful, or false if the index was out of range.
452 bool Insert(size_t index
, Value
* in_value
);
454 // Searches for the first instance of |value| in the list using the Equals
455 // method of the Value type.
456 // Returns a const_iterator to the found item or to end() if none exists.
457 const_iterator
Find(const Value
& value
) const;
459 // Swaps contents with the |other| list.
460 virtual void Swap(ListValue
* other
);
463 iterator
begin() { return list_
.begin(); }
464 iterator
end() { return list_
.end(); }
466 const_iterator
begin() const { return list_
.begin(); }
467 const_iterator
end() const { return list_
.end(); }
469 // Overridden from Value:
470 virtual bool GetAsList(ListValue
** out_value
) OVERRIDE
;
471 virtual bool GetAsList(const ListValue
** out_value
) const OVERRIDE
;
472 virtual ListValue
* DeepCopy() const OVERRIDE
;
473 virtual bool Equals(const Value
* other
) const OVERRIDE
;
478 DISALLOW_COPY_AND_ASSIGN(ListValue
);
481 // This interface is implemented by classes that know how to serialize and
482 // deserialize Value objects.
483 class BASE_EXPORT ValueSerializer
{
485 virtual ~ValueSerializer();
487 virtual bool Serialize(const Value
& root
) = 0;
489 // This method deserializes the subclass-specific format into a Value object.
490 // If the return value is non-NULL, the caller takes ownership of returned
491 // Value. If the return value is NULL, and if error_code is non-NULL,
492 // error_code will be set with the underlying error.
493 // If |error_message| is non-null, it will be filled in with a formatted
494 // error message including the location of the error if appropriate.
495 virtual Value
* Deserialize(int* error_code
, std::string
* error_str
) = 0;
498 // Stream operator so Values can be used in assertion statements. In order that
499 // gtest uses this operator to print readable output on test failures, we must
500 // override each specific type. Otherwise, the default template implementation
501 // is preferred over an upcast.
502 BASE_EXPORT
std::ostream
& operator<<(std::ostream
& out
, const Value
& value
);
504 BASE_EXPORT
inline std::ostream
& operator<<(std::ostream
& out
,
505 const FundamentalValue
& value
) {
506 return out
<< static_cast<const Value
&>(value
);
509 BASE_EXPORT
inline std::ostream
& operator<<(std::ostream
& out
,
510 const StringValue
& value
) {
511 return out
<< static_cast<const Value
&>(value
);
514 BASE_EXPORT
inline std::ostream
& operator<<(std::ostream
& out
,
515 const DictionaryValue
& value
) {
516 return out
<< static_cast<const Value
&>(value
);
519 BASE_EXPORT
inline std::ostream
& operator<<(std::ostream
& out
,
520 const ListValue
& value
) {
521 return out
<< static_cast<const Value
&>(value
);
526 #endif // BASE_VALUES_H_