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

Side by Side Diff: base/values.cc

Issue 13230: Added std::string to Value via Set/GetString overloading. (Closed)
Patch Set: Final upload check. Created 12 years 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
« 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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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/logging.h" 5 #include "base/logging.h"
6 #include "base/string_util.h"
6 #include "base/values.h" 7 #include "base/values.h"
7 8
8 ///////////////////// Value //////////////////// 9 ///////////////////// Value ////////////////////
9 10
10 Value::~Value() { 11 Value::~Value() {
11 } 12 }
12 13
13 // static 14 // static
14 Value* Value::CreateNullValue() { 15 Value* Value::CreateNullValue() {
15 return new Value(TYPE_NULL); 16 return new Value(TYPE_NULL);
16 } 17 }
17 18
18 // static 19 // static
19 Value* Value::CreateBooleanValue(bool in_value) { 20 Value* Value::CreateBooleanValue(bool in_value) {
20 return new FundamentalValue(in_value); 21 return new FundamentalValue(in_value);
21 } 22 }
22 23
23 // static 24 // static
24 Value* Value::CreateIntegerValue(int in_value) { 25 Value* Value::CreateIntegerValue(int in_value) {
25 return new FundamentalValue(in_value); 26 return new FundamentalValue(in_value);
26 } 27 }
27 28
28 // static 29 // static
29 Value* Value::CreateRealValue(double in_value) { 30 Value* Value::CreateRealValue(double in_value) {
30 return new FundamentalValue(in_value); 31 return new FundamentalValue(in_value);
31 } 32 }
32 33
33 // static 34 // static
35 Value* Value::CreateStringValue(const std::string& in_value) {
36 return new StringValue(in_value);
37 }
38
39 // static
34 Value* Value::CreateStringValue(const std::wstring& in_value) { 40 Value* Value::CreateStringValue(const std::wstring& in_value) {
35 return new StringValue(in_value); 41 return new StringValue(in_value);
36 } 42 }
37 43
38 // static 44 // static
39 BinaryValue* Value::CreateBinaryValue(char* buffer, size_t size) { 45 BinaryValue* Value::CreateBinaryValue(char* buffer, size_t size) {
40 return BinaryValue::Create(buffer, size); 46 return BinaryValue::Create(buffer, size);
41 } 47 }
42 48
43 bool Value::GetAsBoolean(bool* in_value) const { 49 bool Value::GetAsBoolean(bool* in_value) const {
44 return false; 50 return false;
45 } 51 }
46 52
47 bool Value::GetAsInteger(int* in_value) const { 53 bool Value::GetAsInteger(int* in_value) const {
48 return false; 54 return false;
49 } 55 }
50 56
51 bool Value::GetAsReal(double* in_value) const { 57 bool Value::GetAsReal(double* in_value) const {
52 return false; 58 return false;
53 } 59 }
54 60
61 bool Value::GetAsString(std::string* in_value) const {
62 return false;
63 }
64
55 bool Value::GetAsString(std::wstring* in_value) const { 65 bool Value::GetAsString(std::wstring* in_value) const {
56 return false; 66 return false;
57 } 67 }
58 68
59 Value* Value::DeepCopy() const { 69 Value* Value::DeepCopy() const {
60 // This method should only be getting called for null Values--all subclasses 70 // This method should only be getting called for null Values--all subclasses
61 // need to provide their own implementation;. 71 // need to provide their own implementation;.
62 DCHECK(IsType(TYPE_NULL)); 72 DCHECK(IsType(TYPE_NULL));
63 return CreateNullValue(); 73 return CreateNullValue();
64 } 74 }
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 return GetAsReal(&lhs) && other->GetAsReal(&rhs) && lhs == rhs; 138 return GetAsReal(&lhs) && other->GetAsReal(&rhs) && lhs == rhs;
129 } 139 }
130 default: 140 default:
131 NOTREACHED(); 141 NOTREACHED();
132 return false; 142 return false;
133 } 143 }
134 } 144 }
135 145
136 ///////////////////// StringValue //////////////////// 146 ///////////////////// StringValue ////////////////////
137 147
148 StringValue::StringValue(const std::string& in_value)
149 : Value(TYPE_STRING),
150 value_(in_value) {
151 DCHECK(IsStringUTF8(in_value));
152 }
153
154 StringValue::StringValue(const std::wstring& in_value)
155 : Value(TYPE_STRING),
156 value_(WideToUTF8(in_value)) {
157 }
158
138 StringValue::~StringValue() { 159 StringValue::~StringValue() {
139 } 160 }
140 161
162 bool StringValue::GetAsString(std::string* out_value) const {
163 if (out_value)
164 *out_value = value_;
165 return true;
166 }
167
141 bool StringValue::GetAsString(std::wstring* out_value) const { 168 bool StringValue::GetAsString(std::wstring* out_value) const {
142 if (out_value) 169 if (out_value)
143 *out_value = value_; 170 *out_value = UTF8ToWide(value_);
144 return true; 171 return true;
145 } 172 }
146 173
147 Value* StringValue::DeepCopy() const { 174 Value* StringValue::DeepCopy() const {
148 return CreateStringValue(value_); 175 return CreateStringValue(value_);
149 } 176 }
150 177
151 bool StringValue::Equals(const Value* other) const { 178 bool StringValue::Equals(const Value* other) const {
152 if (other->GetType() != GetType()) 179 if (other->GetType() != GetType())
153 return false; 180 return false;
154 std::wstring lhs, rhs; 181 std::string lhs, rhs;
155 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; 182 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
156 } 183 }
157 184
158 ///////////////////// BinaryValue //////////////////// 185 ///////////////////// BinaryValue ////////////////////
159 186
160 //static 187 //static
161 BinaryValue* BinaryValue::Create(char* buffer, size_t size) { 188 BinaryValue* BinaryValue::Create(char* buffer, size_t size) {
162 if (!buffer) 189 if (!buffer)
163 return NULL; 190 return NULL;
164 191
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 296
270 bool DictionaryValue::SetInteger(const std::wstring& path, int in_value) { 297 bool DictionaryValue::SetInteger(const std::wstring& path, int in_value) {
271 return Set(path, CreateIntegerValue(in_value)); 298 return Set(path, CreateIntegerValue(in_value));
272 } 299 }
273 300
274 bool DictionaryValue::SetReal(const std::wstring& path, double in_value) { 301 bool DictionaryValue::SetReal(const std::wstring& path, double in_value) {
275 return Set(path, CreateRealValue(in_value)); 302 return Set(path, CreateRealValue(in_value));
276 } 303 }
277 304
278 bool DictionaryValue::SetString(const std::wstring& path, 305 bool DictionaryValue::SetString(const std::wstring& path,
306 const std::string& in_value) {
307 return Set(path, CreateStringValue(in_value));
308 }
309
310 bool DictionaryValue::SetString(const std::wstring& path,
279 const std::wstring& in_value) { 311 const std::wstring& in_value) {
280 return Set(path, CreateStringValue(in_value)); 312 return Set(path, CreateStringValue(in_value));
281 } 313 }
282 314
283 bool DictionaryValue::Get(const std::wstring& path, Value** out_value) const { 315 bool DictionaryValue::Get(const std::wstring& path, Value** out_value) const {
284 std::wstring key = path; 316 std::wstring key = path;
285 317
286 size_t delimiter_position = path.find_first_of(L".", 0); 318 size_t delimiter_position = path.find_first_of(L".", 0);
287 if (delimiter_position != std::wstring::npos) { 319 if (delimiter_position != std::wstring::npos) {
288 key = path.substr(0, delimiter_position); 320 key = path.substr(0, delimiter_position);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 bool DictionaryValue::GetReal(const std::wstring& path, 360 bool DictionaryValue::GetReal(const std::wstring& path,
329 double* out_value) const { 361 double* out_value) const {
330 Value* value; 362 Value* value;
331 if (!Get(path, &value)) 363 if (!Get(path, &value))
332 return false; 364 return false;
333 365
334 return value->GetAsReal(out_value); 366 return value->GetAsReal(out_value);
335 } 367 }
336 368
337 bool DictionaryValue::GetString(const std::wstring& path, 369 bool DictionaryValue::GetString(const std::wstring& path,
370 std::string* out_value) const {
371 Value* value;
372 if (!Get(path, &value))
373 return false;
374
375 return value->GetAsString(out_value);
376 }
377
378 bool DictionaryValue::GetString(const std::wstring& path,
338 std::wstring* out_value) const { 379 std::wstring* out_value) const {
339 Value* value; 380 Value* value;
340 if (!Get(path, &value)) 381 if (!Get(path, &value))
341 return false; 382 return false;
342 383
343 return value->GetAsString(out_value); 384 return value->GetAsString(out_value);
344 } 385 }
345 386
346 bool DictionaryValue::GetBinary(const std::wstring& path, 387 bool DictionaryValue::GetBinary(const std::wstring& path,
347 BinaryValue** out_value) const { 388 BinaryValue** out_value) const {
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 ++lhs_it, ++rhs_it) { 590 ++lhs_it, ++rhs_it) {
550 if (!(*lhs_it)->Equals(*rhs_it)) 591 if (!(*lhs_it)->Equals(*rhs_it))
551 return false; 592 return false;
552 } 593 }
553 if (lhs_it != end() || rhs_it != other_list->end()) 594 if (lhs_it != end() || rhs_it != other_list->end())
554 return false; 595 return false;
555 596
556 return true; 597 return true;
557 } 598 }
558 599
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