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 11 matching lines...) Expand all Loading... | |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node); | 25 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node); |
| 26 | 26 |
| 27 // Make a deep copy of |node|, but don't include empty lists or dictionaries | 27 // Make a deep copy of |node|, but don't include empty lists or dictionaries |
| 28 // in the copy. It's possible for this function to return NULL and it | 28 // in the copy. It's possible for this function to return NULL and it |
| 29 // expects |node| to always be non-NULL. | 29 // expects |node| to always be non-NULL. |
| 30 std::unique_ptr<ListValue> CopyListWithoutEmptyChildren(const ListValue& list) { | 30 std::unique_ptr<ListValue> CopyListWithoutEmptyChildren(const ListValue& list) { |
| 31 std::unique_ptr<ListValue> copy; | 31 std::unique_ptr<ListValue> copy; |
| 32 for (ListValue::const_iterator it = list.begin(); it != list.end(); ++it) { | 32 for (const auto& entry : list) { |
| 33 std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(**it); | 33 std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(*entry); |
| 34 if (child_copy) { | 34 if (child_copy) { |
| 35 if (!copy) | 35 if (!copy) |
| 36 copy.reset(new ListValue); | 36 copy.reset(new ListValue); |
| 37 copy->Append(std::move(child_copy)); | 37 copy->Append(std::move(child_copy)); |
| 38 } | 38 } |
| 39 } | 39 } |
| 40 return copy; | 40 return copy; |
| 41 } | 41 } |
| 42 | 42 |
| 43 std::unique_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren( | 43 std::unique_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren( |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 61 | 61 |
| 62 case Value::TYPE_DICTIONARY: | 62 case Value::TYPE_DICTIONARY: |
| 63 return CopyDictionaryWithoutEmptyChildren( | 63 return CopyDictionaryWithoutEmptyChildren( |
| 64 static_cast<const DictionaryValue&>(node)); | 64 static_cast<const DictionaryValue&>(node)); |
| 65 | 65 |
| 66 default: | 66 default: |
| 67 return node.CreateDeepCopy(); | 67 return node.CreateDeepCopy(); |
| 68 } | 68 } |
| 69 } | 69 } |
| 70 | 70 |
| 71 // A small functor for comparing Values for std::find_if and similar. | |
| 72 class ValueEquals { | |
| 73 public: | |
| 74 // Pass the value against which all consecutive calls of the () operator will | |
| 75 // compare their argument to. This Value object must not be destroyed while | |
| 76 // the ValueEquals is in use. | |
| 77 explicit ValueEquals(const Value* first) : first_(first) { } | |
| 78 | |
| 79 bool operator ()(const Value* second) const { | |
| 80 return first_->Equals(second); | |
| 81 } | |
| 82 | |
| 83 private: | |
| 84 const Value* first_; | |
| 85 }; | |
| 86 | |
| 87 } // namespace | 71 } // namespace |
| 88 | 72 |
| 89 Value::~Value() { | 73 Value::~Value() { |
| 90 } | 74 } |
| 91 | 75 |
| 92 // static | 76 // static |
| 93 std::unique_ptr<Value> Value::CreateNullValue() { | 77 std::unique_ptr<Value> Value::CreateNullValue() { |
| 94 return WrapUnique(new Value(TYPE_NULL)); | 78 return WrapUnique(new Value(TYPE_NULL)); |
| 95 } | 79 } |
| 96 | 80 |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 376 } | 360 } |
| 377 | 361 |
| 378 bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const { | 362 bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const { |
| 379 if (out_value) | 363 if (out_value) |
| 380 *out_value = this; | 364 *out_value = this; |
| 381 return true; | 365 return true; |
| 382 } | 366 } |
| 383 | 367 |
| 384 bool DictionaryValue::HasKey(const std::string& key) const { | 368 bool DictionaryValue::HasKey(const std::string& key) const { |
| 385 DCHECK(IsStringUTF8(key)); | 369 DCHECK(IsStringUTF8(key)); |
| 386 ValueMap::const_iterator current_entry = dictionary_.find(key); | 370 auto current_entry = dictionary_.find(key); |
| 387 DCHECK((current_entry == dictionary_.end()) || current_entry->second); | 371 DCHECK((current_entry == dictionary_.end()) || current_entry->second); |
| 388 return current_entry != dictionary_.end(); | 372 return current_entry != dictionary_.end(); |
| 389 } | 373 } |
| 390 | 374 |
| 391 void DictionaryValue::Clear() { | 375 void DictionaryValue::Clear() { |
| 392 ValueMap::iterator dict_iterator = dictionary_.begin(); | |
| 393 while (dict_iterator != dictionary_.end()) { | |
| 394 delete dict_iterator->second; | |
| 395 ++dict_iterator; | |
| 396 } | |
| 397 | |
| 398 dictionary_.clear(); | 376 dictionary_.clear(); |
| 399 } | 377 } |
| 400 | 378 |
| 401 void DictionaryValue::Set(const std::string& path, | 379 void DictionaryValue::Set(const std::string& path, |
| 402 std::unique_ptr<Value> in_value) { | 380 std::unique_ptr<Value> in_value) { |
| 403 DCHECK(IsStringUTF8(path)); | 381 DCHECK(IsStringUTF8(path)); |
| 404 DCHECK(in_value); | 382 DCHECK(in_value); |
| 405 | 383 |
| 406 std::string current_path(path); | 384 std::string current_path(path); |
| 407 DictionaryValue* current_dictionary = this; | 385 DictionaryValue* current_dictionary = this; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 445 Set(path, new StringValue(in_value)); | 423 Set(path, new StringValue(in_value)); |
| 446 } | 424 } |
| 447 | 425 |
| 448 void DictionaryValue::SetString(const std::string& path, | 426 void DictionaryValue::SetString(const std::string& path, |
| 449 const string16& in_value) { | 427 const string16& in_value) { |
| 450 Set(path, new StringValue(in_value)); | 428 Set(path, new StringValue(in_value)); |
| 451 } | 429 } |
| 452 | 430 |
| 453 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, | 431 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, |
| 454 std::unique_ptr<Value> in_value) { | 432 std::unique_ptr<Value> in_value) { |
| 455 Value* bare_ptr = in_value.release(); | 433 dictionary_[key] = std::move(in_value); |
| 456 // If there's an existing value here, we need to delete it, because | |
| 457 // we own all our children. | |
| 458 std::pair<ValueMap::iterator, bool> ins_res = | |
| 459 dictionary_.insert(std::make_pair(key, bare_ptr)); | |
| 460 if (!ins_res.second) { | |
| 461 DCHECK_NE(ins_res.first->second, bare_ptr); // This would be bogus | |
| 462 delete ins_res.first->second; | |
| 463 ins_res.first->second = bare_ptr; | |
| 464 } | |
| 465 } | 434 } |
| 466 | 435 |
| 467 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, | 436 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, |
| 468 Value* in_value) { | 437 Value* in_value) { |
| 469 SetWithoutPathExpansion(key, WrapUnique(in_value)); | 438 SetWithoutPathExpansion(key, WrapUnique(in_value)); |
| 470 } | 439 } |
| 471 | 440 |
| 472 void DictionaryValue::SetBooleanWithoutPathExpansion( | 441 void DictionaryValue::SetBooleanWithoutPathExpansion( |
| 473 const std::string& path, bool in_value) { | 442 const std::string& path, bool in_value) { |
| 474 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); | 443 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 638 | 607 |
| 639 bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) { | 608 bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) { |
| 640 return static_cast<const DictionaryValue&>(*this).GetList( | 609 return static_cast<const DictionaryValue&>(*this).GetList( |
| 641 path, | 610 path, |
| 642 const_cast<const ListValue**>(out_value)); | 611 const_cast<const ListValue**>(out_value)); |
| 643 } | 612 } |
| 644 | 613 |
| 645 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, | 614 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, |
| 646 const Value** out_value) const { | 615 const Value** out_value) const { |
| 647 DCHECK(IsStringUTF8(key)); | 616 DCHECK(IsStringUTF8(key)); |
| 648 ValueMap::const_iterator entry_iterator = dictionary_.find(key); | 617 auto entry_iterator = dictionary_.find(key); |
| 649 if (entry_iterator == dictionary_.end()) | 618 if (entry_iterator == dictionary_.end()) |
| 650 return false; | 619 return false; |
| 651 | 620 |
| 652 const Value* entry = entry_iterator->second; | |
| 653 if (out_value) | 621 if (out_value) |
| 654 *out_value = entry; | 622 *out_value = entry_iterator->second.get(); |
| 655 return true; | 623 return true; |
| 656 } | 624 } |
| 657 | 625 |
| 658 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, | 626 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, |
| 659 Value** out_value) { | 627 Value** out_value) { |
| 660 return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion( | 628 return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion( |
| 661 key, | 629 key, |
| 662 const_cast<const Value**>(out_value)); | 630 const_cast<const Value**>(out_value)); |
| 663 } | 631 } |
| 664 | 632 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 768 } | 736 } |
| 769 | 737 |
| 770 return current_dictionary->RemoveWithoutPathExpansion(current_path, | 738 return current_dictionary->RemoveWithoutPathExpansion(current_path, |
| 771 out_value); | 739 out_value); |
| 772 } | 740 } |
| 773 | 741 |
| 774 bool DictionaryValue::RemoveWithoutPathExpansion( | 742 bool DictionaryValue::RemoveWithoutPathExpansion( |
| 775 const std::string& key, | 743 const std::string& key, |
| 776 std::unique_ptr<Value>* out_value) { | 744 std::unique_ptr<Value>* out_value) { |
| 777 DCHECK(IsStringUTF8(key)); | 745 DCHECK(IsStringUTF8(key)); |
| 778 ValueMap::iterator entry_iterator = dictionary_.find(key); | 746 auto entry_iterator = dictionary_.find(key); |
| 779 if (entry_iterator == dictionary_.end()) | 747 if (entry_iterator == dictionary_.end()) |
| 780 return false; | 748 return false; |
| 781 | 749 |
| 782 Value* entry = entry_iterator->second; | |
| 783 if (out_value) | 750 if (out_value) |
| 784 out_value->reset(entry); | 751 *out_value = std::move(entry_iterator->second); |
| 785 else | |
| 786 delete entry; | |
| 787 dictionary_.erase(entry_iterator); | 752 dictionary_.erase(entry_iterator); |
| 788 return true; | 753 return true; |
| 789 } | 754 } |
| 790 | 755 |
| 791 bool DictionaryValue::RemovePath(const std::string& path, | 756 bool DictionaryValue::RemovePath(const std::string& path, |
| 792 std::unique_ptr<Value>* out_value) { | 757 std::unique_ptr<Value>* out_value) { |
| 793 bool result = false; | 758 bool result = false; |
| 794 size_t delimiter_position = path.find('.'); | 759 size_t delimiter_position = path.find('.'); |
| 795 | 760 |
| 796 if (delimiter_position == std::string::npos) | 761 if (delimiter_position == std::string::npos) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 842 : target_(target), | 807 : target_(target), |
| 843 it_(target.dictionary_.begin()) {} | 808 it_(target.dictionary_.begin()) {} |
| 844 | 809 |
| 845 DictionaryValue::Iterator::Iterator(const Iterator& other) = default; | 810 DictionaryValue::Iterator::Iterator(const Iterator& other) = default; |
| 846 | 811 |
| 847 DictionaryValue::Iterator::~Iterator() {} | 812 DictionaryValue::Iterator::~Iterator() {} |
| 848 | 813 |
| 849 DictionaryValue* DictionaryValue::DeepCopy() const { | 814 DictionaryValue* DictionaryValue::DeepCopy() const { |
| 850 DictionaryValue* result = new DictionaryValue; | 815 DictionaryValue* result = new DictionaryValue; |
| 851 | 816 |
| 852 for (ValueMap::const_iterator current_entry(dictionary_.begin()); | 817 for (const auto& current_entry : dictionary_) { |
| 853 current_entry != dictionary_.end(); ++current_entry) { | 818 result->SetWithoutPathExpansion(current_entry.first, |
| 854 result->SetWithoutPathExpansion(current_entry->first, | 819 current_entry.second->CreateDeepCopy()); |
| 855 current_entry->second->DeepCopy()); | |
| 856 } | 820 } |
| 857 | 821 |
| 858 return result; | 822 return result; |
| 859 } | 823 } |
| 860 | 824 |
| 861 std::unique_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const { | 825 std::unique_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const { |
| 862 return WrapUnique(DeepCopy()); | 826 return WrapUnique(DeepCopy()); |
| 863 } | 827 } |
| 864 | 828 |
| 865 bool DictionaryValue::Equals(const Value* other) const { | 829 bool DictionaryValue::Equals(const Value* other) const { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 897 } | 861 } |
| 898 | 862 |
| 899 ListValue::ListValue() : Value(TYPE_LIST) { | 863 ListValue::ListValue() : Value(TYPE_LIST) { |
| 900 } | 864 } |
| 901 | 865 |
| 902 ListValue::~ListValue() { | 866 ListValue::~ListValue() { |
| 903 Clear(); | 867 Clear(); |
| 904 } | 868 } |
| 905 | 869 |
| 906 void ListValue::Clear() { | 870 void ListValue::Clear() { |
| 907 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) | |
| 908 delete *i; | |
| 909 list_.clear(); | 871 list_.clear(); |
| 910 } | 872 } |
| 911 | 873 |
| 912 bool ListValue::Set(size_t index, Value* in_value) { | 874 bool ListValue::Set(size_t index, Value* in_value) { |
| 875 return Set(index, WrapUnique(in_value)); | |
| 876 } | |
| 877 | |
| 878 bool ListValue::Set(size_t index, std::unique_ptr<Value> in_value) { | |
| 913 if (!in_value) | 879 if (!in_value) |
| 914 return false; | 880 return false; |
| 915 | 881 |
| 916 if (index >= list_.size()) { | 882 if (index >= list_.size()) { |
| 917 // Pad out any intermediate indexes with null settings | 883 // Pad out any intermediate indexes with null settings |
| 918 while (index > list_.size()) | 884 while (index > list_.size()) |
| 919 Append(CreateNullValue()); | 885 Append(CreateNullValue()); |
| 920 Append(in_value); | 886 Append(std::move(in_value)); |
| 921 } else { | 887 } else { |
| 888 // TODO(dcheng): remove this DCHECK once the raw pointer version is removed? | |
| 922 DCHECK(list_[index] != in_value); | 889 DCHECK(list_[index] != in_value); |
| 923 delete list_[index]; | 890 list_[index] = std::move(in_value); |
| 924 list_[index] = in_value; | |
| 925 } | 891 } |
| 926 return true; | 892 return true; |
| 927 } | 893 } |
| 928 | 894 |
| 929 bool ListValue::Set(size_t index, std::unique_ptr<Value> in_value) { | |
| 930 return Set(index, in_value.release()); | |
| 931 } | |
| 932 | |
| 933 bool ListValue::Get(size_t index, const Value** out_value) const { | 895 bool ListValue::Get(size_t index, const Value** out_value) const { |
| 934 if (index >= list_.size()) | 896 if (index >= list_.size()) |
| 935 return false; | 897 return false; |
| 936 | 898 |
| 937 if (out_value) | 899 if (out_value) |
| 938 *out_value = list_[index]; | 900 *out_value = list_[index].get(); |
| 939 | 901 |
| 940 return true; | 902 return true; |
| 941 } | 903 } |
| 942 | 904 |
| 943 bool ListValue::Get(size_t index, Value** out_value) { | 905 bool ListValue::Get(size_t index, Value** out_value) { |
| 944 return static_cast<const ListValue&>(*this).Get( | 906 return static_cast<const ListValue&>(*this).Get( |
| 945 index, | 907 index, |
| 946 const_cast<const Value**>(out_value)); | 908 const_cast<const Value**>(out_value)); |
| 947 } | 909 } |
| 948 | 910 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1039 return static_cast<const ListValue&>(*this).GetList( | 1001 return static_cast<const ListValue&>(*this).GetList( |
| 1040 index, | 1002 index, |
| 1041 const_cast<const ListValue**>(out_value)); | 1003 const_cast<const ListValue**>(out_value)); |
| 1042 } | 1004 } |
| 1043 | 1005 |
| 1044 bool ListValue::Remove(size_t index, std::unique_ptr<Value>* out_value) { | 1006 bool ListValue::Remove(size_t index, std::unique_ptr<Value>* out_value) { |
| 1045 if (index >= list_.size()) | 1007 if (index >= list_.size()) |
| 1046 return false; | 1008 return false; |
| 1047 | 1009 |
| 1048 if (out_value) | 1010 if (out_value) |
| 1049 out_value->reset(list_[index]); | 1011 *out_value = std::move(list_[index]); |
| 1050 else | |
| 1051 delete list_[index]; | |
| 1052 | 1012 |
| 1053 list_.erase(list_.begin() + index); | 1013 list_.erase(list_.begin() + index); |
| 1054 return true; | 1014 return true; |
| 1055 } | 1015 } |
| 1056 | 1016 |
| 1057 bool ListValue::Remove(const Value& value, size_t* index) { | 1017 bool ListValue::Remove(const Value& value, size_t* index) { |
| 1058 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) { | 1018 for (auto it = list_.begin(); it != list_.end(); ++it) { |
| 1059 if ((*i)->Equals(&value)) { | 1019 if ((*it)->Equals(&value)) { |
| 1060 size_t previous_index = i - list_.begin(); | 1020 size_t previous_index = it - list_.begin(); |
| 1061 delete *i; | 1021 list_.erase(it); |
| 1062 list_.erase(i); | |
| 1063 | 1022 |
| 1064 if (index) | 1023 if (index) |
| 1065 *index = previous_index; | 1024 *index = previous_index; |
| 1066 return true; | 1025 return true; |
| 1067 } | 1026 } |
| 1068 } | 1027 } |
| 1069 return false; | 1028 return false; |
| 1070 } | 1029 } |
| 1071 | 1030 |
| 1072 ListValue::iterator ListValue::Erase(iterator iter, | 1031 ListValue::iterator ListValue::Erase(iterator iter, |
| 1073 std::unique_ptr<Value>* out_value) { | 1032 std::unique_ptr<Value>* out_value) { |
| 1074 if (out_value) | 1033 if (out_value) |
| 1075 out_value->reset(*iter); | 1034 *out_value = std::move(*Storage::iterator(iter)); |
| 1076 else | |
| 1077 delete *iter; | |
| 1078 | 1035 |
| 1079 return list_.erase(iter); | 1036 return list_.erase(iter); |
| 1080 } | 1037 } |
| 1081 | 1038 |
| 1082 void ListValue::Append(std::unique_ptr<Value> in_value) { | 1039 void ListValue::Append(std::unique_ptr<Value> in_value) { |
| 1083 Append(in_value.release()); | 1040 list_.emplace_back(std::move(in_value)); |
|
danakj
2016/05/24 20:08:34
push_back?
dcheng
2016/05/24 20:39:02
Done.
| |
| 1084 } | 1041 } |
| 1085 | 1042 |
| 1086 void ListValue::Append(Value* in_value) { | 1043 void ListValue::Append(Value* in_value) { |
| 1087 DCHECK(in_value); | 1044 DCHECK(in_value); |
| 1088 list_.push_back(in_value); | 1045 Append(WrapUnique(in_value)); |
|
danakj
2016/05/24 20:08:34
this one would be emplace if u wanted.
dcheng
2016/05/24 20:39:02
As discussed, will just delegate to the other Appe
| |
| 1089 } | 1046 } |
| 1090 | 1047 |
| 1091 void ListValue::AppendBoolean(bool in_value) { | 1048 void ListValue::AppendBoolean(bool in_value) { |
| 1092 Append(new FundamentalValue(in_value)); | 1049 Append(new FundamentalValue(in_value)); |
| 1093 } | 1050 } |
| 1094 | 1051 |
| 1095 void ListValue::AppendInteger(int in_value) { | 1052 void ListValue::AppendInteger(int in_value) { |
| 1096 Append(new FundamentalValue(in_value)); | 1053 Append(new FundamentalValue(in_value)); |
| 1097 } | 1054 } |
| 1098 | 1055 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 1117 | 1074 |
| 1118 void ListValue::AppendStrings(const std::vector<string16>& in_values) { | 1075 void ListValue::AppendStrings(const std::vector<string16>& in_values) { |
| 1119 for (std::vector<string16>::const_iterator it = in_values.begin(); | 1076 for (std::vector<string16>::const_iterator it = in_values.begin(); |
| 1120 it != in_values.end(); ++it) { | 1077 it != in_values.end(); ++it) { |
| 1121 AppendString(*it); | 1078 AppendString(*it); |
| 1122 } | 1079 } |
| 1123 } | 1080 } |
| 1124 | 1081 |
| 1125 bool ListValue::AppendIfNotPresent(Value* in_value) { | 1082 bool ListValue::AppendIfNotPresent(Value* in_value) { |
| 1126 DCHECK(in_value); | 1083 DCHECK(in_value); |
| 1127 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) { | 1084 for (const auto& entry : list_) { |
| 1128 if ((*i)->Equals(in_value)) { | 1085 if (entry->Equals(in_value)) { |
| 1129 delete in_value; | 1086 delete in_value; |
| 1130 return false; | 1087 return false; |
| 1131 } | 1088 } |
| 1132 } | 1089 } |
| 1133 list_.push_back(in_value); | 1090 list_.emplace_back(in_value); |
| 1134 return true; | 1091 return true; |
| 1135 } | 1092 } |
| 1136 | 1093 |
| 1137 bool ListValue::Insert(size_t index, Value* in_value) { | 1094 bool ListValue::Insert(size_t index, Value* in_value) { |
| 1138 DCHECK(in_value); | 1095 DCHECK(in_value); |
| 1139 if (index > list_.size()) | 1096 if (index > list_.size()) |
| 1140 return false; | 1097 return false; |
| 1141 | 1098 |
| 1142 list_.insert(list_.begin() + index, in_value); | 1099 list_.insert(list_.begin() + index, WrapUnique(in_value)); |
| 1143 return true; | 1100 return true; |
| 1144 } | 1101 } |
| 1145 | 1102 |
| 1146 ListValue::const_iterator ListValue::Find(const Value& value) const { | 1103 ListValue::const_iterator ListValue::Find(const Value& value) const { |
| 1147 return std::find_if(list_.begin(), list_.end(), ValueEquals(&value)); | 1104 return std::find_if(list_.begin(), list_.end(), |
| 1105 [&value](const std::unique_ptr<Value>& entry) { | |
| 1106 return entry->Equals(&value); | |
| 1107 }); | |
| 1148 } | 1108 } |
| 1149 | 1109 |
| 1150 void ListValue::Swap(ListValue* other) { | 1110 void ListValue::Swap(ListValue* other) { |
| 1151 list_.swap(other->list_); | 1111 list_.swap(other->list_); |
| 1152 } | 1112 } |
| 1153 | 1113 |
| 1154 bool ListValue::GetAsList(ListValue** out_value) { | 1114 bool ListValue::GetAsList(ListValue** out_value) { |
| 1155 if (out_value) | 1115 if (out_value) |
| 1156 *out_value = this; | 1116 *out_value = this; |
| 1157 return true; | 1117 return true; |
| 1158 } | 1118 } |
| 1159 | 1119 |
| 1160 bool ListValue::GetAsList(const ListValue** out_value) const { | 1120 bool ListValue::GetAsList(const ListValue** out_value) const { |
| 1161 if (out_value) | 1121 if (out_value) |
| 1162 *out_value = this; | 1122 *out_value = this; |
| 1163 return true; | 1123 return true; |
| 1164 } | 1124 } |
| 1165 | 1125 |
| 1166 ListValue* ListValue::DeepCopy() const { | 1126 ListValue* ListValue::DeepCopy() const { |
| 1167 ListValue* result = new ListValue; | 1127 ListValue* result = new ListValue; |
| 1168 | 1128 |
| 1169 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) | 1129 for (const auto& entry : list_) |
| 1170 result->Append((*i)->DeepCopy()); | 1130 result->Append(entry->CreateDeepCopy()); |
| 1171 | 1131 |
| 1172 return result; | 1132 return result; |
| 1173 } | 1133 } |
| 1174 | 1134 |
| 1175 std::unique_ptr<ListValue> ListValue::CreateDeepCopy() const { | 1135 std::unique_ptr<ListValue> ListValue::CreateDeepCopy() const { |
| 1176 return WrapUnique(DeepCopy()); | 1136 return WrapUnique(DeepCopy()); |
| 1177 } | 1137 } |
| 1178 | 1138 |
| 1179 bool ListValue::Equals(const Value* other) const { | 1139 bool ListValue::Equals(const Value* other) const { |
| 1180 if (other->GetType() != GetType()) | 1140 if (other->GetType() != GetType()) |
| 1181 return false; | 1141 return false; |
| 1182 | 1142 |
| 1183 const ListValue* other_list = | 1143 const ListValue* other_list = |
| 1184 static_cast<const ListValue*>(other); | 1144 static_cast<const ListValue*>(other); |
| 1185 const_iterator lhs_it, rhs_it; | 1145 Storage::const_iterator lhs_it, rhs_it; |
| 1186 for (lhs_it = begin(), rhs_it = other_list->begin(); | 1146 for (lhs_it = begin(), rhs_it = other_list->begin(); |
| 1187 lhs_it != end() && rhs_it != other_list->end(); | 1147 lhs_it != end() && rhs_it != other_list->end(); |
| 1188 ++lhs_it, ++rhs_it) { | 1148 ++lhs_it, ++rhs_it) { |
| 1189 if (!(*lhs_it)->Equals(*rhs_it)) | 1149 if (!(*lhs_it)->Equals(rhs_it->get())) |
| 1190 return false; | 1150 return false; |
| 1191 } | 1151 } |
| 1192 if (lhs_it != end() || rhs_it != other_list->end()) | 1152 if (lhs_it != end() || rhs_it != other_list->end()) |
| 1193 return false; | 1153 return false; |
| 1194 | 1154 |
| 1195 return true; | 1155 return true; |
| 1196 } | 1156 } |
| 1197 | 1157 |
| 1198 ValueSerializer::~ValueSerializer() { | 1158 ValueSerializer::~ValueSerializer() { |
| 1199 } | 1159 } |
| 1200 | 1160 |
| 1201 ValueDeserializer::~ValueDeserializer() { | 1161 ValueDeserializer::~ValueDeserializer() { |
| 1202 } | 1162 } |
| 1203 | 1163 |
| 1204 std::ostream& operator<<(std::ostream& out, const Value& value) { | 1164 std::ostream& operator<<(std::ostream& out, const Value& value) { |
| 1205 std::string json; | 1165 std::string json; |
| 1206 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); | 1166 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
| 1207 return out << json; | 1167 return out << json; |
| 1208 } | 1168 } |
| 1209 | 1169 |
| 1210 } // namespace base | 1170 } // namespace base |
| OLD | NEW |