Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(368)

Side by Side Diff: base/values.cc

Issue 2865963002: Use MakeUnique() in base/values.cc. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 return copy.GetList().empty() ? nullptr : MakeUnique<Value>(std::move(copy)); 42 return copy.GetList().empty() ? nullptr : MakeUnique<Value>(std::move(copy));
43 } 43 }
44 44
45 std::unique_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren( 45 std::unique_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren(
46 const DictionaryValue& dict) { 46 const DictionaryValue& dict) {
47 std::unique_ptr<DictionaryValue> copy; 47 std::unique_ptr<DictionaryValue> copy;
48 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { 48 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
49 std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.value()); 49 std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.value());
50 if (child_copy) { 50 if (child_copy) {
51 if (!copy) 51 if (!copy)
52 copy.reset(new DictionaryValue); 52 copy = MakeUnique<DictionaryValue>();
53 copy->SetWithoutPathExpansion(it.key(), std::move(child_copy)); 53 copy->SetWithoutPathExpansion(it.key(), std::move(child_copy));
54 } 54 }
55 } 55 }
56 return copy; 56 return copy;
57 } 57 }
58 58
59 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node) { 59 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node) {
60 switch (node.GetType()) { 60 switch (node.GetType()) {
61 case Value::Type::LIST: 61 case Value::Type::LIST:
62 return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node)); 62 return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node));
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 330
331 bool Value::GetAsDictionary(const DictionaryValue** out_value) const { 331 bool Value::GetAsDictionary(const DictionaryValue** out_value) const {
332 if (out_value && is_dict()) { 332 if (out_value && is_dict()) {
333 *out_value = static_cast<const DictionaryValue*>(this); 333 *out_value = static_cast<const DictionaryValue*>(this);
334 return true; 334 return true;
335 } 335 }
336 return is_dict(); 336 return is_dict();
337 } 337 }
338 338
339 Value* Value::DeepCopy() const { 339 Value* Value::DeepCopy() const {
340 return new Value(*this); 340 return CreateDeepCopy().release();
jdoerrie 2017/05/08 22:44:47 I'd rather like for this to stay the same. As the
Lei Zhang 2017/05/08 22:58:30 Done.
341 } 341 }
342 342
343 std::unique_ptr<Value> Value::CreateDeepCopy() const { 343 std::unique_ptr<Value> Value::CreateDeepCopy() const {
344 return MakeUnique<Value>(*this); 344 return MakeUnique<Value>(*this);
345 } 345 }
346 346
347 bool operator==(const Value& lhs, const Value& rhs) { 347 bool operator==(const Value& lhs, const Value& rhs) {
348 if (lhs.type_ != rhs.type_) 348 if (lhs.type_ != rhs.type_)
349 return false; 349 return false;
350 350
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
617 617
618 StringPiece current_path(path); 618 StringPiece current_path(path);
619 DictionaryValue* current_dictionary = this; 619 DictionaryValue* current_dictionary = this;
620 for (size_t delimiter_position = current_path.find('.'); 620 for (size_t delimiter_position = current_path.find('.');
621 delimiter_position != StringPiece::npos; 621 delimiter_position != StringPiece::npos;
622 delimiter_position = current_path.find('.')) { 622 delimiter_position = current_path.find('.')) {
623 // Assume that we're indexing into a dictionary. 623 // Assume that we're indexing into a dictionary.
624 StringPiece key = current_path.substr(0, delimiter_position); 624 StringPiece key = current_path.substr(0, delimiter_position);
625 DictionaryValue* child_dictionary = nullptr; 625 DictionaryValue* child_dictionary = nullptr;
626 if (!current_dictionary->GetDictionary(key, &child_dictionary)) { 626 if (!current_dictionary->GetDictionary(key, &child_dictionary)) {
627 child_dictionary = new DictionaryValue; 627 auto new_child_dictionary = MakeUnique<DictionaryValue>();
628 child_dictionary = new_child_dictionary.get();
628 current_dictionary->SetWithoutPathExpansion( 629 current_dictionary->SetWithoutPathExpansion(
629 key, base::WrapUnique(child_dictionary)); 630 key, std::move(new_child_dictionary));
jdoerrie 2017/05/08 22:44:47 Consider using the recently added SetDictionaryWit
Lei Zhang 2017/05/08 22:58:30 Done. Thanks.
630 } 631 }
631 632
632 current_dictionary = child_dictionary; 633 current_dictionary = child_dictionary;
633 current_path = current_path.substr(delimiter_position + 1); 634 current_path = current_path.substr(delimiter_position + 1);
634 } 635 }
635 636
636 return current_dictionary->SetWithoutPathExpansion(current_path, 637 return current_dictionary->SetWithoutPathExpansion(current_path,
637 std::move(in_value)); 638 std::move(in_value));
638 } 639 }
639 640
640 Value* DictionaryValue::Set(StringPiece path, Value* in_value) { 641 Value* DictionaryValue::Set(StringPiece path, Value* in_value) {
641 return Set(path, WrapUnique(in_value)); 642 return Set(path, WrapUnique(in_value));
642 } 643 }
643 644
644 Value* DictionaryValue::SetBoolean(StringPiece path, bool in_value) { 645 Value* DictionaryValue::SetBoolean(StringPiece path, bool in_value) {
645 return Set(path, new Value(in_value)); 646 return Set(path, MakeUnique<Value>(in_value));
646 } 647 }
647 648
648 Value* DictionaryValue::SetInteger(StringPiece path, int in_value) { 649 Value* DictionaryValue::SetInteger(StringPiece path, int in_value) {
649 return Set(path, new Value(in_value)); 650 return Set(path, MakeUnique<Value>(in_value));
650 } 651 }
651 652
652 Value* DictionaryValue::SetDouble(StringPiece path, double in_value) { 653 Value* DictionaryValue::SetDouble(StringPiece path, double in_value) {
653 return Set(path, new Value(in_value)); 654 return Set(path, MakeUnique<Value>(in_value));
654 } 655 }
655 656
656 Value* DictionaryValue::SetString(StringPiece path, StringPiece in_value) { 657 Value* DictionaryValue::SetString(StringPiece path, StringPiece in_value) {
657 return Set(path, new Value(in_value)); 658 return Set(path, MakeUnique<Value>(in_value));
658 } 659 }
659 660
660 Value* DictionaryValue::SetString(StringPiece path, const string16& in_value) { 661 Value* DictionaryValue::SetString(StringPiece path, const string16& in_value) {
661 return Set(path, new Value(in_value)); 662 return Set(path, MakeUnique<Value>(in_value));
662 } 663 }
663 664
664 DictionaryValue* DictionaryValue::SetDictionary( 665 DictionaryValue* DictionaryValue::SetDictionary(
665 StringPiece path, 666 StringPiece path,
666 std::unique_ptr<DictionaryValue> in_value) { 667 std::unique_ptr<DictionaryValue> in_value) {
667 return static_cast<DictionaryValue*>(Set(path, std::move(in_value))); 668 return static_cast<DictionaryValue*>(Set(path, std::move(in_value)));
668 } 669 }
669 670
670 ListValue* DictionaryValue::SetList(StringPiece path, 671 ListValue* DictionaryValue::SetList(StringPiece path,
671 std::unique_ptr<ListValue> in_value) { 672 std::unique_ptr<ListValue> in_value) {
672 return static_cast<ListValue*>(Set(path, std::move(in_value))); 673 return static_cast<ListValue*>(Set(path, std::move(in_value)));
673 } 674 }
674 675
675 Value* DictionaryValue::SetWithoutPathExpansion( 676 Value* DictionaryValue::SetWithoutPathExpansion(
676 StringPiece key, 677 StringPiece key,
677 std::unique_ptr<Value> in_value) { 678 std::unique_ptr<Value> in_value) {
678 return ((*dict_)[key.as_string()] = std::move(in_value)).get(); 679 return ((*dict_)[key.as_string()] = std::move(in_value)).get();
679 } 680 }
680 681
681 Value* DictionaryValue::SetWithoutPathExpansion(StringPiece key, 682 Value* DictionaryValue::SetWithoutPathExpansion(StringPiece key,
682 Value* in_value) { 683 Value* in_value) {
683 return SetWithoutPathExpansion(key, WrapUnique(in_value)); 684 return SetWithoutPathExpansion(key, WrapUnique(in_value));
684 } 685 }
685 686
686 Value* DictionaryValue::SetBooleanWithoutPathExpansion(StringPiece path, 687 Value* DictionaryValue::SetBooleanWithoutPathExpansion(StringPiece path,
687 bool in_value) { 688 bool in_value) {
688 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); 689 return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value));
689 } 690 }
690 691
691 Value* DictionaryValue::SetIntegerWithoutPathExpansion(StringPiece path, 692 Value* DictionaryValue::SetIntegerWithoutPathExpansion(StringPiece path,
692 int in_value) { 693 int in_value) {
693 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); 694 return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value));
694 } 695 }
695 696
696 Value* DictionaryValue::SetDoubleWithoutPathExpansion(StringPiece path, 697 Value* DictionaryValue::SetDoubleWithoutPathExpansion(StringPiece path,
697 double in_value) { 698 double in_value) {
698 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); 699 return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value));
699 } 700 }
700 701
701 Value* DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, 702 Value* DictionaryValue::SetStringWithoutPathExpansion(StringPiece path,
702 StringPiece in_value) { 703 StringPiece in_value) {
703 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); 704 return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value));
704 } 705 }
705 706
706 Value* DictionaryValue::SetStringWithoutPathExpansion( 707 Value* DictionaryValue::SetStringWithoutPathExpansion(
707 StringPiece path, 708 StringPiece path,
708 const string16& in_value) { 709 const string16& in_value) {
709 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); 710 return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value));
710 } 711 }
711 712
712 DictionaryValue* DictionaryValue::SetDictionaryWithoutPathExpansion( 713 DictionaryValue* DictionaryValue::SetDictionaryWithoutPathExpansion(
713 StringPiece path, 714 StringPiece path,
714 std::unique_ptr<DictionaryValue> in_value) { 715 std::unique_ptr<DictionaryValue> in_value) {
715 return static_cast<DictionaryValue*>( 716 return static_cast<DictionaryValue*>(
716 SetWithoutPathExpansion(path, std::move(in_value))); 717 SetWithoutPathExpansion(path, std::move(in_value)));
717 } 718 }
718 719
719 ListValue* DictionaryValue::SetListWithoutPathExpansion( 720 ListValue* DictionaryValue::SetListWithoutPathExpansion(
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
1023 RemoveWithoutPathExpansion(subdict_path, NULL); 1024 RemoveWithoutPathExpansion(subdict_path, NULL);
1024 1025
1025 return result; 1026 return result;
1026 } 1027 }
1027 1028
1028 std::unique_ptr<DictionaryValue> DictionaryValue::DeepCopyWithoutEmptyChildren() 1029 std::unique_ptr<DictionaryValue> DictionaryValue::DeepCopyWithoutEmptyChildren()
1029 const { 1030 const {
1030 std::unique_ptr<DictionaryValue> copy = 1031 std::unique_ptr<DictionaryValue> copy =
1031 CopyDictionaryWithoutEmptyChildren(*this); 1032 CopyDictionaryWithoutEmptyChildren(*this);
1032 if (!copy) 1033 if (!copy)
1033 copy.reset(new DictionaryValue); 1034 copy = MakeUnique<DictionaryValue>();
1034 return copy; 1035 return copy;
1035 } 1036 }
1036 1037
1037 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { 1038 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) {
1038 CHECK(dictionary->is_dict()); 1039 CHECK(dictionary->is_dict());
1039 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) { 1040 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) {
1040 const Value* merge_value = &it.value(); 1041 const Value* merge_value = &it.value();
1041 // Check whether we have to merge dictionaries. 1042 // Check whether we have to merge dictionaries.
1042 if (merge_value->IsType(Value::Type::DICTIONARY)) { 1043 if (merge_value->IsType(Value::Type::DICTIONARY)) {
1043 DictionaryValue* sub_dict; 1044 DictionaryValue* sub_dict;
(...skipping 14 matching lines...) Expand all
1058 } 1059 }
1059 1060
1060 DictionaryValue::Iterator::Iterator(const DictionaryValue& target) 1061 DictionaryValue::Iterator::Iterator(const DictionaryValue& target)
1061 : target_(target), it_(target.dict_->begin()) {} 1062 : target_(target), it_(target.dict_->begin()) {}
1062 1063
1063 DictionaryValue::Iterator::Iterator(const Iterator& other) = default; 1064 DictionaryValue::Iterator::Iterator(const Iterator& other) = default;
1064 1065
1065 DictionaryValue::Iterator::~Iterator() {} 1066 DictionaryValue::Iterator::~Iterator() {}
1066 1067
1067 DictionaryValue* DictionaryValue::DeepCopy() const { 1068 DictionaryValue* DictionaryValue::DeepCopy() const {
1068 return new DictionaryValue(*this); 1069 return CreateDeepCopy().release();
jdoerrie 2017/05/08 22:44:47 Same here.
Lei Zhang 2017/05/08 22:58:30 Done.
1069 } 1070 }
1070 1071
1071 std::unique_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const { 1072 std::unique_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const {
1072 return MakeUnique<DictionaryValue>(*this); 1073 return MakeUnique<DictionaryValue>(*this);
1073 } 1074 }
1074 1075
1075 ///////////////////// ListValue //////////////////// 1076 ///////////////////// ListValue ////////////////////
1076 1077
1077 // static 1078 // static
1078 std::unique_ptr<ListValue> ListValue::From(std::unique_ptr<Value> value) { 1079 std::unique_ptr<ListValue> ListValue::From(std::unique_ptr<Value> value) {
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
1309 ListValue::const_iterator ListValue::Find(const Value& value) const { 1310 ListValue::const_iterator ListValue::Find(const Value& value) const {
1310 return std::find(list_->begin(), list_->end(), value); 1311 return std::find(list_->begin(), list_->end(), value);
1311 } 1312 }
1312 1313
1313 void ListValue::Swap(ListValue* other) { 1314 void ListValue::Swap(ListValue* other) {
1314 CHECK(other->is_list()); 1315 CHECK(other->is_list());
1315 list_->swap(*(other->list_)); 1316 list_->swap(*(other->list_));
1316 } 1317 }
1317 1318
1318 ListValue* ListValue::DeepCopy() const { 1319 ListValue* ListValue::DeepCopy() const {
1319 return new ListValue(*this); 1320 return CreateDeepCopy().release();
jdoerrie 2017/05/08 22:44:48 And here.
Lei Zhang 2017/05/08 22:58:30 Done.
1320 } 1321 }
1321 1322
1322 std::unique_ptr<ListValue> ListValue::CreateDeepCopy() const { 1323 std::unique_ptr<ListValue> ListValue::CreateDeepCopy() const {
1323 return MakeUnique<ListValue>(*this); 1324 return MakeUnique<ListValue>(*this);
1324 } 1325 }
1325 1326
1326 ValueSerializer::~ValueSerializer() { 1327 ValueSerializer::~ValueSerializer() {
1327 } 1328 }
1328 1329
1329 ValueDeserializer::~ValueDeserializer() { 1330 ValueDeserializer::~ValueDeserializer() {
1330 } 1331 }
1331 1332
1332 std::ostream& operator<<(std::ostream& out, const Value& value) { 1333 std::ostream& operator<<(std::ostream& out, const Value& value) {
1333 std::string json; 1334 std::string json;
1334 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); 1335 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json);
1335 return out << json; 1336 return out << json;
1336 } 1337 }
1337 1338
1338 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { 1339 std::ostream& operator<<(std::ostream& out, const Value::Type& type) {
1339 if (static_cast<int>(type) < 0 || 1340 if (static_cast<int>(type) < 0 ||
1340 static_cast<size_t>(type) >= arraysize(kTypeNames)) 1341 static_cast<size_t>(type) >= arraysize(kTypeNames))
1341 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; 1342 return out << "Invalid Type (index = " << static_cast<int>(type) << ")";
1342 return out << Value::GetTypeName(type); 1343 return out << Value::GetTypeName(type);
1343 } 1344 }
1344 1345
1345 } // namespace base 1346 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698