Index: base/values.cc |
diff --git a/base/values.cc b/base/values.cc |
index 7a364bd8ff0f14402dac1ff80b5f6b72b656a9d2..9ce5fd165ed9f6e1da32269ac4819ea67a295107 100644 |
--- a/base/values.cc |
+++ b/base/values.cc |
@@ -57,43 +57,32 @@ Value* CopyWithoutEmptyChildren(Value* node) { |
} |
} |
-} // namespace |
- |
-namespace base { |
+class BASE_EXPORT NullValue : public Value { |
+ public: |
+ static Value* New(); |
-///////////////////// Value //////////////////// |
+ private: |
+ NullValue(); |
-Value::~Value() { |
-} |
+ DISALLOW_COPY_AND_ASSIGN(NullValue); |
+}; |
// static |
-Value* Value::CreateNullValue() { |
- return new Value(TYPE_NULL); |
+Value* NullValue::New() { |
+ return new NullValue(); |
} |
-// static |
-FundamentalValue* Value::CreateBooleanValue(bool in_value) { |
- return new FundamentalValue(in_value); |
+NullValue::NullValue() |
+ : Value(TYPE_NULL) { |
} |
-// static |
-FundamentalValue* Value::CreateIntegerValue(int in_value) { |
- return new FundamentalValue(in_value); |
-} |
+} // namespace |
-// static |
-FundamentalValue* Value::CreateDoubleValue(double in_value) { |
- return new FundamentalValue(in_value); |
-} |
+namespace base { |
-// static |
-StringValue* Value::CreateStringValue(const std::string& in_value) { |
- return new StringValue(in_value); |
-} |
+// Value ----------------------------------------------------------------------- |
-// static |
-StringValue* Value::CreateStringValue(const string16& in_value) { |
- return new StringValue(in_value); |
+Value::~Value() { |
} |
bool Value::GetAsBoolean(bool* out_value) const { |
@@ -128,7 +117,7 @@ Value* Value::DeepCopy() const { |
// This method should only be getting called for null Values--all subclasses |
// need to provide their own implementation;. |
DCHECK(IsType(TYPE_NULL)); |
- return CreateNullValue(); |
+ return NullValue(); |
} |
bool Value::Equals(const Value* other) const { |
@@ -148,84 +137,110 @@ bool Value::Equals(const Value* a, const Value* b) { |
Value::Value(Type type) : type_(type) { |
} |
-///////////////////// FundamentalValue //////////////////// |
+// NullValue ------------------------------------------------------------------- |
-FundamentalValue::FundamentalValue(bool in_value) |
- : Value(TYPE_BOOLEAN), boolean_value_(in_value) { |
+Value* NullValue() { |
+ return NullValue::New(); |
} |
-FundamentalValue::FundamentalValue(int in_value) |
- : Value(TYPE_INTEGER), integer_value_(in_value) { |
+BooleanValue* TrueValue() { |
+ return BooleanValue::New(true); |
} |
-FundamentalValue::FundamentalValue(double in_value) |
- : Value(TYPE_DOUBLE), double_value_(in_value) { |
+BooleanValue* FalseValue() { |
+ return BooleanValue::New(false); |
} |
-FundamentalValue::~FundamentalValue() { |
+// BooleanValue ---------------------------------------------------------------- |
+ |
+BooleanValue::BooleanValue(bool value) |
+ : Value(TYPE_BOOLEAN), value_(value) { |
} |
-bool FundamentalValue::GetAsBoolean(bool* out_value) const { |
- if (out_value && IsType(TYPE_BOOLEAN)) |
- *out_value = boolean_value_; |
- return (IsType(TYPE_BOOLEAN)); |
+BooleanValue::~BooleanValue() { |
} |
-bool FundamentalValue::GetAsInteger(int* out_value) const { |
+// static |
+BooleanValue* BooleanValue::New(bool value) { |
+ return new BooleanValue(value); |
+} |
+ |
+bool BooleanValue::GetAsBoolean(bool* out_value) const { |
+ if (out_value) |
+ *out_value = value_; |
+ return true; |
+} |
+ |
+BooleanValue* BooleanValue::DeepCopy() const { |
+ return new BooleanValue(value_); |
+} |
+ |
+bool BooleanValue::Equals(const Value* other) const { |
+ if (other->GetType() != GetType()) |
+ return false; |
+ |
+ bool lhs, rhs; |
+ return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs; |
+} |
+ |
+// NumberValue ----------------------------------------------------------------- |
+ |
+NumberValue::NumberValue(int value) |
+ : Value(TYPE_INTEGER), integer_value_(value) { |
+} |
+ |
+NumberValue::NumberValue(double value) |
+ : Value(TYPE_DOUBLE), double_value_(value) { |
+} |
+ |
+NumberValue::~NumberValue() { |
+} |
+ |
+// static |
+NumberValue* NumberValue::New(int value) { |
+ return new NumberValue(value); |
+} |
+ |
+// static |
+NumberValue* NumberValue::New(double value) { |
+ return new NumberValue(value); |
+} |
+ |
+bool NumberValue::GetAsInteger(int* out_value) const { |
if (out_value && IsType(TYPE_INTEGER)) |
*out_value = integer_value_; |
- return (IsType(TYPE_INTEGER)); |
+ return IsType(TYPE_INTEGER); |
} |
-bool FundamentalValue::GetAsDouble(double* out_value) const { |
+bool NumberValue::GetAsDouble(double* out_value) const { |
if (out_value && IsType(TYPE_DOUBLE)) |
*out_value = double_value_; |
else if (out_value && IsType(TYPE_INTEGER)) |
*out_value = integer_value_; |
- return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER)); |
+ return IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER); |
} |
-FundamentalValue* FundamentalValue::DeepCopy() const { |
- switch (GetType()) { |
- case TYPE_BOOLEAN: |
- return CreateBooleanValue(boolean_value_); |
- |
- case TYPE_INTEGER: |
- return CreateIntegerValue(integer_value_); |
- |
- case TYPE_DOUBLE: |
- return CreateDoubleValue(double_value_); |
- |
- default: |
- NOTREACHED(); |
- return NULL; |
- } |
+NumberValue* NumberValue::DeepCopy() const { |
+ if (IsType(TYPE_INTEGER)) |
+ return NumberValue::New(integer_value_); |
+ else |
+ return NumberValue::New(double_value_); |
} |
-bool FundamentalValue::Equals(const Value* other) const { |
+bool NumberValue::Equals(const Value* other) const { |
if (other->GetType() != GetType()) |
return false; |
- switch (GetType()) { |
- case TYPE_BOOLEAN: { |
- bool lhs, rhs; |
- return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs; |
- } |
- case TYPE_INTEGER: { |
- int lhs, rhs; |
- return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs; |
- } |
- case TYPE_DOUBLE: { |
- double lhs, rhs; |
- return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs; |
- } |
- default: |
- NOTREACHED(); |
- return false; |
+ if (IsType(TYPE_INTEGER)) { |
+ int lhs, rhs; |
+ return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs; |
+ } else { |
+ double lhs, rhs; |
+ return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs; |
} |
} |
-///////////////////// StringValue //////////////////// |
+// StringValue ----------------------------------------------------------------- |
StringValue::StringValue(const std::string& in_value) |
: Value(TYPE_STRING), |
@@ -241,6 +256,16 @@ StringValue::StringValue(const string16& in_value) |
StringValue::~StringValue() { |
} |
+// static |
+StringValue* StringValue::New(const std::string& value) { |
+ return new StringValue(value); |
+} |
+ |
+// static |
+StringValue* StringValue::New(const string16& value) { |
+ return new StringValue(value); |
+} |
+ |
bool StringValue::GetAsString(std::string* out_value) const { |
if (out_value) |
*out_value = value_; |
@@ -254,7 +279,7 @@ bool StringValue::GetAsString(string16* out_value) const { |
} |
StringValue* StringValue::DeepCopy() const { |
- return CreateStringValue(value_); |
+ return StringValue::New(value_); |
} |
bool StringValue::Equals(const Value* other) const { |
@@ -264,7 +289,7 @@ bool StringValue::Equals(const Value* other) const { |
return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; |
} |
-///////////////////// BinaryValue //////////////////// |
+// BinaryValue ----------------------------------------------------------------- |
BinaryValue::~BinaryValue() { |
DCHECK(buffer_); |
@@ -311,7 +336,7 @@ BinaryValue::BinaryValue(char* buffer, size_t size) |
DCHECK(buffer_); |
} |
-///////////////////// DictionaryValue //////////////////// |
+// DictionaryValue ------------------------------------------------------------- |
DictionaryValue::DictionaryValue() |
: Value(TYPE_DICTIONARY) { |
@@ -363,25 +388,25 @@ void DictionaryValue::Set(const std::string& path, Value* in_value) { |
} |
void DictionaryValue::SetBoolean(const std::string& path, bool in_value) { |
- Set(path, CreateBooleanValue(in_value)); |
+ Set(path, BooleanValue::New(in_value)); |
} |
void DictionaryValue::SetInteger(const std::string& path, int in_value) { |
- Set(path, CreateIntegerValue(in_value)); |
+ Set(path, NumberValue::New(in_value)); |
} |
void DictionaryValue::SetDouble(const std::string& path, double in_value) { |
- Set(path, CreateDoubleValue(in_value)); |
+ Set(path, NumberValue::New(in_value)); |
} |
void DictionaryValue::SetString(const std::string& path, |
const std::string& in_value) { |
- Set(path, CreateStringValue(in_value)); |
+ Set(path, StringValue::New(in_value)); |
} |
void DictionaryValue::SetString(const std::string& path, |
const string16& in_value) { |
- Set(path, CreateStringValue(in_value)); |
+ Set(path, StringValue::New(in_value)); |
} |
void DictionaryValue::SetWithoutPathExpansion(const std::string& key, |
@@ -687,7 +712,7 @@ bool DictionaryValue::Equals(const Value* other) const { |
return true; |
} |
-///////////////////// ListValue //////////////////// |
+// ListValue ------------------------------------------------------------------- |
ListValue::ListValue() : Value(TYPE_LIST) { |
} |
@@ -709,7 +734,7 @@ bool ListValue::Set(size_t index, Value* in_value) { |
if (index >= list_.size()) { |
// Pad out any intermediate indexes with null settings |
while (index > list_.size()) |
- Append(CreateNullValue()); |
+ Append(NullValue()); |
Append(in_value); |
} else { |
DCHECK(list_[index] != in_value); |
@@ -899,6 +924,8 @@ bool ListValue::Equals(const Value* other) const { |
return true; |
} |
+// ValueSerializer ------------------------------------------------------------- |
+ |
ValueSerializer::~ValueSerializer() { |
} |