diff options
author | viettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-03 15:42:58 +0000 |
---|---|---|
committer | viettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-03 15:42:58 +0000 |
commit | e2e593d0f2786242b85ca4b8cb000f9f875b2db3 (patch) | |
tree | f47ce111779fb7bd11ceab480912b56232d171ff /base | |
parent | 5713f3265b87f12432a31c4a90e59b5f3f2f8f7e (diff) | |
download | chromium_src-e2e593d0f2786242b85ca4b8cb000f9f875b2db3.zip chromium_src-e2e593d0f2786242b85ca4b8cb000f9f875b2db3.tar.gz chromium_src-e2e593d0f2786242b85ca4b8cb000f9f875b2db3.tar.bz2 |
Remove Value/StringValue's ...UTF16() methods in favour of overloading.
Still to do: do the same for DictionaryValue.
BUG=none
TEST=builds
Review URL: http://codereview.chromium.org/3023037
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@54751 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/values.cc | 52 | ||||
-rw-r--r-- | base/values.h | 26 | ||||
-rw-r--r-- | base/values_unittest.cc | 93 |
3 files changed, 96 insertions, 75 deletions
diff --git a/base/values.cc b/base/values.cc index 3688c35..031e1ca 100644 --- a/base/values.cc +++ b/base/values.cc @@ -90,43 +90,49 @@ Value* Value::CreateStringValue(const std::string& in_value) { } // static -Value* Value::CreateStringValue(const std::wstring& in_value) { +Value* Value::CreateStringValue(const string16& in_value) { return new StringValue(in_value); } +#if !defined(WCHAR_T_IS_UTF16) +// TODO(viettrungluu): Deprecated and to be removed: // static -Value* Value::CreateStringValueFromUTF16(const string16& in_value) { +Value* Value::CreateStringValue(const std::wstring& in_value) { return new StringValue(in_value); } +#endif // static BinaryValue* Value::CreateBinaryValue(char* buffer, size_t size) { return BinaryValue::Create(buffer, size); } -bool Value::GetAsBoolean(bool* in_value) const { +bool Value::GetAsBoolean(bool* out_value) const { return false; } -bool Value::GetAsInteger(int* in_value) const { +bool Value::GetAsInteger(int* out_value) const { return false; } -bool Value::GetAsReal(double* in_value) const { +bool Value::GetAsReal(double* out_value) const { return false; } -bool Value::GetAsString(std::string* in_value) const { +bool Value::GetAsString(std::string* out_value) const { return false; } -bool Value::GetAsString(std::wstring* in_value) const { +bool Value::GetAsString(string16* out_value) const { return false; } -bool Value::GetAsUTF16(string16* out_value) const { +#if !defined(WCHAR_T_IS_UTF16) +// TODO(viettrungluu): Deprecated and to be removed: +bool Value::GetAsString(std::wstring* out_value) const { return false; } +#endif Value* Value::DeepCopy() const { // This method should only be getting called for null Values--all subclasses @@ -228,15 +234,16 @@ StringValue::StringValue(const std::string& in_value) DCHECK(IsStringUTF8(in_value)); } -StringValue::StringValue(const std::wstring& in_value) +StringValue::StringValue(const string16& in_value) : Value(TYPE_STRING), - value_(WideToUTF8(in_value)) { + value_(UTF16ToUTF8(in_value)) { } #if !defined(WCHAR_T_IS_UTF16) -StringValue::StringValue(const string16& in_value) +// TODO(viettrungluu): Deprecated and to be removed: +StringValue::StringValue(const std::wstring& in_value) : Value(TYPE_STRING), - value_(UTF16ToUTF8(in_value)) { + value_(WideToUTF8(in_value)) { } #endif @@ -249,17 +256,20 @@ bool StringValue::GetAsString(std::string* out_value) const { return true; } -bool StringValue::GetAsString(std::wstring* out_value) const { +bool StringValue::GetAsString(string16* out_value) const { if (out_value) - *out_value = UTF8ToWide(value_); + *out_value = UTF8ToUTF16(value_); return true; } -bool StringValue::GetAsUTF16(string16* out_value) const { +#if !defined(WCHAR_T_IS_UTF16) +// TODO(viettrungluu): Deprecated and to be removed: +bool StringValue::GetAsString(std::wstring* out_value) const { if (out_value) - *out_value = UTF8ToUTF16(value_); + *out_value = UTF8ToWide(value_); return true; } +#endif Value* StringValue::DeepCopy() const { return CreateStringValue(value_); @@ -440,7 +450,7 @@ void DictionaryValue::SetString(const std::string& path, void DictionaryValue::SetStringFromUTF16(const std::string& path, const string16& in_value) { - Set(path, CreateStringValueFromUTF16(in_value)); + Set(path, CreateStringValue(in_value)); } // TODO(viettrungluu): Deprecated and to be removed: @@ -473,7 +483,7 @@ void DictionaryValue::SetString(const std::wstring& path, // TODO(viettrungluu): Deprecated and to be removed: void DictionaryValue::SetStringFromUTF16(const std::wstring& path, const string16& in_value) { - Set(path, CreateStringValueFromUTF16(in_value)); + Set(path, CreateStringValue(in_value)); } void DictionaryValue::SetWithoutPathExpansion(const std::string& key, @@ -559,7 +569,7 @@ bool DictionaryValue::GetStringAsUTF16(const std::string& path, if (!Get(path, &value)) return false; - return value->GetAsUTF16(out_value); + return value->GetAsString(out_value); } bool DictionaryValue::GetStringASCII(const std::string& path, @@ -718,7 +728,7 @@ bool DictionaryValue::GetStringAsUTF16WithoutPathExpansion( if (!GetWithoutPathExpansion(key, &value)) return false; - return value->GetAsUTF16(out_value); + return value->GetAsString(out_value); } bool DictionaryValue::GetDictionaryWithoutPathExpansion( @@ -951,7 +961,7 @@ bool ListValue::GetStringAsUTF16(size_t index, string16* out_value) const { if (!Get(index, &value)) return false; - return value->GetAsUTF16(out_value); + return value->GetAsString(out_value); } bool ListValue::GetBinary(size_t index, BinaryValue** out_value) const { diff --git a/base/values.h b/base/values.h index 2fc2044..d99a46f 100644 --- a/base/values.h +++ b/base/values.h @@ -56,8 +56,10 @@ class Value { static Value* CreateIntegerValue(int in_value); static Value* CreateRealValue(double in_value); static Value* CreateStringValue(const std::string& in_value); - static Value* CreateStringValue(const std::wstring& in_value); - static Value* CreateStringValueFromUTF16(const string16& in_value); + static Value* CreateStringValue(const string16& in_value); +#if !defined(WCHAR_T_IS_UTF16) + /*DEPRECATED*/static Value* CreateStringValue(const std::wstring& in_value); +#endif // This one can return NULL if the input isn't valid. If the return value // is non-null, the new object has taken ownership of the buffer pointer. @@ -92,8 +94,10 @@ class Value { virtual bool GetAsInteger(int* out_value) const; virtual bool GetAsReal(double* out_value) const; virtual bool GetAsString(std::string* out_value) const; - virtual bool GetAsString(std::wstring* out_value) const; - virtual bool GetAsUTF16(string16* out_value) const; + virtual bool GetAsString(string16* out_value) const; +#if !defined(WCHAR_T_IS_UTF16) + /*DEPRECATED*/virtual bool GetAsString(std::wstring* out_value) const; +#endif // This creates a deep copy of the entire Value tree, and returns a pointer // to the copy. The caller gets ownership of the copy, of course. @@ -145,20 +149,22 @@ class StringValue : public Value { // Initializes a StringValue with a UTF-8 narrow character string. explicit StringValue(const std::string& in_value); - // Initializes a StringValue with a wide character string. - explicit StringValue(const std::wstring& in_value); - -#if !defined(WCHAR_T_IS_UTF16) // Initializes a StringValue with a string16. explicit StringValue(const string16& in_value); + +#if !defined(WCHAR_T_IS_UTF16) + // Initializes a StringValue with a wide character string. + /*DEPRECATED*/explicit StringValue(const std::wstring& in_value); #endif ~StringValue(); // Subclassed methods bool GetAsString(std::string* out_value) const; - bool GetAsString(std::wstring* out_value) const; - bool GetAsUTF16(string16* out_value) const; + bool GetAsString(string16* out_value) const; +#if !defined(WCHAR_T_IS_UTF16) + /*DEPRECATED*/bool GetAsString(std::wstring* out_value) const; +#endif Value* DeepCopy() const; virtual bool Equals(const Value* other) const; diff --git a/base/values_unittest.cc b/base/values_unittest.cc index f0b9d9e..fb414c1 100644 --- a/base/values_unittest.cc +++ b/base/values_unittest.cc @@ -18,7 +18,8 @@ class ValuesTest: public testing::Test { // to std::string. I've temporarily kept the old methods taking std::wstring for // compatibility. The ...Deprecated tests are the old tests which use these // methods, and remain to test compatibility. They will be removed once the old -// methods are removed. +// methods are removed. There are also parts of tests marked DEPRECATED which +// are to be deleted. TEST(ValuesTest, Basic) { // Test basic dictionary getting/setting @@ -182,11 +183,36 @@ TEST(ValuesTest, StringValue) { scoped_ptr<Value> narrow_value(Value::CreateStringValue("narrow")); ASSERT_TRUE(narrow_value.get()); ASSERT_TRUE(narrow_value->IsType(Value::TYPE_STRING)); + scoped_ptr<Value> utf16_value( + Value::CreateStringValue(ASCIIToUTF16("utf16"))); + ASSERT_TRUE(utf16_value.get()); + ASSERT_TRUE(utf16_value->IsType(Value::TYPE_STRING)); + + // Test overloaded GetString. + std::string narrow = "http://google.com"; + string16 utf16 = ASCIIToUTF16("http://google.com"); + ASSERT_TRUE(narrow_value->GetAsString(&narrow)); + ASSERT_TRUE(narrow_value->GetAsString(&utf16)); + ASSERT_EQ(std::string("narrow"), narrow); + ASSERT_EQ(ASCIIToUTF16("narrow"), utf16); + + ASSERT_TRUE(utf16_value->GetAsString(&narrow)); + ASSERT_TRUE(utf16_value->GetAsString(&utf16)); + ASSERT_EQ(std::string("utf16"), narrow); + ASSERT_EQ(ASCIIToUTF16("utf16"), utf16); +} + +// TODO(viettrungluu): deprecate: +TEST(ValuesTest, StringValueDeprecated) { + // Test overloaded CreateStringValue. + scoped_ptr<Value> narrow_value(Value::CreateStringValue("narrow")); + ASSERT_TRUE(narrow_value.get()); + ASSERT_TRUE(narrow_value->IsType(Value::TYPE_STRING)); scoped_ptr<Value> wide_value(Value::CreateStringValue(L"wide")); ASSERT_TRUE(wide_value.get()); ASSERT_TRUE(wide_value->IsType(Value::TYPE_STRING)); scoped_ptr<Value> utf16_value( - Value::CreateStringValueFromUTF16(ASCIIToUTF16("utf16"))); + Value::CreateStringValue(ASCIIToUTF16("utf16"))); ASSERT_TRUE(utf16_value.get()); ASSERT_TRUE(utf16_value->IsType(Value::TYPE_STRING)); @@ -196,21 +222,21 @@ TEST(ValuesTest, StringValue) { string16 utf16 = ASCIIToUTF16("http://google.com"); ASSERT_TRUE(narrow_value->GetAsString(&narrow)); ASSERT_TRUE(narrow_value->GetAsString(&wide)); - ASSERT_TRUE(narrow_value->GetAsUTF16(&utf16)); + ASSERT_TRUE(narrow_value->GetAsString(&utf16)); ASSERT_EQ(std::string("narrow"), narrow); ASSERT_EQ(std::wstring(L"narrow"), wide); ASSERT_EQ(ASCIIToUTF16("narrow"), utf16); ASSERT_TRUE(wide_value->GetAsString(&narrow)); ASSERT_TRUE(wide_value->GetAsString(&wide)); - ASSERT_TRUE(wide_value->GetAsUTF16(&utf16)); + ASSERT_TRUE(wide_value->GetAsString(&utf16)); ASSERT_EQ(std::string("wide"), narrow); ASSERT_EQ(std::wstring(L"wide"), wide); ASSERT_EQ(ASCIIToUTF16("wide"), utf16); ASSERT_TRUE(utf16_value->GetAsString(&narrow)); ASSERT_TRUE(utf16_value->GetAsString(&wide)); - ASSERT_TRUE(utf16_value->GetAsUTF16(&utf16)); + ASSERT_TRUE(utf16_value->GetAsString(&utf16)); ASSERT_EQ(std::string("utf16"), narrow); ASSERT_EQ(std::wstring(L"utf16"), wide); ASSERT_EQ(ASCIIToUTF16("utf16"), utf16); @@ -483,11 +509,8 @@ TEST(ValuesTest, DeepCopy) { original_dict.Set("real", original_real); Value* original_string = Value::CreateStringValue("hello"); original_dict.Set("string", original_string); - Value* original_wstring = Value::CreateStringValue(L"peek-a-boo"); - original_dict.Set("wstring", original_wstring); - Value* original_utf16 = - Value::CreateStringValueFromUTF16(ASCIIToUTF16("hello16")); - original_dict.Set("utf16", original_utf16); + Value* original_string16 = Value::CreateStringValue(ASCIIToUTF16("hello16")); + original_dict.Set("string16", original_string16); char* original_buffer = new char[42]; memset(original_buffer, '!', 42); @@ -545,38 +568,21 @@ TEST(ValuesTest, DeepCopy) { ASSERT_NE(copy_string, original_string); ASSERT_TRUE(copy_string->IsType(Value::TYPE_STRING)); std::string copy_string_value; - std::wstring copy_wstring_value; - string16 copy_utf16_value; + string16 copy_string16_value; ASSERT_TRUE(copy_string->GetAsString(©_string_value)); - ASSERT_TRUE(copy_string->GetAsString(©_wstring_value)); - ASSERT_TRUE(copy_string->GetAsUTF16(©_utf16_value)); + ASSERT_TRUE(copy_string->GetAsString(©_string16_value)); ASSERT_EQ(std::string("hello"), copy_string_value); - ASSERT_EQ(std::wstring(L"hello"), copy_wstring_value); - ASSERT_EQ(ASCIIToUTF16("hello"), copy_utf16_value); - - Value* copy_wstring = NULL; - ASSERT_TRUE(copy_dict->Get("wstring", ©_wstring)); - ASSERT_TRUE(copy_wstring); - ASSERT_NE(copy_wstring, original_wstring); - ASSERT_TRUE(copy_wstring->IsType(Value::TYPE_STRING)); - ASSERT_TRUE(copy_wstring->GetAsString(©_string_value)); - ASSERT_TRUE(copy_wstring->GetAsString(©_wstring_value)); - ASSERT_TRUE(copy_wstring->GetAsUTF16(©_utf16_value)); - ASSERT_EQ(std::string("peek-a-boo"), copy_string_value); - ASSERT_EQ(std::wstring(L"peek-a-boo"), copy_wstring_value); - ASSERT_EQ(ASCIIToUTF16("peek-a-boo"), copy_utf16_value); - - Value* copy_utf16 = NULL; - ASSERT_TRUE(copy_dict->Get("utf16", ©_utf16)); - ASSERT_TRUE(copy_utf16); - ASSERT_NE(copy_utf16, original_utf16); - ASSERT_TRUE(copy_utf16->IsType(Value::TYPE_STRING)); - ASSERT_TRUE(copy_utf16->GetAsString(©_string_value)); - ASSERT_TRUE(copy_utf16->GetAsString(©_wstring_value)); - ASSERT_TRUE(copy_utf16->GetAsUTF16(©_utf16_value)); + ASSERT_EQ(ASCIIToUTF16("hello"), copy_string16_value); + + Value* copy_string16 = NULL; + ASSERT_TRUE(copy_dict->Get("string16", ©_string16)); + ASSERT_TRUE(copy_string16); + ASSERT_NE(copy_string16, original_string16); + ASSERT_TRUE(copy_string16->IsType(Value::TYPE_STRING)); + ASSERT_TRUE(copy_string16->GetAsString(©_string_value)); + ASSERT_TRUE(copy_string16->GetAsString(©_string16_value)); ASSERT_EQ(std::string("hello16"), copy_string_value); - ASSERT_EQ(std::wstring(L"hello16"), copy_wstring_value); - ASSERT_EQ(ASCIIToUTF16("hello16"), copy_utf16_value); + ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value); Value* copy_binary = NULL; ASSERT_TRUE(copy_dict->Get("binary", ©_binary)); @@ -631,8 +637,7 @@ TEST(ValuesTest, DeepCopyDeprecated) { original_dict.Set(L"string", original_string); Value* original_wstring = Value::CreateStringValue(L"peek-a-boo"); original_dict.Set(L"wstring", original_wstring); - Value* original_utf16 = - Value::CreateStringValueFromUTF16(ASCIIToUTF16("hello16")); + Value* original_utf16 = Value::CreateStringValue(ASCIIToUTF16("hello16")); original_dict.Set(L"utf16", original_utf16); char* original_buffer = new char[42]; @@ -695,7 +700,7 @@ TEST(ValuesTest, DeepCopyDeprecated) { string16 copy_utf16_value; ASSERT_TRUE(copy_string->GetAsString(©_string_value)); ASSERT_TRUE(copy_string->GetAsString(©_wstring_value)); - ASSERT_TRUE(copy_string->GetAsUTF16(©_utf16_value)); + ASSERT_TRUE(copy_string->GetAsString(©_utf16_value)); ASSERT_EQ(std::string("hello"), copy_string_value); ASSERT_EQ(std::wstring(L"hello"), copy_wstring_value); ASSERT_EQ(ASCIIToUTF16("hello"), copy_utf16_value); @@ -707,7 +712,7 @@ TEST(ValuesTest, DeepCopyDeprecated) { ASSERT_TRUE(copy_wstring->IsType(Value::TYPE_STRING)); ASSERT_TRUE(copy_wstring->GetAsString(©_string_value)); ASSERT_TRUE(copy_wstring->GetAsString(©_wstring_value)); - ASSERT_TRUE(copy_wstring->GetAsUTF16(©_utf16_value)); + ASSERT_TRUE(copy_wstring->GetAsString(©_utf16_value)); ASSERT_EQ(std::string("peek-a-boo"), copy_string_value); ASSERT_EQ(std::wstring(L"peek-a-boo"), copy_wstring_value); ASSERT_EQ(ASCIIToUTF16("peek-a-boo"), copy_utf16_value); @@ -719,7 +724,7 @@ TEST(ValuesTest, DeepCopyDeprecated) { ASSERT_TRUE(copy_utf16->IsType(Value::TYPE_STRING)); ASSERT_TRUE(copy_utf16->GetAsString(©_string_value)); ASSERT_TRUE(copy_utf16->GetAsString(©_wstring_value)); - ASSERT_TRUE(copy_utf16->GetAsUTF16(©_utf16_value)); + ASSERT_TRUE(copy_utf16->GetAsString(©_utf16_value)); ASSERT_EQ(std::string("hello16"), copy_string_value); ASSERT_EQ(std::wstring(L"hello16"), copy_wstring_value); ASSERT_EQ(ASCIIToUTF16("hello16"), copy_utf16_value); |