1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003 |
- #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/strings/string16.h"
- #include "base/strings/string_piece.h"
- #include "base/value_iterators.h"
- #include "third_party/abseil-cpp/absl/types/variant.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>;
- 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() noexcept;
- Value(Value&& that) 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(const Value&) = delete;
- Value& operator=(const Value&) = delete;
- ~Value();
-
- static const char* GetTypeName(Type type);
-
- Type type() const { return static_cast<Type>(data_.index()); }
-
- 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) {
- 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);
-
-
-
-
-
-
-
-
-
-
-
- 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:
-
- const DictStorage& dict() const { return absl::get<DictStorage>(data_); }
- DictStorage& dict() { return absl::get<DictStorage>(data_); }
- const ListStorage& list() const { return absl::get<ListStorage>(data_); }
- ListStorage& list() { return absl::get<ListStorage>(data_); }
- private:
-
-
-
-
-
-
-
-
- using DoubleStorage = struct { alignas(4) char v[sizeof(double)]; };
-
- explicit Value(absl::monostate);
- explicit Value(DoubleStorage storage);
- friend class ValuesTest_SizeOfValue_Test;
- double AsDoubleInternal() const;
-
-
- Value* SetKeyInternal(StringPiece key, std::unique_ptr<Value>&& val_ptr);
- Value* SetPathInternal(StringPiece path, std::unique_ptr<Value>&& value_ptr);
- absl::variant<absl::monostate,
- bool,
- int,
- DoubleStorage,
- std::string,
- BlobStorage,
- DictStorage,
- ListStorage>
- data_;
- };
- 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_.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_message) = 0;
-
-
-
-
-
-
-
- enum ErrorCode {
- kErrorCodeNoError = 0,
-
-
-
- kErrorCodeInvalidFormat = 1,
-
-
- kErrorCodeFirstMetadataError = 1000,
- };
-
-
-
- static inline bool ErrorCodeIsDataError(int error_code) {
- return (kErrorCodeInvalidFormat <= error_code) &&
- (error_code < kErrorCodeFirstMetadataError);
- }
- };
- 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
|