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