Home | History | Annotate | Download | only in base

Lines Matching refs:Value

5 // This file specifies a recursive data storage class called Value intended for
8 // A Value represents something that can be stored in JSON or passed to/from
14 // something like this, either use a double or make a string value containing
17 // NOTE: A Value parameter that is always a Value::STRING should just be passed
18 // as a std::string. Similarly for Values that are always Value::DICTIONARY
19 // (should be flat_map), Value::LIST (should be std::vector), et cetera.
46 class Value;
48 // The Value class is the base class for Values. A Value can be instantiated
53 // base::Value is currently in the process of being refactored. Design doc:
56 // Previously (which is how most code that currently exists is written), Value
57 // used derived types to implement the individual data types, and base::Value
63 // std::unique_ptr<base::Value> GetFoo() {
69 // The new design makes base::Value a variant type that holds everything in
70 // a union. It is now recommended to pass by value with std::move rather than
76 // base::Value GetFoo() {
77 // base::Value dict(base::Value::Type::DICTIONARY);
78 // dict.SetKey("mykey", base::Value(foo));
81 class BASE_EXPORT Value {
84 using DictStorage = flat_map<std::string, std::unique_ptr<Value>>;
85 using ListStorage = std::vector<Value>;
102 // DEPRECATED, use std::make_unique<Value>(const BlobStorage&) instead.
104 static std::unique_ptr<Value> CreateWithCopiedBuffer(const char* buffer,
108 static Value FromUniquePtrValue(std::unique_ptr<Value> val);
109 static std::unique_ptr<Value> ToUniquePtrValue(Value val);
111 Value(Value&& that) noexcept;
112 Value() noexcept; // A null value.
114 // Value's copy constructor and copy assignment operator are deleted. Use this
116 Value Clone() const;
118 explicit Value(Type type);
119 explicit Value(bool in_bool);
120 explicit Value(int in_int);
121 explicit Value(double in_double);
123 // Value(const char*) and Value(const char16*) are required despite
124 // Value(StringPiece) and Value(StringPiece16) because otherwise the
125 // compiler will choose the Value(bool) constructor for these arguments.
126 // Value(std::string&&) allow for efficient move construction.
127 explicit Value(const char* in_string);
128 explicit Value(StringPiece in_string);
129 explicit Value(std::string&& in_string) noexcept;
130 explicit Value(const char16* in_string16);
131 explicit Value(StringPiece16 in_string16);
133 explicit Value(const BlobStorage& in_blob);
134 explicit Value(BlobStorage&& in_blob) noexcept;
136 explicit Value(const DictStorage& in_dict);
137 explicit Value(DictStorage&& in_dict) noexcept;
139 explicit Value(const ListStorage& in_list);
140 explicit Value(ListStorage&& in_list) noexcept;
142 Value& operator=(Value&& that) noexcept;
144 ~Value();
149 // Returns the type of the value stored by the current Value object.
180 Value* FindKey(StringPiece key);
181 const Value* FindKey(StringPiece key) const;
184 // value to have type |type|. If no type is found, or the found value is of a
192 Value* FindKeyOfType(StringPiece key, Type type);
193 const Value* FindKeyOfType(StringPiece key, Type type) const;
196 // value to |value|. If |key| could not be found, a new element is inserted.
202 Value* SetKey(StringPiece key, Value value);
204 Value* SetKey(std::string&& key, Value value);
206 Value* SetKey(const char* key, Value value);
208 // This attemps to remove the value associated with |key|. In case of failure,
218 // Searches a hierarchy of dictionary values for a given value. If a path
223 // The type of the leaf Value is not checked.
226 // will actually be into another Value, so it can't be compared to iterators
236 Value* FindPath(std::initializer_list<StringPiece> path);
237 Value* FindPath(span<const StringPiece> path);
238 const Value* FindPath(std::initializer_list<StringPiece> path) const;
239 const Value* FindPath(span<const StringPiece> path) const;
241 // Like FindPath() but will only return the value if the leaf Value type
246 Value* FindPathOfType(std::initializer_list<StringPiece> path, Type type);
247 Value* FindPathOfType(span<const StringPiece> path, Type type);
248 const Value* FindPathOfType(std::initializer_list<StringPiece> path,
250 const Value* FindPathOfType(span<const StringPiece> path, Type type) const;
254 // If the current value is not a dictionary, the function returns nullptr. If
256 // components matches a value that is not a dictionary, the function will fail
257 // (it will not overwrite the value) and return nullptr. The last path
262 // value.SetPath({"foo", "bar"}, std::move(myvalue));
265 // value.SetPath(components, std::move(myvalue));
268 Value* SetPath(std::initializer_list<StringPiece> path, Value value);
269 Value* SetPath(span<const StringPiece> path, Value value);
271 // Tries to remove a Value at the given path.
273 // If the current value is not a dictionary or any path components does not
279 // bool success = value.RemovePath({"foo", "bar"});
282 // bool success = value.RemovePath(components);
304 // These methods allow the convenient retrieval of the contents of the Value.
305 // If the current object can be converted into the given type, the value is
317 bool GetAsString(const Value** out_value) const;
328 // This creates a deep copy of the entire Value tree, and returns a pointer
332 // DEPRECATED, use Value::Clone() instead.
334 Value* DeepCopy() const;
335 // DEPRECATED, use Value::Clone() instead.
337 std::unique_ptr<Value> CreateDeepCopy() const;
341 BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs);
342 BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs);
343 BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs);
344 BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs);
345 BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs);
346 BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs);
348 // Compares if two Value objects have equal contents.
349 // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead.
351 bool Equals(const Value* other) const;
373 void InternalMoveConstructFrom(Value&& that);
376 DISALLOW_COPY_AND_ASSIGN(Value);
379 // DictionaryValue provides a key-value dictionary with (optional) "path"
382 class BASE_EXPORT DictionaryValue : public Value {
387 // Returns |value| if it is a dictionary, nullptr otherwise.
388 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
394 // Returns true if the current dictionary has a value for the given key.
395 // DEPRECATED, use Value::FindKey(key) instead.
407 // Sets the Value associated with the given path starting from this object.
414 // Returns a pointer to the inserted value.
415 // DEPRECATED, use Value::SetPath(path, value) instead.
416 Value* Set(StringPiece path, std::unique_ptr<Value> in_value);
419 // value at that path, even if it has a different type.
420 // DEPRECATED, use Value::SetPath(path, Value(bool)) instead.
421 Value* SetBoolean(StringPiece path, bool in_value);
422 // DEPRECATED, use Value::SetPath(path, Value(int)) instead.
423 Value* SetInteger(StringPiece path, int in_value);
424 // DEPRECATED, use Value::SetPath(path, Value(double)) instead.
425 Value* SetDouble(StringPiece path, double in_value);
426 // DEPRECATED, use Value::SetPath(path, Value(StringPiece)) instead.
427 Value* SetString(StringPiece path, StringPiece in_value);
428 // DEPRECATED, use Value::SetPath(path, Value(const string& 16)) instead.
429 Value* SetString(StringPiece path, const string16& in_value);
430 // DEPRECATED, use Value::SetPath(path, Value(Type::DICTIONARY)) instead.
433 // DEPRECATED, use Value::SetPath(path, Value(Type::LIST)) instead.
438 // DEPRECATED, use Value::SetKey(key, value) instead.
439 Value* SetWithoutPathExpansion(StringPiece key,
440 std::unique_ptr<Value> in_value);
442 // Gets the Value associated with the given path starting from this object.
445 // successfully, the value for the last key in the path will be returned
448 // Note that the dictionary always owns the value that's returned.
450 // DEPRECATED, use Value::FindPath(path) instead.
451 bool Get(StringPiece path, const Value** out_value) const;
452 // DEPRECATED, use Value::FindPath(path) instead.
453 bool Get(StringPiece path, Value** out_value);
455 // These are convenience forms of Get(). The value will be retrieved
456 // and the return value will be true if the path is valid and the value at
459 // DEPRECATED, use Value::FindPath(path) and Value::GetBool() instead.
461 // DEPRECATED, use Value::FindPath(path) and Value::GetInt() instead.
465 // DEPRECATED, use Value::FindPath(path) and Value::GetDouble() instead.
467 // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead.
469 // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead.
471 // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead.
473 // DEPRECATED, use Value::FindPath(path) and Value::GetBlob() instead.
474 bool GetBinary(StringPiece path, const Value** out_value) const;
475 // DEPRECATED, use Value::FindPath(path) and Value::GetBlob() instead.
476 bool GetBinary(StringPiece path, Value** out_value);
477 // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead.
480 // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead.
482 // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead.
484 // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead.
489 // DEPRECATED, use Value::FindKey(key) instead.
490 bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const;
491 // DEPRECATED, use Value::FindKey(key) instead.
492 bool GetWithoutPathExpansion(StringPiece key, Value** out_value);
493 // DEPRECATED, use Value::FindKey(key) and Value::GetBool() instead.
495 // DEPRECATED, use Value::FindKey(key) and Value::GetInt() instead.
497 // DEPRECATED, use Value::FindKey(key) and Value::GetDouble() instead.
499 // DEPRECATED, use Value::FindKey(key) and Value::GetString() instead.
502 // DEPRECATED, use Value::FindKey(key) and Value::GetString() instead.
505 // DEPRECATED, use Value::FindKey(key) and Value's Dictionary API instead.
509 // DEPRECATED, use Value::FindKey(key) and Value's Dictionary API instead.
512 // DEPRECATED, use Value::FindKey(key) and Value::GetList() instead.
515 // DEPRECATED, use Value::FindKey(key) and Value::GetList() instead.
518 // Removes the Value with the specified path from this dictionary (or one
520 // If |out_value| is non-NULL, the removed Value will be passed out via
521 // |out_value|. If |out_value| is NULL, the removed value will be deleted.
524 // DEPRECATED, use Value::RemovePath(path) instead.
525 bool Remove(StringPiece path, std::unique_ptr<Value>* out_value);
529 // DEPRECATED, use Value::RemoveKey(key) instead.
531 std::unique_ptr<Value>* out_value);
534 // after removing the value at |path|.
535 // DEPRECATED, use Value::RemovePath(path) instead.
536 bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value);
538 using Value::RemovePath; // DictionaryValue::RemovePath shadows otherwise.
556 // DEPRECATED, use Value::DictItems() instead.
567 const Value& value() const { return *it_->second; }
575 // DEPRECATED, use Value::DictItems() instead.
579 // DEPRECATED, use Value::DictItems() instead.
583 // DEPRECATED, use Value::Clone() instead.
586 // DEPRECATED, use Value::Clone() instead.
591 // This type of Value represents a list of other Value values.
592 class BASE_EXPORT ListValue : public Value {
597 // Returns |value| if it is a list, nullptr otherwise.
598 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
620 // Sets the list item at the given index to be the Value specified by
621 // the value given. If the index beyond the current end of the list, null
624 // the value is a null pointer.
626 bool Set(size_t index, std::unique_ptr<Value> in_value);
628 // Gets the Value at the given index. Modifies |out_value| (and returns true)
630 // Note that the list always owns the Value passed out via |out_value|.
633 bool Get(size_t index, const Value** out_value) const;
634 bool Get(size_t index, Value** out_value);
637 // only if the index is valid and the Value at that index can be returned
655 using Value::GetList;
660 // Removes the Value with the specified index from this list.
661 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
662 // passed out via |out_value|. If |out_value| is NULL, the removed value will
666 bool Remove(size_t index, std::unique_ptr<Value>* out_value);
668 // Removes the first instance of |value| found in the list, if any, and
669 // deletes it. |index| is the location where |value| was found. Returns false
672 bool Remove(const Value& value, size_t* index);
674 // Removes the element at |iter|. If |out_value| is NULL, the value will be
675 // deleted, otherwise ownership of the value is passed back to the caller.
679 iterator Erase(iterator iter, std::unique_ptr<Value>* out_value);
681 // Appends a Value to the end of the list.
683 void Append(std::unique_ptr<Value> in_value);
696 // Appends a Value if it's not already present. Returns true if successful,
697 // or false if the value was already
699 bool AppendIfNotPresent(std::unique_ptr<Value> in_value);
701 // Insert a Value at index.
704 bool Insert(size_t index, std::unique_ptr<Value> in_value);
706 // Searches for the first instance of |value| in the list using the Equals
707 // method of the Value type.
710 const_iterator Find(const Value& value) const;
727 // DEPRECATED, use Value::Clone() instead.
730 // DEPRECATED, use Value::Clone() instead.
736 // Value objects.
741 virtual bool Serialize(const Value& root) = 0;
744 // This interface is implemented by classes that know how to deserialize Value
750 // This method deserializes the subclass-specific format into a Value object.
751 // If the return value is non-NULL, the caller takes ownership of returned
752 // Value. If the return value is NULL, and if error_code is non-NULL,
756 virtual std::unique_ptr<Value> Deserialize(int* error_code,
764 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
767 const DictionaryValue& value) {
768 return out << static_cast<const Value&>(value);
772 const ListValue& value) {
773 return out << static_cast<const Value&>(value);
778 const Value::Type& type);