Update CrxUpdateService Memcheck::Leak suppression to match new signature.
[chromium-blink-merge.git] / base / values.h
blob026fcf664b0510c884f08c4368b0aa52c2b229ef
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
3 // found in the LICENSE file.
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
7 // specify (recursive) lists and dictionaries, so it's fairly expressive.
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
10 // easily do things like:
12 // root->SetString("global.pages.homepage", "http://goateleporter.com");
13 // std::string homepage = "http://google.com"; // default/fallback value
14 // root->GetString("global.pages.homepage", &homepage);
16 // where "global" and "pages" are also DictionaryValues, and "homepage" is a
17 // string setting. If some elements of the path didn't exist yet, the
18 // SetString() method would create the missing elements and attach them to root
19 // before attaching the homepage value.
21 #ifndef BASE_VALUES_H_
22 #define BASE_VALUES_H_
23 #pragma once
25 #include <iterator>
26 #include <map>
27 #include <string>
28 #include <vector>
30 #include "base/base_export.h"
31 #include "base/basictypes.h"
32 #include "base/compiler_specific.h"
33 #include "base/string16.h"
35 // This file declares "using base::Value", etc. at the bottom, so that
36 // current code can use these classes without the base namespace. In
37 // new code, please always use base::Value, etc. or add your own
38 // "using" declaration.
39 // http://crbug.com/88666
40 namespace base {
42 class BinaryValue;
43 class DictionaryValue;
44 class FundamentalValue;
45 class ListValue;
46 class StringValue;
47 class Value;
49 typedef std::vector<Value*> ValueVector;
50 typedef std::map<std::string, Value*> ValueMap;
52 // The Value class is the base class for Values. A Value can be instantiated
53 // via the Create*Value() factory methods, or by directly creating instances of
54 // the subclasses.
55 class BASE_EXPORT Value {
56 public:
57 enum Type {
58 TYPE_NULL = 0,
59 TYPE_BOOLEAN,
60 TYPE_INTEGER,
61 TYPE_DOUBLE,
62 TYPE_STRING,
63 TYPE_BINARY,
64 TYPE_DICTIONARY,
65 TYPE_LIST
68 virtual ~Value();
70 // Convenience methods for creating Value objects for various
71 // kinds of values without thinking about which class implements them.
72 // These can always be expected to return a valid Value*.
73 static Value* CreateNullValue();
74 static FundamentalValue* CreateBooleanValue(bool in_value);
75 static FundamentalValue* CreateIntegerValue(int in_value);
76 static FundamentalValue* CreateDoubleValue(double in_value);
77 static StringValue* CreateStringValue(const std::string& in_value);
78 static StringValue* CreateStringValue(const string16& in_value);
80 // Returns the type of the value stored by the current Value object.
81 // Each type will be implemented by only one subclass of Value, so it's
82 // safe to use the Type to determine whether you can cast from
83 // Value* to (Implementing Class)*. Also, a Value object never changes
84 // its type after construction.
85 Type GetType() const { return type_; }
87 // Returns true if the current object represents a given type.
88 bool IsType(Type type) const { return type == type_; }
90 // These methods allow the convenient retrieval of settings.
91 // If the current setting object can be converted into the given type,
92 // the value is returned through the |out_value| parameter and true is
93 // returned; otherwise, false is returned and |out_value| is unchanged.
94 virtual bool GetAsBoolean(bool* out_value) const;
95 virtual bool GetAsInteger(int* out_value) const;
96 virtual bool GetAsDouble(double* out_value) const;
97 virtual bool GetAsString(std::string* out_value) const;
98 virtual bool GetAsString(string16* out_value) const;
99 virtual bool GetAsList(ListValue** out_value);
100 virtual bool GetAsList(const ListValue** out_value) const;
101 virtual bool GetAsDictionary(DictionaryValue** out_value);
102 virtual bool GetAsDictionary(const DictionaryValue** out_value) const;
104 // This creates a deep copy of the entire Value tree, and returns a pointer
105 // to the copy. The caller gets ownership of the copy, of course.
107 // Subclasses return their own type directly in their overrides;
108 // this works because C++ supports covariant return types.
109 virtual Value* DeepCopy() const;
111 // Compares if two Value objects have equal contents.
112 virtual bool Equals(const Value* other) const;
114 // Compares if two Value objects have equal contents. Can handle NULLs.
115 // NULLs are considered equal but different from Value::CreateNullValue().
116 static bool Equals(const Value* a, const Value* b);
118 protected:
119 // This isn't safe for end-users (they should use the Create*Value()
120 // static methods above), but it's useful for subclasses.
121 explicit Value(Type type);
123 private:
124 Value();
126 Type type_;
128 DISALLOW_COPY_AND_ASSIGN(Value);
131 // FundamentalValue represents the simple fundamental types of values.
132 class BASE_EXPORT FundamentalValue : public Value {
133 public:
134 explicit FundamentalValue(bool in_value);
135 explicit FundamentalValue(int in_value);
136 explicit FundamentalValue(double in_value);
137 virtual ~FundamentalValue();
139 // Overridden from Value:
140 virtual bool GetAsBoolean(bool* out_value) const OVERRIDE;
141 virtual bool GetAsInteger(int* out_value) const OVERRIDE;
142 virtual bool GetAsDouble(double* out_value) const OVERRIDE;
143 virtual FundamentalValue* DeepCopy() const OVERRIDE;
144 virtual bool Equals(const Value* other) const OVERRIDE;
146 private:
147 union {
148 bool boolean_value_;
149 int integer_value_;
150 double double_value_;
153 DISALLOW_COPY_AND_ASSIGN(FundamentalValue);
156 class BASE_EXPORT StringValue : public Value {
157 public:
158 // Initializes a StringValue with a UTF-8 narrow character string.
159 explicit StringValue(const std::string& in_value);
161 // Initializes a StringValue with a string16.
162 explicit StringValue(const string16& in_value);
164 virtual ~StringValue();
166 // Overridden from Value:
167 virtual bool GetAsString(std::string* out_value) const OVERRIDE;
168 virtual bool GetAsString(string16* out_value) const OVERRIDE;
169 virtual StringValue* DeepCopy() const OVERRIDE;
170 virtual bool Equals(const Value* other) const OVERRIDE;
172 private:
173 std::string value_;
175 DISALLOW_COPY_AND_ASSIGN(StringValue);
178 class BASE_EXPORT BinaryValue: public Value {
179 public:
180 virtual ~BinaryValue();
182 // Creates a Value to represent a binary buffer. The new object takes
183 // ownership of the pointer passed in, if successful.
184 // Returns NULL if buffer is NULL.
185 static BinaryValue* Create(char* buffer, size_t size);
187 // For situations where you want to keep ownership of your buffer, this
188 // factory method creates a new BinaryValue by copying the contents of the
189 // buffer that's passed in.
190 // Returns NULL if buffer is NULL.
191 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size);
193 size_t GetSize() const { return size_; }
194 char* GetBuffer() { return buffer_; }
195 const char* GetBuffer() const { return buffer_; }
197 // Overridden from Value:
198 virtual BinaryValue* DeepCopy() const OVERRIDE;
199 virtual bool Equals(const Value* other) const OVERRIDE;
201 private:
202 // Constructor is private so that only objects with valid buffer pointers
203 // and size values can be created.
204 BinaryValue(char* buffer, size_t size);
206 char* buffer_;
207 size_t size_;
209 DISALLOW_COPY_AND_ASSIGN(BinaryValue);
212 // DictionaryValue provides a key-value dictionary with (optional) "path"
213 // parsing for recursive access; see the comment at the top of the file. Keys
214 // are |std::string|s and should be UTF-8 encoded.
215 class BASE_EXPORT DictionaryValue : public Value {
216 public:
217 DictionaryValue();
218 virtual ~DictionaryValue();
220 // Overridden from Value:
221 virtual bool GetAsDictionary(DictionaryValue** out_value) OVERRIDE;
222 virtual bool GetAsDictionary(
223 const DictionaryValue** out_value) const OVERRIDE;
225 // Returns true if the current dictionary has a value for the given key.
226 bool HasKey(const std::string& key) const;
228 // Returns the number of Values in this dictionary.
229 size_t size() const { return dictionary_.size(); }
231 // Returns whether the dictionary is empty.
232 bool empty() const { return dictionary_.empty(); }
234 // Clears any current contents of this dictionary.
235 void Clear();
237 // Sets the Value associated with the given path starting from this object.
238 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
239 // into the next DictionaryValue down. Obviously, "." can't be used
240 // within a key, but there are no other restrictions on keys.
241 // If the key at any step of the way doesn't exist, or exists but isn't
242 // a DictionaryValue, a new DictionaryValue will be created and attached
243 // to the path in that location.
244 // Note that the dictionary takes ownership of the value referenced by
245 // |in_value|, and therefore |in_value| must be non-NULL.
246 void Set(const std::string& path, Value* in_value);
248 // Convenience forms of Set(). These methods will replace any existing
249 // value at that path, even if it has a different type.
250 void SetBoolean(const std::string& path, bool in_value);
251 void SetInteger(const std::string& path, int in_value);
252 void SetDouble(const std::string& path, double in_value);
253 void SetString(const std::string& path, const std::string& in_value);
254 void SetString(const std::string& path, const string16& in_value);
256 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
257 // be used as paths.
258 void SetWithoutPathExpansion(const std::string& key, Value* in_value);
260 // Gets the Value associated with the given path starting from this object.
261 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
262 // into the next DictionaryValue down. If the path can be resolved
263 // successfully, the value for the last key in the path will be returned
264 // through the |out_value| parameter, and the function will return true.
265 // Otherwise, it will return false and |out_value| will be untouched.
266 // Note that the dictionary always owns the value that's returned.
267 bool Get(const std::string& path, Value** out_value) const;
269 // These are convenience forms of Get(). The value will be retrieved
270 // and the return value will be true if the path is valid and the value at
271 // the end of the path can be returned in the form specified.
272 bool GetBoolean(const std::string& path, bool* out_value) const;
273 bool GetInteger(const std::string& path, int* out_value) const;
274 bool GetDouble(const std::string& path, double* out_value) const;
275 bool GetString(const std::string& path, std::string* out_value) const;
276 bool GetString(const std::string& path, string16* out_value) const;
277 bool GetStringASCII(const std::string& path, std::string* out_value) const;
278 bool GetBinary(const std::string& path, BinaryValue** out_value) const;
279 bool GetDictionary(const std::string& path,
280 DictionaryValue** out_value) const;
281 bool GetList(const std::string& path, ListValue** out_value) const;
283 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
284 // be used as paths.
285 bool GetWithoutPathExpansion(const std::string& key,
286 Value** out_value) const;
287 bool GetIntegerWithoutPathExpansion(const std::string& key,
288 int* out_value) const;
289 bool GetDoubleWithoutPathExpansion(const std::string& key,
290 double* out_value) const;
291 bool GetStringWithoutPathExpansion(const std::string& key,
292 std::string* out_value) const;
293 bool GetStringWithoutPathExpansion(const std::string& key,
294 string16* out_value) const;
295 bool GetDictionaryWithoutPathExpansion(const std::string& key,
296 DictionaryValue** out_value) const;
297 bool GetListWithoutPathExpansion(const std::string& key,
298 ListValue** out_value) const;
300 // Removes the Value with the specified path from this dictionary (or one
301 // of its child dictionaries, if the path is more than just a local key).
302 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
303 // passed out via out_value. If |out_value| is NULL, the removed value will
304 // be deleted. This method returns true if |path| is a valid path; otherwise
305 // it will return false and the DictionaryValue object will be unchanged.
306 bool Remove(const std::string& path, Value** out_value);
308 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
309 // to be used as paths.
310 bool RemoveWithoutPathExpansion(const std::string& key, Value** out_value);
312 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
313 // the copy. This never returns NULL, even if |this| itself is empty.
314 DictionaryValue* DeepCopyWithoutEmptyChildren();
316 // Merge a given dictionary into this dictionary. This is done recursively,
317 // i.e. any subdictionaries will be merged as well. In case of key collisions,
318 // the passed in dictionary takes precedence and data already present will be
319 // replaced.
320 void MergeDictionary(const DictionaryValue* dictionary);
322 // Swaps contents with the |other| dictionary.
323 void Swap(DictionaryValue* other) {
324 dictionary_.swap(other->dictionary_);
327 // This class provides an iterator for the keys in the dictionary.
328 // It can't be used to modify the dictionary.
330 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT
331 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any
332 // keys have '.'s in them.
333 class key_iterator
334 : private std::iterator<std::input_iterator_tag, const std::string> {
335 public:
336 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; }
337 key_iterator operator++() {
338 ++itr_;
339 return *this;
341 const std::string& operator*() { return itr_->first; }
342 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; }
343 bool operator==(const key_iterator& other) { return itr_ == other.itr_; }
345 private:
346 ValueMap::const_iterator itr_;
349 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); }
350 key_iterator end_keys() const { return key_iterator(dictionary_.end()); }
352 // This class provides an iterator over both keys and values in the
353 // dictionary. It can't be used to modify the dictionary.
354 class Iterator {
355 public:
356 explicit Iterator(const DictionaryValue& target)
357 : target_(target), it_(target.dictionary_.begin()) {}
359 bool HasNext() const { return it_ != target_.dictionary_.end(); }
360 void Advance() { ++it_; }
362 const std::string& key() const { return it_->first; }
363 const Value& value() const { return *it_->second; }
365 private:
366 const DictionaryValue& target_;
367 ValueMap::const_iterator it_;
370 // Overridden from Value:
371 virtual DictionaryValue* DeepCopy() const OVERRIDE;
372 virtual bool Equals(const Value* other) const OVERRIDE;
374 private:
375 ValueMap dictionary_;
377 DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
380 // This type of Value represents a list of other Value values.
381 class BASE_EXPORT ListValue : public Value {
382 public:
383 typedef ValueVector::iterator iterator;
384 typedef ValueVector::const_iterator const_iterator;
386 ListValue();
387 virtual ~ListValue();
389 // Clears the contents of this ListValue
390 void Clear();
392 // Returns the number of Values in this list.
393 size_t GetSize() const { return list_.size(); }
395 // Returns whether the list is empty.
396 bool empty() const { return list_.empty(); }
398 // Sets the list item at the given index to be the Value specified by
399 // the value given. If the index beyond the current end of the list, null
400 // Values will be used to pad out the list.
401 // Returns true if successful, or false if the index was negative or
402 // the value is a null pointer.
403 bool Set(size_t index, Value* in_value);
405 // Gets the Value at the given index. Modifies |out_value| (and returns true)
406 // only if the index falls within the current list range.
407 // Note that the list always owns the Value passed out via |out_value|.
408 bool Get(size_t index, Value** out_value) const;
410 // Convenience forms of Get(). Modifies |out_value| (and returns true)
411 // only if the index is valid and the Value at that index can be returned
412 // in the specified form.
413 bool GetBoolean(size_t index, bool* out_value) const;
414 bool GetInteger(size_t index, int* out_value) const;
415 bool GetDouble(size_t index, double* out_value) const;
416 bool GetString(size_t index, std::string* out_value) const;
417 bool GetString(size_t index, string16* out_value) const;
418 bool GetBinary(size_t index, BinaryValue** out_value) const;
419 bool GetDictionary(size_t index, DictionaryValue** out_value) const;
420 bool GetList(size_t index, ListValue** out_value) const;
422 // Removes the Value with the specified index from this list.
423 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
424 // passed out via |out_value|. If |out_value| is NULL, the removed value will
425 // be deleted. This method returns true if |index| is valid; otherwise
426 // it will return false and the ListValue object will be unchanged.
427 bool Remove(size_t index, Value** out_value);
429 // Removes the first instance of |value| found in the list, if any, and
430 // deletes it. |index| is the location where |value| was found. Returns false
431 // if not found.
432 bool Remove(const Value& value, size_t* index);
434 // Appends a Value to the end of the list.
435 void Append(Value* in_value);
437 // Appends a Value if it's not already present. Takes ownership of the
438 // |in_value|. Returns true if successful, or false if the value was already
439 // present. If the value was already present the |in_value| is deleted.
440 bool AppendIfNotPresent(Value* in_value);
442 // Insert a Value at index.
443 // Returns true if successful, or false if the index was out of range.
444 bool Insert(size_t index, Value* in_value);
446 // Searches for the first instance of |value| in the list using the Equals
447 // method of the Value type.
448 // Returns a const_iterator to the found item or to end() if none exists.
449 const_iterator Find(const Value& value) const;
451 // Swaps contents with the |other| list.
452 void Swap(ListValue* other) {
453 list_.swap(other->list_);
456 // Iteration.
457 iterator begin() { return list_.begin(); }
458 iterator end() { return list_.end(); }
460 const_iterator begin() const { return list_.begin(); }
461 const_iterator end() const { return list_.end(); }
463 // Overridden from Value:
464 virtual bool GetAsList(ListValue** out_value) OVERRIDE;
465 virtual bool GetAsList(const ListValue** out_value) const OVERRIDE;
466 virtual ListValue* DeepCopy() const OVERRIDE;
467 virtual bool Equals(const Value* other) const OVERRIDE;
469 private:
470 ValueVector list_;
472 DISALLOW_COPY_AND_ASSIGN(ListValue);
475 // This interface is implemented by classes that know how to serialize and
476 // deserialize Value objects.
477 class BASE_EXPORT ValueSerializer {
478 public:
479 virtual ~ValueSerializer();
481 virtual bool Serialize(const Value& root) = 0;
483 // This method deserializes the subclass-specific format into a Value object.
484 // If the return value is non-NULL, the caller takes ownership of returned
485 // Value. If the return value is NULL, and if error_code is non-NULL,
486 // error_code will be set with the underlying error.
487 // If |error_message| is non-null, it will be filled in with a formatted
488 // error message including the location of the error if appropriate.
489 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0;
492 } // namespace base
494 // http://crbug.com/88666
495 using base::DictionaryValue;
496 using base::ListValue;
497 using base::StringValue;
498 using base::Value;
500 #endif // BASE_VALUES_H_