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