| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/float_util.h" | 7 #include "base/float_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 // static | 90 // static |
| 91 StringValue* Value::CreateStringValue(const std::string& in_value) { | 91 StringValue* Value::CreateStringValue(const std::string& in_value) { |
| 92 return new StringValue(in_value); | 92 return new StringValue(in_value); |
| 93 } | 93 } |
| 94 | 94 |
| 95 // static | 95 // static |
| 96 StringValue* Value::CreateStringValue(const string16& in_value) { | 96 StringValue* Value::CreateStringValue(const string16& in_value) { |
| 97 return new StringValue(in_value); | 97 return new StringValue(in_value); |
| 98 } | 98 } |
| 99 | 99 |
| 100 bool Value::IsNull() const { |
| 101 return type_ == TYPE_NULL; |
| 102 } |
| 103 |
| 104 bool Value::IsBoolean() const { |
| 105 return type_ == TYPE_BOOLEAN; |
| 106 } |
| 107 |
| 108 bool Value::IsInteger() const { |
| 109 return type_ == TYPE_INTEGER; |
| 110 } |
| 111 |
| 112 bool Value::IsDouble() const { |
| 113 return type_ == TYPE_DOUBLE; |
| 114 } |
| 115 |
| 116 bool Value::IsString() const { |
| 117 return type_ == TYPE_STRING; |
| 118 } |
| 119 |
| 120 bool Value::IsBinary() const { |
| 121 return type_ == TYPE_BINARY; |
| 122 } |
| 123 |
| 124 bool Value::IsDictionary() const { |
| 125 return type_ == TYPE_DICTIONARY; |
| 126 } |
| 127 |
| 128 bool Value::IsList() const { |
| 129 return type_ == TYPE_LIST; |
| 130 } |
| 131 |
| 100 bool Value::GetAsBoolean(bool* out_value) const { | 132 bool Value::GetAsBoolean(bool* out_value) const { |
| 101 return false; | 133 return false; |
| 102 } | 134 } |
| 103 | 135 |
| 104 bool Value::GetAsInteger(int* out_value) const { | 136 bool Value::GetAsInteger(int* out_value) const { |
| 105 return false; | 137 return false; |
| 106 } | 138 } |
| 107 | 139 |
| 108 bool Value::GetAsDouble(double* out_value) const { | 140 bool Value::GetAsDouble(double* out_value) const { |
| 109 return false; | 141 return false; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 121 return false; | 153 return false; |
| 122 } | 154 } |
| 123 | 155 |
| 124 bool Value::GetAsList(const ListValue** out_value) const { | 156 bool Value::GetAsList(const ListValue** out_value) const { |
| 125 return false; | 157 return false; |
| 126 } | 158 } |
| 127 | 159 |
| 128 Value* Value::DeepCopy() const { | 160 Value* Value::DeepCopy() const { |
| 129 // This method should only be getting called for null Values--all subclasses | 161 // This method should only be getting called for null Values--all subclasses |
| 130 // need to provide their own implementation;. | 162 // need to provide their own implementation;. |
| 131 DCHECK(IsType(TYPE_NULL)); | 163 DCHECK(IsNull()); |
| 132 return CreateNullValue(); | 164 return CreateNullValue(); |
| 133 } | 165 } |
| 134 | 166 |
| 135 bool Value::Equals(const Value* other) const { | 167 bool Value::Equals(const Value* other) const { |
| 136 // This method should only be getting called for null Values--all subclasses | 168 // This method should only be getting called for null Values--all subclasses |
| 137 // need to provide their own implementation;. | 169 // need to provide their own implementation;. |
| 138 DCHECK(IsType(TYPE_NULL)); | 170 DCHECK(IsNull()); |
| 139 return other->IsType(TYPE_NULL); | 171 return other->IsNull(); |
| 140 } | 172 } |
| 141 | 173 |
| 142 // static | 174 // static |
| 143 bool Value::Equals(const Value* a, const Value* b) { | 175 bool Value::Equals(const Value* a, const Value* b) { |
| 144 if ((a == NULL) && (b == NULL)) return true; | 176 if ((a == NULL) && (b == NULL)) return true; |
| 145 if ((a == NULL) ^ (b == NULL)) return false; | 177 if ((a == NULL) ^ (b == NULL)) return false; |
| 146 return a->Equals(b); | 178 return a->Equals(b); |
| 147 } | 179 } |
| 148 | 180 |
| 149 Value::Value(Type type) : type_(type) { | 181 Value::Value(Type type) : type_(type) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 162 FundamentalValue::FundamentalValue(double in_value) | 194 FundamentalValue::FundamentalValue(double in_value) |
| 163 : Value(TYPE_DOUBLE), double_value_(in_value) { | 195 : Value(TYPE_DOUBLE), double_value_(in_value) { |
| 164 // JSON doesn't support NaN or positive or negative infinity. | 196 // JSON doesn't support NaN or positive or negative infinity. |
| 165 DCHECK(IsFinite(in_value)); | 197 DCHECK(IsFinite(in_value)); |
| 166 } | 198 } |
| 167 | 199 |
| 168 FundamentalValue::~FundamentalValue() { | 200 FundamentalValue::~FundamentalValue() { |
| 169 } | 201 } |
| 170 | 202 |
| 171 bool FundamentalValue::GetAsBoolean(bool* out_value) const { | 203 bool FundamentalValue::GetAsBoolean(bool* out_value) const { |
| 172 if (out_value && IsType(TYPE_BOOLEAN)) | 204 if (out_value && IsBoolean()) |
| 173 *out_value = boolean_value_; | 205 *out_value = boolean_value_; |
| 174 return (IsType(TYPE_BOOLEAN)); | 206 return (IsBoolean()); |
| 175 } | 207 } |
| 176 | 208 |
| 177 bool FundamentalValue::GetAsInteger(int* out_value) const { | 209 bool FundamentalValue::GetAsInteger(int* out_value) const { |
| 178 if (out_value && IsType(TYPE_INTEGER)) | 210 if (out_value && IsInteger()) |
| 179 *out_value = integer_value_; | 211 *out_value = integer_value_; |
| 180 return (IsType(TYPE_INTEGER)); | 212 return (IsInteger()); |
| 181 } | 213 } |
| 182 | 214 |
| 183 bool FundamentalValue::GetAsDouble(double* out_value) const { | 215 bool FundamentalValue::GetAsDouble(double* out_value) const { |
| 184 if (out_value && IsType(TYPE_DOUBLE)) | 216 if (out_value && IsDouble()) |
| 185 *out_value = double_value_; | 217 *out_value = double_value_; |
| 186 else if (out_value && IsType(TYPE_INTEGER)) | 218 else if (out_value && IsInteger()) |
| 187 *out_value = integer_value_; | 219 *out_value = integer_value_; |
| 188 return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER)); | 220 return (IsDouble() || IsInteger()); |
| 189 } | 221 } |
| 190 | 222 |
| 191 FundamentalValue* FundamentalValue::DeepCopy() const { | 223 FundamentalValue* FundamentalValue::DeepCopy() const { |
| 192 switch (GetType()) { | 224 switch (GetType()) { |
| 193 case TYPE_BOOLEAN: | 225 case TYPE_BOOLEAN: |
| 194 return CreateBooleanValue(boolean_value_); | 226 return CreateBooleanValue(boolean_value_); |
| 195 | 227 |
| 196 case TYPE_INTEGER: | 228 case TYPE_INTEGER: |
| 197 return CreateIntegerValue(integer_value_); | 229 return CreateIntegerValue(integer_value_); |
| 198 | 230 |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 } | 507 } |
| 476 | 508 |
| 477 out_value->assign(out); | 509 out_value->assign(out); |
| 478 return true; | 510 return true; |
| 479 } | 511 } |
| 480 | 512 |
| 481 bool DictionaryValue::GetBinary(const std::string& path, | 513 bool DictionaryValue::GetBinary(const std::string& path, |
| 482 BinaryValue** out_value) const { | 514 BinaryValue** out_value) const { |
| 483 Value* value; | 515 Value* value; |
| 484 bool result = Get(path, &value); | 516 bool result = Get(path, &value); |
| 485 if (!result || !value->IsType(TYPE_BINARY)) | 517 if (!result || !value->IsBinary()) |
| 486 return false; | 518 return false; |
| 487 | 519 |
| 488 if (out_value) | 520 if (out_value) |
| 489 *out_value = static_cast<BinaryValue*>(value); | 521 *out_value = static_cast<BinaryValue*>(value); |
| 490 | 522 |
| 491 return true; | 523 return true; |
| 492 } | 524 } |
| 493 | 525 |
| 494 bool DictionaryValue::GetDictionary(const std::string& path, | 526 bool DictionaryValue::GetDictionary(const std::string& path, |
| 495 DictionaryValue** out_value) const { | 527 DictionaryValue** out_value) const { |
| 496 Value* value; | 528 Value* value; |
| 497 bool result = Get(path, &value); | 529 bool result = Get(path, &value); |
| 498 if (!result || !value->IsType(TYPE_DICTIONARY)) | 530 if (!result || !value->IsDictionary()) |
| 499 return false; | 531 return false; |
| 500 | 532 |
| 501 if (out_value) | 533 if (out_value) |
| 502 *out_value = static_cast<DictionaryValue*>(value); | 534 *out_value = static_cast<DictionaryValue*>(value); |
| 503 | 535 |
| 504 return true; | 536 return true; |
| 505 } | 537 } |
| 506 | 538 |
| 507 bool DictionaryValue::GetList(const std::string& path, | 539 bool DictionaryValue::GetList(const std::string& path, |
| 508 ListValue** out_value) const { | 540 ListValue** out_value) const { |
| 509 Value* value; | 541 Value* value; |
| 510 bool result = Get(path, &value); | 542 bool result = Get(path, &value); |
| 511 if (!result || !value->IsType(TYPE_LIST)) | 543 if (!result || !value->IsList()) |
| 512 return false; | 544 return false; |
| 513 | 545 |
| 514 if (out_value) | 546 if (out_value) |
| 515 *out_value = static_cast<ListValue*>(value); | 547 *out_value = static_cast<ListValue*>(value); |
| 516 | 548 |
| 517 return true; | 549 return true; |
| 518 } | 550 } |
| 519 | 551 |
| 520 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, | 552 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, |
| 521 Value** out_value) const { | 553 Value** out_value) const { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 return false; | 598 return false; |
| 567 | 599 |
| 568 return value->GetAsString(out_value); | 600 return value->GetAsString(out_value); |
| 569 } | 601 } |
| 570 | 602 |
| 571 bool DictionaryValue::GetDictionaryWithoutPathExpansion( | 603 bool DictionaryValue::GetDictionaryWithoutPathExpansion( |
| 572 const std::string& key, | 604 const std::string& key, |
| 573 DictionaryValue** out_value) const { | 605 DictionaryValue** out_value) const { |
| 574 Value* value; | 606 Value* value; |
| 575 bool result = GetWithoutPathExpansion(key, &value); | 607 bool result = GetWithoutPathExpansion(key, &value); |
| 576 if (!result || !value->IsType(TYPE_DICTIONARY)) | 608 if (!result || !value->IsDictionary()) |
| 577 return false; | 609 return false; |
| 578 | 610 |
| 579 if (out_value) | 611 if (out_value) |
| 580 *out_value = static_cast<DictionaryValue*>(value); | 612 *out_value = static_cast<DictionaryValue*>(value); |
| 581 | 613 |
| 582 return true; | 614 return true; |
| 583 } | 615 } |
| 584 | 616 |
| 585 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key, | 617 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key, |
| 586 ListValue** out_value) const { | 618 ListValue** out_value) const { |
| 587 Value* value; | 619 Value* value; |
| 588 bool result = GetWithoutPathExpansion(key, &value); | 620 bool result = GetWithoutPathExpansion(key, &value); |
| 589 if (!result || !value->IsType(TYPE_LIST)) | 621 if (!result || !value->IsList()) |
| 590 return false; | 622 return false; |
| 591 | 623 |
| 592 if (out_value) | 624 if (out_value) |
| 593 *out_value = static_cast<ListValue*>(value); | 625 *out_value = static_cast<ListValue*>(value); |
| 594 | 626 |
| 595 return true; | 627 return true; |
| 596 } | 628 } |
| 597 | 629 |
| 598 bool DictionaryValue::Remove(const std::string& path, Value** out_value) { | 630 bool DictionaryValue::Remove(const std::string& path, Value** out_value) { |
| 599 DCHECK(IsStringUTF8(path)); | 631 DCHECK(IsStringUTF8(path)); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 Value* copy = CopyWithoutEmptyChildren(this); | 663 Value* copy = CopyWithoutEmptyChildren(this); |
| 632 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue; | 664 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue; |
| 633 } | 665 } |
| 634 | 666 |
| 635 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { | 667 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { |
| 636 for (DictionaryValue::key_iterator key(dictionary->begin_keys()); | 668 for (DictionaryValue::key_iterator key(dictionary->begin_keys()); |
| 637 key != dictionary->end_keys(); ++key) { | 669 key != dictionary->end_keys(); ++key) { |
| 638 Value* merge_value; | 670 Value* merge_value; |
| 639 if (dictionary->GetWithoutPathExpansion(*key, &merge_value)) { | 671 if (dictionary->GetWithoutPathExpansion(*key, &merge_value)) { |
| 640 // Check whether we have to merge dictionaries. | 672 // Check whether we have to merge dictionaries. |
| 641 if (merge_value->IsType(Value::TYPE_DICTIONARY)) { | 673 if (merge_value->IsDictionary()) { |
| 642 DictionaryValue* sub_dict; | 674 DictionaryValue* sub_dict; |
| 643 if (GetDictionaryWithoutPathExpansion(*key, &sub_dict)) { | 675 if (GetDictionaryWithoutPathExpansion(*key, &sub_dict)) { |
| 644 sub_dict->MergeDictionary( | 676 sub_dict->MergeDictionary( |
| 645 static_cast<const DictionaryValue*>(merge_value)); | 677 static_cast<const DictionaryValue*>(merge_value)); |
| 646 continue; | 678 continue; |
| 647 } | 679 } |
| 648 } | 680 } |
| 649 // All other cases: Make a copy and hook it up. | 681 // All other cases: Make a copy and hook it up. |
| 650 SetWithoutPathExpansion(*key, merge_value->DeepCopy()); | 682 SetWithoutPathExpansion(*key, merge_value->DeepCopy()); |
| 651 } | 683 } |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 Value* value; | 800 Value* value; |
| 769 if (!Get(index, &value)) | 801 if (!Get(index, &value)) |
| 770 return false; | 802 return false; |
| 771 | 803 |
| 772 return value->GetAsString(out_value); | 804 return value->GetAsString(out_value); |
| 773 } | 805 } |
| 774 | 806 |
| 775 bool ListValue::GetBinary(size_t index, BinaryValue** out_value) const { | 807 bool ListValue::GetBinary(size_t index, BinaryValue** out_value) const { |
| 776 Value* value; | 808 Value* value; |
| 777 bool result = Get(index, &value); | 809 bool result = Get(index, &value); |
| 778 if (!result || !value->IsType(TYPE_BINARY)) | 810 if (!result || !value->IsBinary()) |
| 779 return false; | 811 return false; |
| 780 | 812 |
| 781 if (out_value) | 813 if (out_value) |
| 782 *out_value = static_cast<BinaryValue*>(value); | 814 *out_value = static_cast<BinaryValue*>(value); |
| 783 | 815 |
| 784 return true; | 816 return true; |
| 785 } | 817 } |
| 786 | 818 |
| 787 bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) const { | 819 bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) const { |
| 788 Value* value; | 820 Value* value; |
| 789 bool result = Get(index, &value); | 821 bool result = Get(index, &value); |
| 790 if (!result || !value->IsType(TYPE_DICTIONARY)) | 822 if (!result || !value->IsDictionary()) |
| 791 return false; | 823 return false; |
| 792 | 824 |
| 793 if (out_value) | 825 if (out_value) |
| 794 *out_value = static_cast<DictionaryValue*>(value); | 826 *out_value = static_cast<DictionaryValue*>(value); |
| 795 | 827 |
| 796 return true; | 828 return true; |
| 797 } | 829 } |
| 798 | 830 |
| 799 bool ListValue::GetList(size_t index, ListValue** out_value) const { | 831 bool ListValue::GetList(size_t index, ListValue** out_value) const { |
| 800 Value* value; | 832 Value* value; |
| 801 bool result = Get(index, &value); | 833 bool result = Get(index, &value); |
| 802 if (!result || !value->IsType(TYPE_LIST)) | 834 if (!result || !value->IsList()) |
| 803 return false; | 835 return false; |
| 804 | 836 |
| 805 if (out_value) | 837 if (out_value) |
| 806 *out_value = static_cast<ListValue*>(value); | 838 *out_value = static_cast<ListValue*>(value); |
| 807 | 839 |
| 808 return true; | 840 return true; |
| 809 } | 841 } |
| 810 | 842 |
| 811 bool ListValue::Remove(size_t index, Value** out_value) { | 843 bool ListValue::Remove(size_t index, Value** out_value) { |
| 812 if (index >= list_.size()) | 844 if (index >= list_.size()) |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 899 if (lhs_it != end() || rhs_it != other_list->end()) | 931 if (lhs_it != end() || rhs_it != other_list->end()) |
| 900 return false; | 932 return false; |
| 901 | 933 |
| 902 return true; | 934 return true; |
| 903 } | 935 } |
| 904 | 936 |
| 905 ValueSerializer::~ValueSerializer() { | 937 ValueSerializer::~ValueSerializer() { |
| 906 } | 938 } |
| 907 | 939 |
| 908 } // namespace base | 940 } // namespace base |
| OLD | NEW |