Chromium Code Reviews| Index: base/values.cc |
| diff --git a/base/values.cc b/base/values.cc |
| index 3d2bb6c6e6a2b2ca3d7b7b3fd743ab1de521a10d..c34b0e5e28615b08a34d2e76d8d129bfd0c628dd 100644 |
| --- a/base/values.cc |
| +++ b/base/values.cc |
| @@ -88,6 +88,19 @@ Value::Value(double in_double) : type_(TYPE_DOUBLE), double_value_(in_double) { |
| } |
| } |
| +Value::Value(const char* in_string) : type_(TYPE_STRING) { |
| + string_value_.Init(in_string); |
| +} |
| + |
| +Value::Value(StringPiece in_string) : type_(TYPE_STRING) { |
| + string_value_.Init(in_string.as_string()); |
| + DCHECK(IsStringUTF8(in_string)); |
| +} |
| + |
| +Value::Value(const string16& in_string) : type_(TYPE_STRING) { |
| + string_value_.Init(UTF16ToUTF8(in_string)); |
| +} |
| + |
| Value::~Value() { |
| } |
| @@ -122,6 +135,16 @@ double Value::GetDouble() const { |
| return 0.0; |
| } |
| +std::string* Value::GetString() { |
| + CHECK(is_string()); |
| + return string_value_.get(); |
| +} |
| + |
| +const std::string& Value::GetString() const { |
| + CHECK(is_string()); |
| + return *string_value_; |
| +} |
| + |
| bool Value::GetAsBinary(const BinaryValue** out_value) const { |
| return false; |
| } |
| @@ -155,15 +178,27 @@ bool Value::GetAsDouble(double* out_value) const { |
| } |
| bool Value::GetAsString(std::string* out_value) const { |
| - return false; |
| + if (out_value && is_string()) { |
| + *out_value = *string_value_; |
| + return true; |
| + } |
| + return is_string(); |
| } |
| bool Value::GetAsString(string16* out_value) const { |
| - return false; |
| + if (out_value && is_string()) { |
| + *out_value = UTF8ToUTF16(*string_value_); |
| + return true; |
| + } |
| + return is_string(); |
| } |
| bool Value::GetAsString(const StringValue** out_value) const { |
| - return false; |
| + if (out_value && is_string()) { |
| + *out_value = static_cast<const StringValue*>(this); |
| + return true; |
| + } |
| + return is_string(); |
| } |
| bool Value::GetAsList(ListValue** out_value) { |
| @@ -197,6 +232,10 @@ Value* Value::DeepCopy() const { |
| return new FundamentalValue(int_value_); |
| case TYPE_DOUBLE: |
| return new FundamentalValue(double_value_); |
| + // For now, make StringValues for backward-compatibility. Convert to |
| + // Value when that code is deleted. |
| + case TYPE_STRING: |
| + return new StringValue(*string_value_); |
| default: |
| // All other types should be handled by subclasses. |
| @@ -222,6 +261,11 @@ bool Value::Equals(const Value* other) const { |
| return int_value_ == other->int_value_; |
| case TYPE_DOUBLE: |
| return double_value_ == other->double_value_; |
| + // TODO(jdoerrie): Simplify this once JSONStringValue is migrated. |
| + case TYPE_STRING: { |
| + std::string lhs, rhs; |
| + return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; |
| + } |
| default: |
| // This method should only be getting called for the above types -- all |
| // subclasses need to provide their own implementation;. |
| @@ -265,56 +309,13 @@ FundamentalValue::~FundamentalValue() { |
| ///////////////////// StringValue //////////////////// |
| -StringValue::StringValue(StringPiece in_value) |
| - : Value(TYPE_STRING), value_(in_value.as_string()) { |
| - DCHECK(IsStringUTF8(in_value)); |
| -} |
| +StringValue::StringValue(StringPiece in_value) : Value(in_value) {} |
| -StringValue::StringValue(const string16& in_value) |
| - : Value(TYPE_STRING), |
| - value_(UTF16ToUTF8(in_value)) { |
| -} |
| +StringValue::StringValue(const string16& in_value) : Value(in_value) {} |
| StringValue::~StringValue() { |
| } |
|
vabr (Chromium)
2016/12/07 10:17:27
Should this (or ~Value) run string_value_.Destroy(
jdoerrie
2016/12/08 09:59:34
Done. Implemented it as part of InternalCleanup. C
|
| -std::string* StringValue::GetString() { |
| - return &value_; |
| -} |
| - |
| -const std::string& StringValue::GetString() const { |
| - return value_; |
| -} |
| - |
| -bool StringValue::GetAsString(std::string* out_value) const { |
| - if (out_value) |
| - *out_value = value_; |
| - return true; |
| -} |
| - |
| -bool StringValue::GetAsString(string16* out_value) const { |
| - if (out_value) |
| - *out_value = UTF8ToUTF16(value_); |
| - return true; |
| -} |
| - |
| -bool StringValue::GetAsString(const StringValue** out_value) const { |
| - if (out_value) |
| - *out_value = this; |
| - return true; |
| -} |
| - |
| -StringValue* StringValue::DeepCopy() const { |
| - return new StringValue(value_); |
| -} |
| - |
| -bool StringValue::Equals(const Value* other) const { |
| - if (other->GetType() != GetType()) |
| - return false; |
| - std::string lhs, rhs; |
| - return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; |
| -} |
| - |
| ///////////////////// BinaryValue //////////////////// |
| BinaryValue::BinaryValue() |