Chromecast: Add media_caps.h to unblock internal builds
[chromium-blink-merge.git] / base / values.cc
blobb478b620ca56b0e2a3ed13ff2a007841dd5d9de7
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
3 // found in the LICENSE file.
5 #include "base/values.h"
7 #include <string.h>
9 #include <algorithm>
10 #include <ostream>
12 #include "base/float_util.h"
13 #include "base/json/json_writer.h"
14 #include "base/logging.h"
15 #include "base/move.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
19 namespace base {
21 namespace {
23 // Make a deep copy of |node|, but don't include empty lists or dictionaries
24 // in the copy. It's possible for this function to return NULL and it
25 // expects |node| to always be non-NULL.
26 Value* CopyWithoutEmptyChildren(const Value* node) {
27 DCHECK(node);
28 switch (node->GetType()) {
29 case Value::TYPE_LIST: {
30 const ListValue* list = static_cast<const ListValue*>(node);
31 ListValue* copy = new ListValue;
32 for (ListValue::const_iterator it = list->begin(); it != list->end();
33 ++it) {
34 Value* child_copy = CopyWithoutEmptyChildren(*it);
35 if (child_copy)
36 copy->Append(child_copy);
38 if (!copy->empty())
39 return copy;
41 delete copy;
42 return NULL;
45 case Value::TYPE_DICTIONARY: {
46 const DictionaryValue* dict = static_cast<const DictionaryValue*>(node);
47 DictionaryValue* copy = new DictionaryValue;
48 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) {
49 Value* child_copy = CopyWithoutEmptyChildren(&it.value());
50 if (child_copy)
51 copy->SetWithoutPathExpansion(it.key(), child_copy);
53 if (!copy->empty())
54 return copy;
56 delete copy;
57 return NULL;
60 default:
61 // For everything else, just make a copy.
62 return node->DeepCopy();
66 // A small functor for comparing Values for std::find_if and similar.
67 class ValueEquals {
68 public:
69 // Pass the value against which all consecutive calls of the () operator will
70 // compare their argument to. This Value object must not be destroyed while
71 // the ValueEquals is in use.
72 explicit ValueEquals(const Value* first) : first_(first) { }
74 bool operator ()(const Value* second) const {
75 return first_->Equals(second);
78 private:
79 const Value* first_;
82 } // namespace
84 Value::~Value() {
87 // static
88 Value* Value::CreateNullValue() {
89 return new Value(TYPE_NULL);
92 bool Value::GetAsBoolean(bool* out_value) const {
93 return false;
96 bool Value::GetAsInteger(int* out_value) const {
97 return false;
100 bool Value::GetAsDouble(double* out_value) const {
101 return false;
104 bool Value::GetAsString(std::string* out_value) const {
105 return false;
108 bool Value::GetAsString(string16* out_value) const {
109 return false;
112 bool Value::GetAsString(const StringValue** out_value) const {
113 return false;
116 bool Value::GetAsList(ListValue** out_value) {
117 return false;
120 bool Value::GetAsList(const ListValue** out_value) const {
121 return false;
124 bool Value::GetAsDictionary(DictionaryValue** out_value) {
125 return false;
128 bool Value::GetAsDictionary(const DictionaryValue** out_value) const {
129 return false;
132 Value* Value::DeepCopy() const {
133 // This method should only be getting called for null Values--all subclasses
134 // need to provide their own implementation;.
135 DCHECK(IsType(TYPE_NULL));
136 return CreateNullValue();
139 bool Value::Equals(const Value* other) const {
140 // This method should only be getting called for null Values--all subclasses
141 // need to provide their own implementation;.
142 DCHECK(IsType(TYPE_NULL));
143 return other->IsType(TYPE_NULL);
146 // static
147 bool Value::Equals(const Value* a, const Value* b) {
148 if ((a == NULL) && (b == NULL)) return true;
149 if ((a == NULL) ^ (b == NULL)) return false;
150 return a->Equals(b);
153 Value::Value(Type type) : type_(type) {}
155 Value::Value(const Value& that) : type_(that.type_) {}
157 Value& Value::operator=(const Value& that) {
158 type_ = that.type_;
159 return *this;
162 ///////////////////// FundamentalValue ////////////////////
164 FundamentalValue::FundamentalValue(bool in_value)
165 : Value(TYPE_BOOLEAN), boolean_value_(in_value) {
168 FundamentalValue::FundamentalValue(int in_value)
169 : Value(TYPE_INTEGER), integer_value_(in_value) {
172 FundamentalValue::FundamentalValue(double in_value)
173 : Value(TYPE_DOUBLE), double_value_(in_value) {
174 if (!IsFinite(double_value_)) {
175 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
176 << "values cannot be represented in JSON";
177 double_value_ = 0.0;
181 FundamentalValue::~FundamentalValue() {
184 bool FundamentalValue::GetAsBoolean(bool* out_value) const {
185 if (out_value && IsType(TYPE_BOOLEAN))
186 *out_value = boolean_value_;
187 return (IsType(TYPE_BOOLEAN));
190 bool FundamentalValue::GetAsInteger(int* out_value) const {
191 if (out_value && IsType(TYPE_INTEGER))
192 *out_value = integer_value_;
193 return (IsType(TYPE_INTEGER));
196 bool FundamentalValue::GetAsDouble(double* out_value) const {
197 if (out_value && IsType(TYPE_DOUBLE))
198 *out_value = double_value_;
199 else if (out_value && IsType(TYPE_INTEGER))
200 *out_value = integer_value_;
201 return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER));
204 FundamentalValue* FundamentalValue::DeepCopy() const {
205 switch (GetType()) {
206 case TYPE_BOOLEAN:
207 return new FundamentalValue(boolean_value_);
209 case TYPE_INTEGER:
210 return new FundamentalValue(integer_value_);
212 case TYPE_DOUBLE:
213 return new FundamentalValue(double_value_);
215 default:
216 NOTREACHED();
217 return NULL;
221 bool FundamentalValue::Equals(const Value* other) const {
222 if (other->GetType() != GetType())
223 return false;
225 switch (GetType()) {
226 case TYPE_BOOLEAN: {
227 bool lhs, rhs;
228 return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs;
230 case TYPE_INTEGER: {
231 int lhs, rhs;
232 return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs;
234 case TYPE_DOUBLE: {
235 double lhs, rhs;
236 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs;
238 default:
239 NOTREACHED();
240 return false;
244 ///////////////////// StringValue ////////////////////
246 StringValue::StringValue(const std::string& in_value)
247 : Value(TYPE_STRING),
248 value_(in_value) {
249 DCHECK(IsStringUTF8(in_value));
252 StringValue::StringValue(const string16& in_value)
253 : Value(TYPE_STRING),
254 value_(UTF16ToUTF8(in_value)) {
257 StringValue::~StringValue() {
260 std::string* StringValue::GetString() {
261 return &value_;
264 const std::string& StringValue::GetString() const {
265 return value_;
268 bool StringValue::GetAsString(std::string* out_value) const {
269 if (out_value)
270 *out_value = value_;
271 return true;
274 bool StringValue::GetAsString(string16* out_value) const {
275 if (out_value)
276 *out_value = UTF8ToUTF16(value_);
277 return true;
280 bool StringValue::GetAsString(const StringValue** out_value) const {
281 if (out_value)
282 *out_value = this;
283 return true;
286 StringValue* StringValue::DeepCopy() const {
287 return new StringValue(value_);
290 bool StringValue::Equals(const Value* other) const {
291 if (other->GetType() != GetType())
292 return false;
293 std::string lhs, rhs;
294 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
297 ///////////////////// BinaryValue ////////////////////
299 BinaryValue::BinaryValue()
300 : Value(TYPE_BINARY),
301 size_(0) {
304 BinaryValue::BinaryValue(scoped_ptr<char[]> buffer, size_t size)
305 : Value(TYPE_BINARY),
306 buffer_(buffer.Pass()),
307 size_(size) {
310 BinaryValue::~BinaryValue() {
313 // static
314 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer,
315 size_t size) {
316 char* buffer_copy = new char[size];
317 memcpy(buffer_copy, buffer, size);
318 scoped_ptr<char[]> scoped_buffer_copy(buffer_copy);
319 return new BinaryValue(scoped_buffer_copy.Pass(), size);
322 BinaryValue* BinaryValue::DeepCopy() const {
323 return CreateWithCopiedBuffer(buffer_.get(), size_);
326 bool BinaryValue::Equals(const Value* other) const {
327 if (other->GetType() != GetType())
328 return false;
329 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other);
330 if (other_binary->size_ != size_)
331 return false;
332 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_);
335 ///////////////////// DictionaryValue ////////////////////
337 DictionaryValue::DictionaryValue()
338 : Value(TYPE_DICTIONARY) {
341 DictionaryValue::~DictionaryValue() {
342 Clear();
345 bool DictionaryValue::GetAsDictionary(DictionaryValue** out_value) {
346 if (out_value)
347 *out_value = this;
348 return true;
351 bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const {
352 if (out_value)
353 *out_value = this;
354 return true;
357 bool DictionaryValue::HasKey(const std::string& key) const {
358 DCHECK(IsStringUTF8(key));
359 ValueMap::const_iterator current_entry = dictionary_.find(key);
360 DCHECK((current_entry == dictionary_.end()) || current_entry->second);
361 return current_entry != dictionary_.end();
364 void DictionaryValue::Clear() {
365 ValueMap::iterator dict_iterator = dictionary_.begin();
366 while (dict_iterator != dictionary_.end()) {
367 delete dict_iterator->second;
368 ++dict_iterator;
371 dictionary_.clear();
374 void DictionaryValue::Set(const std::string& path, scoped_ptr<Value> in_value) {
375 DCHECK(IsStringUTF8(path));
376 DCHECK(in_value);
378 std::string current_path(path);
379 DictionaryValue* current_dictionary = this;
380 for (size_t delimiter_position = current_path.find('.');
381 delimiter_position != std::string::npos;
382 delimiter_position = current_path.find('.')) {
383 // Assume that we're indexing into a dictionary.
384 std::string key(current_path, 0, delimiter_position);
385 DictionaryValue* child_dictionary = NULL;
386 if (!current_dictionary->GetDictionary(key, &child_dictionary)) {
387 child_dictionary = new DictionaryValue;
388 current_dictionary->SetWithoutPathExpansion(key, child_dictionary);
391 current_dictionary = child_dictionary;
392 current_path.erase(0, delimiter_position + 1);
395 current_dictionary->SetWithoutPathExpansion(current_path, in_value.Pass());
398 void DictionaryValue::Set(const std::string& path, Value* in_value) {
399 Set(path, make_scoped_ptr(in_value));
402 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) {
403 Set(path, new FundamentalValue(in_value));
406 void DictionaryValue::SetInteger(const std::string& path, int in_value) {
407 Set(path, new FundamentalValue(in_value));
410 void DictionaryValue::SetDouble(const std::string& path, double in_value) {
411 Set(path, new FundamentalValue(in_value));
414 void DictionaryValue::SetString(const std::string& path,
415 const std::string& in_value) {
416 Set(path, new StringValue(in_value));
419 void DictionaryValue::SetString(const std::string& path,
420 const string16& in_value) {
421 Set(path, new StringValue(in_value));
424 void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
425 scoped_ptr<Value> in_value) {
426 Value* bare_ptr = in_value.release();
427 // If there's an existing value here, we need to delete it, because
428 // we own all our children.
429 std::pair<ValueMap::iterator, bool> ins_res =
430 dictionary_.insert(std::make_pair(key, bare_ptr));
431 if (!ins_res.second) {
432 DCHECK_NE(ins_res.first->second, bare_ptr); // This would be bogus
433 delete ins_res.first->second;
434 ins_res.first->second = bare_ptr;
438 void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
439 Value* in_value) {
440 SetWithoutPathExpansion(key, make_scoped_ptr(in_value));
443 void DictionaryValue::SetBooleanWithoutPathExpansion(
444 const std::string& path, bool in_value) {
445 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
448 void DictionaryValue::SetIntegerWithoutPathExpansion(
449 const std::string& path, int in_value) {
450 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
453 void DictionaryValue::SetDoubleWithoutPathExpansion(
454 const std::string& path, double in_value) {
455 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
458 void DictionaryValue::SetStringWithoutPathExpansion(
459 const std::string& path, const std::string& in_value) {
460 SetWithoutPathExpansion(path, new StringValue(in_value));
463 void DictionaryValue::SetStringWithoutPathExpansion(
464 const std::string& path, const string16& in_value) {
465 SetWithoutPathExpansion(path, new StringValue(in_value));
468 bool DictionaryValue::Get(const std::string& path,
469 const Value** out_value) const {
470 DCHECK(IsStringUTF8(path));
471 std::string current_path(path);
472 const DictionaryValue* current_dictionary = this;
473 for (size_t delimiter_position = current_path.find('.');
474 delimiter_position != std::string::npos;
475 delimiter_position = current_path.find('.')) {
476 const DictionaryValue* child_dictionary = NULL;
477 if (!current_dictionary->GetDictionary(
478 current_path.substr(0, delimiter_position), &child_dictionary))
479 return false;
481 current_dictionary = child_dictionary;
482 current_path.erase(0, delimiter_position + 1);
485 return current_dictionary->GetWithoutPathExpansion(current_path, out_value);
488 bool DictionaryValue::Get(const std::string& path, Value** out_value) {
489 return static_cast<const DictionaryValue&>(*this).Get(
490 path,
491 const_cast<const Value**>(out_value));
494 bool DictionaryValue::GetBoolean(const std::string& path,
495 bool* bool_value) const {
496 const Value* value;
497 if (!Get(path, &value))
498 return false;
500 return value->GetAsBoolean(bool_value);
503 bool DictionaryValue::GetInteger(const std::string& path,
504 int* out_value) const {
505 const Value* value;
506 if (!Get(path, &value))
507 return false;
509 return value->GetAsInteger(out_value);
512 bool DictionaryValue::GetDouble(const std::string& path,
513 double* out_value) const {
514 const Value* value;
515 if (!Get(path, &value))
516 return false;
518 return value->GetAsDouble(out_value);
521 bool DictionaryValue::GetString(const std::string& path,
522 std::string* out_value) const {
523 const Value* value;
524 if (!Get(path, &value))
525 return false;
527 return value->GetAsString(out_value);
530 bool DictionaryValue::GetString(const std::string& path,
531 string16* out_value) const {
532 const Value* value;
533 if (!Get(path, &value))
534 return false;
536 return value->GetAsString(out_value);
539 bool DictionaryValue::GetStringASCII(const std::string& path,
540 std::string* out_value) const {
541 std::string out;
542 if (!GetString(path, &out))
543 return false;
545 if (!IsStringASCII(out)) {
546 NOTREACHED();
547 return false;
550 out_value->assign(out);
551 return true;
554 bool DictionaryValue::GetBinary(const std::string& path,
555 const BinaryValue** out_value) const {
556 const Value* value;
557 bool result = Get(path, &value);
558 if (!result || !value->IsType(TYPE_BINARY))
559 return false;
561 if (out_value)
562 *out_value = static_cast<const BinaryValue*>(value);
564 return true;
567 bool DictionaryValue::GetBinary(const std::string& path,
568 BinaryValue** out_value) {
569 return static_cast<const DictionaryValue&>(*this).GetBinary(
570 path,
571 const_cast<const BinaryValue**>(out_value));
574 bool DictionaryValue::GetDictionary(const std::string& path,
575 const DictionaryValue** out_value) const {
576 const Value* value;
577 bool result = Get(path, &value);
578 if (!result || !value->IsType(TYPE_DICTIONARY))
579 return false;
581 if (out_value)
582 *out_value = static_cast<const DictionaryValue*>(value);
584 return true;
587 bool DictionaryValue::GetDictionary(const std::string& path,
588 DictionaryValue** out_value) {
589 return static_cast<const DictionaryValue&>(*this).GetDictionary(
590 path,
591 const_cast<const DictionaryValue**>(out_value));
594 bool DictionaryValue::GetList(const std::string& path,
595 const ListValue** out_value) const {
596 const Value* value;
597 bool result = Get(path, &value);
598 if (!result || !value->IsType(TYPE_LIST))
599 return false;
601 if (out_value)
602 *out_value = static_cast<const ListValue*>(value);
604 return true;
607 bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) {
608 return static_cast<const DictionaryValue&>(*this).GetList(
609 path,
610 const_cast<const ListValue**>(out_value));
613 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
614 const Value** out_value) const {
615 DCHECK(IsStringUTF8(key));
616 ValueMap::const_iterator entry_iterator = dictionary_.find(key);
617 if (entry_iterator == dictionary_.end())
618 return false;
620 const Value* entry = entry_iterator->second;
621 if (out_value)
622 *out_value = entry;
623 return true;
626 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
627 Value** out_value) {
628 return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion(
629 key,
630 const_cast<const Value**>(out_value));
633 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string& key,
634 bool* out_value) const {
635 const Value* value;
636 if (!GetWithoutPathExpansion(key, &value))
637 return false;
639 return value->GetAsBoolean(out_value);
642 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key,
643 int* out_value) const {
644 const Value* value;
645 if (!GetWithoutPathExpansion(key, &value))
646 return false;
648 return value->GetAsInteger(out_value);
651 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key,
652 double* out_value) const {
653 const Value* value;
654 if (!GetWithoutPathExpansion(key, &value))
655 return false;
657 return value->GetAsDouble(out_value);
660 bool DictionaryValue::GetStringWithoutPathExpansion(
661 const std::string& key,
662 std::string* out_value) const {
663 const Value* value;
664 if (!GetWithoutPathExpansion(key, &value))
665 return false;
667 return value->GetAsString(out_value);
670 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key,
671 string16* out_value) const {
672 const Value* value;
673 if (!GetWithoutPathExpansion(key, &value))
674 return false;
676 return value->GetAsString(out_value);
679 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
680 const std::string& key,
681 const DictionaryValue** out_value) const {
682 const Value* value;
683 bool result = GetWithoutPathExpansion(key, &value);
684 if (!result || !value->IsType(TYPE_DICTIONARY))
685 return false;
687 if (out_value)
688 *out_value = static_cast<const DictionaryValue*>(value);
690 return true;
693 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
694 const std::string& key,
695 DictionaryValue** out_value) {
696 const DictionaryValue& const_this =
697 static_cast<const DictionaryValue&>(*this);
698 return const_this.GetDictionaryWithoutPathExpansion(
699 key,
700 const_cast<const DictionaryValue**>(out_value));
703 bool DictionaryValue::GetListWithoutPathExpansion(
704 const std::string& key,
705 const ListValue** out_value) const {
706 const Value* value;
707 bool result = GetWithoutPathExpansion(key, &value);
708 if (!result || !value->IsType(TYPE_LIST))
709 return false;
711 if (out_value)
712 *out_value = static_cast<const ListValue*>(value);
714 return true;
717 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key,
718 ListValue** out_value) {
719 return
720 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion(
721 key,
722 const_cast<const ListValue**>(out_value));
725 bool DictionaryValue::Remove(const std::string& path,
726 scoped_ptr<Value>* out_value) {
727 DCHECK(IsStringUTF8(path));
728 std::string current_path(path);
729 DictionaryValue* current_dictionary = this;
730 size_t delimiter_position = current_path.rfind('.');
731 if (delimiter_position != std::string::npos) {
732 if (!GetDictionary(current_path.substr(0, delimiter_position),
733 &current_dictionary))
734 return false;
735 current_path.erase(0, delimiter_position + 1);
738 return current_dictionary->RemoveWithoutPathExpansion(current_path,
739 out_value);
742 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string& key,
743 scoped_ptr<Value>* out_value) {
744 DCHECK(IsStringUTF8(key));
745 ValueMap::iterator entry_iterator = dictionary_.find(key);
746 if (entry_iterator == dictionary_.end())
747 return false;
749 Value* entry = entry_iterator->second;
750 if (out_value)
751 out_value->reset(entry);
752 else
753 delete entry;
754 dictionary_.erase(entry_iterator);
755 return true;
758 bool DictionaryValue::RemovePath(const std::string& path,
759 scoped_ptr<Value>* out_value) {
760 bool result = false;
761 size_t delimiter_position = path.find('.');
763 if (delimiter_position == std::string::npos)
764 return RemoveWithoutPathExpansion(path, out_value);
766 const std::string subdict_path = path.substr(0, delimiter_position);
767 DictionaryValue* subdict = NULL;
768 if (!GetDictionary(subdict_path, &subdict))
769 return false;
770 result = subdict->RemovePath(path.substr(delimiter_position + 1),
771 out_value);
772 if (result && subdict->empty())
773 RemoveWithoutPathExpansion(subdict_path, NULL);
775 return result;
778 DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() const {
779 Value* copy = CopyWithoutEmptyChildren(this);
780 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue;
783 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) {
784 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) {
785 const Value* merge_value = &it.value();
786 // Check whether we have to merge dictionaries.
787 if (merge_value->IsType(Value::TYPE_DICTIONARY)) {
788 DictionaryValue* sub_dict;
789 if (GetDictionaryWithoutPathExpansion(it.key(), &sub_dict)) {
790 sub_dict->MergeDictionary(
791 static_cast<const DictionaryValue*>(merge_value));
792 continue;
795 // All other cases: Make a copy and hook it up.
796 SetWithoutPathExpansion(it.key(), merge_value->DeepCopy());
800 void DictionaryValue::Swap(DictionaryValue* other) {
801 dictionary_.swap(other->dictionary_);
804 DictionaryValue::Iterator::Iterator(const DictionaryValue& target)
805 : target_(target),
806 it_(target.dictionary_.begin()) {}
808 DictionaryValue::Iterator::~Iterator() {}
810 DictionaryValue* DictionaryValue::DeepCopy() const {
811 DictionaryValue* result = new DictionaryValue;
813 for (ValueMap::const_iterator current_entry(dictionary_.begin());
814 current_entry != dictionary_.end(); ++current_entry) {
815 result->SetWithoutPathExpansion(current_entry->first,
816 current_entry->second->DeepCopy());
819 return result;
822 bool DictionaryValue::Equals(const Value* other) const {
823 if (other->GetType() != GetType())
824 return false;
826 const DictionaryValue* other_dict =
827 static_cast<const DictionaryValue*>(other);
828 Iterator lhs_it(*this);
829 Iterator rhs_it(*other_dict);
830 while (!lhs_it.IsAtEnd() && !rhs_it.IsAtEnd()) {
831 if (lhs_it.key() != rhs_it.key() ||
832 !lhs_it.value().Equals(&rhs_it.value())) {
833 return false;
835 lhs_it.Advance();
836 rhs_it.Advance();
838 if (!lhs_it.IsAtEnd() || !rhs_it.IsAtEnd())
839 return false;
841 return true;
844 ///////////////////// ListValue ////////////////////
846 ListValue::ListValue() : Value(TYPE_LIST) {
849 ListValue::~ListValue() {
850 Clear();
853 void ListValue::Clear() {
854 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i)
855 delete *i;
856 list_.clear();
859 bool ListValue::Set(size_t index, Value* in_value) {
860 if (!in_value)
861 return false;
863 if (index >= list_.size()) {
864 // Pad out any intermediate indexes with null settings
865 while (index > list_.size())
866 Append(CreateNullValue());
867 Append(in_value);
868 } else {
869 DCHECK(list_[index] != in_value);
870 delete list_[index];
871 list_[index] = in_value;
873 return true;
876 bool ListValue::Get(size_t index, const Value** out_value) const {
877 if (index >= list_.size())
878 return false;
880 if (out_value)
881 *out_value = list_[index];
883 return true;
886 bool ListValue::Get(size_t index, Value** out_value) {
887 return static_cast<const ListValue&>(*this).Get(
888 index,
889 const_cast<const Value**>(out_value));
892 bool ListValue::GetBoolean(size_t index, bool* bool_value) const {
893 const Value* value;
894 if (!Get(index, &value))
895 return false;
897 return value->GetAsBoolean(bool_value);
900 bool ListValue::GetInteger(size_t index, int* out_value) const {
901 const Value* value;
902 if (!Get(index, &value))
903 return false;
905 return value->GetAsInteger(out_value);
908 bool ListValue::GetDouble(size_t index, double* out_value) const {
909 const Value* value;
910 if (!Get(index, &value))
911 return false;
913 return value->GetAsDouble(out_value);
916 bool ListValue::GetString(size_t index, std::string* out_value) const {
917 const Value* value;
918 if (!Get(index, &value))
919 return false;
921 return value->GetAsString(out_value);
924 bool ListValue::GetString(size_t index, string16* out_value) const {
925 const Value* value;
926 if (!Get(index, &value))
927 return false;
929 return value->GetAsString(out_value);
932 bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const {
933 const Value* value;
934 bool result = Get(index, &value);
935 if (!result || !value->IsType(TYPE_BINARY))
936 return false;
938 if (out_value)
939 *out_value = static_cast<const BinaryValue*>(value);
941 return true;
944 bool ListValue::GetBinary(size_t index, BinaryValue** out_value) {
945 return static_cast<const ListValue&>(*this).GetBinary(
946 index,
947 const_cast<const BinaryValue**>(out_value));
950 bool ListValue::GetDictionary(size_t index,
951 const DictionaryValue** out_value) const {
952 const Value* value;
953 bool result = Get(index, &value);
954 if (!result || !value->IsType(TYPE_DICTIONARY))
955 return false;
957 if (out_value)
958 *out_value = static_cast<const DictionaryValue*>(value);
960 return true;
963 bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) {
964 return static_cast<const ListValue&>(*this).GetDictionary(
965 index,
966 const_cast<const DictionaryValue**>(out_value));
969 bool ListValue::GetList(size_t index, const ListValue** out_value) const {
970 const Value* value;
971 bool result = Get(index, &value);
972 if (!result || !value->IsType(TYPE_LIST))
973 return false;
975 if (out_value)
976 *out_value = static_cast<const ListValue*>(value);
978 return true;
981 bool ListValue::GetList(size_t index, ListValue** out_value) {
982 return static_cast<const ListValue&>(*this).GetList(
983 index,
984 const_cast<const ListValue**>(out_value));
987 bool ListValue::Remove(size_t index, scoped_ptr<Value>* out_value) {
988 if (index >= list_.size())
989 return false;
991 if (out_value)
992 out_value->reset(list_[index]);
993 else
994 delete list_[index];
996 list_.erase(list_.begin() + index);
997 return true;
1000 bool ListValue::Remove(const Value& value, size_t* index) {
1001 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) {
1002 if ((*i)->Equals(&value)) {
1003 size_t previous_index = i - list_.begin();
1004 delete *i;
1005 list_.erase(i);
1007 if (index)
1008 *index = previous_index;
1009 return true;
1012 return false;
1015 ListValue::iterator ListValue::Erase(iterator iter,
1016 scoped_ptr<Value>* out_value) {
1017 if (out_value)
1018 out_value->reset(*iter);
1019 else
1020 delete *iter;
1022 return list_.erase(iter);
1025 void ListValue::Append(Value* in_value) {
1026 DCHECK(in_value);
1027 list_.push_back(in_value);
1030 void ListValue::AppendBoolean(bool in_value) {
1031 Append(new FundamentalValue(in_value));
1034 void ListValue::AppendInteger(int in_value) {
1035 Append(new FundamentalValue(in_value));
1038 void ListValue::AppendDouble(double in_value) {
1039 Append(new FundamentalValue(in_value));
1042 void ListValue::AppendString(const std::string& in_value) {
1043 Append(new StringValue(in_value));
1046 void ListValue::AppendString(const string16& in_value) {
1047 Append(new StringValue(in_value));
1050 void ListValue::AppendStrings(const std::vector<std::string>& in_values) {
1051 for (std::vector<std::string>::const_iterator it = in_values.begin();
1052 it != in_values.end(); ++it) {
1053 AppendString(*it);
1057 void ListValue::AppendStrings(const std::vector<string16>& in_values) {
1058 for (std::vector<string16>::const_iterator it = in_values.begin();
1059 it != in_values.end(); ++it) {
1060 AppendString(*it);
1064 bool ListValue::AppendIfNotPresent(Value* in_value) {
1065 DCHECK(in_value);
1066 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) {
1067 if ((*i)->Equals(in_value)) {
1068 delete in_value;
1069 return false;
1072 list_.push_back(in_value);
1073 return true;
1076 bool ListValue::Insert(size_t index, Value* in_value) {
1077 DCHECK(in_value);
1078 if (index > list_.size())
1079 return false;
1081 list_.insert(list_.begin() + index, in_value);
1082 return true;
1085 ListValue::const_iterator ListValue::Find(const Value& value) const {
1086 return std::find_if(list_.begin(), list_.end(), ValueEquals(&value));
1089 void ListValue::Swap(ListValue* other) {
1090 list_.swap(other->list_);
1093 bool ListValue::GetAsList(ListValue** out_value) {
1094 if (out_value)
1095 *out_value = this;
1096 return true;
1099 bool ListValue::GetAsList(const ListValue** out_value) const {
1100 if (out_value)
1101 *out_value = this;
1102 return true;
1105 ListValue* ListValue::DeepCopy() const {
1106 ListValue* result = new ListValue;
1108 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i)
1109 result->Append((*i)->DeepCopy());
1111 return result;
1114 bool ListValue::Equals(const Value* other) const {
1115 if (other->GetType() != GetType())
1116 return false;
1118 const ListValue* other_list =
1119 static_cast<const ListValue*>(other);
1120 const_iterator lhs_it, rhs_it;
1121 for (lhs_it = begin(), rhs_it = other_list->begin();
1122 lhs_it != end() && rhs_it != other_list->end();
1123 ++lhs_it, ++rhs_it) {
1124 if (!(*lhs_it)->Equals(*rhs_it))
1125 return false;
1127 if (lhs_it != end() || rhs_it != other_list->end())
1128 return false;
1130 return true;
1133 ValueSerializer::~ValueSerializer() {
1136 std::ostream& operator<<(std::ostream& out, const Value& value) {
1137 std::string json;
1138 JSONWriter::WriteWithOptions(&value,
1139 JSONWriter::OPTIONS_PRETTY_PRINT,
1140 &json);
1141 return out << json;
1144 } // namespace base