Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(888)

Side by Side Diff: base/values.cc

Issue 254473002: Add support for int64 and uint64 in values.h's API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix test compile Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/values.h ('k') | base/values_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
11 11
12 #include "base/float_util.h" 12 #include "base/float_util.h"
13 #include "base/json/json_writer.h" 13 #include "base/json/json_writer.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/move.h" 15 #include "base/move.h"
16 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
18 19
19 namespace base { 20 namespace base {
20 21
21 namespace { 22 namespace {
22 23
23 // Make a deep copy of |node|, but don't include empty lists or dictionaries 24 // Make a deep copy of |node|, but don't include empty lists or dictionaries
24 // in the copy. It's possible for this function to return NULL and it 25 // in the copy. It's possible for this function to return NULL and it
25 // expects |node| to always be non-NULL. 26 // expects |node| to always be non-NULL.
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 } 132 }
132 133
133 bool Value::GetAsString(string16* out_value) const { 134 bool Value::GetAsString(string16* out_value) const {
134 return false; 135 return false;
135 } 136 }
136 137
137 bool Value::GetAsString(const StringValue** out_value) const { 138 bool Value::GetAsString(const StringValue** out_value) const {
138 return false; 139 return false;
139 } 140 }
140 141
142 bool Value::GetAsInt64(int64* out_value) const {
143 return false;
144 }
145
146 bool Value::GetAsUint64(uint64* out_value) const {
147 return false;
148 }
149
141 bool Value::GetAsList(ListValue** out_value) { 150 bool Value::GetAsList(ListValue** out_value) {
142 return false; 151 return false;
143 } 152 }
144 153
145 bool Value::GetAsList(const ListValue** out_value) const { 154 bool Value::GetAsList(const ListValue** out_value) const {
146 return false; 155 return false;
147 } 156 }
148 157
149 bool Value::GetAsDictionary(DictionaryValue** out_value) { 158 bool Value::GetAsDictionary(DictionaryValue** out_value) {
150 return false; 159 return false;
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 : Value(TYPE_STRING), 281 : Value(TYPE_STRING),
273 value_(in_value) { 282 value_(in_value) {
274 DCHECK(IsStringUTF8(in_value)); 283 DCHECK(IsStringUTF8(in_value));
275 } 284 }
276 285
277 StringValue::StringValue(const string16& in_value) 286 StringValue::StringValue(const string16& in_value)
278 : Value(TYPE_STRING), 287 : Value(TYPE_STRING),
279 value_(UTF16ToUTF8(in_value)) { 288 value_(UTF16ToUTF8(in_value)) {
280 } 289 }
281 290
291 StringValue::StringValue(int64 in_value)
292 : Value(TYPE_STRING), value_(Int64ToString(in_value)) {
293 }
294
295 StringValue::StringValue(uint64 in_value)
296 : Value(TYPE_STRING), value_(Uint64ToString(in_value)) {
297 }
298
282 StringValue::~StringValue() { 299 StringValue::~StringValue() {
283 } 300 }
284 301
285 std::string* StringValue::GetString() { 302 std::string* StringValue::GetString() {
286 return &value_; 303 return &value_;
287 } 304 }
288 305
289 const std::string& StringValue::GetString() const { 306 const std::string& StringValue::GetString() const {
290 return value_; 307 return value_;
291 } 308 }
292 309
293 bool StringValue::GetAsString(std::string* out_value) const { 310 bool StringValue::GetAsString(std::string* out_value) const {
294 if (out_value) 311 if (out_value)
295 *out_value = value_; 312 *out_value = value_;
296 return true; 313 return true;
297 } 314 }
298 315
299 bool StringValue::GetAsString(string16* out_value) const { 316 bool StringValue::GetAsString(string16* out_value) const {
300 if (out_value) 317 if (out_value)
301 *out_value = UTF8ToUTF16(value_); 318 *out_value = UTF8ToUTF16(value_);
302 return true; 319 return true;
303 } 320 }
304 321
305 bool StringValue::GetAsString(const StringValue** out_value) const { 322 bool StringValue::GetAsString(const StringValue** out_value) const {
306 if (out_value) 323 if (out_value)
307 *out_value = this; 324 *out_value = this;
308 return true; 325 return true;
309 } 326 }
310 327
328 bool StringValue::GetAsInt64(int64* out_value) const {
329 int64 stored_value;
330 bool valid = StringToInt64(value_, &stored_value);
331 if (valid && out_value)
332 *out_value = stored_value;
333 return valid;
334 }
335
336 bool StringValue::GetAsUint64(uint64* out_value) const {
337 uint64 stored_value;
338 bool valid = base::StringToUint64(value_, &stored_value);
339 if (valid && out_value)
340 *out_value = stored_value;
341 return valid;
342 }
343
311 StringValue* StringValue::DeepCopy() const { 344 StringValue* StringValue::DeepCopy() const {
312 return new StringValue(value_); 345 return new StringValue(value_);
313 } 346 }
314 347
315 bool StringValue::Equals(const Value* other) const { 348 bool StringValue::Equals(const Value* other) const {
316 if (other->GetType() != GetType()) 349 if (other->GetType() != GetType())
317 return false; 350 return false;
318 std::string lhs, rhs; 351 std::string lhs, rhs;
319 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; 352 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
320 } 353 }
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 void DictionaryValue::SetString(const std::string& path, 468 void DictionaryValue::SetString(const std::string& path,
436 const std::string& in_value) { 469 const std::string& in_value) {
437 Set(path, new StringValue(in_value)); 470 Set(path, new StringValue(in_value));
438 } 471 }
439 472
440 void DictionaryValue::SetString(const std::string& path, 473 void DictionaryValue::SetString(const std::string& path,
441 const string16& in_value) { 474 const string16& in_value) {
442 Set(path, new StringValue(in_value)); 475 Set(path, new StringValue(in_value));
443 } 476 }
444 477
478 void DictionaryValue::SetInt64(const std::string& path, int64 in_value) {
479 Set(path, new StringValue(in_value));
480 }
481
482 void DictionaryValue::SetUint64(const std::string& path, uint64 in_value) {
483 Set(path, new StringValue(in_value));
484 }
485
445 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, 486 void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
446 Value* in_value) { 487 Value* in_value) {
447 // If there's an existing value here, we need to delete it, because 488 // If there's an existing value here, we need to delete it, because
448 // we own all our children. 489 // we own all our children.
449 std::pair<ValueMap::iterator, bool> ins_res = 490 std::pair<ValueMap::iterator, bool> ins_res =
450 dictionary_.insert(std::make_pair(key, in_value)); 491 dictionary_.insert(std::make_pair(key, in_value));
451 if (!ins_res.second) { 492 if (!ins_res.second) {
452 DCHECK_NE(ins_res.first->second, in_value); // This would be bogus 493 DCHECK_NE(ins_res.first->second, in_value); // This would be bogus
453 delete ins_res.first->second; 494 delete ins_res.first->second;
454 ins_res.first->second = in_value; 495 ins_res.first->second = in_value;
(...skipping 18 matching lines...) Expand all
473 void DictionaryValue::SetStringWithoutPathExpansion( 514 void DictionaryValue::SetStringWithoutPathExpansion(
474 const std::string& path, const std::string& in_value) { 515 const std::string& path, const std::string& in_value) {
475 SetWithoutPathExpansion(path, new StringValue(in_value)); 516 SetWithoutPathExpansion(path, new StringValue(in_value));
476 } 517 }
477 518
478 void DictionaryValue::SetStringWithoutPathExpansion( 519 void DictionaryValue::SetStringWithoutPathExpansion(
479 const std::string& path, const string16& in_value) { 520 const std::string& path, const string16& in_value) {
480 SetWithoutPathExpansion(path, new StringValue(in_value)); 521 SetWithoutPathExpansion(path, new StringValue(in_value));
481 } 522 }
482 523
524 void DictionaryValue::SetInt64WithoutPathExpansion(const std::string& path,
525 int64 in_value) {
526 SetWithoutPathExpansion(path, new StringValue(in_value));
527 }
528
529 void DictionaryValue::SetUint64WithoutPathExpansion(const std::string& path,
530 uint64 in_value) {
531 SetWithoutPathExpansion(path, new StringValue(in_value));
532 }
533
483 bool DictionaryValue::Get(const std::string& path, 534 bool DictionaryValue::Get(const std::string& path,
484 const Value** out_value) const { 535 const Value** out_value) const {
485 DCHECK(IsStringUTF8(path)); 536 DCHECK(IsStringUTF8(path));
486 std::string current_path(path); 537 std::string current_path(path);
487 const DictionaryValue* current_dictionary = this; 538 const DictionaryValue* current_dictionary = this;
488 for (size_t delimiter_position = current_path.find('.'); 539 for (size_t delimiter_position = current_path.find('.');
489 delimiter_position != std::string::npos; 540 delimiter_position != std::string::npos;
490 delimiter_position = current_path.find('.')) { 541 delimiter_position = current_path.find('.')) {
491 const DictionaryValue* child_dictionary = NULL; 542 const DictionaryValue* child_dictionary = NULL;
492 if (!current_dictionary->GetDictionary( 543 if (!current_dictionary->GetDictionary(
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 595
545 bool DictionaryValue::GetString(const std::string& path, 596 bool DictionaryValue::GetString(const std::string& path,
546 string16* out_value) const { 597 string16* out_value) const {
547 const Value* value; 598 const Value* value;
548 if (!Get(path, &value)) 599 if (!Get(path, &value))
549 return false; 600 return false;
550 601
551 return value->GetAsString(out_value); 602 return value->GetAsString(out_value);
552 } 603 }
553 604
605 bool DictionaryValue::GetInt64(const std::string& path,
606 int64* out_value) const {
607 const Value* value;
608 if (!Get(path, &value))
609 return false;
610
611 return value->GetAsInt64(out_value);
612 }
613
614 bool DictionaryValue::GetUint64(const std::string& path,
615 uint64* out_value) const {
616 const Value* value;
617 if (!Get(path, &value))
618 return false;
619
620 return value->GetAsUint64(out_value);
621 }
622
554 bool DictionaryValue::GetStringASCII(const std::string& path, 623 bool DictionaryValue::GetStringASCII(const std::string& path,
555 std::string* out_value) const { 624 std::string* out_value) const {
556 std::string out; 625 std::string out;
557 if (!GetString(path, &out)) 626 if (!GetString(path, &out))
558 return false; 627 return false;
559 628
560 if (!IsStringASCII(out)) { 629 if (!IsStringASCII(out)) {
561 NOTREACHED(); 630 NOTREACHED();
562 return false; 631 return false;
563 } 632 }
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
684 753
685 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key, 754 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key,
686 string16* out_value) const { 755 string16* out_value) const {
687 const Value* value; 756 const Value* value;
688 if (!GetWithoutPathExpansion(key, &value)) 757 if (!GetWithoutPathExpansion(key, &value))
689 return false; 758 return false;
690 759
691 return value->GetAsString(out_value); 760 return value->GetAsString(out_value);
692 } 761 }
693 762
763 bool DictionaryValue::GetInt64WithoutPathExpansion(const std::string& key,
764 int64* out_value) const {
765 const Value* value;
766 if (!GetWithoutPathExpansion(key, &value))
767 return false;
768
769 return value->GetAsInt64(out_value);
770 }
771
772 bool DictionaryValue::GetUint64WithoutPathExpansion(const std::string& key,
773 uint64* out_value) const {
774 const Value* value;
775 if (!GetWithoutPathExpansion(key, &value))
776 return false;
777
778 return value->GetAsUint64(out_value);
779 }
780
694 bool DictionaryValue::GetDictionaryWithoutPathExpansion( 781 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
695 const std::string& key, 782 const std::string& key,
696 const DictionaryValue** out_value) const { 783 const DictionaryValue** out_value) const {
697 const Value* value; 784 const Value* value;
698 bool result = GetWithoutPathExpansion(key, &value); 785 bool result = GetWithoutPathExpansion(key, &value);
699 if (!result || !value->IsType(TYPE_DICTIONARY)) 786 if (!result || !value->IsType(TYPE_DICTIONARY))
700 return false; 787 return false;
701 788
702 if (out_value) 789 if (out_value)
703 *out_value = static_cast<const DictionaryValue*>(value); 790 *out_value = static_cast<const DictionaryValue*>(value);
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
937 } 1024 }
938 1025
939 bool ListValue::GetString(size_t index, string16* out_value) const { 1026 bool ListValue::GetString(size_t index, string16* out_value) const {
940 const Value* value; 1027 const Value* value;
941 if (!Get(index, &value)) 1028 if (!Get(index, &value))
942 return false; 1029 return false;
943 1030
944 return value->GetAsString(out_value); 1031 return value->GetAsString(out_value);
945 } 1032 }
946 1033
1034 bool ListValue::GetInt64(size_t index, int64* out_value) const {
1035 const Value* value;
1036 if (!Get(index, &value))
1037 return false;
1038
1039 return value->GetAsInt64(out_value);
1040 }
1041
1042 bool ListValue::GetUint64(size_t index, uint64* out_value) const {
1043 const Value* value;
1044 if (!Get(index, &value))
1045 return false;
1046
1047 return value->GetAsUint64(out_value);
1048 }
1049
947 bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const { 1050 bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const {
948 const Value* value; 1051 const Value* value;
949 bool result = Get(index, &value); 1052 bool result = Get(index, &value);
950 if (!result || !value->IsType(TYPE_BINARY)) 1053 if (!result || !value->IsType(TYPE_BINARY))
951 return false; 1054 return false;
952 1055
953 if (out_value) 1056 if (out_value)
954 *out_value = static_cast<const BinaryValue*>(value); 1057 *out_value = static_cast<const BinaryValue*>(value);
955 1058
956 return true; 1059 return true;
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
1055 } 1158 }
1056 1159
1057 void ListValue::AppendString(const std::string& in_value) { 1160 void ListValue::AppendString(const std::string& in_value) {
1058 Append(new StringValue(in_value)); 1161 Append(new StringValue(in_value));
1059 } 1162 }
1060 1163
1061 void ListValue::AppendString(const string16& in_value) { 1164 void ListValue::AppendString(const string16& in_value) {
1062 Append(new StringValue(in_value)); 1165 Append(new StringValue(in_value));
1063 } 1166 }
1064 1167
1168 void ListValue::AppendInt64(int64 in_value) {
1169 Append(new StringValue(in_value));
1170 }
1171
1172 void ListValue::AppendUint64(uint64 in_value) {
1173 Append(new StringValue(in_value));
1174 }
1175
1065 void ListValue::AppendStrings(const std::vector<std::string>& in_values) { 1176 void ListValue::AppendStrings(const std::vector<std::string>& in_values) {
1066 for (std::vector<std::string>::const_iterator it = in_values.begin(); 1177 for (std::vector<std::string>::const_iterator it = in_values.begin();
1067 it != in_values.end(); ++it) { 1178 it != in_values.end(); ++it) {
1068 AppendString(*it); 1179 AppendString(*it);
1069 } 1180 }
1070 } 1181 }
1071 1182
1072 void ListValue::AppendStrings(const std::vector<string16>& in_values) { 1183 void ListValue::AppendStrings(const std::vector<string16>& in_values) {
1073 for (std::vector<string16>::const_iterator it = in_values.begin(); 1184 for (std::vector<string16>::const_iterator it = in_values.begin();
1074 it != in_values.end(); ++it) { 1185 it != in_values.end(); ++it) {
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1150 1261
1151 std::ostream& operator<<(std::ostream& out, const Value& value) { 1262 std::ostream& operator<<(std::ostream& out, const Value& value) {
1152 std::string json; 1263 std::string json;
1153 JSONWriter::WriteWithOptions(&value, 1264 JSONWriter::WriteWithOptions(&value,
1154 JSONWriter::OPTIONS_PRETTY_PRINT, 1265 JSONWriter::OPTIONS_PRETTY_PRINT,
1155 &json); 1266 &json);
1156 return out << json; 1267 return out << json;
1157 } 1268 }
1158 1269
1159 } // namespace base 1270 } // namespace base
OLDNEW
« no previous file with comments | « base/values.h ('k') | base/values_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698