Chromium Code Reviews| Index: tools/json_schema_compiler/util.h |
| diff --git a/tools/json_schema_compiler/util.h b/tools/json_schema_compiler/util.h |
| index b844dd2882c03137cfa7410504fea566e515aeed..dbd010884502e933b507d5557239359ce020725a 100644 |
| --- a/tools/json_schema_compiler/util.h |
| +++ b/tools/json_schema_compiler/util.h |
| @@ -16,26 +16,21 @@ namespace json_schema_compiler { |
| namespace util { |
| -// Creates a new item at |out| from |from|[|index|]. These are used by template |
| +// Populates the item |out| from the value |from|. These are used by template |
| // specializations of |Get(Optional)ArrayFromList|. |
| -bool GetItemFromList(const base::ListValue& from, int index, int* out); |
| -bool GetItemFromList(const base::ListValue& from, int index, bool* out); |
| -bool GetItemFromList(const base::ListValue& from, int index, double* out); |
| -bool GetItemFromList(const base::ListValue& from, int index, std::string* out); |
| -bool GetItemFromList(const base::ListValue& from, |
| - int index, |
| - linked_ptr<base::Value>* out); |
| -bool GetItemFromList(const base::ListValue& from, |
| - int index, |
| - linked_ptr<base::DictionaryValue>* out); |
| +bool PopulateItem(const base::Value& from, int* out); |
| +bool PopulateItem(const base::Value& from, bool* out); |
| +bool PopulateItem(const base::Value& from, double* out); |
| +bool PopulateItem(const base::Value& from, std::string* out); |
| +bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out); |
| +bool PopulateItem(const base::Value& from, |
| + linked_ptr<base::DictionaryValue>* out); |
| // This template is used for types generated by tools/json_schema_compiler. |
| -template<class T> |
| -bool GetItemFromList(const base::ListValue& from, |
| - int index, |
| - linked_ptr<T>* out) { |
| - const base::DictionaryValue* dict; |
| - if (!from.GetDictionary(index, &dict)) |
| +template <class T> |
| +bool PopulateItem(const base::Value& from, linked_ptr<T>* out) { |
| + const base::DictionaryValue* dict = nullptr; |
| + if (!from.GetAsDictionary(&dict)) |
| return false; |
| scoped_ptr<T> obj(new T()); |
| if (!T::Populate(*dict, obj.get())) |
| @@ -47,14 +42,15 @@ bool GetItemFromList(const base::ListValue& from, |
| // Populates |out| with |list|. Returns false if there is no list at the |
| // specified key or if the list has anything other than |T|. |
| template <class T> |
| -bool PopulateArrayFromList( |
| - const base::ListValue& list, std::vector<T>* out) { |
| +bool PopulateArrayFromList(const base::ListValue& list, |
| + std::vector<T>* out) { |
| out->clear(); |
| - T value; |
| + T item; |
| for (size_t i = 0; i < list.GetSize(); ++i) { |
|
not at google - send to devlin
2015/01/15 20:47:30
I notice that ListValue has an iterator-like inter
pneubeck (no reviews)
2015/01/16 08:20:13
Yes, forgot about that.
Did the same to the other
|
| - if (!GetItemFromList(list, i, &value)) |
| + const base::Value* value = nullptr; |
| + if (!list.Get(i, &value) || !PopulateItem(*value, &item)) |
| return false; |
| - out->push_back(value); |
| + out->push_back(item); |
| } |
| return true; |
| @@ -64,19 +60,13 @@ bool PopulateArrayFromList( |
| // true on success or if there is nothing at the specified key. Returns false |
| // if anything other than a list of |T| is at the specified key. |
| template <class T> |
| -bool PopulateOptionalArrayFromList( |
| - const base::ListValue& list, |
| - scoped_ptr<std::vector<T> >* out) { |
| +bool PopulateOptionalArrayFromList(const base::ListValue& list, |
| + scoped_ptr<std::vector<T>>* out) { |
| out->reset(new std::vector<T>()); |
| - T value; |
| - for (size_t i = 0; i < list.GetSize(); ++i) { |
| - if (!GetItemFromList(list, i, &value)) { |
| - out->reset(); |
| - return false; |
| - } |
| - (*out)->push_back(value); |
| + if (!PopulateArrayFromList(list, out->get())) { |
| + out->reset(); |
| + return false; |
| } |
| - |
| return true; |
| } |
| @@ -97,7 +87,7 @@ void AddItemToList(const linked_ptr<T>& from, base::ListValue* out) { |
| out->Append(from->ToValue().release()); |
| } |
| -// Set |out| to the the contents of |from|. Requires GetItemFromList to be |
| +// Set |out| to the the contents of |from|. Requires PopulateItem to be |
| // implemented for |T|. |
| template <class T> |
| void PopulateListFromArray( |
| @@ -110,8 +100,8 @@ void PopulateListFromArray( |
| } |
| } |
| -// Set |out| to the the contents of |from| if |from| is non-NULL. Requires |
| -// GetItemFromList to be implemented for |T|. |
| +// Set |out| to the the contents of |from| if |from| is not null. Requires |
| +// PopulateItem to be implemented for |T|. |
| template <class T> |
| void PopulateListFromOptionalArray( |
| const scoped_ptr<std::vector<T> >& from, |