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

Side by Side Diff: base/values.cc

Issue 31014: Port DictionaryValue to use string16 instead of wstring. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 9 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) 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/string_util.h"
7 #include "base/values.h" 7 #include "base/values.h"
8 8
9 ///////////////////// Value //////////////////// 9 ///////////////////// Value ////////////////////
10 10
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 void DictionaryValue::Clear() { 238 void DictionaryValue::Clear() {
239 ValueMap::iterator dict_iterator = dictionary_.begin(); 239 ValueMap::iterator dict_iterator = dictionary_.begin();
240 while (dict_iterator != dictionary_.end()) { 240 while (dict_iterator != dictionary_.end()) {
241 delete dict_iterator->second; 241 delete dict_iterator->second;
242 ++dict_iterator; 242 ++dict_iterator;
243 } 243 }
244 244
245 dictionary_.clear(); 245 dictionary_.clear();
246 } 246 }
247 247
248 bool DictionaryValue::HasKey(const std::wstring& key) const { 248 bool DictionaryValue::HasKey(const string16& key) const {
249 ValueMap::const_iterator current_entry = dictionary_.find(key); 249 ValueMap::const_iterator current_entry = dictionary_.find(key);
250 DCHECK((current_entry == dictionary_.end()) || current_entry->second); 250 DCHECK((current_entry == dictionary_.end()) || current_entry->second);
251 return current_entry != dictionary_.end(); 251 return current_entry != dictionary_.end();
252 } 252 }
253 253
254 void DictionaryValue::SetInCurrentNode(const std::wstring& key, 254 void DictionaryValue::SetInCurrentNode(const string16& key,
255 Value* in_value) { 255 Value* in_value) {
256 // If there's an existing value here, we need to delete it, because 256 // If there's an existing value here, we need to delete it, because
257 // we own all our children. 257 // we own all our children.
258 if (HasKey(key)) { 258 if (HasKey(key)) {
259 DCHECK(dictionary_[key] != in_value); // This would be bogus 259 DCHECK(dictionary_[key] != in_value); // This would be bogus
260 delete dictionary_[key]; 260 delete dictionary_[key];
261 } 261 }
262 262
263 dictionary_[key] = in_value; 263 dictionary_[key] = in_value;
264 } 264 }
265 265
266 bool DictionaryValue::Set(const std::wstring& path, Value* in_value) { 266 bool DictionaryValue::Set(const string16& path, Value* in_value) {
267 DCHECK(in_value); 267 DCHECK(in_value);
268 268
269 std::wstring key = path; 269 string16 key = path;
270 270
271 size_t delimiter_position = path.find_first_of(L".", 0); 271 size_t delimiter_position = path.find_first_of('.', 0);
272 // If there isn't a dictionary delimiter in the path, we're done. 272 // If there isn't a dictionary delimiter in the path, we're done.
273 if (delimiter_position == std::wstring::npos) { 273 if (delimiter_position == std::wstring::npos) {
274 SetInCurrentNode(key, in_value); 274 SetInCurrentNode(key, in_value);
275 return true; 275 return true;
276 } else { 276 } else {
277 key = path.substr(0, delimiter_position); 277 key = path.substr(0, delimiter_position);
278 } 278 }
279 279
280 // Assume that we're indexing into a dictionary. 280 // Assume that we're indexing into a dictionary.
281 DictionaryValue* entry = NULL; 281 DictionaryValue* entry = NULL;
282 if (!HasKey(key) || (dictionary_[key]->GetType() != TYPE_DICTIONARY)) { 282 if (!HasKey(key) || (dictionary_[key]->GetType() != TYPE_DICTIONARY)) {
283 entry = new DictionaryValue; 283 entry = new DictionaryValue;
284 SetInCurrentNode(key, entry); 284 SetInCurrentNode(key, entry);
285 } else { 285 } else {
286 entry = static_cast<DictionaryValue*>(dictionary_[key]); 286 entry = static_cast<DictionaryValue*>(dictionary_[key]);
287 } 287 }
288 288
289 std::wstring remaining_path = path.substr(delimiter_position + 1); 289 string16 remaining_path = path.substr(delimiter_position + 1);
290 return entry->Set(remaining_path, in_value); 290 return entry->Set(remaining_path, in_value);
291 } 291 }
292 292
293 bool DictionaryValue::SetBoolean(const std::wstring& path, bool in_value) { 293 bool DictionaryValue::SetBoolean(const string16& path, bool in_value) {
294 return Set(path, CreateBooleanValue(in_value)); 294 return Set(path, CreateBooleanValue(in_value));
295 } 295 }
296 296
297 bool DictionaryValue::SetInteger(const std::wstring& path, int in_value) { 297 bool DictionaryValue::SetInteger(const string16& path, int in_value) {
298 return Set(path, CreateIntegerValue(in_value)); 298 return Set(path, CreateIntegerValue(in_value));
299 } 299 }
300 300
301 bool DictionaryValue::SetReal(const std::wstring& path, double in_value) { 301 bool DictionaryValue::SetReal(const string16& path, double in_value) {
302 return Set(path, CreateRealValue(in_value)); 302 return Set(path, CreateRealValue(in_value));
303 } 303 }
304 304
305 bool DictionaryValue::SetString(const std::wstring& path, 305 bool DictionaryValue::SetString(const string16& path,
306 const std::string& in_value) { 306 const std::string& in_value) {
307 return Set(path, CreateStringValue(in_value)); 307 return Set(path, CreateStringValue(in_value));
308 } 308 }
309 309
310 bool DictionaryValue::SetString(const std::wstring& path, 310 bool DictionaryValue::SetString(const string16& path,
311 const std::wstring& in_value) { 311 const string16& in_value) {
312 return Set(path, CreateStringValue(in_value)); 312 return Set(path, CreateStringValue(UTF16ToWideHack(in_value)));
313 } 313 }
314 314
315 bool DictionaryValue::Get(const std::wstring& path, Value** out_value) const { 315 bool DictionaryValue::Get(const string16& path, Value** out_value) const {
316 std::wstring key = path; 316 string16 key = path;
317 317
318 size_t delimiter_position = path.find_first_of(L".", 0); 318 size_t delimiter_position = path.find_first_of('.', 0);
319 if (delimiter_position != std::wstring::npos) { 319 if (delimiter_position != string16::npos) {
320 key = path.substr(0, delimiter_position); 320 key = path.substr(0, delimiter_position);
321 } 321 }
322 322
323 ValueMap::const_iterator entry_iterator = dictionary_.find(key); 323 ValueMap::const_iterator entry_iterator = dictionary_.find(key);
324 if (entry_iterator == dictionary_.end()) 324 if (entry_iterator == dictionary_.end())
325 return false; 325 return false;
326 Value* entry = entry_iterator->second; 326 Value* entry = entry_iterator->second;
327 327
328 if (delimiter_position == std::wstring::npos) { 328 if (delimiter_position == string16::npos) {
329 if (out_value) 329 if (out_value)
330 *out_value = entry; 330 *out_value = entry;
331 return true; 331 return true;
332 } 332 }
333 333
334 if (entry->IsType(TYPE_DICTIONARY)) { 334 if (entry->IsType(TYPE_DICTIONARY)) {
335 DictionaryValue* dictionary = static_cast<DictionaryValue*>(entry); 335 DictionaryValue* dictionary = static_cast<DictionaryValue*>(entry);
336 return dictionary->Get(path.substr(delimiter_position + 1), out_value); 336 return dictionary->Get(path.substr(delimiter_position + 1), out_value);
337 } 337 }
338 338
339 return false; 339 return false;
340 } 340 }
341 341
342 bool DictionaryValue::GetBoolean(const std::wstring& path, 342 bool DictionaryValue::GetBoolean(const string16& path,
343 bool* bool_value) const { 343 bool* bool_value) const {
344 Value* value; 344 Value* value;
345 if (!Get(path, &value)) 345 if (!Get(path, &value))
346 return false; 346 return false;
347 347
348 return value->GetAsBoolean(bool_value); 348 return value->GetAsBoolean(bool_value);
349 } 349 }
350 350
351 bool DictionaryValue::GetInteger(const std::wstring& path, 351 bool DictionaryValue::GetInteger(const string16& path,
352 int* out_value) const { 352 int* out_value) const {
353 Value* value; 353 Value* value;
354 if (!Get(path, &value)) 354 if (!Get(path, &value))
355 return false; 355 return false;
356 356
357 return value->GetAsInteger(out_value); 357 return value->GetAsInteger(out_value);
358 } 358 }
359 359
360 bool DictionaryValue::GetReal(const std::wstring& path, 360 bool DictionaryValue::GetReal(const string16& path,
361 double* out_value) const { 361 double* out_value) const {
362 Value* value; 362 Value* value;
363 if (!Get(path, &value)) 363 if (!Get(path, &value))
364 return false; 364 return false;
365 365
366 return value->GetAsReal(out_value); 366 return value->GetAsReal(out_value);
367 } 367 }
368 368
369 bool DictionaryValue::GetString(const std::wstring& path, 369 bool DictionaryValue::GetString(const string16& path,
370 std::string* out_value) const { 370 std::string* out_value) const {
371 Value* value; 371 Value* value;
372 if (!Get(path, &value)) 372 if (!Get(path, &value))
373 return false; 373 return false;
374 374
375 return value->GetAsString(out_value); 375 return value->GetAsString(out_value);
376 } 376 }
377 377
378 bool DictionaryValue::GetString(const std::wstring& path, 378 bool DictionaryValue::GetString(const string16& path,
379 std::wstring* out_value) const { 379 string16* out_value) const {
380 Value* value; 380 Value* value;
381 if (!Get(path, &value)) 381 if (!Get(path, &value))
382 return false; 382 return false;
383 383
384 return value->GetAsString(out_value); 384 std::wstring wout_value;
385 bool success = value->GetAsString(&wout_value);
386 out_value->assign(WideToUTF16Hack(wout_value));
387 return success;
385 } 388 }
386 389
387 bool DictionaryValue::GetBinary(const std::wstring& path, 390 bool DictionaryValue::GetBinary(const string16& path,
388 BinaryValue** out_value) const { 391 BinaryValue** out_value) const {
389 Value* value; 392 Value* value;
390 bool result = Get(path, &value); 393 bool result = Get(path, &value);
391 if (!result || !value->IsType(TYPE_BINARY)) 394 if (!result || !value->IsType(TYPE_BINARY))
392 return false; 395 return false;
393 396
394 if (out_value) 397 if (out_value)
395 *out_value = static_cast<BinaryValue*>(value); 398 *out_value = static_cast<BinaryValue*>(value);
396 399
397 return true; 400 return true;
398 } 401 }
399 402
400 bool DictionaryValue::GetDictionary(const std::wstring& path, 403 bool DictionaryValue::GetDictionary(const string16& path,
401 DictionaryValue** out_value) const { 404 DictionaryValue** out_value) const {
402 Value* value; 405 Value* value;
403 bool result = Get(path, &value); 406 bool result = Get(path, &value);
404 if (!result || !value->IsType(TYPE_DICTIONARY)) 407 if (!result || !value->IsType(TYPE_DICTIONARY))
405 return false; 408 return false;
406 409
407 if (out_value) 410 if (out_value)
408 *out_value = static_cast<DictionaryValue*>(value); 411 *out_value = static_cast<DictionaryValue*>(value);
409 412
410 return true; 413 return true;
411 } 414 }
412 415
413 bool DictionaryValue::GetList(const std::wstring& path, 416 bool DictionaryValue::GetList(const string16& path,
414 ListValue** out_value) const { 417 ListValue** out_value) const {
415 Value* value; 418 Value* value;
416 bool result = Get(path, &value); 419 bool result = Get(path, &value);
417 if (!result || !value->IsType(TYPE_LIST)) 420 if (!result || !value->IsType(TYPE_LIST))
418 return false; 421 return false;
419 422
420 if (out_value) 423 if (out_value)
421 *out_value = static_cast<ListValue*>(value); 424 *out_value = static_cast<ListValue*>(value);
422 425
423 return true; 426 return true;
424 } 427 }
425 428
426 bool DictionaryValue::Remove(const std::wstring& path, Value** out_value) { 429 bool DictionaryValue::Remove(const string16& path, Value** out_value) {
427 std::wstring key = path; 430 string16 key = path;
428 431
429 size_t delimiter_position = path.find_first_of(L".", 0); 432 size_t delimiter_position = path.find_first_of('.', 0);
430 if (delimiter_position != std::wstring::npos) { 433 if (delimiter_position != string16::npos) {
431 key = path.substr(0, delimiter_position); 434 key = path.substr(0, delimiter_position);
432 } 435 }
433 436
434 ValueMap::iterator entry_iterator = dictionary_.find(key); 437 ValueMap::iterator entry_iterator = dictionary_.find(key);
435 if (entry_iterator == dictionary_.end()) 438 if (entry_iterator == dictionary_.end())
436 return false; 439 return false;
437 Value* entry = entry_iterator->second; 440 Value* entry = entry_iterator->second;
438 441
439 if (delimiter_position == std::wstring::npos) { 442 if (delimiter_position == string16::npos) {
440 if (out_value) 443 if (out_value)
441 *out_value = entry; 444 *out_value = entry;
442 else 445 else
443 delete entry; 446 delete entry;
444 447
445 dictionary_.erase(entry_iterator); 448 dictionary_.erase(entry_iterator);
446 return true; 449 return true;
447 } 450 }
448 451
449 if (entry->IsType(TYPE_DICTIONARY)) { 452 if (entry->IsType(TYPE_DICTIONARY)) {
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
644 lhs_it != end() && rhs_it != other_list->end(); 647 lhs_it != end() && rhs_it != other_list->end();
645 ++lhs_it, ++rhs_it) { 648 ++lhs_it, ++rhs_it) {
646 if (!(*lhs_it)->Equals(*rhs_it)) 649 if (!(*lhs_it)->Equals(*rhs_it))
647 return false; 650 return false;
648 } 651 }
649 if (lhs_it != end() || rhs_it != other_list->end()) 652 if (lhs_it != end() || rhs_it != other_list->end())
650 return false; 653 return false;
651 654
652 return true; 655 return true;
653 } 656 }
654
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