| 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 | 13 |
| 13 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/move.h" | 16 #include "base/move.h" |
| 16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 18 | 19 |
| 19 namespace base { | 20 namespace base { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 scoped_ptr<Value> CopyWithoutEmptyChildren(const Value& node); | 24 scoped_ptr<Value> CopyWithoutEmptyChildren(const Value& node); |
| 24 | 25 |
| 25 // Make a deep copy of |node|, but don't include empty lists or dictionaries | 26 // Make a deep copy of |node|, but don't include empty lists or dictionaries |
| 26 // in the copy. It's possible for this function to return NULL and it | 27 // in the copy. It's possible for this function to return NULL and it |
| 27 // expects |node| to always be non-NULL. | 28 // expects |node| to always be non-NULL. |
| 28 scoped_ptr<ListValue> CopyListWithoutEmptyChildren(const ListValue& list) { | 29 scoped_ptr<ListValue> CopyListWithoutEmptyChildren(const ListValue& list) { |
| 29 scoped_ptr<ListValue> copy; | 30 scoped_ptr<ListValue> copy; |
| 30 for (ListValue::const_iterator it = list.begin(); it != list.end(); ++it) { | 31 for (ListValue::const_iterator it = list.begin(); it != list.end(); ++it) { |
| 31 scoped_ptr<Value> child_copy = CopyWithoutEmptyChildren(**it); | 32 scoped_ptr<Value> child_copy = CopyWithoutEmptyChildren(**it); |
| 32 if (child_copy) { | 33 if (child_copy) { |
| 33 if (!copy) | 34 if (!copy) |
| 34 copy.reset(new ListValue); | 35 copy.reset(new ListValue); |
| 35 copy->Append(child_copy.Pass()); | 36 copy->Append(std::move(child_copy)); |
| 36 } | 37 } |
| 37 } | 38 } |
| 38 return copy; | 39 return copy; |
| 39 } | 40 } |
| 40 | 41 |
| 41 scoped_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren( | 42 scoped_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren( |
| 42 const DictionaryValue& dict) { | 43 const DictionaryValue& dict) { |
| 43 scoped_ptr<DictionaryValue> copy; | 44 scoped_ptr<DictionaryValue> copy; |
| 44 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | 45 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
| 45 scoped_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.value()); | 46 scoped_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.value()); |
| 46 if (child_copy) { | 47 if (child_copy) { |
| 47 if (!copy) | 48 if (!copy) |
| 48 copy.reset(new DictionaryValue); | 49 copy.reset(new DictionaryValue); |
| 49 copy->SetWithoutPathExpansion(it.key(), child_copy.Pass()); | 50 copy->SetWithoutPathExpansion(it.key(), std::move(child_copy)); |
| 50 } | 51 } |
| 51 } | 52 } |
| 52 return copy; | 53 return copy; |
| 53 } | 54 } |
| 54 | 55 |
| 55 scoped_ptr<Value> CopyWithoutEmptyChildren(const Value& node) { | 56 scoped_ptr<Value> CopyWithoutEmptyChildren(const Value& node) { |
| 56 switch (node.GetType()) { | 57 switch (node.GetType()) { |
| 57 case Value::TYPE_LIST: | 58 case Value::TYPE_LIST: |
| 58 return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node)); | 59 return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node)); |
| 59 | 60 |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 } | 307 } |
| 307 | 308 |
| 308 ///////////////////// BinaryValue //////////////////// | 309 ///////////////////// BinaryValue //////////////////// |
| 309 | 310 |
| 310 BinaryValue::BinaryValue() | 311 BinaryValue::BinaryValue() |
| 311 : Value(TYPE_BINARY), | 312 : Value(TYPE_BINARY), |
| 312 size_(0) { | 313 size_(0) { |
| 313 } | 314 } |
| 314 | 315 |
| 315 BinaryValue::BinaryValue(scoped_ptr<char[]> buffer, size_t size) | 316 BinaryValue::BinaryValue(scoped_ptr<char[]> buffer, size_t size) |
| 316 : Value(TYPE_BINARY), | 317 : Value(TYPE_BINARY), buffer_(std::move(buffer)), size_(size) {} |
| 317 buffer_(buffer.Pass()), | |
| 318 size_(size) { | |
| 319 } | |
| 320 | 318 |
| 321 BinaryValue::~BinaryValue() { | 319 BinaryValue::~BinaryValue() { |
| 322 } | 320 } |
| 323 | 321 |
| 324 // static | 322 // static |
| 325 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, | 323 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, |
| 326 size_t size) { | 324 size_t size) { |
| 327 char* buffer_copy = new char[size]; | 325 char* buffer_copy = new char[size]; |
| 328 memcpy(buffer_copy, buffer, size); | 326 memcpy(buffer_copy, buffer, size); |
| 329 scoped_ptr<char[]> scoped_buffer_copy(buffer_copy); | 327 scoped_ptr<char[]> scoped_buffer_copy(buffer_copy); |
| 330 return new BinaryValue(scoped_buffer_copy.Pass(), size); | 328 return new BinaryValue(std::move(scoped_buffer_copy), size); |
| 331 } | 329 } |
| 332 | 330 |
| 333 bool BinaryValue::GetAsBinary(const BinaryValue** out_value) const { | 331 bool BinaryValue::GetAsBinary(const BinaryValue** out_value) const { |
| 334 if (out_value) | 332 if (out_value) |
| 335 *out_value = this; | 333 *out_value = this; |
| 336 return true; | 334 return true; |
| 337 } | 335 } |
| 338 | 336 |
| 339 BinaryValue* BinaryValue::DeepCopy() const { | 337 BinaryValue* BinaryValue::DeepCopy() const { |
| 340 return CreateWithCopiedBuffer(buffer_.get(), size_); | 338 return CreateWithCopiedBuffer(buffer_.get(), size_); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 DictionaryValue* child_dictionary = NULL; | 410 DictionaryValue* child_dictionary = NULL; |
| 413 if (!current_dictionary->GetDictionary(key, &child_dictionary)) { | 411 if (!current_dictionary->GetDictionary(key, &child_dictionary)) { |
| 414 child_dictionary = new DictionaryValue; | 412 child_dictionary = new DictionaryValue; |
| 415 current_dictionary->SetWithoutPathExpansion(key, child_dictionary); | 413 current_dictionary->SetWithoutPathExpansion(key, child_dictionary); |
| 416 } | 414 } |
| 417 | 415 |
| 418 current_dictionary = child_dictionary; | 416 current_dictionary = child_dictionary; |
| 419 current_path.erase(0, delimiter_position + 1); | 417 current_path.erase(0, delimiter_position + 1); |
| 420 } | 418 } |
| 421 | 419 |
| 422 current_dictionary->SetWithoutPathExpansion(current_path, in_value.Pass()); | 420 current_dictionary->SetWithoutPathExpansion(current_path, |
| 421 std::move(in_value)); |
| 423 } | 422 } |
| 424 | 423 |
| 425 void DictionaryValue::Set(const std::string& path, Value* in_value) { | 424 void DictionaryValue::Set(const std::string& path, Value* in_value) { |
| 426 Set(path, make_scoped_ptr(in_value)); | 425 Set(path, make_scoped_ptr(in_value)); |
| 427 } | 426 } |
| 428 | 427 |
| 429 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) { | 428 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) { |
| 430 Set(path, new FundamentalValue(in_value)); | 429 Set(path, new FundamentalValue(in_value)); |
| 431 } | 430 } |
| 432 | 431 |
| (...skipping 762 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1195 ValueDeserializer::~ValueDeserializer() { | 1194 ValueDeserializer::~ValueDeserializer() { |
| 1196 } | 1195 } |
| 1197 | 1196 |
| 1198 std::ostream& operator<<(std::ostream& out, const Value& value) { | 1197 std::ostream& operator<<(std::ostream& out, const Value& value) { |
| 1199 std::string json; | 1198 std::string json; |
| 1200 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); | 1199 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
| 1201 return out << json; | 1200 return out << json; |
| 1202 } | 1201 } |
| 1203 | 1202 |
| 1204 } // namespace base | 1203 } // namespace base |
| OLD | NEW |