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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 BOOLEAN, | 54 BOOLEAN, |
55 INTEGER, | 55 INTEGER, |
56 DOUBLE, | 56 DOUBLE, |
57 STRING, | 57 STRING, |
58 BINARY, | 58 BINARY, |
59 DICTIONARY, | 59 DICTIONARY, |
60 LIST | 60 LIST |
61 // Note: Do not add more types. See the file-level comment above for why. | 61 // Note: Do not add more types. See the file-level comment above for why. |
62 }; | 62 }; |
63 | 63 |
64 Value(); // A null value. | |
65 | |
64 virtual ~Value(); | 66 virtual ~Value(); |
65 | 67 |
66 static std::unique_ptr<Value> CreateNullValue(); | 68 static std::unique_ptr<Value> CreateNullValue(); |
67 | 69 |
68 // Returns the name for a given |type|. | 70 // Returns the name for a given |type|. |
69 static const char* GetTypeName(Type type); | 71 static const char* GetTypeName(Type type); |
70 | 72 |
71 // Returns the type of the value stored by the current Value object. | 73 // Returns the type of the value stored by the current Value object. |
72 // Each type will be implemented by only one subclass of Value, so it's | 74 // Each type will be implemented by only one subclass of Value, so it's |
73 // safe to use the Type to determine whether you can cast from | 75 // safe to use the Type to determine whether you can cast from |
74 // Value* to (Implementing Class)*. Also, a Value object never changes | 76 // Value* to (Implementing Class)*. Also, a Value object never changes |
75 // its type after construction. | 77 // its type after construction. |
76 Type GetType() const { return type_; } | 78 Type GetType() const { return type_; } // DEPRECATED, use type(). |
79 Type type() const { return type_; } | |
77 | 80 |
78 // Returns true if the current object represents a given type. | 81 // Returns true if the current object represents a given type. |
79 bool IsType(Type type) const { return type == type_; } | 82 bool IsType(Type type) const { return type == type_; } |
83 bool is_bool() const { return type() == Type::BOOLEAN; } | |
84 bool is_int() const { return type() == Type::INTEGER; } | |
85 bool is_double() const { return type() == Type::DOUBLE; } | |
86 bool is_string() const { return type() == Type::STRING; } | |
87 bool is_blob() const { return type() == Type::BINARY; } | |
88 bool is_dict() const { return type() == Type::DICTIONARY; } | |
89 bool is_list() const { return type() == Type::LIST; } | |
90 | |
91 // These will all fatally assert if the type doesn't match. | |
92 bool GetBool() const; | |
93 int GetInt() const; | |
94 double GetDouble() const; // Implicitly converts from int if necessary. | |
80 | 95 |
81 // These methods allow the convenient retrieval of the contents of the Value. | 96 // These methods allow the convenient retrieval of the contents of the Value. |
82 // If the current object can be converted into the given type, the value is | 97 // If the current object can be converted into the given type, the value is |
83 // returned through the |out_value| parameter and true is returned; | 98 // returned through the |out_value| parameter and true is returned; |
84 // otherwise, false is returned and |out_value| is unchanged. | 99 // otherwise, false is returned and |out_value| is unchanged. |
85 virtual bool GetAsBoolean(bool* out_value) const; | 100 virtual bool GetAsBoolean(bool* out_value) const; |
86 virtual bool GetAsInteger(int* out_value) const; | 101 virtual bool GetAsInteger(int* out_value) const; |
87 virtual bool GetAsDouble(double* out_value) const; | 102 virtual bool GetAsDouble(double* out_value) const; |
88 virtual bool GetAsString(std::string* out_value) const; | 103 virtual bool GetAsString(std::string* out_value) const; |
89 virtual bool GetAsString(string16* out_value) const; | 104 virtual bool GetAsString(string16* out_value) const; |
(...skipping 20 matching lines...) Expand all Loading... | |
110 virtual bool Equals(const Value* other) const; | 125 virtual bool Equals(const Value* other) const; |
111 | 126 |
112 // Compares if two Value objects have equal contents. Can handle NULLs. | 127 // Compares if two Value objects have equal contents. Can handle NULLs. |
113 // NULLs are considered equal but different from Value::CreateNullValue(). | 128 // NULLs are considered equal but different from Value::CreateNullValue(). |
114 static bool Equals(const Value* a, const Value* b); | 129 static bool Equals(const Value* a, const Value* b); |
115 | 130 |
116 protected: | 131 protected: |
117 // These aren't safe for end-users, but they are useful for subclasses. | 132 // These aren't safe for end-users, but they are useful for subclasses. |
118 explicit Value(Type type); | 133 explicit Value(Type type); |
119 Value(const Value& that); | 134 Value(const Value& that); |
135 Value(Value&& that); | |
120 Value& operator=(const Value& that); | 136 Value& operator=(const Value& that); |
137 Value& operator=(Value&& that); | |
138 | |
139 // TODO(brettw) move to public when FundamentalValue is gone. | |
140 Value(bool in_bool); | |
141 Value(int in_int); | |
142 Value(double in_double); | |
jdoerrie
2016/12/08 10:13:08
These constructors probably should be explicit.
vabr (Chromium)
2016/12/08 10:49:48
Done.
| |
121 | 143 |
122 private: | 144 private: |
145 void InternalCopyFrom(const Value& that); | |
146 | |
123 Type type_; | 147 Type type_; |
148 | |
149 union { | |
150 bool bool_value_; | |
151 int int_value_; | |
152 double double_value_; | |
153 }; | |
124 }; | 154 }; |
125 | 155 |
126 // FundamentalValue represents the simple fundamental types of values. | 156 // FundamentalValue represents the simple fundamental types of values. |
157 // TODO(brettw) remove when callers are updated to use raw Value. | |
127 class BASE_EXPORT FundamentalValue : public Value { | 158 class BASE_EXPORT FundamentalValue : public Value { |
128 public: | 159 public: |
129 explicit FundamentalValue(bool in_value); | 160 explicit FundamentalValue(bool in_value); |
130 explicit FundamentalValue(int in_value); | 161 explicit FundamentalValue(int in_value); |
131 explicit FundamentalValue(double in_value); | 162 explicit FundamentalValue(double in_value); |
132 ~FundamentalValue() override; | 163 ~FundamentalValue() override; |
133 | |
134 // Overridden from Value: | |
135 bool GetAsBoolean(bool* out_value) const override; | |
136 bool GetAsInteger(int* out_value) const override; | |
137 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as | |
138 // doubles. | |
139 bool GetAsDouble(double* out_value) const override; | |
140 FundamentalValue* DeepCopy() const override; | |
141 bool Equals(const Value* other) const override; | |
142 | |
143 private: | |
144 union { | |
145 bool boolean_value_; | |
146 int integer_value_; | |
147 double double_value_; | |
148 }; | |
149 }; | 164 }; |
150 | 165 |
151 class BASE_EXPORT StringValue : public Value { | 166 class BASE_EXPORT StringValue : public Value { |
152 public: | 167 public: |
153 // Initializes a StringValue with a UTF-8 narrow character string. | 168 // Initializes a StringValue with a UTF-8 narrow character string. |
154 explicit StringValue(StringPiece in_value); | 169 explicit StringValue(StringPiece in_value); |
155 | 170 |
156 // Initializes a StringValue with a string16. | 171 // Initializes a StringValue with a string16. |
157 explicit StringValue(const string16& in_value); | 172 explicit StringValue(const string16& in_value); |
158 | 173 |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
562 return out << static_cast<const Value&>(value); | 577 return out << static_cast<const Value&>(value); |
563 } | 578 } |
564 | 579 |
565 // Stream operator so that enum class Types can be used in log statements. | 580 // Stream operator so that enum class Types can be used in log statements. |
566 BASE_EXPORT std::ostream& operator<<(std::ostream& out, | 581 BASE_EXPORT std::ostream& operator<<(std::ostream& out, |
567 const Value::Type& type); | 582 const Value::Type& type); |
568 | 583 |
569 } // namespace base | 584 } // namespace base |
570 | 585 |
571 #endif // BASE_VALUES_H_ | 586 #endif // BASE_VALUES_H_ |
OLD | NEW |