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 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 } | 283 } |
284 | 284 |
285 bool Value::GetAsString(string16* out_value) const { | 285 bool Value::GetAsString(string16* out_value) const { |
286 if (out_value && is_string()) { | 286 if (out_value && is_string()) { |
287 *out_value = UTF8ToUTF16(*string_value_); | 287 *out_value = UTF8ToUTF16(*string_value_); |
288 return true; | 288 return true; |
289 } | 289 } |
290 return is_string(); | 290 return is_string(); |
291 } | 291 } |
292 | 292 |
293 bool Value::GetAsString(const StringValue** out_value) const { | 293 bool Value::GetAsString(const Value** out_value) const { |
294 if (out_value && is_string()) { | 294 if (out_value && is_string()) { |
295 *out_value = static_cast<const StringValue*>(this); | 295 *out_value = static_cast<const Value*>(this); |
296 return true; | 296 return true; |
297 } | 297 } |
298 return is_string(); | 298 return is_string(); |
299 } | 299 } |
300 | 300 |
301 bool Value::GetAsString(StringPiece* out_value) const { | 301 bool Value::GetAsString(StringPiece* out_value) const { |
302 if (out_value && is_string()) { | 302 if (out_value && is_string()) { |
303 *out_value = *string_value_; | 303 *out_value = *string_value_; |
304 return true; | 304 return true; |
305 } | 305 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 switch (type()) { | 352 switch (type()) { |
353 case Type::NONE: | 353 case Type::NONE: |
354 return CreateNullValue().release(); | 354 return CreateNullValue().release(); |
355 | 355 |
356 case Type::BOOLEAN: | 356 case Type::BOOLEAN: |
357 return new Value(bool_value_); | 357 return new Value(bool_value_); |
358 case Type::INTEGER: | 358 case Type::INTEGER: |
359 return new Value(int_value_); | 359 return new Value(int_value_); |
360 case Type::DOUBLE: | 360 case Type::DOUBLE: |
361 return new Value(double_value_); | 361 return new Value(double_value_); |
362 // For now, make StringValues for backward-compatibility. Convert to | |
363 // Value when that code is deleted. | |
364 case Type::STRING: | 362 case Type::STRING: |
365 return new StringValue(*string_value_); | 363 return new Value(*string_value_); |
366 // For now, make BinaryValues for backward-compatibility. Convert to | 364 // For now, make BinaryValues for backward-compatibility. Convert to |
367 // Value when that code is deleted. | 365 // Value when that code is deleted. |
368 case Type::BINARY: | 366 case Type::BINARY: |
369 return new BinaryValue(*binary_value_); | 367 return new Value(*binary_value_); |
370 | 368 |
371 // TODO(crbug.com/646113): Clean this up when DictionaryValue and ListValue | 369 // TODO(crbug.com/646113): Clean this up when DictionaryValue and ListValue |
372 // are completely inlined. | 370 // are completely inlined. |
373 case Type::DICTIONARY: { | 371 case Type::DICTIONARY: { |
374 DictionaryValue* result = new DictionaryValue; | 372 DictionaryValue* result = new DictionaryValue; |
375 | 373 |
376 for (const auto& current_entry : **dict_ptr_) { | 374 for (const auto& current_entry : **dict_ptr_) { |
377 result->SetWithoutPathExpansion(current_entry.first, | 375 result->SetWithoutPathExpansion(current_entry.first, |
378 current_entry.second->CreateDeepCopy()); | 376 current_entry.second->CreateDeepCopy()); |
379 } | 377 } |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 | 674 |
677 void DictionaryValue::SetInteger(StringPiece path, int in_value) { | 675 void DictionaryValue::SetInteger(StringPiece path, int in_value) { |
678 Set(path, new Value(in_value)); | 676 Set(path, new Value(in_value)); |
679 } | 677 } |
680 | 678 |
681 void DictionaryValue::SetDouble(StringPiece path, double in_value) { | 679 void DictionaryValue::SetDouble(StringPiece path, double in_value) { |
682 Set(path, new Value(in_value)); | 680 Set(path, new Value(in_value)); |
683 } | 681 } |
684 | 682 |
685 void DictionaryValue::SetString(StringPiece path, StringPiece in_value) { | 683 void DictionaryValue::SetString(StringPiece path, StringPiece in_value) { |
686 Set(path, new StringValue(in_value)); | 684 Set(path, new Value(in_value)); |
687 } | 685 } |
688 | 686 |
689 void DictionaryValue::SetString(StringPiece path, const string16& in_value) { | 687 void DictionaryValue::SetString(StringPiece path, const string16& in_value) { |
690 Set(path, new StringValue(in_value)); | 688 Set(path, new Value(in_value)); |
691 } | 689 } |
692 | 690 |
693 void DictionaryValue::SetWithoutPathExpansion(StringPiece key, | 691 void DictionaryValue::SetWithoutPathExpansion(StringPiece key, |
694 std::unique_ptr<Value> in_value) { | 692 std::unique_ptr<Value> in_value) { |
695 (**dict_ptr_)[key.as_string()] = std::move(in_value); | 693 (**dict_ptr_)[key.as_string()] = std::move(in_value); |
696 } | 694 } |
697 | 695 |
698 void DictionaryValue::SetWithoutPathExpansion(StringPiece key, | 696 void DictionaryValue::SetWithoutPathExpansion(StringPiece key, |
699 Value* in_value) { | 697 Value* in_value) { |
700 SetWithoutPathExpansion(key, WrapUnique(in_value)); | 698 SetWithoutPathExpansion(key, WrapUnique(in_value)); |
701 } | 699 } |
702 | 700 |
703 void DictionaryValue::SetBooleanWithoutPathExpansion(StringPiece path, | 701 void DictionaryValue::SetBooleanWithoutPathExpansion(StringPiece path, |
704 bool in_value) { | 702 bool in_value) { |
705 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); | 703 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); |
706 } | 704 } |
707 | 705 |
708 void DictionaryValue::SetIntegerWithoutPathExpansion(StringPiece path, | 706 void DictionaryValue::SetIntegerWithoutPathExpansion(StringPiece path, |
709 int in_value) { | 707 int in_value) { |
710 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); | 708 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); |
711 } | 709 } |
712 | 710 |
713 void DictionaryValue::SetDoubleWithoutPathExpansion(StringPiece path, | 711 void DictionaryValue::SetDoubleWithoutPathExpansion(StringPiece path, |
714 double in_value) { | 712 double in_value) { |
715 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); | 713 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); |
716 } | 714 } |
717 | 715 |
718 void DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, | 716 void DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, |
719 StringPiece in_value) { | 717 StringPiece in_value) { |
720 SetWithoutPathExpansion(path, base::MakeUnique<base::StringValue>(in_value)); | 718 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); |
721 } | 719 } |
722 | 720 |
723 void DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, | 721 void DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, |
724 const string16& in_value) { | 722 const string16& in_value) { |
725 SetWithoutPathExpansion(path, base::MakeUnique<base::StringValue>(in_value)); | 723 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); |
726 } | 724 } |
727 | 725 |
728 bool DictionaryValue::Get(StringPiece path, | 726 bool DictionaryValue::Get(StringPiece path, |
729 const Value** out_value) const { | 727 const Value** out_value) const { |
730 DCHECK(IsStringUTF8(path)); | 728 DCHECK(IsStringUTF8(path)); |
731 StringPiece current_path(path); | 729 StringPiece current_path(path); |
732 const DictionaryValue* current_dictionary = this; | 730 const DictionaryValue* current_dictionary = this; |
733 for (size_t delimiter_position = current_path.find('.'); | 731 for (size_t delimiter_position = current_path.find('.'); |
734 delimiter_position != std::string::npos; | 732 delimiter_position != std::string::npos; |
735 delimiter_position = current_path.find('.')) { | 733 delimiter_position = current_path.find('.')) { |
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1274 | 1272 |
1275 void ListValue::AppendInteger(int in_value) { | 1273 void ListValue::AppendInteger(int in_value) { |
1276 Append(MakeUnique<Value>(in_value)); | 1274 Append(MakeUnique<Value>(in_value)); |
1277 } | 1275 } |
1278 | 1276 |
1279 void ListValue::AppendDouble(double in_value) { | 1277 void ListValue::AppendDouble(double in_value) { |
1280 Append(MakeUnique<Value>(in_value)); | 1278 Append(MakeUnique<Value>(in_value)); |
1281 } | 1279 } |
1282 | 1280 |
1283 void ListValue::AppendString(StringPiece in_value) { | 1281 void ListValue::AppendString(StringPiece in_value) { |
1284 Append(MakeUnique<StringValue>(in_value)); | 1282 Append(MakeUnique<Value>(in_value)); |
1285 } | 1283 } |
1286 | 1284 |
1287 void ListValue::AppendString(const string16& in_value) { | 1285 void ListValue::AppendString(const string16& in_value) { |
1288 Append(MakeUnique<StringValue>(in_value)); | 1286 Append(MakeUnique<Value>(in_value)); |
1289 } | 1287 } |
1290 | 1288 |
1291 void ListValue::AppendStrings(const std::vector<std::string>& in_values) { | 1289 void ListValue::AppendStrings(const std::vector<std::string>& in_values) { |
1292 for (std::vector<std::string>::const_iterator it = in_values.begin(); | 1290 for (std::vector<std::string>::const_iterator it = in_values.begin(); |
1293 it != in_values.end(); ++it) { | 1291 it != in_values.end(); ++it) { |
1294 AppendString(*it); | 1292 AppendString(*it); |
1295 } | 1293 } |
1296 } | 1294 } |
1297 | 1295 |
1298 void ListValue::AppendStrings(const std::vector<string16>& in_values) { | 1296 void ListValue::AppendStrings(const std::vector<string16>& in_values) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1354 } | 1352 } |
1355 | 1353 |
1356 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { | 1354 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { |
1357 if (static_cast<int>(type) < 0 || | 1355 if (static_cast<int>(type) < 0 || |
1358 static_cast<size_t>(type) >= arraysize(kTypeNames)) | 1356 static_cast<size_t>(type) >= arraysize(kTypeNames)) |
1359 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; | 1357 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; |
1360 return out << Value::GetTypeName(type); | 1358 return out << Value::GetTypeName(type); |
1361 } | 1359 } |
1362 | 1360 |
1363 } // namespace base | 1361 } // namespace base |
OLD | NEW |