| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ | 5 #ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |
| 6 #define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ | 6 #define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/linked_ptr.h" | 11 #include "base/memory/linked_ptr.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 | 14 |
| 15 namespace json_schema_compiler { | 15 namespace json_schema_compiler { |
| 16 | 16 |
| 17 namespace util { | 17 namespace util { |
| 18 | 18 |
| 19 // Creates a new item at |out| from |from|[|index|]. These are used by template | 19 // Populates the item |out| from the value |from|. These are used by template |
| 20 // specializations of |Get(Optional)ArrayFromList|. | 20 // specializations of |Get(Optional)ArrayFromList|. |
| 21 bool GetItemFromList(const base::ListValue& from, int index, int* out); | 21 bool PopulateItem(const base::Value& from, int* out); |
| 22 bool GetItemFromList(const base::ListValue& from, int index, bool* out); | 22 bool PopulateItem(const base::Value& from, bool* out); |
| 23 bool GetItemFromList(const base::ListValue& from, int index, double* out); | 23 bool PopulateItem(const base::Value& from, double* out); |
| 24 bool GetItemFromList(const base::ListValue& from, int index, std::string* out); | 24 bool PopulateItem(const base::Value& from, std::string* out); |
| 25 bool GetItemFromList(const base::ListValue& from, | 25 bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out); |
| 26 int index, | 26 bool PopulateItem(const base::Value& from, |
| 27 linked_ptr<base::Value>* out); | 27 linked_ptr<base::DictionaryValue>* out); |
| 28 bool GetItemFromList(const base::ListValue& from, | |
| 29 int index, | |
| 30 linked_ptr<base::DictionaryValue>* out); | |
| 31 | 28 |
| 32 // This template is used for types generated by tools/json_schema_compiler. | 29 // This template is used for types generated by tools/json_schema_compiler. |
| 33 template<class T> | 30 template <class T> |
| 34 bool GetItemFromList(const base::ListValue& from, | 31 bool PopulateItem(const base::Value& from, linked_ptr<T>* out) { |
| 35 int index, | 32 const base::DictionaryValue* dict = nullptr; |
| 36 linked_ptr<T>* out) { | 33 if (!from.GetAsDictionary(&dict)) |
| 37 const base::DictionaryValue* dict; | |
| 38 if (!from.GetDictionary(index, &dict)) | |
| 39 return false; | 34 return false; |
| 40 scoped_ptr<T> obj(new T()); | 35 scoped_ptr<T> obj(new T()); |
| 41 if (!T::Populate(*dict, obj.get())) | 36 if (!T::Populate(*dict, obj.get())) |
| 42 return false; | 37 return false; |
| 43 *out = linked_ptr<T>(obj.release()); | 38 *out = linked_ptr<T>(obj.release()); |
| 44 return true; | 39 return true; |
| 45 } | 40 } |
| 46 | 41 |
| 47 // Populates |out| with |list|. Returns false if there is no list at the | 42 // Populates |out| with |list|. Returns false if there is no list at the |
| 48 // specified key or if the list has anything other than |T|. | 43 // specified key or if the list has anything other than |T|. |
| 49 template <class T> | 44 template <class T> |
| 50 bool PopulateArrayFromList( | 45 bool PopulateArrayFromList(const base::ListValue& list, std::vector<T>* out) { |
| 51 const base::ListValue& list, std::vector<T>* out) { | |
| 52 out->clear(); | 46 out->clear(); |
| 53 T value; | 47 T item; |
| 54 for (size_t i = 0; i < list.GetSize(); ++i) { | 48 for (const base::Value* value : list) { |
| 55 if (!GetItemFromList(list, i, &value)) | 49 if (!PopulateItem(*value, &item)) |
| 56 return false; | 50 return false; |
| 57 out->push_back(value); | 51 out->push_back(item); |
| 58 } | 52 } |
| 59 | 53 |
| 60 return true; | 54 return true; |
| 61 } | 55 } |
| 62 | 56 |
| 63 // Creates a new vector containing |list| at |out|. Returns | 57 // Creates a new vector containing |list| at |out|. Returns |
| 64 // true on success or if there is nothing at the specified key. Returns false | 58 // true on success or if there is nothing at the specified key. Returns false |
| 65 // if anything other than a list of |T| is at the specified key. | 59 // if anything other than a list of |T| is at the specified key. |
| 66 template <class T> | 60 template <class T> |
| 67 bool PopulateOptionalArrayFromList( | 61 bool PopulateOptionalArrayFromList(const base::ListValue& list, |
| 68 const base::ListValue& list, | 62 scoped_ptr<std::vector<T>>* out) { |
| 69 scoped_ptr<std::vector<T> >* out) { | |
| 70 out->reset(new std::vector<T>()); | 63 out->reset(new std::vector<T>()); |
| 71 T value; | 64 if (!PopulateArrayFromList(list, out->get())) { |
| 72 for (size_t i = 0; i < list.GetSize(); ++i) { | 65 out->reset(); |
| 73 if (!GetItemFromList(list, i, &value)) { | 66 return false; |
| 74 out->reset(); | |
| 75 return false; | |
| 76 } | |
| 77 (*out)->push_back(value); | |
| 78 } | 67 } |
| 79 | |
| 80 return true; | 68 return true; |
| 81 } | 69 } |
| 82 | 70 |
| 83 // Appends a Value newly created from |from| to |out|. These used by template | 71 // Appends a Value newly created from |from| to |out|. These used by template |
| 84 // specializations of |Set(Optional)ArrayToList|. | 72 // specializations of |Set(Optional)ArrayToList|. |
| 85 void AddItemToList(const int from, base::ListValue* out); | 73 void AddItemToList(const int from, base::ListValue* out); |
| 86 void AddItemToList(const bool from, base::ListValue* out); | 74 void AddItemToList(const bool from, base::ListValue* out); |
| 87 void AddItemToList(const double from, base::ListValue* out); | 75 void AddItemToList(const double from, base::ListValue* out); |
| 88 void AddItemToList(const std::string& from, base::ListValue* out); | 76 void AddItemToList(const std::string& from, base::ListValue* out); |
| 89 void AddItemToList(const linked_ptr<base::Value>& from, | 77 void AddItemToList(const linked_ptr<base::Value>& from, base::ListValue* out); |
| 90 base::ListValue* out); | |
| 91 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, | 78 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, |
| 92 base::ListValue* out); | 79 base::ListValue* out); |
| 93 | 80 |
| 94 // This template is used for types generated by tools/json_schema_compiler. | 81 // This template is used for types generated by tools/json_schema_compiler. |
| 95 template<class T> | 82 template <class T> |
| 96 void AddItemToList(const linked_ptr<T>& from, base::ListValue* out) { | 83 void AddItemToList(const linked_ptr<T>& from, base::ListValue* out) { |
| 97 out->Append(from->ToValue().release()); | 84 out->Append(from->ToValue().release()); |
| 98 } | 85 } |
| 99 | 86 |
| 100 // Set |out| to the the contents of |from|. Requires GetItemFromList to be | 87 // Set |out| to the the contents of |from|. Requires PopulateItem to be |
| 101 // implemented for |T|. | 88 // implemented for |T|. |
| 102 template <class T> | 89 template <class T> |
| 103 void PopulateListFromArray( | 90 void PopulateListFromArray(const std::vector<T>& from, base::ListValue* out) { |
| 104 const std::vector<T>& from, | |
| 105 base::ListValue* out) { | |
| 106 out->Clear(); | 91 out->Clear(); |
| 107 for (const auto& it : from) { | 92 for (const auto& item : from) |
| 108 AddItemToList(it, out); | 93 AddItemToList(item, out); |
| 109 } | |
| 110 } | 94 } |
| 111 | 95 |
| 112 // Set |out| to the the contents of |from| if |from| is non-NULL. Requires | 96 // Set |out| to the the contents of |from| if |from| is not null. Requires |
| 113 // GetItemFromList to be implemented for |T|. | 97 // PopulateItem to be implemented for |T|. |
| 114 template <class T> | 98 template <class T> |
| 115 void PopulateListFromOptionalArray( | 99 void PopulateListFromOptionalArray(const scoped_ptr<std::vector<T>>& from, |
| 116 const scoped_ptr<std::vector<T> >& from, | 100 base::ListValue* out) { |
| 117 base::ListValue* out) { | |
| 118 if (from.get()) | 101 if (from.get()) |
| 119 PopulateListFromArray(*from, out); | 102 PopulateListFromArray(*from, out); |
| 120 | |
| 121 } | 103 } |
| 122 | 104 |
| 123 template <class T> | 105 template <class T> |
| 124 scoped_ptr<base::Value> CreateValueFromArray(const std::vector<T>& from) { | 106 scoped_ptr<base::Value> CreateValueFromArray(const std::vector<T>& from) { |
| 125 base::ListValue* list = new base::ListValue(); | 107 base::ListValue* list = new base::ListValue(); |
| 126 PopulateListFromArray(from, list); | 108 PopulateListFromArray(from, list); |
| 127 return scoped_ptr<base::Value>(list); | 109 return scoped_ptr<base::Value>(list); |
| 128 } | 110 } |
| 129 | 111 |
| 130 template <class T> | 112 template <class T> |
| 131 scoped_ptr<base::Value> CreateValueFromOptionalArray( | 113 scoped_ptr<base::Value> CreateValueFromOptionalArray( |
| 132 const scoped_ptr<std::vector<T> >& from) { | 114 const scoped_ptr<std::vector<T>>& from) { |
| 133 if (from.get()) | 115 if (from.get()) |
| 134 return CreateValueFromArray(*from); | 116 return CreateValueFromArray(*from); |
| 135 return scoped_ptr<base::Value>(); | 117 return scoped_ptr<base::Value>(); |
| 136 } | 118 } |
| 137 | 119 |
| 138 std::string ValueTypeToString(base::Value::Type type); | 120 std::string ValueTypeToString(base::Value::Type type); |
| 139 | 121 |
| 140 } // namespace util | 122 } // namespace util |
| 141 } // namespace json_schema_compiler | 123 } // namespace json_schema_compiler |
| 142 | 124 |
| 143 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ | 125 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |
| OLD | NEW |