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 <ostream> | 10 #include <ostream> |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 if (out_value && IsType(TYPE_DOUBLE)) | 218 if (out_value && IsType(TYPE_DOUBLE)) |
219 *out_value = double_value_; | 219 *out_value = double_value_; |
220 else if (out_value && IsType(TYPE_INTEGER)) | 220 else if (out_value && IsType(TYPE_INTEGER)) |
221 *out_value = integer_value_; | 221 *out_value = integer_value_; |
222 return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER)); | 222 return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER)); |
223 } | 223 } |
224 | 224 |
225 FundamentalValue* FundamentalValue::DeepCopy() const { | 225 FundamentalValue* FundamentalValue::DeepCopy() const { |
226 switch (GetType()) { | 226 switch (GetType()) { |
227 case TYPE_BOOLEAN: | 227 case TYPE_BOOLEAN: |
228 return CreateBooleanValue(boolean_value_); | 228 return new FundamentalValue(boolean_value_); |
229 | 229 |
230 case TYPE_INTEGER: | 230 case TYPE_INTEGER: |
231 return CreateIntegerValue(integer_value_); | 231 return new FundamentalValue(integer_value_); |
232 | 232 |
233 case TYPE_DOUBLE: | 233 case TYPE_DOUBLE: |
234 return CreateDoubleValue(double_value_); | 234 return new FundamentalValue(double_value_); |
235 | 235 |
236 default: | 236 default: |
237 NOTREACHED(); | 237 NOTREACHED(); |
238 return NULL; | 238 return NULL; |
239 } | 239 } |
240 } | 240 } |
241 | 241 |
242 bool FundamentalValue::Equals(const Value* other) const { | 242 bool FundamentalValue::Equals(const Value* other) const { |
243 if (other->GetType() != GetType()) | 243 if (other->GetType() != GetType()) |
244 return false; | 244 return false; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 return true; | 284 return true; |
285 } | 285 } |
286 | 286 |
287 bool StringValue::GetAsString(string16* out_value) const { | 287 bool StringValue::GetAsString(string16* out_value) const { |
288 if (out_value) | 288 if (out_value) |
289 *out_value = UTF8ToUTF16(value_); | 289 *out_value = UTF8ToUTF16(value_); |
290 return true; | 290 return true; |
291 } | 291 } |
292 | 292 |
293 StringValue* StringValue::DeepCopy() const { | 293 StringValue* StringValue::DeepCopy() const { |
294 return CreateStringValue(value_); | 294 return new StringValue(value_); |
295 } | 295 } |
296 | 296 |
297 bool StringValue::Equals(const Value* other) const { | 297 bool StringValue::Equals(const Value* other) const { |
298 if (other->GetType() != GetType()) | 298 if (other->GetType() != GetType()) |
299 return false; | 299 return false; |
300 std::string lhs, rhs; | 300 std::string lhs, rhs; |
301 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; | 301 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; |
302 } | 302 } |
303 | 303 |
304 ///////////////////// BinaryValue //////////////////// | 304 ///////////////////// BinaryValue //////////////////// |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 } | 396 } |
397 | 397 |
398 current_dictionary = child_dictionary; | 398 current_dictionary = child_dictionary; |
399 current_path.erase(0, delimiter_position + 1); | 399 current_path.erase(0, delimiter_position + 1); |
400 } | 400 } |
401 | 401 |
402 current_dictionary->SetWithoutPathExpansion(current_path, in_value); | 402 current_dictionary->SetWithoutPathExpansion(current_path, in_value); |
403 } | 403 } |
404 | 404 |
405 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) { | 405 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) { |
406 Set(path, CreateBooleanValue(in_value)); | 406 Set(path, new FundamentalValue(in_value)); |
407 } | 407 } |
408 | 408 |
409 void DictionaryValue::SetInteger(const std::string& path, int in_value) { | 409 void DictionaryValue::SetInteger(const std::string& path, int in_value) { |
410 Set(path, CreateIntegerValue(in_value)); | 410 Set(path, new FundamentalValue(in_value)); |
411 } | 411 } |
412 | 412 |
413 void DictionaryValue::SetDouble(const std::string& path, double in_value) { | 413 void DictionaryValue::SetDouble(const std::string& path, double in_value) { |
414 Set(path, CreateDoubleValue(in_value)); | 414 Set(path, new FundamentalValue(in_value)); |
415 } | 415 } |
416 | 416 |
417 void DictionaryValue::SetString(const std::string& path, | 417 void DictionaryValue::SetString(const std::string& path, |
418 const std::string& in_value) { | 418 const std::string& in_value) { |
419 Set(path, CreateStringValue(in_value)); | 419 Set(path, new StringValue(in_value)); |
420 } | 420 } |
421 | 421 |
422 void DictionaryValue::SetString(const std::string& path, | 422 void DictionaryValue::SetString(const std::string& path, |
423 const string16& in_value) { | 423 const string16& in_value) { |
424 Set(path, CreateStringValue(in_value)); | 424 Set(path, new StringValue(in_value)); |
425 } | 425 } |
426 | 426 |
427 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, | 427 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, |
428 Value* in_value) { | 428 Value* in_value) { |
429 // If there's an existing value here, we need to delete it, because | 429 // If there's an existing value here, we need to delete it, because |
430 // we own all our children. | 430 // we own all our children. |
431 std::pair<ValueMap::iterator, bool> ins_res = | 431 std::pair<ValueMap::iterator, bool> ins_res = |
432 dictionary_.insert(std::make_pair(key, in_value)); | 432 dictionary_.insert(std::make_pair(key, in_value)); |
433 if (!ins_res.second) { | 433 if (!ins_res.second) { |
434 DCHECK_NE(ins_res.first->second, in_value); // This would be bogus | 434 DCHECK_NE(ins_res.first->second, in_value); // This would be bogus |
435 delete ins_res.first->second; | 435 delete ins_res.first->second; |
436 ins_res.first->second = in_value; | 436 ins_res.first->second = in_value; |
437 } | 437 } |
438 } | 438 } |
439 | 439 |
440 void DictionaryValue::SetBooleanWithoutPathExpansion( | 440 void DictionaryValue::SetBooleanWithoutPathExpansion( |
441 const std::string& path, bool in_value) { | 441 const std::string& path, bool in_value) { |
442 SetWithoutPathExpansion(path, CreateBooleanValue(in_value)); | 442 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); |
443 } | 443 } |
444 | 444 |
445 void DictionaryValue::SetIntegerWithoutPathExpansion( | 445 void DictionaryValue::SetIntegerWithoutPathExpansion( |
446 const std::string& path, int in_value) { | 446 const std::string& path, int in_value) { |
447 SetWithoutPathExpansion(path, CreateIntegerValue(in_value)); | 447 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); |
448 } | 448 } |
449 | 449 |
450 void DictionaryValue::SetDoubleWithoutPathExpansion( | 450 void DictionaryValue::SetDoubleWithoutPathExpansion( |
451 const std::string& path, double in_value) { | 451 const std::string& path, double in_value) { |
452 SetWithoutPathExpansion(path, CreateDoubleValue(in_value)); | 452 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); |
453 } | 453 } |
454 | 454 |
455 void DictionaryValue::SetStringWithoutPathExpansion( | 455 void DictionaryValue::SetStringWithoutPathExpansion( |
456 const std::string& path, const std::string& in_value) { | 456 const std::string& path, const std::string& in_value) { |
457 SetWithoutPathExpansion(path, CreateStringValue(in_value)); | 457 SetWithoutPathExpansion(path, new StringValue(in_value)); |
458 } | 458 } |
459 | 459 |
460 void DictionaryValue::SetStringWithoutPathExpansion( | 460 void DictionaryValue::SetStringWithoutPathExpansion( |
461 const std::string& path, const string16& in_value) { | 461 const std::string& path, const string16& in_value) { |
462 SetWithoutPathExpansion(path, CreateStringValue(in_value)); | 462 SetWithoutPathExpansion(path, new StringValue(in_value)); |
463 } | 463 } |
464 | 464 |
465 bool DictionaryValue::Get(const std::string& path, | 465 bool DictionaryValue::Get(const std::string& path, |
466 const Value** out_value) const { | 466 const Value** out_value) const { |
467 DCHECK(IsStringUTF8(path)); | 467 DCHECK(IsStringUTF8(path)); |
468 std::string current_path(path); | 468 std::string current_path(path); |
469 const DictionaryValue* current_dictionary = this; | 469 const DictionaryValue* current_dictionary = this; |
470 for (size_t delimiter_position = current_path.find('.'); | 470 for (size_t delimiter_position = current_path.find('.'); |
471 delimiter_position != std::string::npos; | 471 delimiter_position != std::string::npos; |
472 delimiter_position = current_path.find('.')) { | 472 delimiter_position = current_path.find('.')) { |
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1018 | 1018 |
1019 return list_.erase(iter); | 1019 return list_.erase(iter); |
1020 } | 1020 } |
1021 | 1021 |
1022 void ListValue::Append(Value* in_value) { | 1022 void ListValue::Append(Value* in_value) { |
1023 DCHECK(in_value); | 1023 DCHECK(in_value); |
1024 list_.push_back(in_value); | 1024 list_.push_back(in_value); |
1025 } | 1025 } |
1026 | 1026 |
1027 void ListValue::AppendBoolean(bool in_value) { | 1027 void ListValue::AppendBoolean(bool in_value) { |
1028 Append(CreateBooleanValue(in_value)); | 1028 Append(new FundamentalValue(in_value)); |
1029 } | 1029 } |
1030 | 1030 |
1031 void ListValue::AppendInteger(int in_value) { | 1031 void ListValue::AppendInteger(int in_value) { |
1032 Append(CreateIntegerValue(in_value)); | 1032 Append(new FundamentalValue(in_value)); |
1033 } | 1033 } |
1034 | 1034 |
1035 void ListValue::AppendDouble(double in_value) { | 1035 void ListValue::AppendDouble(double in_value) { |
1036 Append(CreateDoubleValue(in_value)); | 1036 Append(new FundamentalValue(in_value)); |
1037 } | 1037 } |
1038 | 1038 |
1039 void ListValue::AppendString(const std::string& in_value) { | 1039 void ListValue::AppendString(const std::string& in_value) { |
1040 Append(CreateStringValue(in_value)); | 1040 Append(new StringValue(in_value)); |
1041 } | 1041 } |
1042 | 1042 |
1043 void ListValue::AppendString(const string16& in_value) { | 1043 void ListValue::AppendString(const string16& in_value) { |
1044 Append(CreateStringValue(in_value)); | 1044 Append(new StringValue(in_value)); |
1045 } | 1045 } |
1046 | 1046 |
1047 void ListValue::AppendStrings(const std::vector<std::string>& in_values) { | 1047 void ListValue::AppendStrings(const std::vector<std::string>& in_values) { |
1048 for (std::vector<std::string>::const_iterator it = in_values.begin(); | 1048 for (std::vector<std::string>::const_iterator it = in_values.begin(); |
1049 it != in_values.end(); ++it) { | 1049 it != in_values.end(); ++it) { |
1050 AppendString(*it); | 1050 AppendString(*it); |
1051 } | 1051 } |
1052 } | 1052 } |
1053 | 1053 |
1054 void ListValue::AppendStrings(const std::vector<string16>& in_values) { | 1054 void ListValue::AppendStrings(const std::vector<string16>& in_values) { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1132 | 1132 |
1133 std::ostream& operator<<(std::ostream& out, const Value& value) { | 1133 std::ostream& operator<<(std::ostream& out, const Value& value) { |
1134 std::string json; | 1134 std::string json; |
1135 JSONWriter::WriteWithOptions(&value, | 1135 JSONWriter::WriteWithOptions(&value, |
1136 JSONWriter::OPTIONS_PRETTY_PRINT, | 1136 JSONWriter::OPTIONS_PRETTY_PRINT, |
1137 &json); | 1137 &json); |
1138 return out << json; | 1138 return out << json; |
1139 } | 1139 } |
1140 | 1140 |
1141 } // namespace base | 1141 } // namespace base |
OLD | NEW |