diff options
author | scherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-11 01:23:17 +0000 |
---|---|---|
committer | scherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-11 01:23:17 +0000 |
commit | 4cd5f6a6319e7f6b34b72d5a1fde81b27cdaa633 (patch) | |
tree | ee5c5a7c2dddf593babe6da644ee7c3018fa9ae2 /base/values.cc | |
parent | e4fe3611d6ebf53ecb78d88c15c17887e71b5ac3 (diff) | |
download | chromium_src-4cd5f6a6319e7f6b34b72d5a1fde81b27cdaa633.zip chromium_src-4cd5f6a6319e7f6b34b72d5a1fde81b27cdaa633.tar.gz chromium_src-4cd5f6a6319e7f6b34b72d5a1fde81b27cdaa633.tar.bz2 |
Added std::string to Value via Set/GetString overloading.
All input is converted to UTF-8. Output is converted back to wide during std::wstring version of GetString().
This is part one of some more patches to come where I switch clients over to UTF-8 strings.
Review URL: http://codereview.chromium.org/13230
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6768 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/values.cc')
-rw-r--r-- | base/values.cc | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/base/values.cc b/base/values.cc index dd8062b..75b8e4f 100644 --- a/base/values.cc +++ b/base/values.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "base/logging.h" +#include "base/string_util.h" #include "base/values.h" ///////////////////// Value //////////////////// @@ -31,6 +32,11 @@ Value* Value::CreateRealValue(double in_value) { } // static +Value* Value::CreateStringValue(const std::string& in_value) { + return new StringValue(in_value); +} + +// static Value* Value::CreateStringValue(const std::wstring& in_value) { return new StringValue(in_value); } @@ -52,6 +58,10 @@ bool Value::GetAsReal(double* in_value) const { return false; } +bool Value::GetAsString(std::string* in_value) const { + return false; +} + bool Value::GetAsString(std::wstring* in_value) const { return false; } @@ -135,15 +145,32 @@ bool FundamentalValue::Equals(const Value* other) const { ///////////////////// StringValue //////////////////// +StringValue::StringValue(const std::string& in_value) + : Value(TYPE_STRING), + value_(in_value) { + DCHECK(IsStringUTF8(in_value)); +} + +StringValue::StringValue(const std::wstring& in_value) + : Value(TYPE_STRING), + value_(WideToUTF8(in_value)) { +} + StringValue::~StringValue() { } -bool StringValue::GetAsString(std::wstring* out_value) const { +bool StringValue::GetAsString(std::string* out_value) const { if (out_value) *out_value = value_; return true; } +bool StringValue::GetAsString(std::wstring* out_value) const { + if (out_value) + *out_value = UTF8ToWide(value_); + return true; +} + Value* StringValue::DeepCopy() const { return CreateStringValue(value_); } @@ -151,7 +178,7 @@ Value* StringValue::DeepCopy() const { bool StringValue::Equals(const Value* other) const { if (other->GetType() != GetType()) return false; - std::wstring lhs, rhs; + std::string lhs, rhs; return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; } @@ -276,6 +303,11 @@ bool DictionaryValue::SetReal(const std::wstring& path, double in_value) { } bool DictionaryValue::SetString(const std::wstring& path, + const std::string& in_value) { + return Set(path, CreateStringValue(in_value)); +} + +bool DictionaryValue::SetString(const std::wstring& path, const std::wstring& in_value) { return Set(path, CreateStringValue(in_value)); } @@ -335,6 +367,15 @@ bool DictionaryValue::GetReal(const std::wstring& path, } bool DictionaryValue::GetString(const std::wstring& path, + std::string* out_value) const { + Value* value; + if (!Get(path, &value)) + return false; + + return value->GetAsString(out_value); +} + +bool DictionaryValue::GetString(const std::wstring& path, std::wstring* out_value) const { Value* value; if (!Get(path, &value)) |