| 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> |
| 11 #include <ostream> | 11 #include <ostream> |
| 12 #include <utility> | 12 #include <utility> |
| 13 | 13 |
| 14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/ptr_util.h" |
| 16 #include "base/move.h" | 17 #include "base/move.h" |
| 17 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 19 | 20 |
| 20 namespace base { | 21 namespace base { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 scoped_ptr<Value> CopyWithoutEmptyChildren(const Value& node); | 25 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node); |
| 25 | 26 |
| 26 // 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 |
| 27 // 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 |
| 28 // expects |node| to always be non-NULL. | 29 // expects |node| to always be non-NULL. |
| 29 scoped_ptr<ListValue> CopyListWithoutEmptyChildren(const ListValue& list) { | 30 std::unique_ptr<ListValue> CopyListWithoutEmptyChildren(const ListValue& list) { |
| 30 scoped_ptr<ListValue> copy; | 31 std::unique_ptr<ListValue> copy; |
| 31 for (ListValue::const_iterator it = list.begin(); it != list.end(); ++it) { | 32 for (ListValue::const_iterator it = list.begin(); it != list.end(); ++it) { |
| 32 scoped_ptr<Value> child_copy = CopyWithoutEmptyChildren(**it); | 33 std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(**it); |
| 33 if (child_copy) { | 34 if (child_copy) { |
| 34 if (!copy) | 35 if (!copy) |
| 35 copy.reset(new ListValue); | 36 copy.reset(new ListValue); |
| 36 copy->Append(std::move(child_copy)); | 37 copy->Append(std::move(child_copy)); |
| 37 } | 38 } |
| 38 } | 39 } |
| 39 return copy; | 40 return copy; |
| 40 } | 41 } |
| 41 | 42 |
| 42 scoped_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren( | 43 std::unique_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren( |
| 43 const DictionaryValue& dict) { | 44 const DictionaryValue& dict) { |
| 44 scoped_ptr<DictionaryValue> copy; | 45 std::unique_ptr<DictionaryValue> copy; |
| 45 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | 46 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
| 46 scoped_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.value()); | 47 std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.value()); |
| 47 if (child_copy) { | 48 if (child_copy) { |
| 48 if (!copy) | 49 if (!copy) |
| 49 copy.reset(new DictionaryValue); | 50 copy.reset(new DictionaryValue); |
| 50 copy->SetWithoutPathExpansion(it.key(), std::move(child_copy)); | 51 copy->SetWithoutPathExpansion(it.key(), std::move(child_copy)); |
| 51 } | 52 } |
| 52 } | 53 } |
| 53 return copy; | 54 return copy; |
| 54 } | 55 } |
| 55 | 56 |
| 56 scoped_ptr<Value> CopyWithoutEmptyChildren(const Value& node) { | 57 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node) { |
| 57 switch (node.GetType()) { | 58 switch (node.GetType()) { |
| 58 case Value::TYPE_LIST: | 59 case Value::TYPE_LIST: |
| 59 return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node)); | 60 return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node)); |
| 60 | 61 |
| 61 case Value::TYPE_DICTIONARY: | 62 case Value::TYPE_DICTIONARY: |
| 62 return CopyDictionaryWithoutEmptyChildren( | 63 return CopyDictionaryWithoutEmptyChildren( |
| 63 static_cast<const DictionaryValue&>(node)); | 64 static_cast<const DictionaryValue&>(node)); |
| 64 | 65 |
| 65 default: | 66 default: |
| 66 return node.CreateDeepCopy(); | 67 return node.CreateDeepCopy(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 82 private: | 83 private: |
| 83 const Value* first_; | 84 const Value* first_; |
| 84 }; | 85 }; |
| 85 | 86 |
| 86 } // namespace | 87 } // namespace |
| 87 | 88 |
| 88 Value::~Value() { | 89 Value::~Value() { |
| 89 } | 90 } |
| 90 | 91 |
| 91 // static | 92 // static |
| 92 scoped_ptr<Value> Value::CreateNullValue() { | 93 std::unique_ptr<Value> Value::CreateNullValue() { |
| 93 return make_scoped_ptr(new Value(TYPE_NULL)); | 94 return WrapUnique(new Value(TYPE_NULL)); |
| 94 } | 95 } |
| 95 | 96 |
| 96 bool Value::GetAsBinary(const BinaryValue** out_value) const { | 97 bool Value::GetAsBinary(const BinaryValue** out_value) const { |
| 97 return false; | 98 return false; |
| 98 } | 99 } |
| 99 | 100 |
| 100 bool Value::GetAsBoolean(bool* out_value) const { | 101 bool Value::GetAsBoolean(bool* out_value) const { |
| 101 return false; | 102 return false; |
| 102 } | 103 } |
| 103 | 104 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 return false; | 138 return false; |
| 138 } | 139 } |
| 139 | 140 |
| 140 Value* Value::DeepCopy() const { | 141 Value* Value::DeepCopy() const { |
| 141 // This method should only be getting called for null Values--all subclasses | 142 // This method should only be getting called for null Values--all subclasses |
| 142 // need to provide their own implementation;. | 143 // need to provide their own implementation;. |
| 143 DCHECK(IsType(TYPE_NULL)); | 144 DCHECK(IsType(TYPE_NULL)); |
| 144 return CreateNullValue().release(); | 145 return CreateNullValue().release(); |
| 145 } | 146 } |
| 146 | 147 |
| 147 scoped_ptr<Value> Value::CreateDeepCopy() const { | 148 std::unique_ptr<Value> Value::CreateDeepCopy() const { |
| 148 return make_scoped_ptr(DeepCopy()); | 149 return WrapUnique(DeepCopy()); |
| 149 } | 150 } |
| 150 | 151 |
| 151 bool Value::Equals(const Value* other) const { | 152 bool Value::Equals(const Value* other) const { |
| 152 // This method should only be getting called for null Values--all subclasses | 153 // This method should only be getting called for null Values--all subclasses |
| 153 // need to provide their own implementation;. | 154 // need to provide their own implementation;. |
| 154 DCHECK(IsType(TYPE_NULL)); | 155 DCHECK(IsType(TYPE_NULL)); |
| 155 return other->IsType(TYPE_NULL); | 156 return other->IsType(TYPE_NULL); |
| 156 } | 157 } |
| 157 | 158 |
| 158 // static | 159 // static |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; | 307 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; |
| 307 } | 308 } |
| 308 | 309 |
| 309 ///////////////////// BinaryValue //////////////////// | 310 ///////////////////// BinaryValue //////////////////// |
| 310 | 311 |
| 311 BinaryValue::BinaryValue() | 312 BinaryValue::BinaryValue() |
| 312 : Value(TYPE_BINARY), | 313 : Value(TYPE_BINARY), |
| 313 size_(0) { | 314 size_(0) { |
| 314 } | 315 } |
| 315 | 316 |
| 316 BinaryValue::BinaryValue(scoped_ptr<char[]> buffer, size_t size) | 317 BinaryValue::BinaryValue(std::unique_ptr<char[]> buffer, size_t size) |
| 317 : Value(TYPE_BINARY), buffer_(std::move(buffer)), size_(size) {} | 318 : Value(TYPE_BINARY), buffer_(std::move(buffer)), size_(size) {} |
| 318 | 319 |
| 319 BinaryValue::~BinaryValue() { | 320 BinaryValue::~BinaryValue() { |
| 320 } | 321 } |
| 321 | 322 |
| 322 // static | 323 // static |
| 323 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, | 324 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, |
| 324 size_t size) { | 325 size_t size) { |
| 325 char* buffer_copy = new char[size]; | 326 char* buffer_copy = new char[size]; |
| 326 memcpy(buffer_copy, buffer, size); | 327 memcpy(buffer_copy, buffer, size); |
| 327 scoped_ptr<char[]> scoped_buffer_copy(buffer_copy); | 328 std::unique_ptr<char[]> scoped_buffer_copy(buffer_copy); |
| 328 return new BinaryValue(std::move(scoped_buffer_copy), size); | 329 return new BinaryValue(std::move(scoped_buffer_copy), size); |
| 329 } | 330 } |
| 330 | 331 |
| 331 bool BinaryValue::GetAsBinary(const BinaryValue** out_value) const { | 332 bool BinaryValue::GetAsBinary(const BinaryValue** out_value) const { |
| 332 if (out_value) | 333 if (out_value) |
| 333 *out_value = this; | 334 *out_value = this; |
| 334 return true; | 335 return true; |
| 335 } | 336 } |
| 336 | 337 |
| 337 BinaryValue* BinaryValue::DeepCopy() const { | 338 BinaryValue* BinaryValue::DeepCopy() const { |
| 338 return CreateWithCopiedBuffer(buffer_.get(), size_); | 339 return CreateWithCopiedBuffer(buffer_.get(), size_); |
| 339 } | 340 } |
| 340 | 341 |
| 341 bool BinaryValue::Equals(const Value* other) const { | 342 bool BinaryValue::Equals(const Value* other) const { |
| 342 if (other->GetType() != GetType()) | 343 if (other->GetType() != GetType()) |
| 343 return false; | 344 return false; |
| 344 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); | 345 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); |
| 345 if (other_binary->size_ != size_) | 346 if (other_binary->size_ != size_) |
| 346 return false; | 347 return false; |
| 347 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_); | 348 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_); |
| 348 } | 349 } |
| 349 | 350 |
| 350 ///////////////////// DictionaryValue //////////////////// | 351 ///////////////////// DictionaryValue //////////////////// |
| 351 | 352 |
| 352 // static | 353 // static |
| 353 scoped_ptr<DictionaryValue> DictionaryValue::From(scoped_ptr<Value> value) { | 354 std::unique_ptr<DictionaryValue> DictionaryValue::From( |
| 355 std::unique_ptr<Value> value) { |
| 354 DictionaryValue* out; | 356 DictionaryValue* out; |
| 355 if (value && value->GetAsDictionary(&out)) { | 357 if (value && value->GetAsDictionary(&out)) { |
| 356 ignore_result(value.release()); | 358 ignore_result(value.release()); |
| 357 return make_scoped_ptr(out); | 359 return WrapUnique(out); |
| 358 } | 360 } |
| 359 return nullptr; | 361 return nullptr; |
| 360 } | 362 } |
| 361 | 363 |
| 362 DictionaryValue::DictionaryValue() | 364 DictionaryValue::DictionaryValue() |
| 363 : Value(TYPE_DICTIONARY) { | 365 : Value(TYPE_DICTIONARY) { |
| 364 } | 366 } |
| 365 | 367 |
| 366 DictionaryValue::~DictionaryValue() { | 368 DictionaryValue::~DictionaryValue() { |
| 367 Clear(); | 369 Clear(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 389 void DictionaryValue::Clear() { | 391 void DictionaryValue::Clear() { |
| 390 ValueMap::iterator dict_iterator = dictionary_.begin(); | 392 ValueMap::iterator dict_iterator = dictionary_.begin(); |
| 391 while (dict_iterator != dictionary_.end()) { | 393 while (dict_iterator != dictionary_.end()) { |
| 392 delete dict_iterator->second; | 394 delete dict_iterator->second; |
| 393 ++dict_iterator; | 395 ++dict_iterator; |
| 394 } | 396 } |
| 395 | 397 |
| 396 dictionary_.clear(); | 398 dictionary_.clear(); |
| 397 } | 399 } |
| 398 | 400 |
| 399 void DictionaryValue::Set(const std::string& path, scoped_ptr<Value> in_value) { | 401 void DictionaryValue::Set(const std::string& path, |
| 402 std::unique_ptr<Value> in_value) { |
| 400 DCHECK(IsStringUTF8(path)); | 403 DCHECK(IsStringUTF8(path)); |
| 401 DCHECK(in_value); | 404 DCHECK(in_value); |
| 402 | 405 |
| 403 std::string current_path(path); | 406 std::string current_path(path); |
| 404 DictionaryValue* current_dictionary = this; | 407 DictionaryValue* current_dictionary = this; |
| 405 for (size_t delimiter_position = current_path.find('.'); | 408 for (size_t delimiter_position = current_path.find('.'); |
| 406 delimiter_position != std::string::npos; | 409 delimiter_position != std::string::npos; |
| 407 delimiter_position = current_path.find('.')) { | 410 delimiter_position = current_path.find('.')) { |
| 408 // Assume that we're indexing into a dictionary. | 411 // Assume that we're indexing into a dictionary. |
| 409 std::string key(current_path, 0, delimiter_position); | 412 std::string key(current_path, 0, delimiter_position); |
| 410 DictionaryValue* child_dictionary = NULL; | 413 DictionaryValue* child_dictionary = NULL; |
| 411 if (!current_dictionary->GetDictionary(key, &child_dictionary)) { | 414 if (!current_dictionary->GetDictionary(key, &child_dictionary)) { |
| 412 child_dictionary = new DictionaryValue; | 415 child_dictionary = new DictionaryValue; |
| 413 current_dictionary->SetWithoutPathExpansion(key, child_dictionary); | 416 current_dictionary->SetWithoutPathExpansion(key, child_dictionary); |
| 414 } | 417 } |
| 415 | 418 |
| 416 current_dictionary = child_dictionary; | 419 current_dictionary = child_dictionary; |
| 417 current_path.erase(0, delimiter_position + 1); | 420 current_path.erase(0, delimiter_position + 1); |
| 418 } | 421 } |
| 419 | 422 |
| 420 current_dictionary->SetWithoutPathExpansion(current_path, | 423 current_dictionary->SetWithoutPathExpansion(current_path, |
| 421 std::move(in_value)); | 424 std::move(in_value)); |
| 422 } | 425 } |
| 423 | 426 |
| 424 void DictionaryValue::Set(const std::string& path, Value* in_value) { | 427 void DictionaryValue::Set(const std::string& path, Value* in_value) { |
| 425 Set(path, make_scoped_ptr(in_value)); | 428 Set(path, WrapUnique(in_value)); |
| 426 } | 429 } |
| 427 | 430 |
| 428 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) { | 431 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) { |
| 429 Set(path, new FundamentalValue(in_value)); | 432 Set(path, new FundamentalValue(in_value)); |
| 430 } | 433 } |
| 431 | 434 |
| 432 void DictionaryValue::SetInteger(const std::string& path, int in_value) { | 435 void DictionaryValue::SetInteger(const std::string& path, int in_value) { |
| 433 Set(path, new FundamentalValue(in_value)); | 436 Set(path, new FundamentalValue(in_value)); |
| 434 } | 437 } |
| 435 | 438 |
| 436 void DictionaryValue::SetDouble(const std::string& path, double in_value) { | 439 void DictionaryValue::SetDouble(const std::string& path, double in_value) { |
| 437 Set(path, new FundamentalValue(in_value)); | 440 Set(path, new FundamentalValue(in_value)); |
| 438 } | 441 } |
| 439 | 442 |
| 440 void DictionaryValue::SetString(const std::string& path, | 443 void DictionaryValue::SetString(const std::string& path, |
| 441 const std::string& in_value) { | 444 const std::string& in_value) { |
| 442 Set(path, new StringValue(in_value)); | 445 Set(path, new StringValue(in_value)); |
| 443 } | 446 } |
| 444 | 447 |
| 445 void DictionaryValue::SetString(const std::string& path, | 448 void DictionaryValue::SetString(const std::string& path, |
| 446 const string16& in_value) { | 449 const string16& in_value) { |
| 447 Set(path, new StringValue(in_value)); | 450 Set(path, new StringValue(in_value)); |
| 448 } | 451 } |
| 449 | 452 |
| 450 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, | 453 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, |
| 451 scoped_ptr<Value> in_value) { | 454 std::unique_ptr<Value> in_value) { |
| 452 Value* bare_ptr = in_value.release(); | 455 Value* bare_ptr = in_value.release(); |
| 453 // If there's an existing value here, we need to delete it, because | 456 // If there's an existing value here, we need to delete it, because |
| 454 // we own all our children. | 457 // we own all our children. |
| 455 std::pair<ValueMap::iterator, bool> ins_res = | 458 std::pair<ValueMap::iterator, bool> ins_res = |
| 456 dictionary_.insert(std::make_pair(key, bare_ptr)); | 459 dictionary_.insert(std::make_pair(key, bare_ptr)); |
| 457 if (!ins_res.second) { | 460 if (!ins_res.second) { |
| 458 DCHECK_NE(ins_res.first->second, bare_ptr); // This would be bogus | 461 DCHECK_NE(ins_res.first->second, bare_ptr); // This would be bogus |
| 459 delete ins_res.first->second; | 462 delete ins_res.first->second; |
| 460 ins_res.first->second = bare_ptr; | 463 ins_res.first->second = bare_ptr; |
| 461 } | 464 } |
| 462 } | 465 } |
| 463 | 466 |
| 464 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, | 467 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, |
| 465 Value* in_value) { | 468 Value* in_value) { |
| 466 SetWithoutPathExpansion(key, make_scoped_ptr(in_value)); | 469 SetWithoutPathExpansion(key, WrapUnique(in_value)); |
| 467 } | 470 } |
| 468 | 471 |
| 469 void DictionaryValue::SetBooleanWithoutPathExpansion( | 472 void DictionaryValue::SetBooleanWithoutPathExpansion( |
| 470 const std::string& path, bool in_value) { | 473 const std::string& path, bool in_value) { |
| 471 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); | 474 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); |
| 472 } | 475 } |
| 473 | 476 |
| 474 void DictionaryValue::SetIntegerWithoutPathExpansion( | 477 void DictionaryValue::SetIntegerWithoutPathExpansion( |
| 475 const std::string& path, int in_value) { | 478 const std::string& path, int in_value) { |
| 476 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); | 479 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 | 748 |
| 746 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key, | 749 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key, |
| 747 ListValue** out_value) { | 750 ListValue** out_value) { |
| 748 return | 751 return |
| 749 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion( | 752 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion( |
| 750 key, | 753 key, |
| 751 const_cast<const ListValue**>(out_value)); | 754 const_cast<const ListValue**>(out_value)); |
| 752 } | 755 } |
| 753 | 756 |
| 754 bool DictionaryValue::Remove(const std::string& path, | 757 bool DictionaryValue::Remove(const std::string& path, |
| 755 scoped_ptr<Value>* out_value) { | 758 std::unique_ptr<Value>* out_value) { |
| 756 DCHECK(IsStringUTF8(path)); | 759 DCHECK(IsStringUTF8(path)); |
| 757 std::string current_path(path); | 760 std::string current_path(path); |
| 758 DictionaryValue* current_dictionary = this; | 761 DictionaryValue* current_dictionary = this; |
| 759 size_t delimiter_position = current_path.rfind('.'); | 762 size_t delimiter_position = current_path.rfind('.'); |
| 760 if (delimiter_position != std::string::npos) { | 763 if (delimiter_position != std::string::npos) { |
| 761 if (!GetDictionary(current_path.substr(0, delimiter_position), | 764 if (!GetDictionary(current_path.substr(0, delimiter_position), |
| 762 ¤t_dictionary)) | 765 ¤t_dictionary)) |
| 763 return false; | 766 return false; |
| 764 current_path.erase(0, delimiter_position + 1); | 767 current_path.erase(0, delimiter_position + 1); |
| 765 } | 768 } |
| 766 | 769 |
| 767 return current_dictionary->RemoveWithoutPathExpansion(current_path, | 770 return current_dictionary->RemoveWithoutPathExpansion(current_path, |
| 768 out_value); | 771 out_value); |
| 769 } | 772 } |
| 770 | 773 |
| 771 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string& key, | 774 bool DictionaryValue::RemoveWithoutPathExpansion( |
| 772 scoped_ptr<Value>* out_value) { | 775 const std::string& key, |
| 776 std::unique_ptr<Value>* out_value) { |
| 773 DCHECK(IsStringUTF8(key)); | 777 DCHECK(IsStringUTF8(key)); |
| 774 ValueMap::iterator entry_iterator = dictionary_.find(key); | 778 ValueMap::iterator entry_iterator = dictionary_.find(key); |
| 775 if (entry_iterator == dictionary_.end()) | 779 if (entry_iterator == dictionary_.end()) |
| 776 return false; | 780 return false; |
| 777 | 781 |
| 778 Value* entry = entry_iterator->second; | 782 Value* entry = entry_iterator->second; |
| 779 if (out_value) | 783 if (out_value) |
| 780 out_value->reset(entry); | 784 out_value->reset(entry); |
| 781 else | 785 else |
| 782 delete entry; | 786 delete entry; |
| 783 dictionary_.erase(entry_iterator); | 787 dictionary_.erase(entry_iterator); |
| 784 return true; | 788 return true; |
| 785 } | 789 } |
| 786 | 790 |
| 787 bool DictionaryValue::RemovePath(const std::string& path, | 791 bool DictionaryValue::RemovePath(const std::string& path, |
| 788 scoped_ptr<Value>* out_value) { | 792 std::unique_ptr<Value>* out_value) { |
| 789 bool result = false; | 793 bool result = false; |
| 790 size_t delimiter_position = path.find('.'); | 794 size_t delimiter_position = path.find('.'); |
| 791 | 795 |
| 792 if (delimiter_position == std::string::npos) | 796 if (delimiter_position == std::string::npos) |
| 793 return RemoveWithoutPathExpansion(path, out_value); | 797 return RemoveWithoutPathExpansion(path, out_value); |
| 794 | 798 |
| 795 const std::string subdict_path = path.substr(0, delimiter_position); | 799 const std::string subdict_path = path.substr(0, delimiter_position); |
| 796 DictionaryValue* subdict = NULL; | 800 DictionaryValue* subdict = NULL; |
| 797 if (!GetDictionary(subdict_path, &subdict)) | 801 if (!GetDictionary(subdict_path, &subdict)) |
| 798 return false; | 802 return false; |
| 799 result = subdict->RemovePath(path.substr(delimiter_position + 1), | 803 result = subdict->RemovePath(path.substr(delimiter_position + 1), |
| 800 out_value); | 804 out_value); |
| 801 if (result && subdict->empty()) | 805 if (result && subdict->empty()) |
| 802 RemoveWithoutPathExpansion(subdict_path, NULL); | 806 RemoveWithoutPathExpansion(subdict_path, NULL); |
| 803 | 807 |
| 804 return result; | 808 return result; |
| 805 } | 809 } |
| 806 | 810 |
| 807 scoped_ptr<DictionaryValue> DictionaryValue::DeepCopyWithoutEmptyChildren() | 811 std::unique_ptr<DictionaryValue> DictionaryValue::DeepCopyWithoutEmptyChildren() |
| 808 const { | 812 const { |
| 809 scoped_ptr<DictionaryValue> copy = CopyDictionaryWithoutEmptyChildren(*this); | 813 std::unique_ptr<DictionaryValue> copy = |
| 814 CopyDictionaryWithoutEmptyChildren(*this); |
| 810 if (!copy) | 815 if (!copy) |
| 811 copy.reset(new DictionaryValue); | 816 copy.reset(new DictionaryValue); |
| 812 return copy; | 817 return copy; |
| 813 } | 818 } |
| 814 | 819 |
| 815 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { | 820 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { |
| 816 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) { | 821 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) { |
| 817 const Value* merge_value = &it.value(); | 822 const Value* merge_value = &it.value(); |
| 818 // Check whether we have to merge dictionaries. | 823 // Check whether we have to merge dictionaries. |
| 819 if (merge_value->IsType(Value::TYPE_DICTIONARY)) { | 824 if (merge_value->IsType(Value::TYPE_DICTIONARY)) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 846 | 851 |
| 847 for (ValueMap::const_iterator current_entry(dictionary_.begin()); | 852 for (ValueMap::const_iterator current_entry(dictionary_.begin()); |
| 848 current_entry != dictionary_.end(); ++current_entry) { | 853 current_entry != dictionary_.end(); ++current_entry) { |
| 849 result->SetWithoutPathExpansion(current_entry->first, | 854 result->SetWithoutPathExpansion(current_entry->first, |
| 850 current_entry->second->DeepCopy()); | 855 current_entry->second->DeepCopy()); |
| 851 } | 856 } |
| 852 | 857 |
| 853 return result; | 858 return result; |
| 854 } | 859 } |
| 855 | 860 |
| 856 scoped_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const { | 861 std::unique_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const { |
| 857 return make_scoped_ptr(DeepCopy()); | 862 return WrapUnique(DeepCopy()); |
| 858 } | 863 } |
| 859 | 864 |
| 860 bool DictionaryValue::Equals(const Value* other) const { | 865 bool DictionaryValue::Equals(const Value* other) const { |
| 861 if (other->GetType() != GetType()) | 866 if (other->GetType() != GetType()) |
| 862 return false; | 867 return false; |
| 863 | 868 |
| 864 const DictionaryValue* other_dict = | 869 const DictionaryValue* other_dict = |
| 865 static_cast<const DictionaryValue*>(other); | 870 static_cast<const DictionaryValue*>(other); |
| 866 Iterator lhs_it(*this); | 871 Iterator lhs_it(*this); |
| 867 Iterator rhs_it(*other_dict); | 872 Iterator rhs_it(*other_dict); |
| 868 while (!lhs_it.IsAtEnd() && !rhs_it.IsAtEnd()) { | 873 while (!lhs_it.IsAtEnd() && !rhs_it.IsAtEnd()) { |
| 869 if (lhs_it.key() != rhs_it.key() || | 874 if (lhs_it.key() != rhs_it.key() || |
| 870 !lhs_it.value().Equals(&rhs_it.value())) { | 875 !lhs_it.value().Equals(&rhs_it.value())) { |
| 871 return false; | 876 return false; |
| 872 } | 877 } |
| 873 lhs_it.Advance(); | 878 lhs_it.Advance(); |
| 874 rhs_it.Advance(); | 879 rhs_it.Advance(); |
| 875 } | 880 } |
| 876 if (!lhs_it.IsAtEnd() || !rhs_it.IsAtEnd()) | 881 if (!lhs_it.IsAtEnd() || !rhs_it.IsAtEnd()) |
| 877 return false; | 882 return false; |
| 878 | 883 |
| 879 return true; | 884 return true; |
| 880 } | 885 } |
| 881 | 886 |
| 882 ///////////////////// ListValue //////////////////// | 887 ///////////////////// ListValue //////////////////// |
| 883 | 888 |
| 884 // static | 889 // static |
| 885 scoped_ptr<ListValue> ListValue::From(scoped_ptr<Value> value) { | 890 std::unique_ptr<ListValue> ListValue::From(std::unique_ptr<Value> value) { |
| 886 ListValue* out; | 891 ListValue* out; |
| 887 if (value && value->GetAsList(&out)) { | 892 if (value && value->GetAsList(&out)) { |
| 888 ignore_result(value.release()); | 893 ignore_result(value.release()); |
| 889 return make_scoped_ptr(out); | 894 return WrapUnique(out); |
| 890 } | 895 } |
| 891 return nullptr; | 896 return nullptr; |
| 892 } | 897 } |
| 893 | 898 |
| 894 ListValue::ListValue() : Value(TYPE_LIST) { | 899 ListValue::ListValue() : Value(TYPE_LIST) { |
| 895 } | 900 } |
| 896 | 901 |
| 897 ListValue::~ListValue() { | 902 ListValue::~ListValue() { |
| 898 Clear(); | 903 Clear(); |
| 899 } | 904 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 914 Append(CreateNullValue()); | 919 Append(CreateNullValue()); |
| 915 Append(in_value); | 920 Append(in_value); |
| 916 } else { | 921 } else { |
| 917 DCHECK(list_[index] != in_value); | 922 DCHECK(list_[index] != in_value); |
| 918 delete list_[index]; | 923 delete list_[index]; |
| 919 list_[index] = in_value; | 924 list_[index] = in_value; |
| 920 } | 925 } |
| 921 return true; | 926 return true; |
| 922 } | 927 } |
| 923 | 928 |
| 924 bool ListValue::Set(size_t index, scoped_ptr<Value> in_value) { | 929 bool ListValue::Set(size_t index, std::unique_ptr<Value> in_value) { |
| 925 return Set(index, in_value.release()); | 930 return Set(index, in_value.release()); |
| 926 } | 931 } |
| 927 | 932 |
| 928 bool ListValue::Get(size_t index, const Value** out_value) const { | 933 bool ListValue::Get(size_t index, const Value** out_value) const { |
| 929 if (index >= list_.size()) | 934 if (index >= list_.size()) |
| 930 return false; | 935 return false; |
| 931 | 936 |
| 932 if (out_value) | 937 if (out_value) |
| 933 *out_value = list_[index]; | 938 *out_value = list_[index]; |
| 934 | 939 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1029 | 1034 |
| 1030 return true; | 1035 return true; |
| 1031 } | 1036 } |
| 1032 | 1037 |
| 1033 bool ListValue::GetList(size_t index, ListValue** out_value) { | 1038 bool ListValue::GetList(size_t index, ListValue** out_value) { |
| 1034 return static_cast<const ListValue&>(*this).GetList( | 1039 return static_cast<const ListValue&>(*this).GetList( |
| 1035 index, | 1040 index, |
| 1036 const_cast<const ListValue**>(out_value)); | 1041 const_cast<const ListValue**>(out_value)); |
| 1037 } | 1042 } |
| 1038 | 1043 |
| 1039 bool ListValue::Remove(size_t index, scoped_ptr<Value>* out_value) { | 1044 bool ListValue::Remove(size_t index, std::unique_ptr<Value>* out_value) { |
| 1040 if (index >= list_.size()) | 1045 if (index >= list_.size()) |
| 1041 return false; | 1046 return false; |
| 1042 | 1047 |
| 1043 if (out_value) | 1048 if (out_value) |
| 1044 out_value->reset(list_[index]); | 1049 out_value->reset(list_[index]); |
| 1045 else | 1050 else |
| 1046 delete list_[index]; | 1051 delete list_[index]; |
| 1047 | 1052 |
| 1048 list_.erase(list_.begin() + index); | 1053 list_.erase(list_.begin() + index); |
| 1049 return true; | 1054 return true; |
| 1050 } | 1055 } |
| 1051 | 1056 |
| 1052 bool ListValue::Remove(const Value& value, size_t* index) { | 1057 bool ListValue::Remove(const Value& value, size_t* index) { |
| 1053 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) { | 1058 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) { |
| 1054 if ((*i)->Equals(&value)) { | 1059 if ((*i)->Equals(&value)) { |
| 1055 size_t previous_index = i - list_.begin(); | 1060 size_t previous_index = i - list_.begin(); |
| 1056 delete *i; | 1061 delete *i; |
| 1057 list_.erase(i); | 1062 list_.erase(i); |
| 1058 | 1063 |
| 1059 if (index) | 1064 if (index) |
| 1060 *index = previous_index; | 1065 *index = previous_index; |
| 1061 return true; | 1066 return true; |
| 1062 } | 1067 } |
| 1063 } | 1068 } |
| 1064 return false; | 1069 return false; |
| 1065 } | 1070 } |
| 1066 | 1071 |
| 1067 ListValue::iterator ListValue::Erase(iterator iter, | 1072 ListValue::iterator ListValue::Erase(iterator iter, |
| 1068 scoped_ptr<Value>* out_value) { | 1073 std::unique_ptr<Value>* out_value) { |
| 1069 if (out_value) | 1074 if (out_value) |
| 1070 out_value->reset(*iter); | 1075 out_value->reset(*iter); |
| 1071 else | 1076 else |
| 1072 delete *iter; | 1077 delete *iter; |
| 1073 | 1078 |
| 1074 return list_.erase(iter); | 1079 return list_.erase(iter); |
| 1075 } | 1080 } |
| 1076 | 1081 |
| 1077 void ListValue::Append(scoped_ptr<Value> in_value) { | 1082 void ListValue::Append(std::unique_ptr<Value> in_value) { |
| 1078 Append(in_value.release()); | 1083 Append(in_value.release()); |
| 1079 } | 1084 } |
| 1080 | 1085 |
| 1081 void ListValue::Append(Value* in_value) { | 1086 void ListValue::Append(Value* in_value) { |
| 1082 DCHECK(in_value); | 1087 DCHECK(in_value); |
| 1083 list_.push_back(in_value); | 1088 list_.push_back(in_value); |
| 1084 } | 1089 } |
| 1085 | 1090 |
| 1086 void ListValue::AppendBoolean(bool in_value) { | 1091 void ListValue::AppendBoolean(bool in_value) { |
| 1087 Append(new FundamentalValue(in_value)); | 1092 Append(new FundamentalValue(in_value)); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1160 | 1165 |
| 1161 ListValue* ListValue::DeepCopy() const { | 1166 ListValue* ListValue::DeepCopy() const { |
| 1162 ListValue* result = new ListValue; | 1167 ListValue* result = new ListValue; |
| 1163 | 1168 |
| 1164 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) | 1169 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) |
| 1165 result->Append((*i)->DeepCopy()); | 1170 result->Append((*i)->DeepCopy()); |
| 1166 | 1171 |
| 1167 return result; | 1172 return result; |
| 1168 } | 1173 } |
| 1169 | 1174 |
| 1170 scoped_ptr<ListValue> ListValue::CreateDeepCopy() const { | 1175 std::unique_ptr<ListValue> ListValue::CreateDeepCopy() const { |
| 1171 return make_scoped_ptr(DeepCopy()); | 1176 return WrapUnique(DeepCopy()); |
| 1172 } | 1177 } |
| 1173 | 1178 |
| 1174 bool ListValue::Equals(const Value* other) const { | 1179 bool ListValue::Equals(const Value* other) const { |
| 1175 if (other->GetType() != GetType()) | 1180 if (other->GetType() != GetType()) |
| 1176 return false; | 1181 return false; |
| 1177 | 1182 |
| 1178 const ListValue* other_list = | 1183 const ListValue* other_list = |
| 1179 static_cast<const ListValue*>(other); | 1184 static_cast<const ListValue*>(other); |
| 1180 const_iterator lhs_it, rhs_it; | 1185 const_iterator lhs_it, rhs_it; |
| 1181 for (lhs_it = begin(), rhs_it = other_list->begin(); | 1186 for (lhs_it = begin(), rhs_it = other_list->begin(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1196 ValueDeserializer::~ValueDeserializer() { | 1201 ValueDeserializer::~ValueDeserializer() { |
| 1197 } | 1202 } |
| 1198 | 1203 |
| 1199 std::ostream& operator<<(std::ostream& out, const Value& value) { | 1204 std::ostream& operator<<(std::ostream& out, const Value& value) { |
| 1200 std::string json; | 1205 std::string json; |
| 1201 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); | 1206 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
| 1202 return out << json; | 1207 return out << json; |
| 1203 } | 1208 } |
| 1204 | 1209 |
| 1205 } // namespace base | 1210 } // namespace base |
| OLD | NEW |