Chromium Code Reviews| 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 #include "base/values.h" | 5 #include "base/values.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cmath> | 10 #include <cmath> |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 } | 81 } |
| 82 | 82 |
| 83 Value::Value(double in_double) : type_(TYPE_DOUBLE), double_value_(in_double) { | 83 Value::Value(double in_double) : type_(TYPE_DOUBLE), double_value_(in_double) { |
| 84 if (!std::isfinite(double_value_)) { | 84 if (!std::isfinite(double_value_)) { |
| 85 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) " | 85 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) " |
| 86 << "values cannot be represented in JSON"; | 86 << "values cannot be represented in JSON"; |
| 87 double_value_ = 0.0; | 87 double_value_ = 0.0; |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 | 90 |
| 91 Value::Value(const char* in_string) : type_(TYPE_STRING) { | |
| 92 string_value_.Init(in_string); | |
| 93 } | |
| 94 | |
| 95 Value::Value(StringPiece in_string) : type_(TYPE_STRING) { | |
| 96 string_value_.Init(in_string.as_string()); | |
| 97 DCHECK(IsStringUTF8(in_string)); | |
| 98 } | |
| 99 | |
| 100 Value::Value(const string16& in_string) : type_(TYPE_STRING) { | |
| 101 string_value_.Init(UTF16ToUTF8(in_string)); | |
| 102 } | |
| 103 | |
| 91 Value::~Value() { | 104 Value::~Value() { |
| 92 } | 105 } |
| 93 | 106 |
| 94 // static | 107 // static |
| 95 std::unique_ptr<Value> Value::CreateNullValue() { | 108 std::unique_ptr<Value> Value::CreateNullValue() { |
| 96 return WrapUnique(new Value(TYPE_NULL)); | 109 return WrapUnique(new Value(TYPE_NULL)); |
| 97 } | 110 } |
| 98 | 111 |
| 99 // static | 112 // static |
| 100 const char* Value::GetTypeName(Value::Type type) { | 113 const char* Value::GetTypeName(Value::Type type) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 115 | 128 |
| 116 double Value::GetDouble() const { | 129 double Value::GetDouble() const { |
| 117 if (is_double()) | 130 if (is_double()) |
| 118 return double_value_; | 131 return double_value_; |
| 119 if (is_int()) | 132 if (is_int()) |
| 120 return int_value_; | 133 return int_value_; |
| 121 CHECK(false); | 134 CHECK(false); |
| 122 return 0.0; | 135 return 0.0; |
| 123 } | 136 } |
| 124 | 137 |
| 138 std::string* Value::GetString() { | |
| 139 CHECK(is_string()); | |
| 140 return string_value_.get(); | |
| 141 } | |
| 142 | |
| 143 const std::string& Value::GetString() const { | |
| 144 CHECK(is_string()); | |
| 145 return *string_value_; | |
| 146 } | |
| 147 | |
| 125 bool Value::GetAsBinary(const BinaryValue** out_value) const { | 148 bool Value::GetAsBinary(const BinaryValue** out_value) const { |
| 126 return false; | 149 return false; |
| 127 } | 150 } |
| 128 | 151 |
| 129 bool Value::GetAsBoolean(bool* out_value) const { | 152 bool Value::GetAsBoolean(bool* out_value) const { |
| 130 if (out_value && is_bool()) { | 153 if (out_value && is_bool()) { |
| 131 *out_value = bool_value_; | 154 *out_value = bool_value_; |
| 132 return true; | 155 return true; |
| 133 } | 156 } |
| 134 return is_bool(); | 157 return is_bool(); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 148 return true; | 171 return true; |
| 149 } else if (out_value && is_int()) { | 172 } else if (out_value && is_int()) { |
| 150 // Allow promotion from int to double. | 173 // Allow promotion from int to double. |
| 151 *out_value = int_value_; | 174 *out_value = int_value_; |
| 152 return true; | 175 return true; |
| 153 } | 176 } |
| 154 return is_double() || is_int(); | 177 return is_double() || is_int(); |
| 155 } | 178 } |
| 156 | 179 |
| 157 bool Value::GetAsString(std::string* out_value) const { | 180 bool Value::GetAsString(std::string* out_value) const { |
| 158 return false; | 181 if (out_value && is_string()) { |
| 182 *out_value = *string_value_; | |
| 183 return true; | |
| 184 } | |
| 185 return is_string(); | |
| 159 } | 186 } |
| 160 | 187 |
| 161 bool Value::GetAsString(string16* out_value) const { | 188 bool Value::GetAsString(string16* out_value) const { |
| 162 return false; | 189 if (out_value && is_string()) { |
| 190 *out_value = UTF8ToUTF16(*string_value_); | |
| 191 return true; | |
| 192 } | |
| 193 return is_string(); | |
| 163 } | 194 } |
| 164 | 195 |
| 165 bool Value::GetAsString(const StringValue** out_value) const { | 196 bool Value::GetAsString(const StringValue** out_value) const { |
| 166 return false; | 197 if (out_value && is_string()) { |
| 198 *out_value = static_cast<const StringValue*>(this); | |
| 199 return true; | |
| 200 } | |
| 201 return is_string(); | |
| 167 } | 202 } |
| 168 | 203 |
| 169 bool Value::GetAsList(ListValue** out_value) { | 204 bool Value::GetAsList(ListValue** out_value) { |
| 170 return false; | 205 return false; |
| 171 } | 206 } |
| 172 | 207 |
| 173 bool Value::GetAsList(const ListValue** out_value) const { | 208 bool Value::GetAsList(const ListValue** out_value) const { |
| 174 return false; | 209 return false; |
| 175 } | 210 } |
| 176 | 211 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 190 return CreateNullValue().release(); | 225 return CreateNullValue().release(); |
| 191 | 226 |
| 192 // For now, make FundamentalValues for backward-compatibility. Convert to | 227 // For now, make FundamentalValues for backward-compatibility. Convert to |
| 193 // Value when that code is deleted. | 228 // Value when that code is deleted. |
| 194 case TYPE_BOOLEAN: | 229 case TYPE_BOOLEAN: |
| 195 return new FundamentalValue(bool_value_); | 230 return new FundamentalValue(bool_value_); |
| 196 case TYPE_INTEGER: | 231 case TYPE_INTEGER: |
| 197 return new FundamentalValue(int_value_); | 232 return new FundamentalValue(int_value_); |
| 198 case TYPE_DOUBLE: | 233 case TYPE_DOUBLE: |
| 199 return new FundamentalValue(double_value_); | 234 return new FundamentalValue(double_value_); |
| 235 // For now, make StringValues for backward-compatibility. Convert to | |
| 236 // Value when that code is deleted. | |
| 237 case TYPE_STRING: | |
| 238 return new StringValue(*string_value_); | |
| 200 | 239 |
| 201 default: | 240 default: |
| 202 // All other types should be handled by subclasses. | 241 // All other types should be handled by subclasses. |
| 203 NOTREACHED(); | 242 NOTREACHED(); |
| 204 return nullptr; | 243 return nullptr; |
| 205 } | 244 } |
| 206 } | 245 } |
| 207 | 246 |
| 208 std::unique_ptr<Value> Value::CreateDeepCopy() const { | 247 std::unique_ptr<Value> Value::CreateDeepCopy() const { |
| 209 return WrapUnique(DeepCopy()); | 248 return WrapUnique(DeepCopy()); |
| 210 } | 249 } |
| 211 | 250 |
| 212 bool Value::Equals(const Value* other) const { | 251 bool Value::Equals(const Value* other) const { |
| 213 if (other->type() != type()) | 252 if (other->type() != type()) |
| 214 return false; | 253 return false; |
| 215 | 254 |
| 216 switch (type()) { | 255 switch (type()) { |
| 217 case TYPE_NULL: | 256 case TYPE_NULL: |
| 218 return true; | 257 return true; |
| 219 case TYPE_BOOLEAN: | 258 case TYPE_BOOLEAN: |
| 220 return bool_value_ == other->bool_value_; | 259 return bool_value_ == other->bool_value_; |
| 221 case TYPE_INTEGER: | 260 case TYPE_INTEGER: |
| 222 return int_value_ == other->int_value_; | 261 return int_value_ == other->int_value_; |
| 223 case TYPE_DOUBLE: | 262 case TYPE_DOUBLE: |
| 224 return double_value_ == other->double_value_; | 263 return double_value_ == other->double_value_; |
| 264 // TODO(jdoerrie): Simplify this once JSONStringValue is migrated. | |
| 265 case TYPE_STRING: { | |
| 266 std::string lhs, rhs; | |
| 267 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; | |
| 268 } | |
| 225 default: | 269 default: |
| 226 // This method should only be getting called for the above types -- all | 270 // This method should only be getting called for the above types -- all |
| 227 // subclasses need to provide their own implementation;. | 271 // subclasses need to provide their own implementation;. |
| 228 NOTREACHED(); | 272 NOTREACHED(); |
| 229 return false; | 273 return false; |
| 230 } | 274 } |
| 231 } | 275 } |
| 232 | 276 |
| 233 // static | 277 // static |
| 234 bool Value::Equals(const Value* a, const Value* b) { | 278 bool Value::Equals(const Value* a, const Value* b) { |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 258 | 302 |
| 259 FundamentalValue::FundamentalValue(double in_value) | 303 FundamentalValue::FundamentalValue(double in_value) |
| 260 : Value(in_value) { | 304 : Value(in_value) { |
| 261 } | 305 } |
| 262 | 306 |
| 263 FundamentalValue::~FundamentalValue() { | 307 FundamentalValue::~FundamentalValue() { |
| 264 } | 308 } |
| 265 | 309 |
| 266 ///////////////////// StringValue //////////////////// | 310 ///////////////////// StringValue //////////////////// |
| 267 | 311 |
| 268 StringValue::StringValue(StringPiece in_value) | 312 StringValue::StringValue(StringPiece in_value) : Value(in_value) {} |
| 269 : Value(TYPE_STRING), value_(in_value.as_string()) { | |
| 270 DCHECK(IsStringUTF8(in_value)); | |
| 271 } | |
| 272 | 313 |
| 273 StringValue::StringValue(const string16& in_value) | 314 StringValue::StringValue(const string16& in_value) : Value(in_value) {} |
| 274 : Value(TYPE_STRING), | |
| 275 value_(UTF16ToUTF8(in_value)) { | |
| 276 } | |
| 277 | 315 |
| 278 StringValue::~StringValue() { | 316 StringValue::~StringValue() { |
| 279 } | 317 } |
|
vabr (Chromium)
2016/12/07 10:17:27
Should this (or ~Value) run string_value_.Destroy(
jdoerrie
2016/12/08 09:59:34
Done. Implemented it as part of InternalCleanup. C
| |
| 280 | 318 |
| 281 std::string* StringValue::GetString() { | |
| 282 return &value_; | |
| 283 } | |
| 284 | |
| 285 const std::string& StringValue::GetString() const { | |
| 286 return value_; | |
| 287 } | |
| 288 | |
| 289 bool StringValue::GetAsString(std::string* out_value) const { | |
| 290 if (out_value) | |
| 291 *out_value = value_; | |
| 292 return true; | |
| 293 } | |
| 294 | |
| 295 bool StringValue::GetAsString(string16* out_value) const { | |
| 296 if (out_value) | |
| 297 *out_value = UTF8ToUTF16(value_); | |
| 298 return true; | |
| 299 } | |
| 300 | |
| 301 bool StringValue::GetAsString(const StringValue** out_value) const { | |
| 302 if (out_value) | |
| 303 *out_value = this; | |
| 304 return true; | |
| 305 } | |
| 306 | |
| 307 StringValue* StringValue::DeepCopy() const { | |
| 308 return new StringValue(value_); | |
| 309 } | |
| 310 | |
| 311 bool StringValue::Equals(const Value* other) const { | |
| 312 if (other->GetType() != GetType()) | |
| 313 return false; | |
| 314 std::string lhs, rhs; | |
| 315 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; | |
| 316 } | |
| 317 | |
| 318 ///////////////////// BinaryValue //////////////////// | 319 ///////////////////// BinaryValue //////////////////// |
| 319 | 320 |
| 320 BinaryValue::BinaryValue() | 321 BinaryValue::BinaryValue() |
| 321 : Value(TYPE_BINARY), | 322 : Value(TYPE_BINARY), |
| 322 size_(0) { | 323 size_(0) { |
| 323 } | 324 } |
| 324 | 325 |
| 325 BinaryValue::BinaryValue(std::unique_ptr<char[]> buffer, size_t size) | 326 BinaryValue::BinaryValue(std::unique_ptr<char[]> buffer, size_t size) |
| 326 : Value(TYPE_BINARY), buffer_(std::move(buffer)), size_(size) {} | 327 : Value(TYPE_BINARY), buffer_(std::move(buffer)), size_(size) {} |
| 327 | 328 |
| (...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1181 ValueDeserializer::~ValueDeserializer() { | 1182 ValueDeserializer::~ValueDeserializer() { |
| 1182 } | 1183 } |
| 1183 | 1184 |
| 1184 std::ostream& operator<<(std::ostream& out, const Value& value) { | 1185 std::ostream& operator<<(std::ostream& out, const Value& value) { |
| 1185 std::string json; | 1186 std::string json; |
| 1186 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); | 1187 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
| 1187 return out << json; | 1188 return out << json; |
| 1188 } | 1189 } |
| 1189 | 1190 |
| 1190 } // namespace base | 1191 } // namespace base |
| OLD | NEW |