| 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 #include "base/values.h" | 5 #include "base/values.h" |
| 6 | 6 |
| 7 #include "base/float_util.h" | 7 #include "base/float_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 // Make a deep copy of |node|, but don't include empty lists or dictionaries | 14 // Make a deep copy of |node|, but don't include empty lists or dictionaries |
| 15 // in the copy. It's possible for this function to return NULL and it | 15 // in the copy. It's possible for this function to return NULL and it |
| 16 // expects |node| to always be non-NULL. | 16 // expects |node| to always be non-NULL. |
| 17 Value* CopyWithoutEmptyChildren(Value* node) { | 17 Value* CopyWithoutEmptyChildren(Value* node) { |
| 18 DCHECK(node); | 18 DCHECK(node); |
| 19 switch (node->GetType()) { | 19 switch (node->GetType()) { |
| 20 case Value::TYPE_LIST: { | 20 case Value::TYPE_LIST: { |
| 21 ListValue* list = node->AsList(); | 21 ListValue* list = static_cast<ListValue*>(node); |
| 22 ListValue* copy = new ListValue; | 22 ListValue* copy = new ListValue; |
| 23 for (ListValue::const_iterator it = list->begin(); it != list->end(); | 23 for (ListValue::const_iterator it = list->begin(); it != list->end(); |
| 24 ++it) { | 24 ++it) { |
| 25 Value* child_copy = CopyWithoutEmptyChildren(*it); | 25 Value* child_copy = CopyWithoutEmptyChildren(*it); |
| 26 if (child_copy) | 26 if (child_copy) |
| 27 copy->Append(child_copy); | 27 copy->Append(child_copy); |
| 28 } | 28 } |
| 29 if (!copy->empty()) | 29 if (!copy->empty()) |
| 30 return copy; | 30 return copy; |
| 31 | 31 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 // static | 90 // static |
| 91 StringValue* Value::CreateStringValue(const std::string& in_value) { | 91 StringValue* Value::CreateStringValue(const std::string& in_value) { |
| 92 return new StringValue(in_value); | 92 return new StringValue(in_value); |
| 93 } | 93 } |
| 94 | 94 |
| 95 // static | 95 // static |
| 96 StringValue* Value::CreateStringValue(const string16& in_value) { | 96 StringValue* Value::CreateStringValue(const string16& in_value) { |
| 97 return new StringValue(in_value); | 97 return new StringValue(in_value); |
| 98 } | 98 } |
| 99 | 99 |
| 100 BinaryValue* Value::AsBinary() { | |
| 101 return NULL; | |
| 102 } | |
| 103 | |
| 104 ListValue* Value::AsList() { | |
| 105 return NULL; | |
| 106 } | |
| 107 | |
| 108 bool Value::GetAsBoolean(bool* out_value) const { | 100 bool Value::GetAsBoolean(bool* out_value) const { |
| 109 return false; | 101 return false; |
| 110 } | 102 } |
| 111 | 103 |
| 112 bool Value::GetAsInteger(int* out_value) const { | 104 bool Value::GetAsInteger(int* out_value) const { |
| 113 return false; | 105 return false; |
| 114 } | 106 } |
| 115 | 107 |
| 116 bool Value::GetAsDouble(double* out_value) const { | 108 bool Value::GetAsDouble(double* out_value) const { |
| 117 return false; | 109 return false; |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, | 290 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, |
| 299 size_t size) { | 291 size_t size) { |
| 300 if (!buffer) | 292 if (!buffer) |
| 301 return NULL; | 293 return NULL; |
| 302 | 294 |
| 303 char* buffer_copy = new char[size]; | 295 char* buffer_copy = new char[size]; |
| 304 memcpy(buffer_copy, buffer, size); | 296 memcpy(buffer_copy, buffer, size); |
| 305 return new BinaryValue(buffer_copy, size); | 297 return new BinaryValue(buffer_copy, size); |
| 306 } | 298 } |
| 307 | 299 |
| 308 BinaryValue* BinaryValue::AsBinary() { | |
| 309 return this; | |
| 310 } | |
| 311 | |
| 312 BinaryValue* BinaryValue::DeepCopy() const { | 300 BinaryValue* BinaryValue::DeepCopy() const { |
| 313 return CreateWithCopiedBuffer(buffer_, size_); | 301 return CreateWithCopiedBuffer(buffer_, size_); |
| 314 } | 302 } |
| 315 | 303 |
| 316 bool BinaryValue::Equals(const Value* other) const { | 304 bool BinaryValue::Equals(const Value* other) const { |
| 317 if (other->GetType() != GetType()) | 305 if (other->GetType() != GetType()) |
| 318 return false; | 306 return false; |
| 319 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); | 307 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); |
| 320 if (other_binary->size_ != size_) | 308 if (other_binary->size_ != size_) |
| 321 return false; | 309 return false; |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 } | 478 } |
| 491 | 479 |
| 492 out_value->assign(out); | 480 out_value->assign(out); |
| 493 return true; | 481 return true; |
| 494 } | 482 } |
| 495 | 483 |
| 496 bool DictionaryValue::GetBinary(const std::string& path, | 484 bool DictionaryValue::GetBinary(const std::string& path, |
| 497 BinaryValue** out_value) const { | 485 BinaryValue** out_value) const { |
| 498 Value* value; | 486 Value* value; |
| 499 bool result = Get(path, &value); | 487 bool result = Get(path, &value); |
| 500 if (!result || !value->AsBinary()) | 488 if (!result || !value->IsType(TYPE_BINARY)) |
| 501 return false; | 489 return false; |
| 502 | 490 |
| 503 if (out_value) | 491 if (out_value) |
| 504 *out_value = value->AsBinary(); | 492 *out_value = static_cast<BinaryValue*>(value); |
| 505 | 493 |
| 506 return true; | 494 return true; |
| 507 } | 495 } |
| 508 | 496 |
| 509 bool DictionaryValue::GetDictionary(const std::string& path, | 497 bool DictionaryValue::GetDictionary(const std::string& path, |
| 510 DictionaryValue** out_value) const { | 498 DictionaryValue** out_value) const { |
| 511 Value* value; | 499 Value* value; |
| 512 bool result = Get(path, &value); | 500 bool result = Get(path, &value); |
| 513 if (!result || !value->IsType(TYPE_DICTIONARY)) | 501 if (!result || !value->IsType(TYPE_DICTIONARY)) |
| 514 return false; | 502 return false; |
| 515 | 503 |
| 516 if (out_value) | 504 if (out_value) |
| 517 *out_value = static_cast<DictionaryValue*>(value); | 505 *out_value = static_cast<DictionaryValue*>(value); |
| 518 | 506 |
| 519 return true; | 507 return true; |
| 520 } | 508 } |
| 521 | 509 |
| 522 bool DictionaryValue::GetList(const std::string& path, | 510 bool DictionaryValue::GetList(const std::string& path, |
| 523 ListValue** out_value) const { | 511 ListValue** out_value) const { |
| 524 Value* value; | 512 Value* value; |
| 525 bool result = Get(path, &value); | 513 bool result = Get(path, &value); |
| 526 if (!result || !value->AsList()) | 514 if (!result || !value->IsType(TYPE_LIST)) |
| 527 return false; | 515 return false; |
| 528 | 516 |
| 529 if (out_value) | 517 if (out_value) |
| 530 *out_value = value->AsList(); | 518 *out_value = static_cast<ListValue*>(value); |
| 531 | 519 |
| 532 return true; | 520 return true; |
| 533 } | 521 } |
| 534 | 522 |
| 535 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, | 523 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, |
| 536 Value** out_value) const { | 524 Value** out_value) const { |
| 537 DCHECK(IsStringUTF8(key)); | 525 DCHECK(IsStringUTF8(key)); |
| 538 ValueMap::const_iterator entry_iterator = dictionary_.find(key); | 526 ValueMap::const_iterator entry_iterator = dictionary_.find(key); |
| 539 if (entry_iterator == dictionary_.end()) | 527 if (entry_iterator == dictionary_.end()) |
| 540 return false; | 528 return false; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 if (out_value) | 582 if (out_value) |
| 595 *out_value = static_cast<DictionaryValue*>(value); | 583 *out_value = static_cast<DictionaryValue*>(value); |
| 596 | 584 |
| 597 return true; | 585 return true; |
| 598 } | 586 } |
| 599 | 587 |
| 600 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key, | 588 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key, |
| 601 ListValue** out_value) const { | 589 ListValue** out_value) const { |
| 602 Value* value; | 590 Value* value; |
| 603 bool result = GetWithoutPathExpansion(key, &value); | 591 bool result = GetWithoutPathExpansion(key, &value); |
| 604 if (!result || !value->AsList()) | 592 if (!result || !value->IsType(TYPE_LIST)) |
| 605 return false; | 593 return false; |
| 606 | 594 |
| 607 if (out_value) | 595 if (out_value) |
| 608 *out_value = value->AsList(); | 596 *out_value = static_cast<ListValue*>(value); |
| 609 | 597 |
| 610 return true; | 598 return true; |
| 611 } | 599 } |
| 612 | 600 |
| 613 bool DictionaryValue::Remove(const std::string& path, Value** out_value) { | 601 bool DictionaryValue::Remove(const std::string& path, Value** out_value) { |
| 614 DCHECK(IsStringUTF8(path)); | 602 DCHECK(IsStringUTF8(path)); |
| 615 std::string current_path(path); | 603 std::string current_path(path); |
| 616 DictionaryValue* current_dictionary = this; | 604 DictionaryValue* current_dictionary = this; |
| 617 size_t delimiter_position = current_path.rfind('.'); | 605 size_t delimiter_position = current_path.rfind('.'); |
| 618 if (delimiter_position != std::string::npos) { | 606 if (delimiter_position != std::string::npos) { |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 807 | 795 |
| 808 if (out_value) | 796 if (out_value) |
| 809 *out_value = static_cast<DictionaryValue*>(value); | 797 *out_value = static_cast<DictionaryValue*>(value); |
| 810 | 798 |
| 811 return true; | 799 return true; |
| 812 } | 800 } |
| 813 | 801 |
| 814 bool ListValue::GetList(size_t index, ListValue** out_value) const { | 802 bool ListValue::GetList(size_t index, ListValue** out_value) const { |
| 815 Value* value; | 803 Value* value; |
| 816 bool result = Get(index, &value); | 804 bool result = Get(index, &value); |
| 817 if (!result || !value->AsList()) | 805 if (!result || !value->IsType(TYPE_LIST)) |
| 818 return false; | 806 return false; |
| 819 | 807 |
| 820 if (out_value) | 808 if (out_value) |
| 821 *out_value = value->AsList(); | 809 *out_value = static_cast<ListValue*>(value); |
| 822 | 810 |
| 823 return true; | 811 return true; |
| 824 } | 812 } |
| 825 | 813 |
| 826 bool ListValue::Remove(size_t index, Value** out_value) { | 814 bool ListValue::Remove(size_t index, Value** out_value) { |
| 827 if (index >= list_.size()) | 815 if (index >= list_.size()) |
| 828 return false; | 816 return false; |
| 829 | 817 |
| 830 if (out_value) | 818 if (out_value) |
| 831 *out_value = list_[index]; | 819 *out_value = list_[index]; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 870 | 858 |
| 871 bool ListValue::Insert(size_t index, Value* in_value) { | 859 bool ListValue::Insert(size_t index, Value* in_value) { |
| 872 DCHECK(in_value); | 860 DCHECK(in_value); |
| 873 if (index > list_.size()) | 861 if (index > list_.size()) |
| 874 return false; | 862 return false; |
| 875 | 863 |
| 876 list_.insert(list_.begin() + index, in_value); | 864 list_.insert(list_.begin() + index, in_value); |
| 877 return true; | 865 return true; |
| 878 } | 866 } |
| 879 | 867 |
| 880 ListValue* ListValue::AsList() { | |
| 881 return this; | |
| 882 } | |
| 883 | |
| 884 bool ListValue::GetAsList(ListValue** out_value) { | 868 bool ListValue::GetAsList(ListValue** out_value) { |
| 885 if (out_value) | 869 if (out_value) |
| 886 *out_value = this; | 870 *out_value = this; |
| 887 return true; | 871 return true; |
| 888 } | 872 } |
| 889 | 873 |
| 890 bool ListValue::GetAsList(const ListValue** out_value) const { | 874 bool ListValue::GetAsList(const ListValue** out_value) const { |
| 891 if (out_value) | 875 if (out_value) |
| 892 *out_value = this; | 876 *out_value = this; |
| 893 return true; | 877 return true; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 918 if (lhs_it != end() || rhs_it != other_list->end()) | 902 if (lhs_it != end() || rhs_it != other_list->end()) |
| 919 return false; | 903 return false; |
| 920 | 904 |
| 921 return true; | 905 return true; |
| 922 } | 906 } |
| 923 | 907 |
| 924 ValueSerializer::~ValueSerializer() { | 908 ValueSerializer::~ValueSerializer() { |
| 925 } | 909 } |
| 926 | 910 |
| 927 } // namespace base | 911 } // namespace base |
| OLD | NEW |