OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/float_util.h" | 9 #include "base/float_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 FundamentalValue* Value::CreateBooleanValue(bool in_value) { | 94 FundamentalValue* Value::CreateBooleanValue(bool in_value) { |
95 return new FundamentalValue(in_value); | 95 return new FundamentalValue(in_value); |
96 } | 96 } |
97 | 97 |
98 // static | 98 // static |
99 FundamentalValue* Value::CreateIntegerValue(int in_value) { | 99 FundamentalValue* Value::CreateIntegerValue(int in_value) { |
100 return new FundamentalValue(in_value); | 100 return new FundamentalValue(in_value); |
101 } | 101 } |
102 | 102 |
103 // static | 103 // static |
| 104 FundamentalValue* Value::CreateInteger64Value(int64 in_value) { |
| 105 return new FundamentalValue(in_value); |
| 106 } |
| 107 |
| 108 // static |
104 FundamentalValue* Value::CreateDoubleValue(double in_value) { | 109 FundamentalValue* Value::CreateDoubleValue(double in_value) { |
105 return new FundamentalValue(in_value); | 110 return new FundamentalValue(in_value); |
106 } | 111 } |
107 | 112 |
108 // static | 113 // static |
109 StringValue* Value::CreateStringValue(const std::string& in_value) { | 114 StringValue* Value::CreateStringValue(const std::string& in_value) { |
110 return new StringValue(in_value); | 115 return new StringValue(in_value); |
111 } | 116 } |
112 | 117 |
113 // static | 118 // static |
114 StringValue* Value::CreateStringValue(const string16& in_value) { | 119 StringValue* Value::CreateStringValue(const string16& in_value) { |
115 return new StringValue(in_value); | 120 return new StringValue(in_value); |
116 } | 121 } |
117 | 122 |
118 bool Value::GetAsBoolean(bool* out_value) const { | 123 bool Value::GetAsBoolean(bool* out_value) const { |
119 return false; | 124 return false; |
120 } | 125 } |
121 | 126 |
122 bool Value::GetAsInteger(int* out_value) const { | 127 bool Value::GetAsInteger(int* out_value) const { |
123 return false; | 128 return false; |
124 } | 129 } |
125 | 130 |
| 131 bool Value::GetAsInteger64(int64* out_value) const { |
| 132 return false; |
| 133 } |
| 134 |
126 bool Value::GetAsDouble(double* out_value) const { | 135 bool Value::GetAsDouble(double* out_value) const { |
127 return false; | 136 return false; |
128 } | 137 } |
129 | 138 |
130 bool Value::GetAsString(std::string* out_value) const { | 139 bool Value::GetAsString(std::string* out_value) const { |
131 return false; | 140 return false; |
132 } | 141 } |
133 | 142 |
134 bool Value::GetAsString(string16* out_value) const { | 143 bool Value::GetAsString(string16* out_value) const { |
135 return false; | 144 return false; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 ///////////////////// FundamentalValue //////////////////// | 187 ///////////////////// FundamentalValue //////////////////// |
179 | 188 |
180 FundamentalValue::FundamentalValue(bool in_value) | 189 FundamentalValue::FundamentalValue(bool in_value) |
181 : Value(TYPE_BOOLEAN), boolean_value_(in_value) { | 190 : Value(TYPE_BOOLEAN), boolean_value_(in_value) { |
182 } | 191 } |
183 | 192 |
184 FundamentalValue::FundamentalValue(int in_value) | 193 FundamentalValue::FundamentalValue(int in_value) |
185 : Value(TYPE_INTEGER), integer_value_(in_value) { | 194 : Value(TYPE_INTEGER), integer_value_(in_value) { |
186 } | 195 } |
187 | 196 |
| 197 FundamentalValue::FundamentalValue(int64 in_value) |
| 198 : Value(TYPE_INTEGER64), integer_value_(in_value) { |
| 199 } |
| 200 |
188 FundamentalValue::FundamentalValue(double in_value) | 201 FundamentalValue::FundamentalValue(double in_value) |
189 : Value(TYPE_DOUBLE), double_value_(in_value) { | 202 : Value(TYPE_DOUBLE), double_value_(in_value) { |
190 if (!IsFinite(double_value_)) { | 203 if (!IsFinite(double_value_)) { |
191 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) " | 204 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) " |
192 << "values cannot be represented in JSON"; | 205 << "values cannot be represented in JSON"; |
193 double_value_ = 0.0; | 206 double_value_ = 0.0; |
194 } | 207 } |
195 } | 208 } |
196 | 209 |
197 FundamentalValue::~FundamentalValue() { | 210 FundamentalValue::~FundamentalValue() { |
198 } | 211 } |
199 | 212 |
200 bool FundamentalValue::GetAsBoolean(bool* out_value) const { | 213 bool FundamentalValue::GetAsBoolean(bool* out_value) const { |
201 if (out_value && IsType(TYPE_BOOLEAN)) | 214 if (out_value && IsType(TYPE_BOOLEAN)) |
202 *out_value = boolean_value_; | 215 *out_value = boolean_value_; |
203 return (IsType(TYPE_BOOLEAN)); | 216 return (IsType(TYPE_BOOLEAN)); |
204 } | 217 } |
205 | 218 |
206 bool FundamentalValue::GetAsInteger(int* out_value) const { | 219 bool FundamentalValue::GetAsInteger(int* out_value) const { |
207 if (out_value && IsType(TYPE_INTEGER)) | 220 if (out_value && IsType(TYPE_INTEGER)) |
208 *out_value = integer_value_; | 221 *out_value = integer_value_; |
209 return (IsType(TYPE_INTEGER)); | 222 return (IsType(TYPE_INTEGER)); |
210 } | 223 } |
211 | 224 |
| 225 bool FundamentalValue::GetAsInteger64(int64* out_value) const { |
| 226 if (out_value && IsType(TYPE_INTEGER64)) |
| 227 *out_value = integer_value_; |
| 228 else if (out_value && IsType(TYPE_INTEGER)) |
| 229 *out_value = integer_value_; |
| 230 return (IsType(TYPE_INTEGER64) || IsType(TYPE_INTEGER)); |
| 231 } |
| 232 |
212 bool FundamentalValue::GetAsDouble(double* out_value) const { | 233 bool FundamentalValue::GetAsDouble(double* out_value) const { |
213 if (out_value && IsType(TYPE_DOUBLE)) | 234 if (out_value && IsType(TYPE_DOUBLE)) |
214 *out_value = double_value_; | 235 *out_value = double_value_; |
215 else if (out_value && IsType(TYPE_INTEGER)) | 236 else if (out_value && IsType(TYPE_INTEGER)) |
216 *out_value = integer_value_; | 237 *out_value = integer_value_; |
217 return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER)); | 238 return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER)); |
218 } | 239 } |
219 | 240 |
220 FundamentalValue* FundamentalValue::DeepCopy() const { | 241 FundamentalValue* FundamentalValue::DeepCopy() const { |
221 switch (GetType()) { | 242 switch (GetType()) { |
222 case TYPE_BOOLEAN: | 243 case TYPE_BOOLEAN: |
223 return CreateBooleanValue(boolean_value_); | 244 return CreateBooleanValue(boolean_value_); |
224 | 245 |
225 case TYPE_INTEGER: | 246 case TYPE_INTEGER: |
226 return CreateIntegerValue(integer_value_); | 247 return CreateIntegerValue(integer_value_); |
227 | 248 |
| 249 case TYPE_INTEGER64: |
| 250 return CreateInteger64Value(integer_value_); |
| 251 |
228 case TYPE_DOUBLE: | 252 case TYPE_DOUBLE: |
229 return CreateDoubleValue(double_value_); | 253 return CreateDoubleValue(double_value_); |
230 | 254 |
231 default: | 255 default: |
232 NOTREACHED(); | 256 NOTREACHED(); |
233 return NULL; | 257 return NULL; |
234 } | 258 } |
235 } | 259 } |
236 | 260 |
237 bool FundamentalValue::Equals(const Value* other) const { | 261 bool FundamentalValue::Equals(const Value* other) const { |
238 if (other->GetType() != GetType()) | 262 if (other->GetType() != GetType()) |
239 return false; | 263 return false; |
240 | 264 |
241 switch (GetType()) { | 265 switch (GetType()) { |
242 case TYPE_BOOLEAN: { | 266 case TYPE_BOOLEAN: { |
243 bool lhs, rhs; | 267 bool lhs, rhs; |
244 return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs; | 268 return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs; |
245 } | 269 } |
246 case TYPE_INTEGER: { | 270 case TYPE_INTEGER: |
247 int lhs, rhs; | 271 case TYPE_INTEGER64: { |
248 return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs; | 272 int64 lhs, rhs; |
| 273 return GetAsInteger64(&lhs) && other->GetAsInteger64(&rhs) && lhs == rhs; |
249 } | 274 } |
250 case TYPE_DOUBLE: { | 275 case TYPE_DOUBLE: { |
251 double lhs, rhs; | 276 double lhs, rhs; |
252 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs; | 277 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs; |
253 } | 278 } |
254 default: | 279 default: |
255 NOTREACHED(); | 280 NOTREACHED(); |
256 return false; | 281 return false; |
257 } | 282 } |
258 } | 283 } |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 } | 432 } |
408 | 433 |
409 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) { | 434 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) { |
410 Set(path, CreateBooleanValue(in_value)); | 435 Set(path, CreateBooleanValue(in_value)); |
411 } | 436 } |
412 | 437 |
413 void DictionaryValue::SetInteger(const std::string& path, int in_value) { | 438 void DictionaryValue::SetInteger(const std::string& path, int in_value) { |
414 Set(path, CreateIntegerValue(in_value)); | 439 Set(path, CreateIntegerValue(in_value)); |
415 } | 440 } |
416 | 441 |
| 442 void DictionaryValue::SetInteger64(const std::string& path, int64 in_value) { |
| 443 Set(path, CreateInteger64Value(in_value)); |
| 444 } |
| 445 |
417 void DictionaryValue::SetDouble(const std::string& path, double in_value) { | 446 void DictionaryValue::SetDouble(const std::string& path, double in_value) { |
418 Set(path, CreateDoubleValue(in_value)); | 447 Set(path, CreateDoubleValue(in_value)); |
419 } | 448 } |
420 | 449 |
421 void DictionaryValue::SetString(const std::string& path, | 450 void DictionaryValue::SetString(const std::string& path, |
422 const std::string& in_value) { | 451 const std::string& in_value) { |
423 Set(path, CreateStringValue(in_value)); | 452 Set(path, CreateStringValue(in_value)); |
424 } | 453 } |
425 | 454 |
426 void DictionaryValue::SetString(const std::string& path, | 455 void DictionaryValue::SetString(const std::string& path, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 | 500 |
472 bool DictionaryValue::GetInteger(const std::string& path, | 501 bool DictionaryValue::GetInteger(const std::string& path, |
473 int* out_value) const { | 502 int* out_value) const { |
474 Value* value; | 503 Value* value; |
475 if (!Get(path, &value)) | 504 if (!Get(path, &value)) |
476 return false; | 505 return false; |
477 | 506 |
478 return value->GetAsInteger(out_value); | 507 return value->GetAsInteger(out_value); |
479 } | 508 } |
480 | 509 |
| 510 bool DictionaryValue::GetInteger64(const std::string& path, |
| 511 int64* out_value) const { |
| 512 Value* value; |
| 513 if (!Get(path, &value)) |
| 514 return false; |
| 515 |
| 516 return value->GetAsInteger64(out_value); |
| 517 } |
| 518 |
481 bool DictionaryValue::GetDouble(const std::string& path, | 519 bool DictionaryValue::GetDouble(const std::string& path, |
482 double* out_value) const { | 520 double* out_value) const { |
483 Value* value; | 521 Value* value; |
484 if (!Get(path, &value)) | 522 if (!Get(path, &value)) |
485 return false; | 523 return false; |
486 | 524 |
487 return value->GetAsDouble(out_value); | 525 return value->GetAsDouble(out_value); |
488 } | 526 } |
489 | 527 |
490 bool DictionaryValue::GetString(const std::string& path, | 528 bool DictionaryValue::GetString(const std::string& path, |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 | 612 |
575 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key, | 613 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key, |
576 int* out_value) const { | 614 int* out_value) const { |
577 Value* value; | 615 Value* value; |
578 if (!GetWithoutPathExpansion(key, &value)) | 616 if (!GetWithoutPathExpansion(key, &value)) |
579 return false; | 617 return false; |
580 | 618 |
581 return value->GetAsInteger(out_value); | 619 return value->GetAsInteger(out_value); |
582 } | 620 } |
583 | 621 |
| 622 bool DictionaryValue::GetInteger64WithoutPathExpansion(const std::string& key, |
| 623 int64* out_value) const { |
| 624 Value* value; |
| 625 if (!GetWithoutPathExpansion(key, &value)) |
| 626 return false; |
| 627 |
| 628 return value->GetAsInteger64(out_value); |
| 629 } |
| 630 |
584 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key, | 631 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key, |
585 double* out_value) const { | 632 double* out_value) const { |
586 Value* value; | 633 Value* value; |
587 if (!GetWithoutPathExpansion(key, &value)) | 634 if (!GetWithoutPathExpansion(key, &value)) |
588 return false; | 635 return false; |
589 | 636 |
590 return value->GetAsDouble(out_value); | 637 return value->GetAsDouble(out_value); |
591 } | 638 } |
592 | 639 |
593 bool DictionaryValue::GetStringWithoutPathExpansion( | 640 bool DictionaryValue::GetStringWithoutPathExpansion( |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
783 } | 830 } |
784 | 831 |
785 bool ListValue::GetInteger(size_t index, int* out_value) const { | 832 bool ListValue::GetInteger(size_t index, int* out_value) const { |
786 Value* value; | 833 Value* value; |
787 if (!Get(index, &value)) | 834 if (!Get(index, &value)) |
788 return false; | 835 return false; |
789 | 836 |
790 return value->GetAsInteger(out_value); | 837 return value->GetAsInteger(out_value); |
791 } | 838 } |
792 | 839 |
| 840 bool ListValue::GetInteger64(size_t index, int64* out_value) const { |
| 841 Value* value; |
| 842 if (!Get(index, &value)) |
| 843 return false; |
| 844 |
| 845 return value->GetAsInteger64(out_value); |
| 846 } |
| 847 |
793 bool ListValue::GetDouble(size_t index, double* out_value) const { | 848 bool ListValue::GetDouble(size_t index, double* out_value) const { |
794 Value* value; | 849 Value* value; |
795 if (!Get(index, &value)) | 850 if (!Get(index, &value)) |
796 return false; | 851 return false; |
797 | 852 |
798 return value->GetAsDouble(out_value); | 853 return value->GetAsDouble(out_value); |
799 } | 854 } |
800 | 855 |
801 bool ListValue::GetString(size_t index, std::string* out_value) const { | 856 bool ListValue::GetString(size_t index, std::string* out_value) const { |
802 Value* value; | 857 Value* value; |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
945 if (lhs_it != end() || rhs_it != other_list->end()) | 1000 if (lhs_it != end() || rhs_it != other_list->end()) |
946 return false; | 1001 return false; |
947 | 1002 |
948 return true; | 1003 return true; |
949 } | 1004 } |
950 | 1005 |
951 ValueSerializer::~ValueSerializer() { | 1006 ValueSerializer::~ValueSerializer() { |
952 } | 1007 } |
953 | 1008 |
954 } // namespace base | 1009 } // namespace base |
OLD | NEW |