| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file specifies a recursive data storage class called Value intended for | 5 // This file specifies a recursive data storage class called Value intended for |
| 6 // storing settings and other persistable data. | 6 // storing settings and other persistable data. |
| 7 // | 7 // |
| 8 // A Value represents something that can be stored in JSON or passed to/from | 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 | 9 // JavaScript. As such, it is NOT a generalized variant type, since only the |
| 10 // types supported by JavaScript/JSON are supported. | 10 // types supported by JavaScript/JSON are supported. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 class ListValue; | 41 class ListValue; |
| 42 class Value; | 42 class Value; |
| 43 | 43 |
| 44 // The Value class is the base class for Values. A Value can be instantiated | 44 // The Value class is the base class for Values. A Value can be instantiated |
| 45 // via the Create*Value() factory methods, or by directly creating instances of | 45 // via the Create*Value() factory methods, or by directly creating instances of |
| 46 // the subclasses. | 46 // the subclasses. |
| 47 // | 47 // |
| 48 // See the file-level comment above for more information. | 48 // See the file-level comment above for more information. |
| 49 class BASE_EXPORT Value { | 49 class BASE_EXPORT Value { |
| 50 public: | 50 public: |
| 51 using BlobStorage = std::vector<char>; |
| 51 using DictStorage = base::flat_map<std::string, std::unique_ptr<Value>>; | 52 using DictStorage = base::flat_map<std::string, std::unique_ptr<Value>>; |
| 52 using ListStorage = std::vector<Value>; | 53 using ListStorage = std::vector<Value>; |
| 53 | 54 |
| 54 enum class Type { | 55 enum class Type { |
| 55 NONE = 0, | 56 NONE = 0, |
| 56 BOOLEAN, | 57 BOOLEAN, |
| 57 INTEGER, | 58 INTEGER, |
| 58 DOUBLE, | 59 DOUBLE, |
| 59 STRING, | 60 STRING, |
| 60 BINARY, | 61 BINARY, |
| 61 DICTIONARY, | 62 DICTIONARY, |
| 62 LIST | 63 LIST |
| 63 // Note: Do not add more types. See the file-level comment above for why. | 64 // Note: Do not add more types. See the file-level comment above for why. |
| 64 }; | 65 }; |
| 65 | 66 |
| 66 // For situations where you want to keep ownership of your buffer, this | 67 // For situations where you want to keep ownership of your buffer, this |
| 67 // factory method creates a new BinaryValue by copying the contents of the | 68 // factory method creates a new BinaryValue by copying the contents of the |
| 68 // buffer that's passed in. | 69 // buffer that's passed in. |
| 69 // DEPRECATED, use MakeUnique<Value>(const std::vector<char>&) instead. | 70 // DEPRECATED, use MakeUnique<Value>(const BlobStorage&) instead. |
| 70 // TODO(crbug.com/646113): Delete this and migrate callsites. | 71 // TODO(crbug.com/646113): Delete this and migrate callsites. |
| 71 static std::unique_ptr<Value> CreateWithCopiedBuffer(const char* buffer, | 72 static std::unique_ptr<Value> CreateWithCopiedBuffer(const char* buffer, |
| 72 size_t size); | 73 size_t size); |
| 73 | 74 |
| 74 Value(const Value& that); | 75 Value(const Value& that); |
| 75 Value(Value&& that) noexcept; | 76 Value(Value&& that) noexcept; |
| 76 Value() noexcept; // A null value. | 77 Value() noexcept; // A null value. |
| 77 explicit Value(Type type); | 78 explicit Value(Type type); |
| 78 explicit Value(bool in_bool); | 79 explicit Value(bool in_bool); |
| 79 explicit Value(int in_int); | 80 explicit Value(int in_int); |
| 80 explicit Value(double in_double); | 81 explicit Value(double in_double); |
| 81 | 82 |
| 82 // Value(const char*) and Value(const char16*) are required despite | 83 // Value(const char*) and Value(const char16*) are required despite |
| 83 // Value(const std::string&) and Value(const string16&) because otherwise the | 84 // Value(const std::string&) and Value(const string16&) because otherwise the |
| 84 // compiler will choose the Value(bool) constructor for these arguments. | 85 // compiler will choose the Value(bool) constructor for these arguments. |
| 85 // Value(std::string&&) allow for efficient move construction. | 86 // Value(std::string&&) allow for efficient move construction. |
| 86 // Value(StringPiece) exists due to many callsites passing StringPieces as | 87 // Value(StringPiece) exists due to many callsites passing StringPieces as |
| 87 // arguments. | 88 // arguments. |
| 88 explicit Value(const char* in_string); | 89 explicit Value(const char* in_string); |
| 89 explicit Value(const std::string& in_string); | 90 explicit Value(const std::string& in_string); |
| 90 explicit Value(std::string&& in_string) noexcept; | 91 explicit Value(std::string&& in_string) noexcept; |
| 91 explicit Value(const char16* in_string); | 92 explicit Value(const char16* in_string); |
| 92 explicit Value(const string16& in_string); | 93 explicit Value(const string16& in_string); |
| 93 explicit Value(StringPiece in_string); | 94 explicit Value(StringPiece in_string); |
| 94 | 95 |
| 95 explicit Value(const std::vector<char>& in_blob); | 96 explicit Value(const BlobStorage& in_blob); |
| 96 explicit Value(std::vector<char>&& in_blob) noexcept; | 97 explicit Value(BlobStorage&& in_blob) noexcept; |
| 97 | 98 |
| 98 explicit Value(DictStorage&& in_dict) noexcept; | 99 explicit Value(DictStorage&& in_dict) noexcept; |
| 99 | 100 |
| 100 Value& operator=(const Value& that); | 101 Value& operator=(const Value& that); |
| 101 Value& operator=(Value&& that) noexcept; | 102 Value& operator=(Value&& that) noexcept; |
| 102 | 103 |
| 103 ~Value(); | 104 ~Value(); |
| 104 | 105 |
| 105 // Returns the name for a given |type|. | 106 // Returns the name for a given |type|. |
| 106 static const char* GetTypeName(Type type); | 107 static const char* GetTypeName(Type type); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 121 bool is_string() const { return type() == Type::STRING; } | 122 bool is_string() const { return type() == Type::STRING; } |
| 122 bool is_blob() const { return type() == Type::BINARY; } | 123 bool is_blob() const { return type() == Type::BINARY; } |
| 123 bool is_dict() const { return type() == Type::DICTIONARY; } | 124 bool is_dict() const { return type() == Type::DICTIONARY; } |
| 124 bool is_list() const { return type() == Type::LIST; } | 125 bool is_list() const { return type() == Type::LIST; } |
| 125 | 126 |
| 126 // These will all fatally assert if the type doesn't match. | 127 // These will all fatally assert if the type doesn't match. |
| 127 bool GetBool() const; | 128 bool GetBool() const; |
| 128 int GetInt() const; | 129 int GetInt() const; |
| 129 double GetDouble() const; // Implicitly converts from int if necessary. | 130 double GetDouble() const; // Implicitly converts from int if necessary. |
| 130 const std::string& GetString() const; | 131 const std::string& GetString() const; |
| 131 const std::vector<char>& GetBlob() const; | 132 const BlobStorage& GetBlob() const; |
| 132 | 133 |
| 133 size_t GetSize() const; // DEPRECATED, use GetBlob().size() instead. | 134 size_t GetSize() const; // DEPRECATED, use GetBlob().size() instead. |
| 134 const char* GetBuffer() const; // DEPRECATED, use GetBlob().data() instead. | 135 const char* GetBuffer() const; // DEPRECATED, use GetBlob().data() instead. |
| 135 | 136 |
| 136 // These methods allow the convenient retrieval of the contents of the Value. | 137 // These methods allow the convenient retrieval of the contents of the Value. |
| 137 // If the current object can be converted into the given type, the value is | 138 // If the current object can be converted into the given type, the value is |
| 138 // returned through the |out_value| parameter and true is returned; | 139 // returned through the |out_value| parameter and true is returned; |
| 139 // otherwise, false is returned and |out_value| is unchanged. | 140 // otherwise, false is returned and |out_value| is unchanged. |
| 140 bool GetAsBoolean(bool* out_value) const; | 141 bool GetAsBoolean(bool* out_value) const; |
| 141 bool GetAsInteger(int* out_value) const; | 142 bool GetAsInteger(int* out_value) const; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 protected: | 187 protected: |
| 187 // TODO(crbug.com/646113): Make these private once DictionaryValue and | 188 // TODO(crbug.com/646113): Make these private once DictionaryValue and |
| 188 // ListValue are properly inlined. | 189 // ListValue are properly inlined. |
| 189 Type type_; | 190 Type type_; |
| 190 | 191 |
| 191 union { | 192 union { |
| 192 bool bool_value_; | 193 bool bool_value_; |
| 193 int int_value_; | 194 int int_value_; |
| 194 double double_value_; | 195 double double_value_; |
| 195 ManualConstructor<std::string> string_value_; | 196 ManualConstructor<std::string> string_value_; |
| 196 ManualConstructor<std::vector<char>> binary_value_; | 197 ManualConstructor<BlobStorage> binary_value_; |
| 197 ManualConstructor<DictStorage> dict_; | 198 ManualConstructor<DictStorage> dict_; |
| 198 ManualConstructor<ListStorage> list_; | 199 ManualConstructor<ListStorage> list_; |
| 199 }; | 200 }; |
| 200 | 201 |
| 201 private: | 202 private: |
| 202 void InternalCopyFundamentalValue(const Value& that); | 203 void InternalCopyFundamentalValue(const Value& that); |
| 203 void InternalCopyConstructFrom(const Value& that); | 204 void InternalCopyConstructFrom(const Value& that); |
| 204 void InternalMoveConstructFrom(Value&& that); | 205 void InternalMoveConstructFrom(Value&& that); |
| 205 void InternalCopyAssignFromSameType(const Value& that); | 206 void InternalCopyAssignFromSameType(const Value& that); |
| 206 void InternalCleanup(); | 207 void InternalCleanup(); |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 return out << static_cast<const Value&>(value); | 532 return out << static_cast<const Value&>(value); |
| 532 } | 533 } |
| 533 | 534 |
| 534 // Stream operator so that enum class Types can be used in log statements. | 535 // Stream operator so that enum class Types can be used in log statements. |
| 535 BASE_EXPORT std::ostream& operator<<(std::ostream& out, | 536 BASE_EXPORT std::ostream& operator<<(std::ostream& out, |
| 536 const Value::Type& type); | 537 const Value::Type& type); |
| 537 | 538 |
| 538 } // namespace base | 539 } // namespace base |
| 539 | 540 |
| 540 #endif // BASE_VALUES_H_ | 541 #endif // BASE_VALUES_H_ |
| OLD | NEW |