123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976 |
- #ifndef BASE_VALUES_H_
- #define BASE_VALUES_H_
- #include <stddef.h>
- #include <stdint.h>
- #include <iosfwd>
- #include <map>
- #include <memory>
- #include <string>
- #include <utility>
- #include <vector>
- #include "base/base_export.h"
- #include "base/containers/checked_iterators.h"
- #include "base/containers/checked_range.h"
- #include "base/containers/flat_map.h"
- #include "base/containers/span.h"
- #include "base/macros.h"
- #include "base/strings/string16.h"
- #include "base/strings/string_piece.h"
- #include "base/value_iterators.h"
- namespace base {
- class DictionaryValue;
- class ListValue;
- class Value;
- class BASE_EXPORT Value {
- public:
- using BlobStorage = std::vector<uint8_t>;
- using DictStorage = flat_map<std::string, std::unique_ptr<Value>>;
- using ListStorage = std::vector<Value>;
- using ListView = CheckedContiguousRange<ListStorage>;
- using ConstListView = CheckedContiguousConstRange<ListStorage>;
-
- using DoubleStorage = struct { alignas(4) char v[sizeof(double)]; };
- enum class Type : unsigned char {
- NONE = 0,
- BOOLEAN,
- INTEGER,
- DOUBLE,
- STRING,
- BINARY,
- DICTIONARY,
- LIST,
-
- DEAD
-
- };
-
-
-
-
-
- static std::unique_ptr<Value> CreateWithCopiedBuffer(const char* buffer,
- size_t size);
-
- static Value FromUniquePtrValue(std::unique_ptr<Value> val);
- static std::unique_ptr<Value> ToUniquePtrValue(Value val);
- static const DictionaryValue& AsDictionaryValue(const Value& val);
- static const ListValue& AsListValue(const Value& val);
- Value(Value&& that) noexcept;
- Value() noexcept {}
-
-
-
-
-
- Value Clone() const;
- explicit Value(Type type);
- explicit Value(bool in_bool);
- explicit Value(int in_int);
- explicit Value(double in_double);
-
-
-
-
- explicit Value(const char* in_string);
- explicit Value(StringPiece in_string);
- explicit Value(std::string&& in_string) noexcept;
- explicit Value(const char16* in_string16);
- explicit Value(StringPiece16 in_string16);
- explicit Value(const std::vector<char>& in_blob);
- explicit Value(base::span<const uint8_t> in_blob);
- explicit Value(BlobStorage&& in_blob) noexcept;
- explicit Value(const DictStorage& in_dict);
- explicit Value(DictStorage&& in_dict) noexcept;
- explicit Value(span<const Value> in_list);
- explicit Value(ListStorage&& in_list) noexcept;
- Value& operator=(Value&& that) noexcept;
- ~Value();
-
- static const char* GetTypeName(Type type);
-
- Type type() const { return type_; }
-
- bool is_none() const { return type() == Type::NONE; }
- bool is_bool() const { return type() == Type::BOOLEAN; }
- bool is_int() const { return type() == Type::INTEGER; }
- bool is_double() const { return type() == Type::DOUBLE; }
- bool is_string() const { return type() == Type::STRING; }
- bool is_blob() const { return type() == Type::BINARY; }
- bool is_dict() const { return type() == Type::DICTIONARY; }
- bool is_list() const { return type() == Type::LIST; }
-
- bool GetBool() const;
- int GetInt() const;
- double GetDouble() const;
- const std::string& GetString() const;
- std::string& GetString();
- const BlobStorage& GetBlob() const;
-
-
-
-
- ListView GetList();
- ConstListView GetList() const;
-
-
-
- ListStorage TakeList();
-
-
- void Append(bool value);
- void Append(int value);
- void Append(double value);
- void Append(const char* value);
- void Append(StringPiece value);
- void Append(std::string&& value);
- void Append(const char16* value);
- void Append(StringPiece16 value);
- void Append(Value&& value);
-
-
- CheckedContiguousIterator<Value> Insert(
- CheckedContiguousConstIterator<Value> pos,
- Value&& value);
-
-
-
- bool EraseListIter(CheckedContiguousConstIterator<Value> iter);
-
-
-
- size_t EraseListValue(const Value& val);
-
-
-
- template <typename Predicate>
- size_t EraseListValueIf(Predicate pred) {
- CHECK(is_list());
- return base::EraseIf(list_, pred);
- }
-
-
- void ClearList();
-
-
-
-
-
-
-
-
- Value* FindKey(StringPiece key);
- const Value* FindKey(StringPiece key) const;
-
-
-
-
-
-
-
-
-
- Value* FindKeyOfType(StringPiece key, Type type);
- const Value* FindKeyOfType(StringPiece key, Type type) const;
-
-
-
- base::Optional<bool> FindBoolKey(StringPiece key) const;
- base::Optional<int> FindIntKey(StringPiece key) const;
-
-
- base::Optional<double> FindDoubleKey(StringPiece key) const;
-
- const std::string* FindStringKey(StringPiece key) const;
- std::string* FindStringKey(StringPiece key);
-
- const BlobStorage* FindBlobKey(StringPiece key) const;
-
- const Value* FindDictKey(StringPiece key) const;
- Value* FindDictKey(StringPiece key);
-
- const Value* FindListKey(StringPiece key) const;
- Value* FindListKey(StringPiece key);
-
-
-
-
-
-
-
-
- Value* SetKey(StringPiece key, Value&& value);
-
- Value* SetKey(std::string&& key, Value&& value);
-
- Value* SetKey(const char* key, Value&& value);
-
-
-
-
- Value* SetBoolKey(StringPiece key, bool val);
- Value* SetIntKey(StringPiece key, int val);
- Value* SetDoubleKey(StringPiece key, double val);
- Value* SetStringKey(StringPiece key, StringPiece val);
- Value* SetStringKey(StringPiece key, StringPiece16 val);
-
-
- Value* SetStringKey(StringPiece key, const char* val);
- Value* SetStringKey(StringPiece key, std::string&& val);
-
-
-
-
-
-
-
-
- bool RemoveKey(StringPiece key);
-
-
-
-
-
-
-
-
- Optional<Value> ExtractKey(StringPiece key);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Value* FindPath(StringPiece path);
- const Value* FindPath(StringPiece path) const;
-
-
-
-
-
-
-
-
-
-
- Value* FindPath(std::initializer_list<StringPiece> path);
- Value* FindPath(span<const StringPiece> path);
- const Value* FindPath(std::initializer_list<StringPiece> path) const;
- const Value* FindPath(span<const StringPiece> path) const;
-
-
-
-
-
-
- Value* FindPathOfType(StringPiece path, Type type);
- const Value* FindPathOfType(StringPiece path, Type type) const;
-
-
- base::Optional<bool> FindBoolPath(StringPiece path) const;
- base::Optional<int> FindIntPath(StringPiece path) const;
- base::Optional<double> FindDoublePath(StringPiece path) const;
- const std::string* FindStringPath(StringPiece path) const;
- std::string* FindStringPath(StringPiece path);
- const BlobStorage* FindBlobPath(StringPiece path) const;
- Value* FindDictPath(StringPiece path);
- const Value* FindDictPath(StringPiece path) const;
- Value* FindListPath(StringPiece path);
- const Value* FindListPath(StringPiece path) const;
-
-
- Value* FindPathOfType(std::initializer_list<StringPiece> path, Type type);
- Value* FindPathOfType(span<const StringPiece> path, Type type);
- const Value* FindPathOfType(std::initializer_list<StringPiece> path,
- Type type) const;
- const Value* FindPathOfType(span<const StringPiece> path, Type type) const;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Value* SetPath(StringPiece path, Value&& value);
-
-
- Value* SetBoolPath(StringPiece path, bool value);
- Value* SetIntPath(StringPiece path, int value);
- Value* SetDoublePath(StringPiece path, double value);
- Value* SetStringPath(StringPiece path, StringPiece value);
- Value* SetStringPath(StringPiece path, const char* value);
- Value* SetStringPath(StringPiece path, std::string&& value);
- Value* SetStringPath(StringPiece path, StringPiece16 value);
-
- Value* SetPath(std::initializer_list<StringPiece> path, Value&& value);
- Value* SetPath(span<const StringPiece> path, Value&& value);
-
-
-
-
-
-
-
-
-
-
- bool RemovePath(StringPiece path);
-
- bool RemovePath(std::initializer_list<StringPiece> path);
- bool RemovePath(span<const StringPiece> path);
-
-
-
-
-
-
-
-
-
-
-
- Optional<Value> ExtractPath(StringPiece path);
- using dict_iterator_proxy = detail::dict_iterator_proxy;
- using const_dict_iterator_proxy = detail::const_dict_iterator_proxy;
-
-
-
-
-
-
-
-
-
-
-
-
-
- dict_iterator_proxy DictItems();
- const_dict_iterator_proxy DictItems() const;
-
-
- size_t DictSize() const;
- bool DictEmpty() const;
-
-
-
-
-
-
- void MergeDictionary(const Value* dictionary);
-
-
-
-
-
- bool GetAsBoolean(bool* out_value) const;
-
- bool GetAsInteger(int* out_value) const;
-
- bool GetAsDouble(double* out_value) const;
-
- bool GetAsString(std::string* out_value) const;
- bool GetAsString(string16* out_value) const;
- bool GetAsString(const Value** out_value) const;
- bool GetAsString(StringPiece* out_value) const;
-
-
- bool GetAsList(ListValue** out_value);
- bool GetAsList(const ListValue** out_value) const;
-
- bool GetAsDictionary(DictionaryValue** out_value);
- bool GetAsDictionary(const DictionaryValue** out_value) const;
-
-
-
-
-
-
-
- Value* DeepCopy() const;
-
-
- std::unique_ptr<Value> CreateDeepCopy() const;
-
-
- BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs);
- BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs);
- BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs);
- BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs);
- BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs);
- BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs);
-
-
-
- bool Equals(const Value* other) const;
-
-
-
- size_t EstimateMemoryUsage() const;
- protected:
-
-
-
-
-
-
-
-
- Type type_ = Type::NONE;
- union {
- bool bool_value_;
- int int_value_;
- DoubleStorage double_value_;
- std::string string_value_;
- BlobStorage binary_value_;
- DictStorage dict_;
- ListStorage list_;
- };
- private:
- friend class ValuesTest_SizeOfValue_Test;
- double AsDoubleInternal() const;
- void InternalMoveConstructFrom(Value&& that);
- void InternalCleanup();
-
-
- Value* SetKeyInternal(StringPiece key, std::unique_ptr<Value>&& val_ptr);
- Value* SetPathInternal(StringPiece path, std::unique_ptr<Value>&& value_ptr);
- DISALLOW_COPY_AND_ASSIGN(Value);
- };
- class BASE_EXPORT DictionaryValue : public Value {
- public:
- using const_iterator = DictStorage::const_iterator;
- using iterator = DictStorage::iterator;
-
- static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
- DictionaryValue();
- explicit DictionaryValue(const DictStorage& in_dict);
- explicit DictionaryValue(DictStorage&& in_dict) noexcept;
-
-
- bool HasKey(StringPiece key) const;
-
- size_t size() const { return dict_.size(); }
-
- bool empty() const { return dict_.empty(); }
-
- void Clear();
-
-
-
-
-
-
-
-
-
- Value* Set(StringPiece path, std::unique_ptr<Value> in_value);
-
-
-
- Value* SetBoolean(StringPiece path, bool in_value);
-
- Value* SetInteger(StringPiece path, int in_value);
-
- Value* SetDouble(StringPiece path, double in_value);
-
- Value* SetString(StringPiece path, StringPiece in_value);
-
- Value* SetString(StringPiece path, const string16& in_value);
-
- DictionaryValue* SetDictionary(StringPiece path,
- std::unique_ptr<DictionaryValue> in_value);
-
- ListValue* SetList(StringPiece path, std::unique_ptr<ListValue> in_value);
-
-
-
- Value* SetWithoutPathExpansion(StringPiece key,
- std::unique_ptr<Value> in_value);
-
-
-
-
-
-
-
-
-
- bool Get(StringPiece path, const Value** out_value) const;
-
- bool Get(StringPiece path, Value** out_value);
-
-
-
-
-
- bool GetBoolean(StringPiece path, bool* out_value) const;
-
- bool GetInteger(StringPiece path, int* out_value) const;
-
-
-
- bool GetDouble(StringPiece path, double* out_value) const;
-
- bool GetString(StringPiece path, std::string* out_value) const;
-
- bool GetString(StringPiece path, string16* out_value) const;
-
- bool GetStringASCII(StringPiece path, std::string* out_value) const;
-
- bool GetBinary(StringPiece path, const Value** out_value) const;
-
- bool GetBinary(StringPiece path, Value** out_value);
-
- bool GetDictionary(StringPiece path,
- const DictionaryValue** out_value) const;
-
- bool GetDictionary(StringPiece path, DictionaryValue** out_value);
-
- bool GetList(StringPiece path, const ListValue** out_value) const;
-
- bool GetList(StringPiece path, ListValue** out_value);
-
-
-
- bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const;
-
- bool GetWithoutPathExpansion(StringPiece key, Value** out_value);
-
- bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const;
-
- bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const;
-
- bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const;
-
- bool GetStringWithoutPathExpansion(StringPiece key,
- std::string* out_value) const;
-
- bool GetStringWithoutPathExpansion(StringPiece key,
- string16* out_value) const;
-
- bool GetDictionaryWithoutPathExpansion(
- StringPiece key,
- const DictionaryValue** out_value) const;
-
- bool GetDictionaryWithoutPathExpansion(StringPiece key,
- DictionaryValue** out_value);
-
- bool GetListWithoutPathExpansion(StringPiece key,
- const ListValue** out_value) const;
-
- bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value);
-
-
-
-
-
-
-
-
- bool Remove(StringPiece path, std::unique_ptr<Value>* out_value);
-
-
-
- bool RemoveWithoutPathExpansion(StringPiece key,
- std::unique_ptr<Value>* out_value);
-
-
-
-
- bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value);
- using Value::RemovePath;
-
-
- std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const;
-
- void Swap(DictionaryValue* other);
-
-
-
- class BASE_EXPORT Iterator {
- public:
- explicit Iterator(const DictionaryValue& target);
- Iterator(const Iterator& other);
- ~Iterator();
- bool IsAtEnd() const { return it_ == target_.dict_.end(); }
- void Advance() { ++it_; }
- const std::string& key() const { return it_->first; }
- const Value& value() const { return *it_->second; }
- private:
- const DictionaryValue& target_;
- DictStorage::const_iterator it_;
- };
-
-
- iterator begin() { return dict_.begin(); }
- iterator end() { return dict_.end(); }
-
- const_iterator begin() const { return dict_.begin(); }
- const_iterator end() const { return dict_.end(); }
-
-
- DictionaryValue* DeepCopy() const;
-
-
- std::unique_ptr<DictionaryValue> CreateDeepCopy() const;
- };
- class BASE_EXPORT ListValue : public Value {
- public:
- using const_iterator = ListView::const_iterator;
- using iterator = ListView::iterator;
-
- static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
- ListValue();
- explicit ListValue(span<const Value> in_list);
- explicit ListValue(ListStorage&& in_list) noexcept;
-
-
- void Clear();
-
-
- size_t GetSize() const { return list_.size(); }
-
-
- bool empty() const { return list_.empty(); }
-
-
- void Reserve(size_t n);
-
-
-
-
-
-
- bool Set(size_t index, std::unique_ptr<Value> in_value);
-
-
-
-
-
- bool Get(size_t index, const Value** out_value) const;
- bool Get(size_t index, Value** out_value);
-
-
-
-
-
- bool GetBoolean(size_t index, bool* out_value) const;
-
- bool GetInteger(size_t index, int* out_value) const;
-
-
-
- bool GetDouble(size_t index, double* out_value) const;
-
- bool GetString(size_t index, std::string* out_value) const;
- bool GetString(size_t index, string16* out_value) const;
- bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
- bool GetDictionary(size_t index, DictionaryValue** out_value);
- using Value::GetList;
-
- bool GetList(size_t index, const ListValue** out_value) const;
- bool GetList(size_t index, ListValue** out_value);
-
-
-
-
-
-
- bool Remove(size_t index, std::unique_ptr<Value>* out_value);
-
-
-
-
- bool Remove(const Value& value, size_t* index);
-
-
-
-
-
- iterator Erase(iterator iter, std::unique_ptr<Value>* out_value);
- using Value::Append;
-
-
- void Append(std::unique_ptr<Value> in_value);
-
-
- void AppendBoolean(bool in_value);
- void AppendInteger(int in_value);
- void AppendDouble(double in_value);
- void AppendString(StringPiece in_value);
- void AppendString(const string16& in_value);
-
- void AppendStrings(const std::vector<std::string>& in_values);
- void AppendStrings(const std::vector<string16>& in_values);
-
-
-
- bool AppendIfNotPresent(std::unique_ptr<Value> in_value);
- using Value::Insert;
-
-
-
- bool Insert(size_t index, std::unique_ptr<Value> in_value);
-
-
-
-
- const_iterator Find(const Value& value) const;
-
-
- void Swap(ListValue* other);
-
-
- iterator begin() { return GetList().begin(); }
-
- iterator end() { return GetList().end(); }
-
- const_iterator begin() const { return GetList().begin(); }
-
- const_iterator end() const { return GetList().end(); }
-
-
- ListValue* DeepCopy() const;
-
-
- std::unique_ptr<ListValue> CreateDeepCopy() const;
- };
- class BASE_EXPORT ValueSerializer {
- public:
- virtual ~ValueSerializer();
- virtual bool Serialize(const Value& root) = 0;
- };
- class BASE_EXPORT ValueDeserializer {
- public:
- virtual ~ValueDeserializer();
-
-
-
-
-
-
- virtual std::unique_ptr<Value> Deserialize(int* error_code,
- std::string* error_str) = 0;
- };
- BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
- BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
- const DictionaryValue& value) {
- return out << static_cast<const Value&>(value);
- }
- BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
- const ListValue& value) {
- return out << static_cast<const Value&>(value);
- }
- BASE_EXPORT std::ostream& operator<<(std::ostream& out,
- const Value::Type& type);
- }
- #endif
|