rsesek@chromium.org | c2d2b20 | 2012-05-17 00:23:30 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 5 | #include "base/values.h" |
| 6 | |
pastarmovj@chromium.org | 1602a47 | 2011-09-20 00:23:10 +0900 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
derat@chromium.org | af8c8f0 | 2011-08-17 17:03:43 +0900 | [diff] [blame] | 9 | #include "base/float_util.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 10 | #include "base/logging.h" |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 11 | #include "base/string_util.h" |
brettw@chromium.org | 50c9465 | 2009-10-07 11:10:20 +0900 | [diff] [blame] | 12 | #include "base/utf_string_conversions.h" |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 13 | |
| 14 | namespace { |
| 15 | |
| 16 | // Make a deep copy of |node|, but don't include empty lists or dictionaries |
| 17 | // in the copy. It's possible for this function to return NULL and it |
| 18 | // expects |node| to always be non-NULL. |
| 19 | Value* CopyWithoutEmptyChildren(Value* node) { |
| 20 | DCHECK(node); |
| 21 | switch (node->GetType()) { |
| 22 | case Value::TYPE_LIST: { |
stevenjb@google.com | f4f5827 | 2011-08-26 10:54:00 +0900 | [diff] [blame] | 23 | ListValue* list = static_cast<ListValue*>(node); |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 24 | ListValue* copy = new ListValue; |
| 25 | for (ListValue::const_iterator it = list->begin(); it != list->end(); |
| 26 | ++it) { |
| 27 | Value* child_copy = CopyWithoutEmptyChildren(*it); |
| 28 | if (child_copy) |
| 29 | copy->Append(child_copy); |
| 30 | } |
| 31 | if (!copy->empty()) |
| 32 | return copy; |
| 33 | |
| 34 | delete copy; |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | case Value::TYPE_DICTIONARY: { |
| 39 | DictionaryValue* dict = static_cast<DictionaryValue*>(node); |
| 40 | DictionaryValue* copy = new DictionaryValue; |
| 41 | for (DictionaryValue::key_iterator it = dict->begin_keys(); |
| 42 | it != dict->end_keys(); ++it) { |
| 43 | Value* child = NULL; |
| 44 | bool rv = dict->GetWithoutPathExpansion(*it, &child); |
| 45 | DCHECK(rv); |
| 46 | Value* child_copy = CopyWithoutEmptyChildren(child); |
| 47 | if (child_copy) |
| 48 | copy->SetWithoutPathExpansion(*it, child_copy); |
| 49 | } |
| 50 | if (!copy->empty()) |
| 51 | return copy; |
| 52 | |
| 53 | delete copy; |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | default: |
| 58 | // For everything else, just make a copy. |
| 59 | return node->DeepCopy(); |
| 60 | } |
| 61 | } |
| 62 | |
pastarmovj@chromium.org | 1602a47 | 2011-09-20 00:23:10 +0900 | [diff] [blame] | 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 | |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 79 | } // namespace |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 80 | |
tfarina@chromium.org | de3ef41 | 2011-08-05 08:51:10 +0900 | [diff] [blame] | 81 | namespace base { |
| 82 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 83 | ///////////////////// Value //////////////////// |
| 84 | |
| 85 | Value::~Value() { |
| 86 | } |
| 87 | |
| 88 | // static |
| 89 | Value* Value::CreateNullValue() { |
| 90 | return new Value(TYPE_NULL); |
| 91 | } |
| 92 | |
| 93 | // static |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 94 | FundamentalValue* Value::CreateBooleanValue(bool in_value) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 95 | return new FundamentalValue(in_value); |
| 96 | } |
| 97 | |
| 98 | // static |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 99 | FundamentalValue* Value::CreateIntegerValue(int in_value) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 100 | return new FundamentalValue(in_value); |
| 101 | } |
| 102 | |
| 103 | // static |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 104 | FundamentalValue* Value::CreateDoubleValue(double in_value) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 105 | return new FundamentalValue(in_value); |
| 106 | } |
| 107 | |
| 108 | // static |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 109 | StringValue* Value::CreateStringValue(const std::string& in_value) { |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 110 | return new StringValue(in_value); |
| 111 | } |
| 112 | |
| 113 | // static |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 114 | StringValue* Value::CreateStringValue(const string16& in_value) { |
scherkus@chromium.org | 5ad6276 | 2008-12-11 07:30:57 +0900 | [diff] [blame] | 115 | return new StringValue(in_value); |
scherkus@chromium.org | 92bc0d4 | 2008-12-11 07:18:47 +0900 | [diff] [blame] | 116 | } |
| 117 | |
viettrungluu@chromium.org | 31b80ba | 2010-08-04 00:42:58 +0900 | [diff] [blame] | 118 | bool Value::GetAsBoolean(bool* out_value) const { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
viettrungluu@chromium.org | 31b80ba | 2010-08-04 00:42:58 +0900 | [diff] [blame] | 122 | bool Value::GetAsInteger(int* out_value) const { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 126 | bool Value::GetAsDouble(double* out_value) const { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 127 | return false; |
| 128 | } |
| 129 | |
viettrungluu@chromium.org | 31b80ba | 2010-08-04 00:42:58 +0900 | [diff] [blame] | 130 | bool Value::GetAsString(std::string* out_value) const { |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | |
viettrungluu@chromium.org | 31b80ba | 2010-08-04 00:42:58 +0900 | [diff] [blame] | 134 | bool Value::GetAsString(string16* out_value) const { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | |
scottbyer@google.com | 673b876 | 2010-12-07 09:35:29 +0900 | [diff] [blame] | 138 | bool Value::GetAsList(ListValue** out_value) { |
| 139 | return false; |
| 140 | } |
| 141 | |
bauerb@chromium.org | 6878e50 | 2011-07-12 18:04:38 +0900 | [diff] [blame] | 142 | bool Value::GetAsList(const ListValue** out_value) const { |
| 143 | return false; |
| 144 | } |
| 145 | |
battre@chromium.org | 29eaa25 | 2011-11-26 10:11:44 +0900 | [diff] [blame] | 146 | bool Value::GetAsDictionary(DictionaryValue** out_value) { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | bool Value::GetAsDictionary(const DictionaryValue** out_value) const { |
| 151 | return false; |
| 152 | } |
| 153 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 154 | Value* Value::DeepCopy() const { |
| 155 | // This method should only be getting called for null Values--all subclasses |
| 156 | // need to provide their own implementation;. |
| 157 | DCHECK(IsType(TYPE_NULL)); |
| 158 | return CreateNullValue(); |
| 159 | } |
| 160 | |
| 161 | bool Value::Equals(const Value* other) const { |
| 162 | // This method should only be getting called for null Values--all subclasses |
| 163 | // need to provide their own implementation;. |
| 164 | DCHECK(IsType(TYPE_NULL)); |
| 165 | return other->IsType(TYPE_NULL); |
| 166 | } |
| 167 | |
bauerb@chromium.org | fbd55e1 | 2010-12-07 03:13:43 +0900 | [diff] [blame] | 168 | // static |
| 169 | bool Value::Equals(const Value* a, const Value* b) { |
| 170 | if ((a == NULL) && (b == NULL)) return true; |
| 171 | if ((a == NULL) ^ (b == NULL)) return false; |
| 172 | return a->Equals(b); |
| 173 | } |
| 174 | |
sky@chromium.org | 8d103c7 | 2011-08-25 06:17:59 +0900 | [diff] [blame] | 175 | Value::Value(Type type) : type_(type) { |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 176 | } |
| 177 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 178 | ///////////////////// FundamentalValue //////////////////// |
| 179 | |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 180 | FundamentalValue::FundamentalValue(bool in_value) |
| 181 | : Value(TYPE_BOOLEAN), boolean_value_(in_value) { |
| 182 | } |
| 183 | |
| 184 | FundamentalValue::FundamentalValue(int in_value) |
| 185 | : Value(TYPE_INTEGER), integer_value_(in_value) { |
| 186 | } |
| 187 | |
| 188 | FundamentalValue::FundamentalValue(double in_value) |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 189 | : Value(TYPE_DOUBLE), double_value_(in_value) { |
derat@chromium.org | a40267d | 2011-08-23 13:11:22 +0900 | [diff] [blame] | 190 | if (!IsFinite(double_value_)) { |
| 191 | NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) " |
| 192 | << "values cannot be represented in JSON"; |
| 193 | double_value_ = 0.0; |
| 194 | } |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 195 | } |
| 196 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 197 | FundamentalValue::~FundamentalValue() { |
| 198 | } |
| 199 | |
| 200 | bool FundamentalValue::GetAsBoolean(bool* out_value) const { |
| 201 | if (out_value && IsType(TYPE_BOOLEAN)) |
| 202 | *out_value = boolean_value_; |
| 203 | return (IsType(TYPE_BOOLEAN)); |
| 204 | } |
| 205 | |
| 206 | bool FundamentalValue::GetAsInteger(int* out_value) const { |
| 207 | if (out_value && IsType(TYPE_INTEGER)) |
| 208 | *out_value = integer_value_; |
| 209 | return (IsType(TYPE_INTEGER)); |
| 210 | } |
| 211 | |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 212 | bool FundamentalValue::GetAsDouble(double* out_value) const { |
| 213 | if (out_value && IsType(TYPE_DOUBLE)) |
| 214 | *out_value = double_value_; |
yusukes@google.com | 0b18b53 | 2011-05-02 10:59:21 +0900 | [diff] [blame] | 215 | else if (out_value && IsType(TYPE_INTEGER)) |
| 216 | *out_value = integer_value_; |
| 217 | return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 218 | } |
| 219 | |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 220 | FundamentalValue* FundamentalValue::DeepCopy() const { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 221 | switch (GetType()) { |
| 222 | case TYPE_BOOLEAN: |
| 223 | return CreateBooleanValue(boolean_value_); |
| 224 | |
| 225 | case TYPE_INTEGER: |
| 226 | return CreateIntegerValue(integer_value_); |
| 227 | |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 228 | case TYPE_DOUBLE: |
| 229 | return CreateDoubleValue(double_value_); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 230 | |
| 231 | default: |
| 232 | NOTREACHED(); |
| 233 | return NULL; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | bool FundamentalValue::Equals(const Value* other) const { |
| 238 | if (other->GetType() != GetType()) |
| 239 | return false; |
| 240 | |
| 241 | switch (GetType()) { |
| 242 | case TYPE_BOOLEAN: { |
| 243 | bool lhs, rhs; |
| 244 | return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs; |
| 245 | } |
| 246 | case TYPE_INTEGER: { |
| 247 | int lhs, rhs; |
| 248 | return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs; |
| 249 | } |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 250 | case TYPE_DOUBLE: { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 251 | double lhs, rhs; |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 252 | return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 253 | } |
| 254 | default: |
| 255 | NOTREACHED(); |
| 256 | return false; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | ///////////////////// StringValue //////////////////// |
| 261 | |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 262 | StringValue::StringValue(const std::string& in_value) |
| 263 | : Value(TYPE_STRING), |
| 264 | value_(in_value) { |
| 265 | DCHECK(IsStringUTF8(in_value)); |
| 266 | } |
| 267 | |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 268 | StringValue::StringValue(const string16& in_value) |
| 269 | : Value(TYPE_STRING), |
| 270 | value_(UTF16ToUTF8(in_value)) { |
| 271 | } |
viettrungluu@chromium.org | 31b80ba | 2010-08-04 00:42:58 +0900 | [diff] [blame] | 272 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 273 | StringValue::~StringValue() { |
| 274 | } |
| 275 | |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 276 | bool StringValue::GetAsString(std::string* out_value) const { |
| 277 | if (out_value) |
| 278 | *out_value = value_; |
| 279 | return true; |
| 280 | } |
| 281 | |
viettrungluu@chromium.org | 31b80ba | 2010-08-04 00:42:58 +0900 | [diff] [blame] | 282 | bool StringValue::GetAsString(string16* out_value) const { |
| 283 | if (out_value) |
| 284 | *out_value = UTF8ToUTF16(value_); |
| 285 | return true; |
| 286 | } |
| 287 | |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 288 | StringValue* StringValue::DeepCopy() const { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 289 | return CreateStringValue(value_); |
| 290 | } |
| 291 | |
| 292 | bool StringValue::Equals(const Value* other) const { |
| 293 | if (other->GetType() != GetType()) |
| 294 | return false; |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 295 | std::string lhs, rhs; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 296 | return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; |
| 297 | } |
| 298 | |
| 299 | ///////////////////// BinaryValue //////////////////// |
| 300 | |
mitchellwrosen@chromium.org | 3897f9b | 2012-05-20 04:31:16 +0900 | [diff] [blame] | 301 | BinaryValue::~BinaryValue() { |
jhawkins@chromium.org | 592520e | 2012-05-20 11:34:57 +0900 | [diff] [blame] | 302 | DCHECK(buffer_); |
| 303 | if (buffer_) |
| 304 | delete[] buffer_; |
| 305 | } |
| 306 | |
| 307 | // static |
| 308 | BinaryValue* BinaryValue::Create(char* buffer, size_t size) { |
| 309 | if (!buffer) |
| 310 | return NULL; |
| 311 | |
| 312 | return new BinaryValue(buffer, size); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 313 | } |
| 314 | |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 315 | // static |
mpcomplete@chromium.org | 554d431 | 2009-10-07 03:15:58 +0900 | [diff] [blame] | 316 | BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, |
| 317 | size_t size) { |
jhawkins@chromium.org | 592520e | 2012-05-20 11:34:57 +0900 | [diff] [blame] | 318 | if (!buffer) |
| 319 | return NULL; |
| 320 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 321 | char* buffer_copy = new char[size]; |
mmentovai@google.com | 4a5b627 | 2008-08-07 00:46:59 +0900 | [diff] [blame] | 322 | memcpy(buffer_copy, buffer, size); |
jhawkins@chromium.org | 592520e | 2012-05-20 11:34:57 +0900 | [diff] [blame] | 323 | return new BinaryValue(buffer_copy, size); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 324 | } |
| 325 | |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 326 | BinaryValue* BinaryValue::DeepCopy() const { |
jhawkins@chromium.org | 592520e | 2012-05-20 11:34:57 +0900 | [diff] [blame] | 327 | return CreateWithCopiedBuffer(buffer_, size_); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | bool BinaryValue::Equals(const Value* other) const { |
| 331 | if (other->GetType() != GetType()) |
| 332 | return false; |
| 333 | const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); |
| 334 | if (other_binary->size_ != size_) |
| 335 | return false; |
jhawkins@chromium.org | 592520e | 2012-05-20 11:34:57 +0900 | [diff] [blame] | 336 | return !memcmp(buffer_, other_binary->buffer_, size_); |
| 337 | } |
| 338 | |
| 339 | BinaryValue::BinaryValue(char* buffer, size_t size) |
| 340 | : Value(TYPE_BINARY), |
| 341 | buffer_(buffer), |
| 342 | size_(size) { |
| 343 | DCHECK(buffer_); |
erg@google.com | 67a2543 | 2011-01-08 05:23:43 +0900 | [diff] [blame] | 344 | } |
| 345 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 346 | ///////////////////// DictionaryValue //////////////////// |
| 347 | |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 348 | DictionaryValue::DictionaryValue() |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 349 | : Value(TYPE_DICTIONARY) { |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 350 | } |
| 351 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 352 | DictionaryValue::~DictionaryValue() { |
| 353 | Clear(); |
| 354 | } |
| 355 | |
battre@chromium.org | 29eaa25 | 2011-11-26 10:11:44 +0900 | [diff] [blame] | 356 | bool DictionaryValue::GetAsDictionary(DictionaryValue** out_value) { |
| 357 | if (out_value) |
| 358 | *out_value = this; |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const { |
| 363 | if (out_value) |
| 364 | *out_value = this; |
| 365 | return true; |
| 366 | } |
| 367 | |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 368 | bool DictionaryValue::HasKey(const std::string& key) const { |
viettrungluu@chromium.org | 4c86b85 | 2010-08-06 15:03:25 +0900 | [diff] [blame] | 369 | DCHECK(IsStringUTF8(key)); |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 370 | ValueMap::const_iterator current_entry = dictionary_.find(key); |
| 371 | DCHECK((current_entry == dictionary_.end()) || current_entry->second); |
| 372 | return current_entry != dictionary_.end(); |
| 373 | } |
| 374 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 375 | void DictionaryValue::Clear() { |
| 376 | ValueMap::iterator dict_iterator = dictionary_.begin(); |
| 377 | while (dict_iterator != dictionary_.end()) { |
| 378 | delete dict_iterator->second; |
| 379 | ++dict_iterator; |
| 380 | } |
| 381 | |
| 382 | dictionary_.clear(); |
| 383 | } |
| 384 | |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 385 | void DictionaryValue::Set(const std::string& path, Value* in_value) { |
viettrungluu@chromium.org | 4c86b85 | 2010-08-06 15:03:25 +0900 | [diff] [blame] | 386 | DCHECK(IsStringUTF8(path)); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 387 | DCHECK(in_value); |
| 388 | |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 389 | std::string current_path(path); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 390 | DictionaryValue* current_dictionary = this; |
| 391 | for (size_t delimiter_position = current_path.find('.'); |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 392 | delimiter_position != std::string::npos; |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 393 | delimiter_position = current_path.find('.')) { |
| 394 | // Assume that we're indexing into a dictionary. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 395 | std::string key(current_path, 0, delimiter_position); |
pkasting@chromium.org | 8535dc2 | 2009-11-26 06:02:24 +0900 | [diff] [blame] | 396 | DictionaryValue* child_dictionary = NULL; |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 397 | if (!current_dictionary->GetDictionary(key, &child_dictionary)) { |
| 398 | child_dictionary = new DictionaryValue; |
| 399 | current_dictionary->SetWithoutPathExpansion(key, child_dictionary); |
| 400 | } |
| 401 | |
| 402 | current_dictionary = child_dictionary; |
| 403 | current_path.erase(0, delimiter_position + 1); |
| 404 | } |
| 405 | |
| 406 | current_dictionary->SetWithoutPathExpansion(current_path, in_value); |
| 407 | } |
| 408 | |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 409 | void DictionaryValue::SetBoolean(const std::string& path, bool in_value) { |
| 410 | Set(path, CreateBooleanValue(in_value)); |
| 411 | } |
| 412 | |
| 413 | void DictionaryValue::SetInteger(const std::string& path, int in_value) { |
| 414 | Set(path, CreateIntegerValue(in_value)); |
| 415 | } |
| 416 | |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 417 | void DictionaryValue::SetDouble(const std::string& path, double in_value) { |
| 418 | Set(path, CreateDoubleValue(in_value)); |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | void DictionaryValue::SetString(const std::string& path, |
| 422 | const std::string& in_value) { |
| 423 | Set(path, CreateStringValue(in_value)); |
| 424 | } |
| 425 | |
viettrungluu@chromium.org | b7b6aa0 | 2010-08-05 01:58:12 +0900 | [diff] [blame] | 426 | void DictionaryValue::SetString(const std::string& path, |
| 427 | const string16& in_value) { |
viettrungluu@chromium.org | 31b80ba | 2010-08-04 00:42:58 +0900 | [diff] [blame] | 428 | Set(path, CreateStringValue(in_value)); |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 429 | } |
| 430 | |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 431 | void DictionaryValue::SetWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 432 | Value* in_value) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 433 | // If there's an existing value here, we need to delete it, because |
| 434 | // we own all our children. |
grt@chromium.org | 4c73d40 | 2011-10-22 07:58:52 +0900 | [diff] [blame] | 435 | std::pair<ValueMap::iterator, bool> ins_res = |
| 436 | dictionary_.insert(std::make_pair(key, in_value)); |
| 437 | if (!ins_res.second) { |
| 438 | DCHECK_NE(ins_res.first->second, in_value); // This would be bogus |
| 439 | delete ins_res.first->second; |
| 440 | ins_res.first->second = in_value; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 441 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 442 | } |
| 443 | |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 444 | bool DictionaryValue::Get( |
| 445 | const std::string& path, const Value** out_value) const { |
viettrungluu@chromium.org | 4c86b85 | 2010-08-06 15:03:25 +0900 | [diff] [blame] | 446 | DCHECK(IsStringUTF8(path)); |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 447 | std::string current_path(path); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 448 | const DictionaryValue* current_dictionary = this; |
| 449 | for (size_t delimiter_position = current_path.find('.'); |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 450 | delimiter_position != std::string::npos; |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 451 | delimiter_position = current_path.find('.')) { |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 452 | const DictionaryValue* child_dictionary = NULL; |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 453 | if (!current_dictionary->GetDictionary( |
| 454 | current_path.substr(0, delimiter_position), &child_dictionary)) |
| 455 | return false; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 456 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 457 | current_dictionary = child_dictionary; |
| 458 | current_path.erase(0, delimiter_position + 1); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 459 | } |
| 460 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 461 | return current_dictionary->GetWithoutPathExpansion(current_path, out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 462 | } |
| 463 | |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 464 | bool DictionaryValue::Get(const std::string& path, Value** out_value) { |
| 465 | return static_cast<const DictionaryValue&>(*this).Get( |
| 466 | path, |
| 467 | const_cast<const Value**>(out_value)); |
| 468 | } |
| 469 | |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 470 | bool DictionaryValue::GetBoolean(const std::string& path, |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 471 | bool* bool_value) const { |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 472 | const Value* value; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 473 | if (!Get(path, &value)) |
| 474 | return false; |
| 475 | |
| 476 | return value->GetAsBoolean(bool_value); |
| 477 | } |
| 478 | |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 479 | bool DictionaryValue::GetInteger(const std::string& path, |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 480 | int* out_value) const { |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 481 | const Value* value; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 482 | if (!Get(path, &value)) |
| 483 | return false; |
| 484 | |
| 485 | return value->GetAsInteger(out_value); |
| 486 | } |
| 487 | |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 488 | bool DictionaryValue::GetDouble(const std::string& path, |
| 489 | double* out_value) const { |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 490 | const Value* value; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 491 | if (!Get(path, &value)) |
| 492 | return false; |
| 493 | |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 494 | return value->GetAsDouble(out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 495 | } |
| 496 | |
evan@chromium.org | d9ab8de | 2010-02-19 22:32:16 +0900 | [diff] [blame] | 497 | bool DictionaryValue::GetString(const std::string& path, |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 498 | std::string* out_value) const { |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 499 | const Value* value; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 500 | if (!Get(path, &value)) |
| 501 | return false; |
| 502 | |
| 503 | return value->GetAsString(out_value); |
| 504 | } |
| 505 | |
viettrungluu@chromium.org | 58767ef | 2010-08-05 04:35:33 +0900 | [diff] [blame] | 506 | bool DictionaryValue::GetString(const std::string& path, |
| 507 | string16* out_value) const { |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 508 | const Value* value; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 509 | if (!Get(path, &value)) |
| 510 | return false; |
| 511 | |
viettrungluu@chromium.org | 31b80ba | 2010-08-04 00:42:58 +0900 | [diff] [blame] | 512 | return value->GetAsString(out_value); |
evan@chromium.org | d9ab8de | 2010-02-19 22:32:16 +0900 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | bool DictionaryValue::GetStringASCII(const std::string& path, |
| 516 | std::string* out_value) const { |
| 517 | std::string out; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 518 | if (!GetString(path, &out)) |
evan@chromium.org | d9ab8de | 2010-02-19 22:32:16 +0900 | [diff] [blame] | 519 | return false; |
| 520 | |
| 521 | if (!IsStringASCII(out)) { |
| 522 | NOTREACHED(); |
| 523 | return false; |
| 524 | } |
| 525 | |
| 526 | out_value->assign(out); |
| 527 | return true; |
| 528 | } |
| 529 | |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 530 | bool DictionaryValue::GetBinary(const std::string& path, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 531 | const BinaryValue** out_value) const { |
| 532 | const Value* value; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 533 | bool result = Get(path, &value); |
stevenjb@google.com | f4f5827 | 2011-08-26 10:54:00 +0900 | [diff] [blame] | 534 | if (!result || !value->IsType(TYPE_BINARY)) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 535 | return false; |
| 536 | |
| 537 | if (out_value) |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 538 | *out_value = static_cast<const BinaryValue*>(value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 539 | |
| 540 | return true; |
| 541 | } |
| 542 | |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 543 | bool DictionaryValue::GetBinary(const std::string& path, |
| 544 | BinaryValue** out_value) { |
| 545 | return static_cast<const DictionaryValue&>(*this).GetBinary( |
| 546 | path, |
| 547 | const_cast<const BinaryValue**>(out_value)); |
| 548 | } |
| 549 | |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 550 | bool DictionaryValue::GetDictionary(const std::string& path, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 551 | const DictionaryValue** out_value) const { |
| 552 | const Value* value; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 553 | bool result = Get(path, &value); |
| 554 | if (!result || !value->IsType(TYPE_DICTIONARY)) |
| 555 | return false; |
| 556 | |
| 557 | if (out_value) |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 558 | *out_value = static_cast<const DictionaryValue*>(value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 559 | |
| 560 | return true; |
| 561 | } |
| 562 | |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 563 | bool DictionaryValue::GetDictionary(const std::string& path, |
| 564 | DictionaryValue** out_value) { |
| 565 | return static_cast<const DictionaryValue&>(*this).GetDictionary( |
| 566 | path, |
| 567 | const_cast<const DictionaryValue**>(out_value)); |
| 568 | } |
| 569 | |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 570 | bool DictionaryValue::GetList(const std::string& path, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 571 | const ListValue** out_value) const { |
| 572 | const Value* value; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 573 | bool result = Get(path, &value); |
stevenjb@google.com | f4f5827 | 2011-08-26 10:54:00 +0900 | [diff] [blame] | 574 | if (!result || !value->IsType(TYPE_LIST)) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 575 | return false; |
| 576 | |
| 577 | if (out_value) |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 578 | *out_value = static_cast<const ListValue*>(value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 579 | |
| 580 | return true; |
| 581 | } |
| 582 | |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 583 | bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) { |
| 584 | return static_cast<const DictionaryValue&>(*this).GetList( |
| 585 | path, |
| 586 | const_cast<const ListValue**>(out_value)); |
| 587 | } |
| 588 | |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 589 | bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 590 | const Value** out_value) const { |
viettrungluu@chromium.org | 4c86b85 | 2010-08-06 15:03:25 +0900 | [diff] [blame] | 591 | DCHECK(IsStringUTF8(key)); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 592 | ValueMap::const_iterator entry_iterator = dictionary_.find(key); |
| 593 | if (entry_iterator == dictionary_.end()) |
| 594 | return false; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 595 | |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 596 | const Value* entry = entry_iterator->second; |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 597 | if (out_value) |
| 598 | *out_value = entry; |
| 599 | return true; |
| 600 | } |
| 601 | |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 602 | bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, |
| 603 | Value** out_value) { |
| 604 | return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion( |
| 605 | key, |
| 606 | const_cast<const Value**>(out_value)); |
| 607 | } |
| 608 | |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 609 | bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 610 | int* out_value) const { |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 611 | const Value* value; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 612 | if (!GetWithoutPathExpansion(key, &value)) |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 613 | return false; |
| 614 | |
| 615 | return value->GetAsInteger(out_value); |
| 616 | } |
| 617 | |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 618 | bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key, |
| 619 | double* out_value) const { |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 620 | const Value* value; |
jam@chromium.org | 5bd5392 | 2010-10-01 16:28:25 +0900 | [diff] [blame] | 621 | if (!GetWithoutPathExpansion(key, &value)) |
| 622 | return false; |
| 623 | |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 624 | return value->GetAsDouble(out_value); |
jam@chromium.org | 5bd5392 | 2010-10-01 16:28:25 +0900 | [diff] [blame] | 625 | } |
| 626 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 627 | bool DictionaryValue::GetStringWithoutPathExpansion( |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 628 | const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 629 | std::string* out_value) const { |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 630 | const Value* value; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 631 | if (!GetWithoutPathExpansion(key, &value)) |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 632 | return false; |
| 633 | |
| 634 | return value->GetAsString(out_value); |
| 635 | } |
| 636 | |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 637 | bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key, |
| 638 | string16* out_value) const { |
| 639 | const Value* value; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 640 | if (!GetWithoutPathExpansion(key, &value)) |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 641 | return false; |
| 642 | |
viettrungluu@chromium.org | 31b80ba | 2010-08-04 00:42:58 +0900 | [diff] [blame] | 643 | return value->GetAsString(out_value); |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 644 | } |
| 645 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 646 | bool DictionaryValue::GetDictionaryWithoutPathExpansion( |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 647 | const std::string& key, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 648 | const DictionaryValue** out_value) const { |
| 649 | const Value* value; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 650 | bool result = GetWithoutPathExpansion(key, &value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 651 | if (!result || !value->IsType(TYPE_DICTIONARY)) |
| 652 | return false; |
| 653 | |
| 654 | if (out_value) |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 655 | *out_value = static_cast<const DictionaryValue*>(value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 656 | |
| 657 | return true; |
| 658 | } |
| 659 | |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 660 | bool DictionaryValue::GetDictionaryWithoutPathExpansion( |
| 661 | const std::string& key, |
| 662 | DictionaryValue** out_value) { |
| 663 | const DictionaryValue& const_this = |
| 664 | static_cast<const DictionaryValue&>(*this); |
| 665 | return const_this.GetDictionaryWithoutPathExpansion( |
| 666 | key, |
| 667 | const_cast<const DictionaryValue**>(out_value)); |
| 668 | } |
| 669 | |
| 670 | bool DictionaryValue::GetListWithoutPathExpansion( |
| 671 | const std::string& key, |
| 672 | const ListValue** out_value) const { |
| 673 | const Value* value; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 674 | bool result = GetWithoutPathExpansion(key, &value); |
stevenjb@google.com | f4f5827 | 2011-08-26 10:54:00 +0900 | [diff] [blame] | 675 | if (!result || !value->IsType(TYPE_LIST)) |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 676 | return false; |
| 677 | |
| 678 | if (out_value) |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 679 | *out_value = static_cast<const ListValue*>(value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 680 | |
| 681 | return true; |
| 682 | } |
| 683 | |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 684 | bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key, |
| 685 | ListValue** out_value) { |
| 686 | return |
| 687 | static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion( |
| 688 | key, |
| 689 | const_cast<const ListValue**>(out_value)); |
| 690 | } |
| 691 | |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 692 | bool DictionaryValue::Remove(const std::string& path, Value** out_value) { |
viettrungluu@chromium.org | 4c86b85 | 2010-08-06 15:03:25 +0900 | [diff] [blame] | 693 | DCHECK(IsStringUTF8(path)); |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 694 | std::string current_path(path); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 695 | DictionaryValue* current_dictionary = this; |
| 696 | size_t delimiter_position = current_path.rfind('.'); |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 697 | if (delimiter_position != std::string::npos) { |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 698 | if (!GetDictionary(current_path.substr(0, delimiter_position), |
| 699 | ¤t_dictionary)) |
| 700 | return false; |
| 701 | current_path.erase(0, delimiter_position + 1); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 702 | } |
| 703 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 704 | return current_dictionary->RemoveWithoutPathExpansion(current_path, |
| 705 | out_value); |
| 706 | } |
| 707 | |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 708 | bool DictionaryValue::RemoveWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 709 | Value** out_value) { |
viettrungluu@chromium.org | 4c86b85 | 2010-08-06 15:03:25 +0900 | [diff] [blame] | 710 | DCHECK(IsStringUTF8(key)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 711 | ValueMap::iterator entry_iterator = dictionary_.find(key); |
| 712 | if (entry_iterator == dictionary_.end()) |
| 713 | return false; |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 714 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 715 | Value* entry = entry_iterator->second; |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 716 | if (out_value) |
| 717 | *out_value = entry; |
| 718 | else |
| 719 | delete entry; |
| 720 | dictionary_.erase(entry_iterator); |
| 721 | return true; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 722 | } |
| 723 | |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 724 | DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() { |
| 725 | Value* copy = CopyWithoutEmptyChildren(this); |
| 726 | return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 727 | } |
| 728 | |
mnissler@chromium.org | 17cee0e | 2010-05-14 22:17:40 +0900 | [diff] [blame] | 729 | void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { |
| 730 | for (DictionaryValue::key_iterator key(dictionary->begin_keys()); |
| 731 | key != dictionary->end_keys(); ++key) { |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 732 | const Value* merge_value; |
mnissler@chromium.org | 17cee0e | 2010-05-14 22:17:40 +0900 | [diff] [blame] | 733 | if (dictionary->GetWithoutPathExpansion(*key, &merge_value)) { |
| 734 | // Check whether we have to merge dictionaries. |
| 735 | if (merge_value->IsType(Value::TYPE_DICTIONARY)) { |
| 736 | DictionaryValue* sub_dict; |
| 737 | if (GetDictionaryWithoutPathExpansion(*key, &sub_dict)) { |
| 738 | sub_dict->MergeDictionary( |
| 739 | static_cast<const DictionaryValue*>(merge_value)); |
| 740 | continue; |
| 741 | } |
| 742 | } |
| 743 | // All other cases: Make a copy and hook it up. |
| 744 | SetWithoutPathExpansion(*key, merge_value->DeepCopy()); |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
rsesek@chromium.org | c2d2b20 | 2012-05-17 00:23:30 +0900 | [diff] [blame] | 749 | void DictionaryValue::Swap(DictionaryValue* other) { |
| 750 | dictionary_.swap(other->dictionary_); |
| 751 | } |
| 752 | |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 753 | DictionaryValue* DictionaryValue::DeepCopy() const { |
erg@google.com | 67a2543 | 2011-01-08 05:23:43 +0900 | [diff] [blame] | 754 | DictionaryValue* result = new DictionaryValue; |
| 755 | |
| 756 | for (ValueMap::const_iterator current_entry(dictionary_.begin()); |
| 757 | current_entry != dictionary_.end(); ++current_entry) { |
| 758 | result->SetWithoutPathExpansion(current_entry->first, |
| 759 | current_entry->second->DeepCopy()); |
| 760 | } |
| 761 | |
| 762 | return result; |
| 763 | } |
| 764 | |
| 765 | bool DictionaryValue::Equals(const Value* other) const { |
| 766 | if (other->GetType() != GetType()) |
| 767 | return false; |
| 768 | |
| 769 | const DictionaryValue* other_dict = |
| 770 | static_cast<const DictionaryValue*>(other); |
| 771 | key_iterator lhs_it(begin_keys()); |
| 772 | key_iterator rhs_it(other_dict->begin_keys()); |
| 773 | while (lhs_it != end_keys() && rhs_it != other_dict->end_keys()) { |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 774 | const Value* lhs; |
| 775 | const Value* rhs; |
erg@google.com | 67a2543 | 2011-01-08 05:23:43 +0900 | [diff] [blame] | 776 | if (*lhs_it != *rhs_it || |
| 777 | !GetWithoutPathExpansion(*lhs_it, &lhs) || |
| 778 | !other_dict->GetWithoutPathExpansion(*rhs_it, &rhs) || |
| 779 | !lhs->Equals(rhs)) { |
| 780 | return false; |
| 781 | } |
| 782 | ++lhs_it; |
| 783 | ++rhs_it; |
| 784 | } |
| 785 | if (lhs_it != end_keys() || rhs_it != other_dict->end_keys()) |
| 786 | return false; |
| 787 | |
| 788 | return true; |
| 789 | } |
| 790 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 791 | ///////////////////// ListValue //////////////////// |
| 792 | |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 793 | ListValue::ListValue() : Value(TYPE_LIST) { |
| 794 | } |
| 795 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 796 | ListValue::~ListValue() { |
| 797 | Clear(); |
| 798 | } |
| 799 | |
| 800 | void ListValue::Clear() { |
pkasting@chromium.org | 727139c | 2009-05-09 09:33:04 +0900 | [diff] [blame] | 801 | for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) |
| 802 | delete *i; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 803 | list_.clear(); |
| 804 | } |
| 805 | |
| 806 | bool ListValue::Set(size_t index, Value* in_value) { |
| 807 | if (!in_value) |
| 808 | return false; |
| 809 | |
| 810 | if (index >= list_.size()) { |
| 811 | // Pad out any intermediate indexes with null settings |
| 812 | while (index > list_.size()) |
| 813 | Append(CreateNullValue()); |
| 814 | Append(in_value); |
| 815 | } else { |
| 816 | DCHECK(list_[index] != in_value); |
| 817 | delete list_[index]; |
| 818 | list_[index] = in_value; |
| 819 | } |
| 820 | return true; |
| 821 | } |
| 822 | |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame^] | 823 | bool ListValue::Get(size_t index, const Value** out_value) const { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 824 | if (index >= list_.size()) |
| 825 | return false; |
| 826 | |
| 827 | if (out_value) |
| 828 | *out_value = list_[index]; |
| 829 | |
| 830 | return true; |
| 831 | } |
| 832 | |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame^] | 833 | bool ListValue::Get(size_t index, Value** out_value) { |
| 834 | return static_cast<const ListValue&>(*this).Get( |
| 835 | index, |
| 836 | const_cast<const Value**>(out_value)); |
| 837 | } |
| 838 | |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 839 | bool ListValue::GetBoolean(size_t index, bool* bool_value) const { |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame^] | 840 | const Value* value; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 841 | if (!Get(index, &value)) |
| 842 | return false; |
| 843 | |
| 844 | return value->GetAsBoolean(bool_value); |
| 845 | } |
| 846 | |
| 847 | bool ListValue::GetInteger(size_t index, int* out_value) const { |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame^] | 848 | const Value* value; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 849 | if (!Get(index, &value)) |
| 850 | return false; |
| 851 | |
| 852 | return value->GetAsInteger(out_value); |
| 853 | } |
| 854 | |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 855 | bool ListValue::GetDouble(size_t index, double* out_value) const { |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame^] | 856 | const Value* value; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 857 | if (!Get(index, &value)) |
| 858 | return false; |
| 859 | |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 860 | return value->GetAsDouble(out_value); |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | bool ListValue::GetString(size_t index, std::string* out_value) const { |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame^] | 864 | const Value* value; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 865 | if (!Get(index, &value)) |
| 866 | return false; |
| 867 | |
| 868 | return value->GetAsString(out_value); |
| 869 | } |
| 870 | |
viettrungluu@chromium.org | 58767ef | 2010-08-05 04:35:33 +0900 | [diff] [blame] | 871 | bool ListValue::GetString(size_t index, string16* out_value) const { |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame^] | 872 | const Value* value; |
viettrungluu@chromium.org | 58767ef | 2010-08-05 04:35:33 +0900 | [diff] [blame] | 873 | if (!Get(index, &value)) |
| 874 | return false; |
| 875 | |
| 876 | return value->GetAsString(out_value); |
| 877 | } |
| 878 | |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame^] | 879 | bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const { |
| 880 | const Value* value; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 881 | bool result = Get(index, &value); |
| 882 | if (!result || !value->IsType(TYPE_BINARY)) |
| 883 | return false; |
| 884 | |
| 885 | if (out_value) |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame^] | 886 | *out_value = static_cast<const BinaryValue*>(value); |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 887 | |
| 888 | return true; |
| 889 | } |
| 890 | |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame^] | 891 | bool ListValue::GetBinary(size_t index, BinaryValue** out_value) { |
| 892 | return static_cast<const ListValue&>(*this).GetBinary( |
| 893 | index, |
| 894 | const_cast<const BinaryValue**>(out_value)); |
| 895 | } |
| 896 | |
| 897 | bool ListValue::GetDictionary(size_t index, |
| 898 | const DictionaryValue** out_value) const { |
| 899 | const Value* value; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 900 | bool result = Get(index, &value); |
| 901 | if (!result || !value->IsType(TYPE_DICTIONARY)) |
| 902 | return false; |
| 903 | |
| 904 | if (out_value) |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame^] | 905 | *out_value = static_cast<const DictionaryValue*>(value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 906 | |
| 907 | return true; |
| 908 | } |
| 909 | |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame^] | 910 | bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) { |
| 911 | return static_cast<const ListValue&>(*this).GetDictionary( |
| 912 | index, |
| 913 | const_cast<const DictionaryValue**>(out_value)); |
| 914 | } |
| 915 | |
| 916 | bool ListValue::GetList(size_t index, const ListValue** out_value) const { |
| 917 | const Value* value; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 918 | bool result = Get(index, &value); |
stevenjb@google.com | f4f5827 | 2011-08-26 10:54:00 +0900 | [diff] [blame] | 919 | if (!result || !value->IsType(TYPE_LIST)) |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 920 | return false; |
| 921 | |
| 922 | if (out_value) |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame^] | 923 | *out_value = static_cast<const ListValue*>(value); |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 924 | |
| 925 | return true; |
| 926 | } |
| 927 | |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame^] | 928 | bool ListValue::GetList(size_t index, ListValue** out_value) { |
| 929 | return static_cast<const ListValue&>(*this).GetList( |
| 930 | index, |
| 931 | const_cast<const ListValue**>(out_value)); |
| 932 | } |
| 933 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 934 | bool ListValue::Remove(size_t index, Value** out_value) { |
| 935 | if (index >= list_.size()) |
| 936 | return false; |
| 937 | |
| 938 | if (out_value) |
| 939 | *out_value = list_[index]; |
| 940 | else |
| 941 | delete list_[index]; |
| 942 | |
pkasting@chromium.org | 727139c | 2009-05-09 09:33:04 +0900 | [diff] [blame] | 943 | list_.erase(list_.begin() + index); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 944 | return true; |
| 945 | } |
| 946 | |
tfarina@chromium.org | 09cf434 | 2011-08-14 02:34:31 +0900 | [diff] [blame] | 947 | bool ListValue::Remove(const Value& value, size_t* index) { |
pkasting@chromium.org | 727139c | 2009-05-09 09:33:04 +0900 | [diff] [blame] | 948 | for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) { |
| 949 | if ((*i)->Equals(&value)) { |
tfarina@chromium.org | 09cf434 | 2011-08-14 02:34:31 +0900 | [diff] [blame] | 950 | size_t previous_index = i - list_.begin(); |
pkasting@chromium.org | dfe6a69 | 2009-12-01 04:59:11 +0900 | [diff] [blame] | 951 | delete *i; |
pkasting@chromium.org | 727139c | 2009-05-09 09:33:04 +0900 | [diff] [blame] | 952 | list_.erase(i); |
robertshield@chromium.org | 9fb9e37 | 2010-03-23 05:12:50 +0900 | [diff] [blame] | 953 | |
tfarina@chromium.org | 09cf434 | 2011-08-14 02:34:31 +0900 | [diff] [blame] | 954 | if (index) |
| 955 | *index = previous_index; |
| 956 | return true; |
pkasting@chromium.org | 727139c | 2009-05-09 09:33:04 +0900 | [diff] [blame] | 957 | } |
| 958 | } |
tfarina@chromium.org | 09cf434 | 2011-08-14 02:34:31 +0900 | [diff] [blame] | 959 | return false; |
pkasting@chromium.org | 727139c | 2009-05-09 09:33:04 +0900 | [diff] [blame] | 960 | } |
| 961 | |
estade@chromium.org | 52852d5 | 2012-07-03 11:51:43 +0900 | [diff] [blame] | 962 | void ListValue::Erase(iterator iter, Value** out_value) { |
| 963 | if (out_value) |
| 964 | *out_value = *iter; |
| 965 | else |
| 966 | delete *iter; |
| 967 | |
| 968 | list_.erase(iter); |
| 969 | } |
| 970 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 971 | void ListValue::Append(Value* in_value) { |
| 972 | DCHECK(in_value); |
| 973 | list_.push_back(in_value); |
| 974 | } |
| 975 | |
zork@chromium.org | b5f742b | 2010-04-13 06:48:10 +0900 | [diff] [blame] | 976 | bool ListValue::AppendIfNotPresent(Value* in_value) { |
| 977 | DCHECK(in_value); |
ziadh@chromium.org | 5c7f631 | 2010-07-19 17:00:42 +0900 | [diff] [blame] | 978 | for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) { |
markusheintz@chromium.org | 7862187 | 2011-03-18 22:56:38 +0900 | [diff] [blame] | 979 | if ((*i)->Equals(in_value)) { |
| 980 | delete in_value; |
zork@chromium.org | b5f742b | 2010-04-13 06:48:10 +0900 | [diff] [blame] | 981 | return false; |
markusheintz@chromium.org | 7862187 | 2011-03-18 22:56:38 +0900 | [diff] [blame] | 982 | } |
zork@chromium.org | b5f742b | 2010-04-13 06:48:10 +0900 | [diff] [blame] | 983 | } |
| 984 | list_.push_back(in_value); |
| 985 | return true; |
| 986 | } |
| 987 | |
erikkay@chromium.org | 9034a23 | 2009-08-29 05:26:05 +0900 | [diff] [blame] | 988 | bool ListValue::Insert(size_t index, Value* in_value) { |
| 989 | DCHECK(in_value); |
jhawkins@chromium.org | 33edb6b | 2009-10-01 06:36:37 +0900 | [diff] [blame] | 990 | if (index > list_.size()) |
erikkay@chromium.org | 9034a23 | 2009-08-29 05:26:05 +0900 | [diff] [blame] | 991 | return false; |
| 992 | |
| 993 | list_.insert(list_.begin() + index, in_value); |
| 994 | return true; |
| 995 | } |
| 996 | |
pastarmovj@chromium.org | 1602a47 | 2011-09-20 00:23:10 +0900 | [diff] [blame] | 997 | ListValue::const_iterator ListValue::Find(const Value& value) const { |
| 998 | return std::find_if(list_.begin(), list_.end(), ValueEquals(&value)); |
| 999 | } |
| 1000 | |
rsesek@chromium.org | c2d2b20 | 2012-05-17 00:23:30 +0900 | [diff] [blame] | 1001 | void ListValue::Swap(ListValue* other) { |
| 1002 | list_.swap(other->list_); |
| 1003 | } |
| 1004 | |
scottbyer@google.com | 673b876 | 2010-12-07 09:35:29 +0900 | [diff] [blame] | 1005 | bool ListValue::GetAsList(ListValue** out_value) { |
| 1006 | if (out_value) |
| 1007 | *out_value = this; |
| 1008 | return true; |
| 1009 | } |
| 1010 | |
bauerb@chromium.org | 6878e50 | 2011-07-12 18:04:38 +0900 | [diff] [blame] | 1011 | bool ListValue::GetAsList(const ListValue** out_value) const { |
| 1012 | if (out_value) |
| 1013 | *out_value = this; |
| 1014 | return true; |
| 1015 | } |
| 1016 | |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 1017 | ListValue* ListValue::DeepCopy() const { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1018 | ListValue* result = new ListValue; |
| 1019 | |
pkasting@chromium.org | 727139c | 2009-05-09 09:33:04 +0900 | [diff] [blame] | 1020 | for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) |
| 1021 | result->Append((*i)->DeepCopy()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1022 | |
| 1023 | return result; |
| 1024 | } |
| 1025 | |
| 1026 | bool ListValue::Equals(const Value* other) const { |
| 1027 | if (other->GetType() != GetType()) |
| 1028 | return false; |
| 1029 | |
| 1030 | const ListValue* other_list = |
| 1031 | static_cast<const ListValue*>(other); |
| 1032 | const_iterator lhs_it, rhs_it; |
| 1033 | for (lhs_it = begin(), rhs_it = other_list->begin(); |
| 1034 | lhs_it != end() && rhs_it != other_list->end(); |
| 1035 | ++lhs_it, ++rhs_it) { |
| 1036 | if (!(*lhs_it)->Equals(*rhs_it)) |
| 1037 | return false; |
| 1038 | } |
| 1039 | if (lhs_it != end() || rhs_it != other_list->end()) |
| 1040 | return false; |
| 1041 | |
| 1042 | return true; |
| 1043 | } |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 1044 | |
| 1045 | ValueSerializer::~ValueSerializer() { |
| 1046 | } |
tfarina@chromium.org | de3ef41 | 2011-08-05 08:51:10 +0900 | [diff] [blame] | 1047 | |
| 1048 | } // namespace base |