Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: base/values.cc

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/values.h ('k') | base/values_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/values.h"
6
7 #include <string.h>
8
9 #include <algorithm>
10 #include <cmath>
11 #include <ostream>
12
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"
18
19 namespace base {
20
21 namespace {
22
23 scoped_ptr<Value> CopyWithoutEmptyChildren(const Value& node);
24
25 // 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 // expects |node| to always be non-NULL.
28 scoped_ptr<ListValue> CopyListWithoutEmptyChildren(const ListValue& list) {
29 scoped_ptr<ListValue> copy;
30 for (ListValue::const_iterator it = list.begin(); it != list.end(); ++it) {
31 scoped_ptr<Value> child_copy = CopyWithoutEmptyChildren(**it);
32 if (child_copy) {
33 if (!copy)
34 copy.reset(new ListValue);
35 copy->Append(child_copy.Pass());
36 }
37 }
38 return copy;
39 }
40
41 scoped_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren(
42 const DictionaryValue& dict) {
43 scoped_ptr<DictionaryValue> copy;
44 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
45 scoped_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.value());
46 if (child_copy) {
47 if (!copy)
48 copy.reset(new DictionaryValue);
49 copy->SetWithoutPathExpansion(it.key(), child_copy.Pass());
50 }
51 }
52 return copy;
53 }
54
55 scoped_ptr<Value> CopyWithoutEmptyChildren(const Value& node) {
56 switch (node.GetType()) {
57 case Value::TYPE_LIST:
58 return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node));
59
60 case Value::TYPE_DICTIONARY:
61 return CopyDictionaryWithoutEmptyChildren(
62 static_cast<const DictionaryValue&>(node));
63
64 default:
65 return node.CreateDeepCopy();
66 }
67 }
68
69 // A small functor for comparing Values for std::find_if and similar.
70 class ValueEquals {
71 public:
72 // Pass the value against which all consecutive calls of the () operator will
73 // compare their argument to. This Value object must not be destroyed while
74 // the ValueEquals is in use.
75 explicit ValueEquals(const Value* first) : first_(first) { }
76
77 bool operator ()(const Value* second) const {
78 return first_->Equals(second);
79 }
80
81 private:
82 const Value* first_;
83 };
84
85 } // namespace
86
87 Value::~Value() {
88 }
89
90 // static
91 scoped_ptr<Value> Value::CreateNullValue() {
92 return make_scoped_ptr(new Value(TYPE_NULL));
93 }
94
95 bool Value::GetAsBinary(const BinaryValue** out_value) const {
96 return false;
97 }
98
99 bool Value::GetAsBoolean(bool* out_value) const {
100 return false;
101 }
102
103 bool Value::GetAsInteger(int* out_value) const {
104 return false;
105 }
106
107 bool Value::GetAsDouble(double* out_value) const {
108 return false;
109 }
110
111 bool Value::GetAsString(std::string* out_value) const {
112 return false;
113 }
114
115 bool Value::GetAsString(string16* out_value) const {
116 return false;
117 }
118
119 bool Value::GetAsString(const StringValue** out_value) const {
120 return false;
121 }
122
123 bool Value::GetAsList(ListValue** out_value) {
124 return false;
125 }
126
127 bool Value::GetAsList(const ListValue** out_value) const {
128 return false;
129 }
130
131 bool Value::GetAsDictionary(DictionaryValue** out_value) {
132 return false;
133 }
134
135 bool Value::GetAsDictionary(const DictionaryValue** out_value) const {
136 return false;
137 }
138
139 Value* Value::DeepCopy() 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 CreateNullValue().release();
144 }
145
146 scoped_ptr<Value> Value::CreateDeepCopy() const {
147 return make_scoped_ptr(DeepCopy());
148 }
149
150 bool Value::Equals(const Value* other) const {
151 // This method should only be getting called for null Values--all subclasses
152 // need to provide their own implementation;.
153 DCHECK(IsType(TYPE_NULL));
154 return other->IsType(TYPE_NULL);
155 }
156
157 // static
158 bool Value::Equals(const Value* a, const Value* b) {
159 if ((a == NULL) && (b == NULL)) return true;
160 if ((a == NULL) ^ (b == NULL)) return false;
161 return a->Equals(b);
162 }
163
164 Value::Value(Type type) : type_(type) {}
165
166 Value::Value(const Value& that) : type_(that.type_) {}
167
168 Value& Value::operator=(const Value& that) {
169 type_ = that.type_;
170 return *this;
171 }
172
173 ///////////////////// FundamentalValue ////////////////////
174
175 FundamentalValue::FundamentalValue(bool in_value)
176 : Value(TYPE_BOOLEAN), boolean_value_(in_value) {
177 }
178
179 FundamentalValue::FundamentalValue(int in_value)
180 : Value(TYPE_INTEGER), integer_value_(in_value) {
181 }
182
183 FundamentalValue::FundamentalValue(double in_value)
184 : Value(TYPE_DOUBLE), double_value_(in_value) {
185 if (!std::isfinite(double_value_)) {
186 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
187 << "values cannot be represented in JSON";
188 double_value_ = 0.0;
189 }
190 }
191
192 FundamentalValue::~FundamentalValue() {
193 }
194
195 bool FundamentalValue::GetAsBoolean(bool* out_value) const {
196 if (out_value && IsType(TYPE_BOOLEAN))
197 *out_value = boolean_value_;
198 return (IsType(TYPE_BOOLEAN));
199 }
200
201 bool FundamentalValue::GetAsInteger(int* out_value) const {
202 if (out_value && IsType(TYPE_INTEGER))
203 *out_value = integer_value_;
204 return (IsType(TYPE_INTEGER));
205 }
206
207 bool FundamentalValue::GetAsDouble(double* out_value) const {
208 if (out_value && IsType(TYPE_DOUBLE))
209 *out_value = double_value_;
210 else if (out_value && IsType(TYPE_INTEGER))
211 *out_value = integer_value_;
212 return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER));
213 }
214
215 FundamentalValue* FundamentalValue::DeepCopy() const {
216 switch (GetType()) {
217 case TYPE_BOOLEAN:
218 return new FundamentalValue(boolean_value_);
219
220 case TYPE_INTEGER:
221 return new FundamentalValue(integer_value_);
222
223 case TYPE_DOUBLE:
224 return new FundamentalValue(double_value_);
225
226 default:
227 NOTREACHED();
228 return NULL;
229 }
230 }
231
232 bool FundamentalValue::Equals(const Value* other) const {
233 if (other->GetType() != GetType())
234 return false;
235
236 switch (GetType()) {
237 case TYPE_BOOLEAN: {
238 bool lhs, rhs;
239 return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs;
240 }
241 case TYPE_INTEGER: {
242 int lhs, rhs;
243 return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs;
244 }
245 case TYPE_DOUBLE: {
246 double lhs, rhs;
247 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs;
248 }
249 default:
250 NOTREACHED();
251 return false;
252 }
253 }
254
255 ///////////////////// StringValue ////////////////////
256
257 StringValue::StringValue(const std::string& in_value)
258 : Value(TYPE_STRING),
259 value_(in_value) {
260 DCHECK(IsStringUTF8(in_value));
261 }
262
263 StringValue::StringValue(const string16& in_value)
264 : Value(TYPE_STRING),
265 value_(UTF16ToUTF8(in_value)) {
266 }
267
268 StringValue::~StringValue() {
269 }
270
271 std::string* StringValue::GetString() {
272 return &value_;
273 }
274
275 const std::string& StringValue::GetString() const {
276 return value_;
277 }
278
279 bool StringValue::GetAsString(std::string* out_value) const {
280 if (out_value)
281 *out_value = value_;
282 return true;
283 }
284
285 bool StringValue::GetAsString(string16* out_value) const {
286 if (out_value)
287 *out_value = UTF8ToUTF16(value_);
288 return true;
289 }
290
291 bool StringValue::GetAsString(const StringValue** out_value) const {
292 if (out_value)
293 *out_value = this;
294 return true;
295 }
296
297 StringValue* StringValue::DeepCopy() const {
298 return new StringValue(value_);
299 }
300
301 bool StringValue::Equals(const Value* other) const {
302 if (other->GetType() != GetType())
303 return false;
304 std::string lhs, rhs;
305 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
306 }
307
308 ///////////////////// BinaryValue ////////////////////
309
310 BinaryValue::BinaryValue()
311 : Value(TYPE_BINARY),
312 size_(0) {
313 }
314
315 BinaryValue::BinaryValue(scoped_ptr<char[]> buffer, size_t size)
316 : Value(TYPE_BINARY),
317 buffer_(buffer.Pass()),
318 size_(size) {
319 }
320
321 BinaryValue::~BinaryValue() {
322 }
323
324 // static
325 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer,
326 size_t size) {
327 char* buffer_copy = new char[size];
328 memcpy(buffer_copy, buffer, size);
329 scoped_ptr<char[]> scoped_buffer_copy(buffer_copy);
330 return new BinaryValue(scoped_buffer_copy.Pass(), size);
331 }
332
333 bool BinaryValue::GetAsBinary(const BinaryValue** out_value) const {
334 if (out_value)
335 *out_value = this;
336 return true;
337 }
338
339 BinaryValue* BinaryValue::DeepCopy() const {
340 return CreateWithCopiedBuffer(buffer_.get(), size_);
341 }
342
343 bool BinaryValue::Equals(const Value* other) const {
344 if (other->GetType() != GetType())
345 return false;
346 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other);
347 if (other_binary->size_ != size_)
348 return false;
349 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_);
350 }
351
352 ///////////////////// DictionaryValue ////////////////////
353
354 DictionaryValue::DictionaryValue()
355 : Value(TYPE_DICTIONARY) {
356 }
357
358 DictionaryValue::~DictionaryValue() {
359 Clear();
360 }
361
362 bool DictionaryValue::GetAsDictionary(DictionaryValue** out_value) {
363 if (out_value)
364 *out_value = this;
365 return true;
366 }
367
368 bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const {
369 if (out_value)
370 *out_value = this;
371 return true;
372 }
373
374 bool DictionaryValue::HasKey(const std::string& key) const {
375 DCHECK(IsStringUTF8(key));
376 ValueMap::const_iterator current_entry = dictionary_.find(key);
377 DCHECK((current_entry == dictionary_.end()) || current_entry->second);
378 return current_entry != dictionary_.end();
379 }
380
381 void DictionaryValue::Clear() {
382 ValueMap::iterator dict_iterator = dictionary_.begin();
383 while (dict_iterator != dictionary_.end()) {
384 delete dict_iterator->second;
385 ++dict_iterator;
386 }
387
388 dictionary_.clear();
389 }
390
391 void DictionaryValue::Set(const std::string& path, scoped_ptr<Value> in_value) {
392 DCHECK(IsStringUTF8(path));
393 DCHECK(in_value);
394
395 std::string current_path(path);
396 DictionaryValue* current_dictionary = this;
397 for (size_t delimiter_position = current_path.find('.');
398 delimiter_position != std::string::npos;
399 delimiter_position = current_path.find('.')) {
400 // Assume that we're indexing into a dictionary.
401 std::string key(current_path, 0, delimiter_position);
402 DictionaryValue* child_dictionary = NULL;
403 if (!current_dictionary->GetDictionary(key, &child_dictionary)) {
404 child_dictionary = new DictionaryValue;
405 current_dictionary->SetWithoutPathExpansion(key, child_dictionary);
406 }
407
408 current_dictionary = child_dictionary;
409 current_path.erase(0, delimiter_position + 1);
410 }
411
412 current_dictionary->SetWithoutPathExpansion(current_path, in_value.Pass());
413 }
414
415 void DictionaryValue::Set(const std::string& path, Value* in_value) {
416 Set(path, make_scoped_ptr(in_value));
417 }
418
419 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) {
420 Set(path, new FundamentalValue(in_value));
421 }
422
423 void DictionaryValue::SetInteger(const std::string& path, int in_value) {
424 Set(path, new FundamentalValue(in_value));
425 }
426
427 void DictionaryValue::SetDouble(const std::string& path, double in_value) {
428 Set(path, new FundamentalValue(in_value));
429 }
430
431 void DictionaryValue::SetString(const std::string& path,
432 const std::string& in_value) {
433 Set(path, new StringValue(in_value));
434 }
435
436 void DictionaryValue::SetString(const std::string& path,
437 const string16& in_value) {
438 Set(path, new StringValue(in_value));
439 }
440
441 void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
442 scoped_ptr<Value> in_value) {
443 Value* bare_ptr = in_value.release();
444 // If there's an existing value here, we need to delete it, because
445 // we own all our children.
446 std::pair<ValueMap::iterator, bool> ins_res =
447 dictionary_.insert(std::make_pair(key, bare_ptr));
448 if (!ins_res.second) {
449 DCHECK_NE(ins_res.first->second, bare_ptr); // This would be bogus
450 delete ins_res.first->second;
451 ins_res.first->second = bare_ptr;
452 }
453 }
454
455 void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
456 Value* in_value) {
457 SetWithoutPathExpansion(key, make_scoped_ptr(in_value));
458 }
459
460 void DictionaryValue::SetBooleanWithoutPathExpansion(
461 const std::string& path, bool in_value) {
462 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
463 }
464
465 void DictionaryValue::SetIntegerWithoutPathExpansion(
466 const std::string& path, int in_value) {
467 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
468 }
469
470 void DictionaryValue::SetDoubleWithoutPathExpansion(
471 const std::string& path, double in_value) {
472 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
473 }
474
475 void DictionaryValue::SetStringWithoutPathExpansion(
476 const std::string& path, const std::string& in_value) {
477 SetWithoutPathExpansion(path, new StringValue(in_value));
478 }
479
480 void DictionaryValue::SetStringWithoutPathExpansion(
481 const std::string& path, const string16& in_value) {
482 SetWithoutPathExpansion(path, new StringValue(in_value));
483 }
484
485 bool DictionaryValue::Get(StringPiece path, const Value** out_value) const {
486 DCHECK(IsStringUTF8(path));
487 StringPiece current_path(path);
488 const DictionaryValue* current_dictionary = this;
489 for (size_t delimiter_position = current_path.find('.');
490 delimiter_position != std::string::npos;
491 delimiter_position = current_path.find('.')) {
492 const DictionaryValue* child_dictionary = NULL;
493 if (!current_dictionary->GetDictionaryWithoutPathExpansion(
494 current_path.substr(0, delimiter_position).as_string(),
495 &child_dictionary)) {
496 return false;
497 }
498
499 current_dictionary = child_dictionary;
500 current_path = current_path.substr(delimiter_position + 1);
501 }
502
503 return current_dictionary->GetWithoutPathExpansion(current_path.as_string(),
504 out_value);
505 }
506
507 bool DictionaryValue::Get(StringPiece path, Value** out_value) {
508 return static_cast<const DictionaryValue&>(*this).Get(
509 path,
510 const_cast<const Value**>(out_value));
511 }
512
513 bool DictionaryValue::GetBoolean(const std::string& path,
514 bool* bool_value) const {
515 const Value* value;
516 if (!Get(path, &value))
517 return false;
518
519 return value->GetAsBoolean(bool_value);
520 }
521
522 bool DictionaryValue::GetInteger(const std::string& path,
523 int* out_value) const {
524 const Value* value;
525 if (!Get(path, &value))
526 return false;
527
528 return value->GetAsInteger(out_value);
529 }
530
531 bool DictionaryValue::GetDouble(const std::string& path,
532 double* out_value) const {
533 const Value* value;
534 if (!Get(path, &value))
535 return false;
536
537 return value->GetAsDouble(out_value);
538 }
539
540 bool DictionaryValue::GetString(const std::string& path,
541 std::string* out_value) const {
542 const Value* value;
543 if (!Get(path, &value))
544 return false;
545
546 return value->GetAsString(out_value);
547 }
548
549 bool DictionaryValue::GetString(const std::string& path,
550 string16* out_value) const {
551 const Value* value;
552 if (!Get(path, &value))
553 return false;
554
555 return value->GetAsString(out_value);
556 }
557
558 bool DictionaryValue::GetStringASCII(const std::string& path,
559 std::string* out_value) const {
560 std::string out;
561 if (!GetString(path, &out))
562 return false;
563
564 if (!IsStringASCII(out)) {
565 NOTREACHED();
566 return false;
567 }
568
569 out_value->assign(out);
570 return true;
571 }
572
573 bool DictionaryValue::GetBinary(const std::string& path,
574 const BinaryValue** out_value) const {
575 const Value* value;
576 bool result = Get(path, &value);
577 if (!result || !value->IsType(TYPE_BINARY))
578 return false;
579
580 if (out_value)
581 *out_value = static_cast<const BinaryValue*>(value);
582
583 return true;
584 }
585
586 bool DictionaryValue::GetBinary(const std::string& path,
587 BinaryValue** out_value) {
588 return static_cast<const DictionaryValue&>(*this).GetBinary(
589 path,
590 const_cast<const BinaryValue**>(out_value));
591 }
592
593 bool DictionaryValue::GetDictionary(StringPiece path,
594 const DictionaryValue** out_value) const {
595 const Value* value;
596 bool result = Get(path, &value);
597 if (!result || !value->IsType(TYPE_DICTIONARY))
598 return false;
599
600 if (out_value)
601 *out_value = static_cast<const DictionaryValue*>(value);
602
603 return true;
604 }
605
606 bool DictionaryValue::GetDictionary(StringPiece path,
607 DictionaryValue** out_value) {
608 return static_cast<const DictionaryValue&>(*this).GetDictionary(
609 path,
610 const_cast<const DictionaryValue**>(out_value));
611 }
612
613 bool DictionaryValue::GetList(const std::string& path,
614 const ListValue** out_value) const {
615 const Value* value;
616 bool result = Get(path, &value);
617 if (!result || !value->IsType(TYPE_LIST))
618 return false;
619
620 if (out_value)
621 *out_value = static_cast<const ListValue*>(value);
622
623 return true;
624 }
625
626 bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) {
627 return static_cast<const DictionaryValue&>(*this).GetList(
628 path,
629 const_cast<const ListValue**>(out_value));
630 }
631
632 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
633 const Value** out_value) const {
634 DCHECK(IsStringUTF8(key));
635 ValueMap::const_iterator entry_iterator = dictionary_.find(key);
636 if (entry_iterator == dictionary_.end())
637 return false;
638
639 const Value* entry = entry_iterator->second;
640 if (out_value)
641 *out_value = entry;
642 return true;
643 }
644
645 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
646 Value** out_value) {
647 return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion(
648 key,
649 const_cast<const Value**>(out_value));
650 }
651
652 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string& key,
653 bool* out_value) const {
654 const Value* value;
655 if (!GetWithoutPathExpansion(key, &value))
656 return false;
657
658 return value->GetAsBoolean(out_value);
659 }
660
661 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key,
662 int* out_value) const {
663 const Value* value;
664 if (!GetWithoutPathExpansion(key, &value))
665 return false;
666
667 return value->GetAsInteger(out_value);
668 }
669
670 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key,
671 double* out_value) const {
672 const Value* value;
673 if (!GetWithoutPathExpansion(key, &value))
674 return false;
675
676 return value->GetAsDouble(out_value);
677 }
678
679 bool DictionaryValue::GetStringWithoutPathExpansion(
680 const std::string& key,
681 std::string* out_value) const {
682 const Value* value;
683 if (!GetWithoutPathExpansion(key, &value))
684 return false;
685
686 return value->GetAsString(out_value);
687 }
688
689 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key,
690 string16* out_value) const {
691 const Value* value;
692 if (!GetWithoutPathExpansion(key, &value))
693 return false;
694
695 return value->GetAsString(out_value);
696 }
697
698 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
699 const std::string& key,
700 const DictionaryValue** out_value) const {
701 const Value* value;
702 bool result = GetWithoutPathExpansion(key, &value);
703 if (!result || !value->IsType(TYPE_DICTIONARY))
704 return false;
705
706 if (out_value)
707 *out_value = static_cast<const DictionaryValue*>(value);
708
709 return true;
710 }
711
712 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
713 const std::string& key,
714 DictionaryValue** out_value) {
715 const DictionaryValue& const_this =
716 static_cast<const DictionaryValue&>(*this);
717 return const_this.GetDictionaryWithoutPathExpansion(
718 key,
719 const_cast<const DictionaryValue**>(out_value));
720 }
721
722 bool DictionaryValue::GetListWithoutPathExpansion(
723 const std::string& key,
724 const ListValue** out_value) const {
725 const Value* value;
726 bool result = GetWithoutPathExpansion(key, &value);
727 if (!result || !value->IsType(TYPE_LIST))
728 return false;
729
730 if (out_value)
731 *out_value = static_cast<const ListValue*>(value);
732
733 return true;
734 }
735
736 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key,
737 ListValue** out_value) {
738 return
739 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion(
740 key,
741 const_cast<const ListValue**>(out_value));
742 }
743
744 bool DictionaryValue::Remove(const std::string& path,
745 scoped_ptr<Value>* out_value) {
746 DCHECK(IsStringUTF8(path));
747 std::string current_path(path);
748 DictionaryValue* current_dictionary = this;
749 size_t delimiter_position = current_path.rfind('.');
750 if (delimiter_position != std::string::npos) {
751 if (!GetDictionary(current_path.substr(0, delimiter_position),
752 &current_dictionary))
753 return false;
754 current_path.erase(0, delimiter_position + 1);
755 }
756
757 return current_dictionary->RemoveWithoutPathExpansion(current_path,
758 out_value);
759 }
760
761 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string& key,
762 scoped_ptr<Value>* out_value) {
763 DCHECK(IsStringUTF8(key));
764 ValueMap::iterator entry_iterator = dictionary_.find(key);
765 if (entry_iterator == dictionary_.end())
766 return false;
767
768 Value* entry = entry_iterator->second;
769 if (out_value)
770 out_value->reset(entry);
771 else
772 delete entry;
773 dictionary_.erase(entry_iterator);
774 return true;
775 }
776
777 bool DictionaryValue::RemovePath(const std::string& path,
778 scoped_ptr<Value>* out_value) {
779 bool result = false;
780 size_t delimiter_position = path.find('.');
781
782 if (delimiter_position == std::string::npos)
783 return RemoveWithoutPathExpansion(path, out_value);
784
785 const std::string subdict_path = path.substr(0, delimiter_position);
786 DictionaryValue* subdict = NULL;
787 if (!GetDictionary(subdict_path, &subdict))
788 return false;
789 result = subdict->RemovePath(path.substr(delimiter_position + 1),
790 out_value);
791 if (result && subdict->empty())
792 RemoveWithoutPathExpansion(subdict_path, NULL);
793
794 return result;
795 }
796
797 scoped_ptr<DictionaryValue> DictionaryValue::DeepCopyWithoutEmptyChildren()
798 const {
799 scoped_ptr<DictionaryValue> copy = CopyDictionaryWithoutEmptyChildren(*this);
800 if (!copy)
801 copy.reset(new DictionaryValue);
802 return copy;
803 }
804
805 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) {
806 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) {
807 const Value* merge_value = &it.value();
808 // Check whether we have to merge dictionaries.
809 if (merge_value->IsType(Value::TYPE_DICTIONARY)) {
810 DictionaryValue* sub_dict;
811 if (GetDictionaryWithoutPathExpansion(it.key(), &sub_dict)) {
812 sub_dict->MergeDictionary(
813 static_cast<const DictionaryValue*>(merge_value));
814 continue;
815 }
816 }
817 // All other cases: Make a copy and hook it up.
818 SetWithoutPathExpansion(it.key(), merge_value->DeepCopy());
819 }
820 }
821
822 void DictionaryValue::Swap(DictionaryValue* other) {
823 dictionary_.swap(other->dictionary_);
824 }
825
826 DictionaryValue::Iterator::Iterator(const DictionaryValue& target)
827 : target_(target),
828 it_(target.dictionary_.begin()) {}
829
830 DictionaryValue::Iterator::~Iterator() {}
831
832 DictionaryValue* DictionaryValue::DeepCopy() const {
833 DictionaryValue* result = new DictionaryValue;
834
835 for (ValueMap::const_iterator current_entry(dictionary_.begin());
836 current_entry != dictionary_.end(); ++current_entry) {
837 result->SetWithoutPathExpansion(current_entry->first,
838 current_entry->second->DeepCopy());
839 }
840
841 return result;
842 }
843
844 scoped_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const {
845 return make_scoped_ptr(DeepCopy());
846 }
847
848 bool DictionaryValue::Equals(const Value* other) const {
849 if (other->GetType() != GetType())
850 return false;
851
852 const DictionaryValue* other_dict =
853 static_cast<const DictionaryValue*>(other);
854 Iterator lhs_it(*this);
855 Iterator rhs_it(*other_dict);
856 while (!lhs_it.IsAtEnd() && !rhs_it.IsAtEnd()) {
857 if (lhs_it.key() != rhs_it.key() ||
858 !lhs_it.value().Equals(&rhs_it.value())) {
859 return false;
860 }
861 lhs_it.Advance();
862 rhs_it.Advance();
863 }
864 if (!lhs_it.IsAtEnd() || !rhs_it.IsAtEnd())
865 return false;
866
867 return true;
868 }
869
870 ///////////////////// ListValue ////////////////////
871
872 ListValue::ListValue() : Value(TYPE_LIST) {
873 }
874
875 ListValue::~ListValue() {
876 Clear();
877 }
878
879 void ListValue::Clear() {
880 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i)
881 delete *i;
882 list_.clear();
883 }
884
885 bool ListValue::Set(size_t index, Value* in_value) {
886 if (!in_value)
887 return false;
888
889 if (index >= list_.size()) {
890 // Pad out any intermediate indexes with null settings
891 while (index > list_.size())
892 Append(CreateNullValue());
893 Append(in_value);
894 } else {
895 DCHECK(list_[index] != in_value);
896 delete list_[index];
897 list_[index] = in_value;
898 }
899 return true;
900 }
901
902 bool ListValue::Set(size_t index, scoped_ptr<Value> in_value) {
903 return Set(index, in_value.release());
904 }
905
906 bool ListValue::Get(size_t index, const Value** out_value) const {
907 if (index >= list_.size())
908 return false;
909
910 if (out_value)
911 *out_value = list_[index];
912
913 return true;
914 }
915
916 bool ListValue::Get(size_t index, Value** out_value) {
917 return static_cast<const ListValue&>(*this).Get(
918 index,
919 const_cast<const Value**>(out_value));
920 }
921
922 bool ListValue::GetBoolean(size_t index, bool* bool_value) const {
923 const Value* value;
924 if (!Get(index, &value))
925 return false;
926
927 return value->GetAsBoolean(bool_value);
928 }
929
930 bool ListValue::GetInteger(size_t index, int* out_value) const {
931 const Value* value;
932 if (!Get(index, &value))
933 return false;
934
935 return value->GetAsInteger(out_value);
936 }
937
938 bool ListValue::GetDouble(size_t index, double* out_value) const {
939 const Value* value;
940 if (!Get(index, &value))
941 return false;
942
943 return value->GetAsDouble(out_value);
944 }
945
946 bool ListValue::GetString(size_t index, std::string* out_value) const {
947 const Value* value;
948 if (!Get(index, &value))
949 return false;
950
951 return value->GetAsString(out_value);
952 }
953
954 bool ListValue::GetString(size_t index, string16* out_value) const {
955 const Value* value;
956 if (!Get(index, &value))
957 return false;
958
959 return value->GetAsString(out_value);
960 }
961
962 bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const {
963 const Value* value;
964 bool result = Get(index, &value);
965 if (!result || !value->IsType(TYPE_BINARY))
966 return false;
967
968 if (out_value)
969 *out_value = static_cast<const BinaryValue*>(value);
970
971 return true;
972 }
973
974 bool ListValue::GetBinary(size_t index, BinaryValue** out_value) {
975 return static_cast<const ListValue&>(*this).GetBinary(
976 index,
977 const_cast<const BinaryValue**>(out_value));
978 }
979
980 bool ListValue::GetDictionary(size_t index,
981 const DictionaryValue** out_value) const {
982 const Value* value;
983 bool result = Get(index, &value);
984 if (!result || !value->IsType(TYPE_DICTIONARY))
985 return false;
986
987 if (out_value)
988 *out_value = static_cast<const DictionaryValue*>(value);
989
990 return true;
991 }
992
993 bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) {
994 return static_cast<const ListValue&>(*this).GetDictionary(
995 index,
996 const_cast<const DictionaryValue**>(out_value));
997 }
998
999 bool ListValue::GetList(size_t index, const ListValue** out_value) const {
1000 const Value* value;
1001 bool result = Get(index, &value);
1002 if (!result || !value->IsType(TYPE_LIST))
1003 return false;
1004
1005 if (out_value)
1006 *out_value = static_cast<const ListValue*>(value);
1007
1008 return true;
1009 }
1010
1011 bool ListValue::GetList(size_t index, ListValue** out_value) {
1012 return static_cast<const ListValue&>(*this).GetList(
1013 index,
1014 const_cast<const ListValue**>(out_value));
1015 }
1016
1017 bool ListValue::Remove(size_t index, scoped_ptr<Value>* out_value) {
1018 if (index >= list_.size())
1019 return false;
1020
1021 if (out_value)
1022 out_value->reset(list_[index]);
1023 else
1024 delete list_[index];
1025
1026 list_.erase(list_.begin() + index);
1027 return true;
1028 }
1029
1030 bool ListValue::Remove(const Value& value, size_t* index) {
1031 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) {
1032 if ((*i)->Equals(&value)) {
1033 size_t previous_index = i - list_.begin();
1034 delete *i;
1035 list_.erase(i);
1036
1037 if (index)
1038 *index = previous_index;
1039 return true;
1040 }
1041 }
1042 return false;
1043 }
1044
1045 ListValue::iterator ListValue::Erase(iterator iter,
1046 scoped_ptr<Value>* out_value) {
1047 if (out_value)
1048 out_value->reset(*iter);
1049 else
1050 delete *iter;
1051
1052 return list_.erase(iter);
1053 }
1054
1055 void ListValue::Append(scoped_ptr<Value> in_value) {
1056 Append(in_value.release());
1057 }
1058
1059 void ListValue::Append(Value* in_value) {
1060 DCHECK(in_value);
1061 list_.push_back(in_value);
1062 }
1063
1064 void ListValue::AppendBoolean(bool in_value) {
1065 Append(new FundamentalValue(in_value));
1066 }
1067
1068 void ListValue::AppendInteger(int in_value) {
1069 Append(new FundamentalValue(in_value));
1070 }
1071
1072 void ListValue::AppendDouble(double in_value) {
1073 Append(new FundamentalValue(in_value));
1074 }
1075
1076 void ListValue::AppendString(const std::string& in_value) {
1077 Append(new StringValue(in_value));
1078 }
1079
1080 void ListValue::AppendString(const string16& in_value) {
1081 Append(new StringValue(in_value));
1082 }
1083
1084 void ListValue::AppendStrings(const std::vector<std::string>& in_values) {
1085 for (std::vector<std::string>::const_iterator it = in_values.begin();
1086 it != in_values.end(); ++it) {
1087 AppendString(*it);
1088 }
1089 }
1090
1091 void ListValue::AppendStrings(const std::vector<string16>& in_values) {
1092 for (std::vector<string16>::const_iterator it = in_values.begin();
1093 it != in_values.end(); ++it) {
1094 AppendString(*it);
1095 }
1096 }
1097
1098 bool ListValue::AppendIfNotPresent(Value* in_value) {
1099 DCHECK(in_value);
1100 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) {
1101 if ((*i)->Equals(in_value)) {
1102 delete in_value;
1103 return false;
1104 }
1105 }
1106 list_.push_back(in_value);
1107 return true;
1108 }
1109
1110 bool ListValue::Insert(size_t index, Value* in_value) {
1111 DCHECK(in_value);
1112 if (index > list_.size())
1113 return false;
1114
1115 list_.insert(list_.begin() + index, in_value);
1116 return true;
1117 }
1118
1119 ListValue::const_iterator ListValue::Find(const Value& value) const {
1120 return std::find_if(list_.begin(), list_.end(), ValueEquals(&value));
1121 }
1122
1123 void ListValue::Swap(ListValue* other) {
1124 list_.swap(other->list_);
1125 }
1126
1127 bool ListValue::GetAsList(ListValue** out_value) {
1128 if (out_value)
1129 *out_value = this;
1130 return true;
1131 }
1132
1133 bool ListValue::GetAsList(const ListValue** out_value) const {
1134 if (out_value)
1135 *out_value = this;
1136 return true;
1137 }
1138
1139 ListValue* ListValue::DeepCopy() const {
1140 ListValue* result = new ListValue;
1141
1142 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i)
1143 result->Append((*i)->DeepCopy());
1144
1145 return result;
1146 }
1147
1148 scoped_ptr<ListValue> ListValue::CreateDeepCopy() const {
1149 return make_scoped_ptr(DeepCopy());
1150 }
1151
1152 bool ListValue::Equals(const Value* other) const {
1153 if (other->GetType() != GetType())
1154 return false;
1155
1156 const ListValue* other_list =
1157 static_cast<const ListValue*>(other);
1158 const_iterator lhs_it, rhs_it;
1159 for (lhs_it = begin(), rhs_it = other_list->begin();
1160 lhs_it != end() && rhs_it != other_list->end();
1161 ++lhs_it, ++rhs_it) {
1162 if (!(*lhs_it)->Equals(*rhs_it))
1163 return false;
1164 }
1165 if (lhs_it != end() || rhs_it != other_list->end())
1166 return false;
1167
1168 return true;
1169 }
1170
1171 ValueSerializer::~ValueSerializer() {
1172 }
1173
1174 ValueDeserializer::~ValueDeserializer() {
1175 }
1176
1177 std::ostream& operator<<(std::ostream& out, const Value& value) {
1178 std::string json;
1179 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json);
1180 return out << json;
1181 }
1182
1183 } // namespace base
OLDNEW
« no previous file with comments | « base/values.h ('k') | base/values_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698