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 <algorithm> |
| 8 |
7 #include "base/float_util.h" | 9 #include "base/float_util.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/string_util.h" | 11 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
11 | 13 |
12 namespace { | 14 namespace { |
13 | 15 |
14 // Make a deep copy of |node|, but don't include empty lists or dictionaries | 16 // Make a deep copy of |node|, but don't include empty lists or dictionaries |
15 // in the copy. It's possible for this function to return NULL and it | 17 // in the copy. It's possible for this function to return NULL and it |
16 // expects |node| to always be non-NULL. | 18 // expects |node| to always be non-NULL. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 delete copy; | 53 delete copy; |
52 return NULL; | 54 return NULL; |
53 } | 55 } |
54 | 56 |
55 default: | 57 default: |
56 // For everything else, just make a copy. | 58 // For everything else, just make a copy. |
57 return node->DeepCopy(); | 59 return node->DeepCopy(); |
58 } | 60 } |
59 } | 61 } |
60 | 62 |
| 63 // A small functor for comparing Values for std::find_if and similar. |
| 64 class ValueEquals { |
| 65 public: |
| 66 // Pass the value against which all consecutive calls of the () operator will |
| 67 // compare their argument to. This Value object must not be destroyed while |
| 68 // the ValueEquals is in use. |
| 69 ValueEquals(const Value* first) : first_(first) { } |
| 70 |
| 71 bool operator ()(const Value* second) const { |
| 72 return first_->Equals(second); |
| 73 } |
| 74 |
| 75 private: |
| 76 const Value* first_; |
| 77 }; |
| 78 |
61 } // namespace | 79 } // namespace |
62 | 80 |
63 namespace base { | 81 namespace base { |
64 | 82 |
65 ///////////////////// Value //////////////////// | 83 ///////////////////// Value //////////////////// |
66 | 84 |
67 Value::~Value() { | 85 Value::~Value() { |
68 } | 86 } |
69 | 87 |
70 // static | 88 // static |
(...skipping 787 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
858 | 876 |
859 bool ListValue::Insert(size_t index, Value* in_value) { | 877 bool ListValue::Insert(size_t index, Value* in_value) { |
860 DCHECK(in_value); | 878 DCHECK(in_value); |
861 if (index > list_.size()) | 879 if (index > list_.size()) |
862 return false; | 880 return false; |
863 | 881 |
864 list_.insert(list_.begin() + index, in_value); | 882 list_.insert(list_.begin() + index, in_value); |
865 return true; | 883 return true; |
866 } | 884 } |
867 | 885 |
| 886 ListValue::const_iterator ListValue::Find(const Value& value) const { |
| 887 return std::find_if(list_.begin(), list_.end(), ValueEquals(&value)); |
| 888 } |
| 889 |
868 bool ListValue::GetAsList(ListValue** out_value) { | 890 bool ListValue::GetAsList(ListValue** out_value) { |
869 if (out_value) | 891 if (out_value) |
870 *out_value = this; | 892 *out_value = this; |
871 return true; | 893 return true; |
872 } | 894 } |
873 | 895 |
874 bool ListValue::GetAsList(const ListValue** out_value) const { | 896 bool ListValue::GetAsList(const ListValue** out_value) const { |
875 if (out_value) | 897 if (out_value) |
876 *out_value = this; | 898 *out_value = this; |
877 return true; | 899 return true; |
(...skipping 24 matching lines...) Expand all Loading... |
902 if (lhs_it != end() || rhs_it != other_list->end()) | 924 if (lhs_it != end() || rhs_it != other_list->end()) |
903 return false; | 925 return false; |
904 | 926 |
905 return true; | 927 return true; |
906 } | 928 } |
907 | 929 |
908 ValueSerializer::~ValueSerializer() { | 930 ValueSerializer::~ValueSerializer() { |
909 } | 931 } |
910 | 932 |
911 } // namespace base | 933 } // namespace base |
OLD | NEW |