diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-20 21:05:32 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-20 21:05:32 +0000 |
commit | f82fb495a3ccffc3d2a9fbb215a2e1723acee954 (patch) | |
tree | 99c7d26f6fdbeef40235751cb9344c537e83df49 | |
parent | d1afe0ea2e4df1543bc3f67d2f191b6c032ce119 (diff) | |
download | chromium_src-f82fb495a3ccffc3d2a9fbb215a2e1723acee954.zip chromium_src-f82fb495a3ccffc3d2a9fbb215a2e1723acee954.tar.gz chromium_src-f82fb495a3ccffc3d2a9fbb215a2e1723acee954.tar.bz2 |
Flesh out ListValue class.
Review URL: http://codereview.chromium.org/18200
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8315 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/values.cc | 61 | ||||
-rw-r--r-- | base/values.h | 9 | ||||
-rw-r--r-- | base/values_unittest.cc | 36 |
3 files changed, 101 insertions, 5 deletions
diff --git a/base/values.cc b/base/values.cc index 75b8e4f..cf98c0b 100644 --- a/base/values.cc +++ b/base/values.cc @@ -245,7 +245,7 @@ void DictionaryValue::Clear() { dictionary_.clear(); } -bool DictionaryValue::HasKey(const std::wstring& key) { +bool DictionaryValue::HasKey(const std::wstring& key) const { ValueMap::const_iterator current_entry = dictionary_.find(key); DCHECK((current_entry == dictionary_.end()) || current_entry->second); return current_entry != dictionary_.end(); @@ -532,8 +532,51 @@ bool ListValue::Get(size_t index, Value** out_value) const { return true; } -bool ListValue::GetDictionary(size_t index, - DictionaryValue** out_value) const { +bool ListValue::GetBoolean(size_t index, bool* bool_value) const { + Value* value; + if (!Get(index, &value)) + return false; + + return value->GetAsBoolean(bool_value); +} + +bool ListValue::GetInteger(size_t index, int* out_value) const { + Value* value; + if (!Get(index, &value)) + return false; + + return value->GetAsInteger(out_value); +} + +bool ListValue::GetReal(size_t index, double* out_value) const { + Value* value; + if (!Get(index, &value)) + return false; + + return value->GetAsReal(out_value); +} + +bool ListValue::GetString(size_t index, std::string* out_value) const { + Value* value; + if (!Get(index, &value)) + return false; + + return value->GetAsString(out_value); +} + +bool ListValue::GetBinary(size_t index, BinaryValue** out_value) const { + Value* value; + bool result = Get(index, &value); + if (!result || !value->IsType(TYPE_BINARY)) + return false; + + if (out_value) + *out_value = static_cast<BinaryValue*>(value); + + return true; +} + +bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) const { Value* value; bool result = Get(index, &value); if (!result || !value->IsType(TYPE_DICTIONARY)) @@ -545,6 +588,18 @@ bool ListValue::GetDictionary(size_t index, return true; } +bool ListValue::GetList(size_t index, ListValue** out_value) const { + Value* value; + bool result = Get(index, &value); + if (!result || !value->IsType(TYPE_LIST)) + return false; + + if (out_value) + *out_value = static_cast<ListValue*>(value); + + return true; +} + bool ListValue::Remove(size_t index, Value** out_value) { if (index >= list_.size()) return false; diff --git a/base/values.h b/base/values.h index 6f2f9df..19a1cb9 100644 --- a/base/values.h +++ b/base/values.h @@ -202,7 +202,7 @@ class DictionaryValue : public Value { virtual bool Equals(const Value* other) const; // Returns true if the current dictionary has a value for the given key. - bool HasKey(const std::wstring& key); + bool HasKey(const std::wstring& key) const; // Clears any current contents of this dictionary. void Clear(); @@ -286,7 +286,6 @@ class DictionaryValue : public Value { }; // This type of Value represents a list of other Value values. -// TODO(jhughes): Flesh this out. class ListValue : public Value { public: ListValue() : Value(TYPE_LIST) {} @@ -317,7 +316,13 @@ class ListValue : public Value { // Convenience forms of Get(). Modifies value (and returns true) only if // the index is valid and the Value at that index can be returned in // the specified form. + bool GetBoolean(size_t index, bool* out_value) const; + bool GetInteger(size_t index, int* out_value) const; + bool GetReal(size_t index, double* out_value) const; + bool GetString(size_t index, std::string* out_value) const; + bool GetBinary(size_t index, BinaryValue** out_value) const; bool GetDictionary(size_t index, DictionaryValue** out_value) const; + bool GetList(size_t index, ListValue** out_value) const; // Removes the Value with the specified index from this list. // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be diff --git a/base/values_unittest.cc b/base/values_unittest.cc index 1a78089..ac1ff44 100644 --- a/base/values_unittest.cc +++ b/base/values_unittest.cc @@ -5,6 +5,7 @@ #include <limits> #include "base/values.h" +#include "base/scoped_ptr.h" #include "testing/gtest/include/gtest/gtest.h" class ValuesTest: public testing::Test { @@ -55,6 +56,41 @@ TEST(ValuesTest, Basic) { ASSERT_EQ(std::wstring(L"http://froogle.com"), bookmark_url); } +TEST(ValuesTest, List) { + scoped_ptr<ListValue> mixed_list(new ListValue()); + mixed_list->Set(0, Value::CreateBooleanValue(true)); + mixed_list->Set(1, Value::CreateIntegerValue(42)); + mixed_list->Set(2, Value::CreateRealValue(88.8)); + mixed_list->Set(3, Value::CreateStringValue("foo")); + ASSERT_EQ(4u, mixed_list->GetSize()); + + Value *value = NULL; + bool bool_value = false; + int int_value = 0; + double double_value = 0.0; + std::string string_value; + + ASSERT_FALSE(mixed_list->Get(4, &value)); + + ASSERT_FALSE(mixed_list->GetInteger(0, &int_value)); + ASSERT_EQ(0, int_value); + ASSERT_FALSE(mixed_list->GetReal(1, &double_value)); + ASSERT_EQ(0.0, double_value); + ASSERT_FALSE(mixed_list->GetString(2, &string_value)); + ASSERT_EQ("", string_value); + ASSERT_FALSE(mixed_list->GetBoolean(3, &bool_value)); + ASSERT_EQ(false, bool_value); + + ASSERT_TRUE(mixed_list->GetBoolean(0, &bool_value)); + ASSERT_EQ(true, bool_value); + ASSERT_TRUE(mixed_list->GetInteger(1, &int_value)); + ASSERT_EQ(42, int_value); + ASSERT_TRUE(mixed_list->GetReal(2, &double_value)); + ASSERT_EQ(88.8, double_value); + ASSERT_TRUE(mixed_list->GetString(3, &string_value)); + ASSERT_EQ("foo", string_value); +} + TEST(ValuesTest, BinaryValue) { char* buffer = NULL; // Passing a null buffer pointer doesn't yield a BinaryValue |