OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/values.h" | 5 #include "base/values.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 | 10 |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 | 256 |
257 bool StringValue::Equals(const Value* other) const { | 257 bool StringValue::Equals(const Value* other) const { |
258 if (other->GetType() != GetType()) | 258 if (other->GetType() != GetType()) |
259 return false; | 259 return false; |
260 std::string lhs, rhs; | 260 std::string lhs, rhs; |
261 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; | 261 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; |
262 } | 262 } |
263 | 263 |
264 ///////////////////// BinaryValue //////////////////// | 264 ///////////////////// BinaryValue //////////////////// |
265 | 265 |
| 266 BinaryValue::~BinaryValue() { |
| 267 DCHECK(buffer_); |
| 268 if (buffer_) |
| 269 delete[] buffer_; |
| 270 } |
| 271 |
266 // static | 272 // static |
267 BinaryValue* BinaryValue::Create(char* buffer, size_t size) { | 273 BinaryValue* BinaryValue::Create(char* buffer, size_t size) { |
268 if (!buffer) | 274 if (!buffer) |
269 return NULL; | 275 return NULL; |
270 | 276 |
271 return new BinaryValue(buffer, size); | 277 return new BinaryValue(buffer, size); |
272 } | 278 } |
273 | 279 |
274 // static | 280 // static |
275 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, | 281 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, |
276 size_t size) { | 282 size_t size) { |
277 if (!buffer) | 283 if (!buffer) |
278 return NULL; | 284 return NULL; |
279 | 285 |
280 char* buffer_copy = new char[size]; | 286 char* buffer_copy = new char[size]; |
281 memcpy(buffer_copy, buffer, size); | 287 memcpy(buffer_copy, buffer, size); |
282 return new BinaryValue(buffer_copy, size); | 288 return new BinaryValue(buffer_copy, size); |
283 } | 289 } |
284 | 290 |
285 | |
286 BinaryValue::BinaryValue(char* buffer, size_t size) | |
287 : Value(TYPE_BINARY), | |
288 buffer_(buffer), | |
289 size_(size) { | |
290 DCHECK(buffer_); | |
291 } | |
292 | |
293 BinaryValue::~BinaryValue() { | |
294 DCHECK(buffer_); | |
295 if (buffer_) | |
296 delete[] buffer_; | |
297 } | |
298 | |
299 Value* BinaryValue::DeepCopy() const { | 291 Value* BinaryValue::DeepCopy() const { |
300 return CreateWithCopiedBuffer(buffer_, size_); | 292 return CreateWithCopiedBuffer(buffer_, size_); |
301 } | 293 } |
302 | 294 |
303 bool BinaryValue::Equals(const Value* other) const { | 295 bool BinaryValue::Equals(const Value* other) const { |
304 if (other->GetType() != GetType()) | 296 if (other->GetType() != GetType()) |
305 return false; | 297 return false; |
306 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); | 298 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); |
307 if (other_binary->size_ != size_) | 299 if (other_binary->size_ != size_) |
308 return false; | 300 return false; |
309 return !memcmp(buffer_, other_binary->buffer_, size_); | 301 return !memcmp(buffer_, other_binary->buffer_, size_); |
310 } | 302 } |
311 | 303 |
| 304 BinaryValue::BinaryValue(char* buffer, size_t size) |
| 305 : Value(TYPE_BINARY), |
| 306 buffer_(buffer), |
| 307 size_(size) { |
| 308 DCHECK(buffer_); |
| 309 } |
| 310 |
312 ///////////////////// DictionaryValue //////////////////// | 311 ///////////////////// DictionaryValue //////////////////// |
313 | 312 |
314 DictionaryValue::DictionaryValue() | 313 DictionaryValue::DictionaryValue() |
315 : Value(TYPE_DICTIONARY) { | 314 : Value(TYPE_DICTIONARY) { |
316 } | 315 } |
317 | 316 |
318 DictionaryValue::~DictionaryValue() { | 317 DictionaryValue::~DictionaryValue() { |
319 Clear(); | 318 Clear(); |
320 } | 319 } |
321 | 320 |
322 Value* DictionaryValue::DeepCopy() const { | |
323 DictionaryValue* result = new DictionaryValue; | |
324 | |
325 for (ValueMap::const_iterator current_entry(dictionary_.begin()); | |
326 current_entry != dictionary_.end(); ++current_entry) { | |
327 result->SetWithoutPathExpansion(current_entry->first, | |
328 current_entry->second->DeepCopy()); | |
329 } | |
330 | |
331 return result; | |
332 } | |
333 | |
334 bool DictionaryValue::Equals(const Value* other) const { | |
335 if (other->GetType() != GetType()) | |
336 return false; | |
337 | |
338 const DictionaryValue* other_dict = | |
339 static_cast<const DictionaryValue*>(other); | |
340 key_iterator lhs_it(begin_keys()); | |
341 key_iterator rhs_it(other_dict->begin_keys()); | |
342 while (lhs_it != end_keys() && rhs_it != other_dict->end_keys()) { | |
343 Value* lhs; | |
344 Value* rhs; | |
345 if (*lhs_it != *rhs_it || | |
346 !GetWithoutPathExpansion(*lhs_it, &lhs) || | |
347 !other_dict->GetWithoutPathExpansion(*rhs_it, &rhs) || | |
348 !lhs->Equals(rhs)) { | |
349 return false; | |
350 } | |
351 ++lhs_it; | |
352 ++rhs_it; | |
353 } | |
354 if (lhs_it != end_keys() || rhs_it != other_dict->end_keys()) | |
355 return false; | |
356 | |
357 return true; | |
358 } | |
359 | |
360 bool DictionaryValue::HasKey(const std::string& key) const { | 321 bool DictionaryValue::HasKey(const std::string& key) const { |
361 DCHECK(IsStringUTF8(key)); | 322 DCHECK(IsStringUTF8(key)); |
362 ValueMap::const_iterator current_entry = dictionary_.find(key); | 323 ValueMap::const_iterator current_entry = dictionary_.find(key); |
363 DCHECK((current_entry == dictionary_.end()) || current_entry->second); | 324 DCHECK((current_entry == dictionary_.end()) || current_entry->second); |
364 return current_entry != dictionary_.end(); | 325 return current_entry != dictionary_.end(); |
365 } | 326 } |
366 | 327 |
367 void DictionaryValue::Clear() { | 328 void DictionaryValue::Clear() { |
368 ValueMap::iterator dict_iterator = dictionary_.begin(); | 329 ValueMap::iterator dict_iterator = dictionary_.begin(); |
369 while (dict_iterator != dictionary_.end()) { | 330 while (dict_iterator != dictionary_.end()) { |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 static_cast<const DictionaryValue*>(merge_value)); | 639 static_cast<const DictionaryValue*>(merge_value)); |
679 continue; | 640 continue; |
680 } | 641 } |
681 } | 642 } |
682 // All other cases: Make a copy and hook it up. | 643 // All other cases: Make a copy and hook it up. |
683 SetWithoutPathExpansion(*key, merge_value->DeepCopy()); | 644 SetWithoutPathExpansion(*key, merge_value->DeepCopy()); |
684 } | 645 } |
685 } | 646 } |
686 } | 647 } |
687 | 648 |
| 649 Value* DictionaryValue::DeepCopy() const { |
| 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 |
688 ///////////////////// ListValue //////////////////// | 687 ///////////////////// ListValue //////////////////// |
689 | 688 |
690 ListValue::ListValue() : Value(TYPE_LIST) { | 689 ListValue::ListValue() : Value(TYPE_LIST) { |
691 } | 690 } |
692 | 691 |
693 ListValue::~ListValue() { | 692 ListValue::~ListValue() { |
694 Clear(); | 693 Clear(); |
695 } | 694 } |
696 | 695 |
697 void ListValue::Clear() { | 696 void ListValue::Clear() { |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
886 return false; | 885 return false; |
887 } | 886 } |
888 if (lhs_it != end() || rhs_it != other_list->end()) | 887 if (lhs_it != end() || rhs_it != other_list->end()) |
889 return false; | 888 return false; |
890 | 889 |
891 return true; | 890 return true; |
892 } | 891 } |
893 | 892 |
894 ValueSerializer::~ValueSerializer() { | 893 ValueSerializer::~ValueSerializer() { |
895 } | 894 } |
OLD | NEW |