| 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 // Creates a new item at |out| from |from|[|index|]. These are used by template |
| 20 // specializations of |Get(Optional)ArrayFromList|. | 20 // specializations of |Get(Optional)ArrayFromList|. |
| 21 bool GetItemFromList(const ListValue& from, int index, int* out); | 21 bool GetItemFromList(const base::ListValue& from, int index, int* out); |
| 22 bool GetItemFromList(const ListValue& from, int index, bool* out); | 22 bool GetItemFromList(const base::ListValue& from, int index, bool* out); |
| 23 bool GetItemFromList(const ListValue& from, int index, double* out); | 23 bool GetItemFromList(const base::ListValue& from, int index, double* out); |
| 24 bool GetItemFromList(const ListValue& from, int index, std::string* out); | 24 bool GetItemFromList(const base::ListValue& from, int index, std::string* out); |
| 25 bool GetItemFromList(const ListValue& from, | 25 bool GetItemFromList(const base::ListValue& from, |
| 26 int index, | 26 int index, |
| 27 linked_ptr<base::Value>* out); | 27 linked_ptr<base::Value>* out); |
| 28 bool GetItemFromList(const ListValue& from, | 28 bool GetItemFromList(const base::ListValue& from, |
| 29 int index, | 29 int index, |
| 30 linked_ptr<base::DictionaryValue>* out); | 30 linked_ptr<base::DictionaryValue>* out); |
| 31 | 31 |
| 32 // This template is used for types generated by tools/json_schema_compiler. | 32 // This template is used for types generated by tools/json_schema_compiler. |
| 33 template<class T> | 33 template<class T> |
| 34 bool GetItemFromList(const ListValue& from, int index, linked_ptr<T>* out) { | 34 bool GetItemFromList(const base::ListValue& from, |
| 35 const DictionaryValue* dict; | 35 int index, |
| 36 linked_ptr<T>* out) { |
| 37 const base::DictionaryValue* dict; |
| 36 if (!from.GetDictionary(index, &dict)) | 38 if (!from.GetDictionary(index, &dict)) |
| 37 return false; | 39 return false; |
| 38 scoped_ptr<T> obj(new T()); | 40 scoped_ptr<T> obj(new T()); |
| 39 if (!T::Populate(*dict, obj.get())) | 41 if (!T::Populate(*dict, obj.get())) |
| 40 return false; | 42 return false; |
| 41 *out = linked_ptr<T>(obj.release()); | 43 *out = linked_ptr<T>(obj.release()); |
| 42 return true; | 44 return true; |
| 43 } | 45 } |
| 44 | 46 |
| 45 // This is used for getting an enum out of a ListValue, which will happen if an | 47 // This is used for getting an enum out of a ListValue, which will happen if an |
| 46 // array of enums is a parameter to a function. | 48 // array of enums is a parameter to a function. |
| 47 template<class T> | 49 template<class T> |
| 48 bool GetItemFromList(const ListValue& from, int index, T* out) { | 50 bool GetItemFromList(const base::ListValue& from, int index, T* out) { |
| 49 int value; | 51 int value; |
| 50 if (!from.GetInteger(index, &value)) | 52 if (!from.GetInteger(index, &value)) |
| 51 return false; | 53 return false; |
| 52 *out = static_cast<T>(value); | 54 *out = static_cast<T>(value); |
| 53 return true; | 55 return true; |
| 54 } | 56 } |
| 55 | 57 |
| 56 // Populates |out| with |list|. Returns false if there is no list at the | 58 // Populates |out| with |list|. Returns false if there is no list at the |
| 57 // specified key or if the list has anything other than |T|. | 59 // specified key or if the list has anything other than |T|. |
| 58 template <class T> | 60 template <class T> |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 void AddItemToList(const bool from, base::ListValue* out); | 134 void AddItemToList(const bool from, base::ListValue* out); |
| 133 void AddItemToList(const double from, base::ListValue* out); | 135 void AddItemToList(const double from, base::ListValue* out); |
| 134 void AddItemToList(const std::string& from, base::ListValue* out); | 136 void AddItemToList(const std::string& from, base::ListValue* out); |
| 135 void AddItemToList(const linked_ptr<base::Value>& from, | 137 void AddItemToList(const linked_ptr<base::Value>& from, |
| 136 base::ListValue* out); | 138 base::ListValue* out); |
| 137 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, | 139 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, |
| 138 base::ListValue* out); | 140 base::ListValue* out); |
| 139 | 141 |
| 140 // This template is used for types generated by tools/json_schema_compiler. | 142 // This template is used for types generated by tools/json_schema_compiler. |
| 141 template<class T> | 143 template<class T> |
| 142 void AddItemToList(const linked_ptr<T>& from, ListValue* out) { | 144 void AddItemToList(const linked_ptr<T>& from, base::ListValue* out) { |
| 143 out->Append(from->ToValue().release()); | 145 out->Append(from->ToValue().release()); |
| 144 } | 146 } |
| 145 | 147 |
| 146 // Set |out| to the the contents of |from|. Requires GetItemFromList to be | 148 // Set |out| to the the contents of |from|. Requires GetItemFromList to be |
| 147 // implemented for |T|. | 149 // implemented for |T|. |
| 148 template <class T> | 150 template <class T> |
| 149 void PopulateListFromArray( | 151 void PopulateListFromArray( |
| 150 const std::vector<T>& from, | 152 const std::vector<T>& from, |
| 151 base::ListValue* out) { | 153 base::ListValue* out) { |
| 152 out->Clear(); | 154 out->Clear(); |
| 153 for (typename std::vector<T>::const_iterator it = from.begin(); | 155 for (typename std::vector<T>::const_iterator it = from.begin(); |
| 154 it != from.end(); ++it) { | 156 it != from.end(); ++it) { |
| 155 AddItemToList(*it, out); | 157 AddItemToList(*it, out); |
| 156 } | 158 } |
| 157 } | 159 } |
| 158 | 160 |
| 159 // Set |out| to the the contents of |from| if |from| is non-NULL. Requires | 161 // Set |out| to the the contents of |from| if |from| is non-NULL. Requires |
| 160 // GetItemFromList to be implemented for |T|. | 162 // GetItemFromList to be implemented for |T|. |
| 161 template <class T> | 163 template <class T> |
| 162 void PopulateListFromOptionalArray( | 164 void PopulateListFromOptionalArray( |
| 163 const scoped_ptr<std::vector<T> >& from, | 165 const scoped_ptr<std::vector<T> >& from, |
| 164 base::ListValue* out) { | 166 base::ListValue* out) { |
| 165 if (from.get()) | 167 if (from.get()) |
| 166 PopulateListFromArray(*from, out); | 168 PopulateListFromArray(*from, out); |
| 167 | 169 |
| 168 } | 170 } |
| 169 | 171 |
| 170 template <class T> | 172 template <class T> |
| 171 scoped_ptr<Value> CreateValueFromArray(const std::vector<T>& from) { | 173 scoped_ptr<base::Value> CreateValueFromArray(const std::vector<T>& from) { |
| 172 base::ListValue* list = new base::ListValue(); | 174 base::ListValue* list = new base::ListValue(); |
| 173 PopulateListFromArray(from, list); | 175 PopulateListFromArray(from, list); |
| 174 return scoped_ptr<Value>(list); | 176 return scoped_ptr<base::Value>(list); |
| 175 } | 177 } |
| 176 | 178 |
| 177 template <class T> | 179 template <class T> |
| 178 scoped_ptr<Value> CreateValueFromOptionalArray( | 180 scoped_ptr<base::Value> CreateValueFromOptionalArray( |
| 179 const scoped_ptr<std::vector<T> >& from) { | 181 const scoped_ptr<std::vector<T> >& from) { |
| 180 if (from.get()) | 182 if (from.get()) |
| 181 return CreateValueFromArray(*from); | 183 return CreateValueFromArray(*from); |
| 182 return scoped_ptr<Value>(); | 184 return scoped_ptr<base::Value>(); |
| 183 } | 185 } |
| 184 | 186 |
| 185 } // namespace util | 187 } // namespace util |
| 186 } // namespace json_schema_compiler | 188 } // namespace json_schema_compiler |
| 187 | 189 |
| 188 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ | 190 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |
| OLD | NEW |