Index: tools/json_schema_compiler/util.h |
diff --git a/tools/json_schema_compiler/util.h b/tools/json_schema_compiler/util.h |
index ae0aca24c06ffd98e3a2a6d7c40c3402db3301cd..15c86e7f8c4e9a458187e56fc18c20030e49e9be 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,13 @@ 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; |
- for (size_t i = 0; i < list.GetSize(); ++i) { |
- if (!GetItemFromList(list, i, &value)) |
+ T item; |
+ for (const base::Value* value : list) { |
+ if (!PopulateItem(*value, &item)) |
return false; |
- out->push_back(value); |
+ out->push_back(item); |
} |
return true; |
@@ -64,19 +58,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; |
} |
@@ -86,38 +74,32 @@ void AddItemToList(const int from, base::ListValue* out); |
void AddItemToList(const bool from, base::ListValue* out); |
void AddItemToList(const double from, base::ListValue* out); |
void AddItemToList(const std::string& from, base::ListValue* out); |
-void AddItemToList(const linked_ptr<base::Value>& from, |
- base::ListValue* out); |
+void AddItemToList(const linked_ptr<base::Value>& from, base::ListValue* out); |
void AddItemToList(const linked_ptr<base::DictionaryValue>& from, |
base::ListValue* out); |
// This template is used for types generated by tools/json_schema_compiler. |
-template<class T> |
+template <class T> |
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( |
- const std::vector<T>& from, |
- base::ListValue* out) { |
+void PopulateListFromArray(const std::vector<T>& from, base::ListValue* out) { |
out->Clear(); |
- for (const auto& it : from) { |
- AddItemToList(it, out); |
- } |
+ for (const auto& item : from) |
+ AddItemToList(item, out); |
} |
-// 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, |
- base::ListValue* out) { |
+void PopulateListFromOptionalArray(const scoped_ptr<std::vector<T>>& from, |
+ base::ListValue* out) { |
if (from.get()) |
PopulateListFromArray(*from, out); |
- |
} |
template <class T> |
@@ -129,7 +111,7 @@ scoped_ptr<base::Value> CreateValueFromArray(const std::vector<T>& from) { |
template <class T> |
scoped_ptr<base::Value> CreateValueFromOptionalArray( |
- const scoped_ptr<std::vector<T> >& from) { |
+ const scoped_ptr<std::vector<T>>& from) { |
if (from.get()) |
return CreateValueFromArray(*from); |
return scoped_ptr<base::Value>(); |