summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorreillyg <reillyg@chromium.org>2015-09-10 17:25:54 -0700
committerCommit bot <commit-bot@chromium.org>2015-09-11 00:26:37 +0000
commit259c0a3f44bdd8130c5d835e9f596624a079fbd1 (patch)
tree8c69523a7745aa71e6d67cdedad5b5ce0336b4a9 /base
parent9a1461f5cbacfc7d5c40ac2cd2cc0ec104e9b39c (diff)
downloadchromium_src-259c0a3f44bdd8130c5d835e9f596624a079fbd1.zip
chromium_src-259c0a3f44bdd8130c5d835e9f596624a079fbd1.tar.gz
chromium_src-259c0a3f44bdd8130c5d835e9f596624a079fbd1.tar.bz2
Add scoped_ptr-safe base::Value to Dictionary/List conversion functions.
This change adds two static From() functions to the DictionaryValue and ListValue classes which take a scoped_ptr to a Value and either convert it to a scoped_ptr to a DictionaryValue or ListValue, or return nullptr. These are intended to replace the existing pattern, make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())) with the shorter and safer alternative, base::DictionaryValue::From(value.Pass()) Instances of this pattern in //extensions have been converted as examples. Review URL: https://codereview.chromium.org/1308013005 Cr-Commit-Position: refs/heads/master@{#348294}
Diffstat (limited to 'base')
-rw-r--r--base/values.cc20
-rw-r--r--base/values.h6
2 files changed, 26 insertions, 0 deletions
diff --git a/base/values.cc b/base/values.cc
index c829a7f..9b2483e 100644
--- a/base/values.cc
+++ b/base/values.cc
@@ -351,6 +351,16 @@ bool BinaryValue::Equals(const Value* other) const {
///////////////////// DictionaryValue ////////////////////
+// static
+scoped_ptr<DictionaryValue> DictionaryValue::From(scoped_ptr<Value> value) {
+ DictionaryValue* out;
+ if (value && value->GetAsDictionary(&out)) {
+ ignore_result(value.release());
+ return make_scoped_ptr(out);
+ }
+ return nullptr;
+}
+
DictionaryValue::DictionaryValue()
: Value(TYPE_DICTIONARY) {
}
@@ -870,6 +880,16 @@ bool DictionaryValue::Equals(const Value* other) const {
///////////////////// ListValue ////////////////////
+// static
+scoped_ptr<ListValue> ListValue::From(scoped_ptr<Value> value) {
+ ListValue* out;
+ if (value && value->GetAsList(&out)) {
+ ignore_result(value.release());
+ return make_scoped_ptr(out);
+ }
+ return nullptr;
+}
+
ListValue::ListValue() : Value(TYPE_LIST) {
}
diff --git a/base/values.h b/base/values.h
index 8756deb..56be542 100644
--- a/base/values.h
+++ b/base/values.h
@@ -209,6 +209,9 @@ class BASE_EXPORT BinaryValue: public Value {
// are |std::string|s and should be UTF-8 encoded.
class BASE_EXPORT DictionaryValue : public Value {
public:
+ // Returns |value| if it is a dictionary, nullptr otherwise.
+ static scoped_ptr<DictionaryValue> From(scoped_ptr<Value> value);
+
DictionaryValue();
~DictionaryValue() override;
@@ -387,6 +390,9 @@ class BASE_EXPORT ListValue : public Value {
typedef ValueVector::iterator iterator;
typedef ValueVector::const_iterator const_iterator;
+ // Returns |value| if it is a list, nullptr otherwise.
+ static scoped_ptr<ListValue> From(scoped_ptr<Value> value);
+
ListValue();
~ListValue() override;