OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <ostream> | 10 #include <ostream> |
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 void DictionaryValue::Clear() { | 364 void DictionaryValue::Clear() { |
365 ValueMap::iterator dict_iterator = dictionary_.begin(); | 365 ValueMap::iterator dict_iterator = dictionary_.begin(); |
366 while (dict_iterator != dictionary_.end()) { | 366 while (dict_iterator != dictionary_.end()) { |
367 delete dict_iterator->second; | 367 delete dict_iterator->second; |
368 ++dict_iterator; | 368 ++dict_iterator; |
369 } | 369 } |
370 | 370 |
371 dictionary_.clear(); | 371 dictionary_.clear(); |
372 } | 372 } |
373 | 373 |
374 void DictionaryValue::Set(const std::string& path, Value* in_value) { | 374 void DictionaryValue::Set(const std::string& path, scoped_ptr<Value> in_value) { |
375 DCHECK(IsStringUTF8(path)); | 375 DCHECK(IsStringUTF8(path)); |
376 DCHECK(in_value); | 376 DCHECK(in_value); |
377 | 377 |
378 std::string current_path(path); | 378 std::string current_path(path); |
379 DictionaryValue* current_dictionary = this; | 379 DictionaryValue* current_dictionary = this; |
380 for (size_t delimiter_position = current_path.find('.'); | 380 for (size_t delimiter_position = current_path.find('.'); |
381 delimiter_position != std::string::npos; | 381 delimiter_position != std::string::npos; |
382 delimiter_position = current_path.find('.')) { | 382 delimiter_position = current_path.find('.')) { |
383 // Assume that we're indexing into a dictionary. | 383 // Assume that we're indexing into a dictionary. |
384 std::string key(current_path, 0, delimiter_position); | 384 std::string key(current_path, 0, delimiter_position); |
385 DictionaryValue* child_dictionary = NULL; | 385 DictionaryValue* child_dictionary = NULL; |
386 if (!current_dictionary->GetDictionary(key, &child_dictionary)) { | 386 if (!current_dictionary->GetDictionary(key, &child_dictionary)) { |
387 child_dictionary = new DictionaryValue; | 387 child_dictionary = new DictionaryValue; |
388 current_dictionary->SetWithoutPathExpansion(key, child_dictionary); | 388 current_dictionary->SetWithoutPathExpansion(key, child_dictionary); |
389 } | 389 } |
390 | 390 |
391 current_dictionary = child_dictionary; | 391 current_dictionary = child_dictionary; |
392 current_path.erase(0, delimiter_position + 1); | 392 current_path.erase(0, delimiter_position + 1); |
393 } | 393 } |
394 | 394 |
395 current_dictionary->SetWithoutPathExpansion(current_path, in_value); | 395 current_dictionary->SetWithoutPathExpansion(current_path, in_value.Pass()); |
| 396 } |
| 397 |
| 398 void DictionaryValue::Set(const std::string& path, Value* in_value) { |
| 399 Set(path, make_scoped_ptr(in_value)); |
396 } | 400 } |
397 | 401 |
398 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) { | 402 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) { |
399 Set(path, new FundamentalValue(in_value)); | 403 Set(path, new FundamentalValue(in_value)); |
400 } | 404 } |
401 | 405 |
402 void DictionaryValue::SetInteger(const std::string& path, int in_value) { | 406 void DictionaryValue::SetInteger(const std::string& path, int in_value) { |
403 Set(path, new FundamentalValue(in_value)); | 407 Set(path, new FundamentalValue(in_value)); |
404 } | 408 } |
405 | 409 |
406 void DictionaryValue::SetDouble(const std::string& path, double in_value) { | 410 void DictionaryValue::SetDouble(const std::string& path, double in_value) { |
407 Set(path, new FundamentalValue(in_value)); | 411 Set(path, new FundamentalValue(in_value)); |
408 } | 412 } |
409 | 413 |
410 void DictionaryValue::SetString(const std::string& path, | 414 void DictionaryValue::SetString(const std::string& path, |
411 const std::string& in_value) { | 415 const std::string& in_value) { |
412 Set(path, new StringValue(in_value)); | 416 Set(path, new StringValue(in_value)); |
413 } | 417 } |
414 | 418 |
415 void DictionaryValue::SetString(const std::string& path, | 419 void DictionaryValue::SetString(const std::string& path, |
416 const string16& in_value) { | 420 const string16& in_value) { |
417 Set(path, new StringValue(in_value)); | 421 Set(path, new StringValue(in_value)); |
418 } | 422 } |
419 | 423 |
420 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, | 424 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, |
| 425 scoped_ptr<Value> in_value) { |
| 426 Value* bare_ptr = in_value.release(); |
| 427 // If there's an existing value here, we need to delete it, because |
| 428 // we own all our children. |
| 429 std::pair<ValueMap::iterator, bool> ins_res = |
| 430 dictionary_.insert(std::make_pair(key, bare_ptr)); |
| 431 if (!ins_res.second) { |
| 432 DCHECK_NE(ins_res.first->second, bare_ptr); // This would be bogus |
| 433 delete ins_res.first->second; |
| 434 ins_res.first->second = bare_ptr; |
| 435 } |
| 436 } |
| 437 |
| 438 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, |
421 Value* in_value) { | 439 Value* in_value) { |
422 // If there's an existing value here, we need to delete it, because | 440 SetWithoutPathExpansion(key, make_scoped_ptr(in_value)); |
423 // we own all our children. | |
424 std::pair<ValueMap::iterator, bool> ins_res = | |
425 dictionary_.insert(std::make_pair(key, in_value)); | |
426 if (!ins_res.second) { | |
427 DCHECK_NE(ins_res.first->second, in_value); // This would be bogus | |
428 delete ins_res.first->second; | |
429 ins_res.first->second = in_value; | |
430 } | |
431 } | 441 } |
432 | 442 |
433 void DictionaryValue::SetBooleanWithoutPathExpansion( | 443 void DictionaryValue::SetBooleanWithoutPathExpansion( |
434 const std::string& path, bool in_value) { | 444 const std::string& path, bool in_value) { |
435 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); | 445 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); |
436 } | 446 } |
437 | 447 |
438 void DictionaryValue::SetIntegerWithoutPathExpansion( | 448 void DictionaryValue::SetIntegerWithoutPathExpansion( |
439 const std::string& path, int in_value) { | 449 const std::string& path, int in_value) { |
440 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); | 450 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); |
(...skipping 684 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1125 | 1135 |
1126 std::ostream& operator<<(std::ostream& out, const Value& value) { | 1136 std::ostream& operator<<(std::ostream& out, const Value& value) { |
1127 std::string json; | 1137 std::string json; |
1128 JSONWriter::WriteWithOptions(&value, | 1138 JSONWriter::WriteWithOptions(&value, |
1129 JSONWriter::OPTIONS_PRETTY_PRINT, | 1139 JSONWriter::OPTIONS_PRETTY_PRINT, |
1130 &json); | 1140 &json); |
1131 return out << json; | 1141 return out << json; |
1132 } | 1142 } |
1133 | 1143 |
1134 } // namespace base | 1144 } // namespace base |
OLD | NEW |