OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 setting and other persistable data. It includes the ability to | 6 // storing setting and other persistable data. It includes the ability to |
7 // specify (recursive) lists and dictionaries, so it's fairly expressive. | 7 // specify (recursive) lists and dictionaries, so it's fairly expressive. |
8 // However, the API is optimized for the common case, namely storing a | 8 // However, the API is optimized for the common case, namely storing a |
9 // hierarchical tree of simple values. Given a DictionaryValue root, you can | 9 // hierarchical tree of simple values. Given a DictionaryValue root, you can |
10 // easily do things like: | 10 // easily do things like: |
(...skipping 22 matching lines...) Expand all Loading... |
33 #include "base/string16.h" | 33 #include "base/string16.h" |
34 | 34 |
35 // This file declares "using base::Value", etc. at the bottom, so that | 35 // This file declares "using base::Value", etc. at the bottom, so that |
36 // current code can use these classes without the base namespace. In | 36 // current code can use these classes without the base namespace. In |
37 // new code, please always use base::Value, etc. or add your own | 37 // new code, please always use base::Value, etc. or add your own |
38 // "using" declaration. | 38 // "using" declaration. |
39 // http://crbug.com/88666 | 39 // http://crbug.com/88666 |
40 namespace base { | 40 namespace base { |
41 | 41 |
42 class BinaryValue; | 42 class BinaryValue; |
| 43 class BooleanValue; |
43 class DictionaryValue; | 44 class DictionaryValue; |
44 class FundamentalValue; | |
45 class ListValue; | 45 class ListValue; |
46 class StringValue; | 46 class StringValue; |
47 class Value; | 47 class Value; |
48 | 48 |
49 typedef std::vector<Value*> ValueVector; | 49 typedef std::vector<Value*> ValueVector; |
50 typedef std::map<std::string, Value*> ValueMap; | 50 typedef std::map<std::string, Value*> ValueMap; |
51 | 51 |
52 // The Value class is the base class for Values. A Value can be instantiated | 52 // The Value class is the base class for Values. |
53 // via the Create*Value() factory methods, or by directly creating instances of | |
54 // the subclasses. | |
55 class BASE_EXPORT Value { | 53 class BASE_EXPORT Value { |
56 public: | 54 public: |
57 enum Type { | 55 enum Type { |
58 TYPE_NULL = 0, | 56 TYPE_NULL = 0, |
59 TYPE_BOOLEAN, | 57 TYPE_BOOLEAN, |
60 TYPE_INTEGER, | 58 TYPE_INTEGER, |
61 TYPE_DOUBLE, | 59 TYPE_DOUBLE, |
62 TYPE_STRING, | 60 TYPE_STRING, |
63 TYPE_BINARY, | 61 TYPE_BINARY, |
64 TYPE_DICTIONARY, | 62 TYPE_DICTIONARY, |
65 TYPE_LIST | 63 TYPE_LIST |
66 }; | 64 }; |
67 | 65 |
68 virtual ~Value(); | 66 virtual ~Value(); |
69 | 67 |
70 // Convenience methods for creating Value objects for various | |
71 // kinds of values without thinking about which class implements them. | |
72 // These can always be expected to return a valid Value*. | |
73 static Value* CreateNullValue(); | |
74 static FundamentalValue* CreateBooleanValue(bool in_value); | |
75 static FundamentalValue* CreateIntegerValue(int in_value); | |
76 static FundamentalValue* CreateDoubleValue(double in_value); | |
77 static StringValue* CreateStringValue(const std::string& in_value); | |
78 static StringValue* CreateStringValue(const string16& in_value); | |
79 | |
80 // Returns the type of the value stored by the current Value object. | 68 // Returns the type of the value stored by the current Value object. |
81 // Each type will be implemented by only one subclass of Value, so it's | 69 // Each type will be implemented by only one subclass of Value, so it's |
82 // safe to use the Type to determine whether you can cast from | 70 // safe to use the Type to determine whether you can cast from |
83 // Value* to (Implementing Class)*. Also, a Value object never changes | 71 // Value* to (Implementing Class)*. Also, a Value object never changes |
84 // its type after construction. | 72 // its type after construction. |
85 Type GetType() const { return type_; } | 73 Type GetType() const { return type_; } |
86 | 74 |
87 // Returns true if the current object represents a given type. | 75 // Returns true if the current object represents a given type. |
88 bool IsType(Type type) const { return type == type_; } | 76 bool IsType(Type type) const { return type == type_; } |
89 | 77 |
(...skipping 13 matching lines...) Expand all Loading... |
103 // to the copy. The caller gets ownership of the copy, of course. | 91 // to the copy. The caller gets ownership of the copy, of course. |
104 // | 92 // |
105 // Subclasses return their own type directly in their overrides; | 93 // Subclasses return their own type directly in their overrides; |
106 // this works because C++ supports covariant return types. | 94 // this works because C++ supports covariant return types. |
107 virtual Value* DeepCopy() const; | 95 virtual Value* DeepCopy() const; |
108 | 96 |
109 // Compares if two Value objects have equal contents. | 97 // Compares if two Value objects have equal contents. |
110 virtual bool Equals(const Value* other) const; | 98 virtual bool Equals(const Value* other) const; |
111 | 99 |
112 // Compares if two Value objects have equal contents. Can handle NULLs. | 100 // Compares if two Value objects have equal contents. Can handle NULLs. |
113 // NULLs are considered equal but different from Value::CreateNullValue(). | 101 // NULLs are considered equal but different from NullValue. |
114 static bool Equals(const Value* a, const Value* b); | 102 static bool Equals(const Value* a, const Value* b); |
115 | 103 |
116 protected: | 104 protected: |
117 // This isn't safe for end-users (they should use the Create*Value() | 105 // This isn't safe for end-users (they should use the *Value::New()), |
118 // static methods above), but it's useful for subclasses. | 106 // but it's useful for subclasses. |
119 explicit Value(Type type); | 107 explicit Value(Type type); |
120 | 108 |
121 private: | 109 private: |
122 Value(); | 110 Value(); |
123 | 111 |
124 Type type_; | 112 Type type_; |
125 | 113 |
126 DISALLOW_COPY_AND_ASSIGN(Value); | 114 DISALLOW_COPY_AND_ASSIGN(Value); |
127 }; | 115 }; |
128 | 116 |
129 // FundamentalValue represents the simple fundamental types of values. | 117 BASE_EXPORT Value* NullValue(); |
130 class BASE_EXPORT FundamentalValue : public Value { | 118 BASE_EXPORT BooleanValue* TrueValue(); |
| 119 BASE_EXPORT BooleanValue* FalseValue(); |
| 120 |
| 121 class BASE_EXPORT BooleanValue : public Value { |
131 public: | 122 public: |
132 explicit FundamentalValue(bool in_value); | 123 explicit BooleanValue(bool value); |
133 explicit FundamentalValue(int in_value); | 124 virtual ~BooleanValue(); |
134 explicit FundamentalValue(double in_value); | 125 |
135 virtual ~FundamentalValue(); | 126 static BooleanValue* New(bool value); |
136 | 127 |
137 // Overridden from Value: | 128 // Overridden from Value: |
138 virtual bool GetAsBoolean(bool* out_value) const OVERRIDE; | 129 virtual bool GetAsBoolean(bool* out_value) const OVERRIDE; |
| 130 virtual BooleanValue* DeepCopy() const OVERRIDE; |
| 131 virtual bool Equals(const Value* other) const OVERRIDE; |
| 132 |
| 133 private: |
| 134 bool value_; |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(BooleanValue); |
| 137 }; |
| 138 |
| 139 class BASE_EXPORT NumberValue : public Value { |
| 140 public: |
| 141 explicit NumberValue(int value); |
| 142 explicit NumberValue(double value); |
| 143 virtual ~NumberValue(); |
| 144 |
| 145 static NumberValue* New(int value); |
| 146 static NumberValue* New(double value); |
| 147 |
| 148 // Overridden from Value: |
139 virtual bool GetAsInteger(int* out_value) const OVERRIDE; | 149 virtual bool GetAsInteger(int* out_value) const OVERRIDE; |
140 virtual bool GetAsDouble(double* out_value) const OVERRIDE; | 150 virtual bool GetAsDouble(double* out_value) const OVERRIDE; |
141 virtual FundamentalValue* DeepCopy() const OVERRIDE; | 151 virtual NumberValue* DeepCopy() const OVERRIDE; |
142 virtual bool Equals(const Value* other) const OVERRIDE; | 152 virtual bool Equals(const Value* other) const OVERRIDE; |
143 | 153 |
144 private: | 154 private: |
145 union { | 155 union { |
146 bool boolean_value_; | |
147 int integer_value_; | 156 int integer_value_; |
148 double double_value_; | 157 double double_value_; |
149 }; | 158 }; |
150 | 159 |
151 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); | 160 DISALLOW_COPY_AND_ASSIGN(NumberValue); |
152 }; | 161 }; |
153 | 162 |
154 class BASE_EXPORT StringValue : public Value { | 163 class BASE_EXPORT StringValue : public Value { |
155 public: | 164 public: |
156 // Initializes a StringValue with a UTF-8 narrow character string. | 165 // Initializes a StringValue with a UTF-8 narrow character string. |
157 explicit StringValue(const std::string& in_value); | 166 explicit StringValue(const std::string& in_value); |
158 | 167 |
159 // Initializes a StringValue with a string16. | 168 // Initializes a StringValue with a string16. |
160 explicit StringValue(const string16& in_value); | 169 explicit StringValue(const string16& in_value); |
161 | 170 |
162 virtual ~StringValue(); | 171 virtual ~StringValue(); |
163 | 172 |
| 173 static StringValue* New(const std::string& value); |
| 174 static StringValue* New(const string16& value); |
| 175 |
164 // Overridden from Value: | 176 // Overridden from Value: |
165 virtual bool GetAsString(std::string* out_value) const OVERRIDE; | 177 virtual bool GetAsString(std::string* out_value) const OVERRIDE; |
166 virtual bool GetAsString(string16* out_value) const OVERRIDE; | 178 virtual bool GetAsString(string16* out_value) const OVERRIDE; |
167 virtual StringValue* DeepCopy() const OVERRIDE; | 179 virtual StringValue* DeepCopy() const OVERRIDE; |
168 virtual bool Equals(const Value* other) const OVERRIDE; | 180 virtual bool Equals(const Value* other) const OVERRIDE; |
169 | 181 |
170 private: | 182 private: |
171 std::string value_; | 183 std::string value_; |
172 | 184 |
173 DISALLOW_COPY_AND_ASSIGN(StringValue); | 185 DISALLOW_COPY_AND_ASSIGN(StringValue); |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 | 473 |
462 } // namespace base | 474 } // namespace base |
463 | 475 |
464 // http://crbug.com/88666 | 476 // http://crbug.com/88666 |
465 using base::DictionaryValue; | 477 using base::DictionaryValue; |
466 using base::ListValue; | 478 using base::ListValue; |
467 using base::StringValue; | 479 using base::StringValue; |
468 using base::Value; | 480 using base::Value; |
469 | 481 |
470 #endif // BASE_VALUES_H_ | 482 #endif // BASE_VALUES_H_ |
OLD | NEW |