| 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 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 const DictionaryValue* dict; | 37 const DictionaryValue* dict; |
| 38 if (!from.GetDictionary(index, &dict)) | 38 if (!from.GetDictionary(index, &dict)) |
| 39 return false; | 39 return false; |
| 40 scoped_ptr<T> obj(new T()); | 40 scoped_ptr<T> obj(new T()); |
| 41 if (!T::Populate(*dict, obj.get())) | 41 if (!T::Populate(*dict, obj.get())) |
| 42 return false; | 42 return false; |
| 43 *out = linked_ptr<T>(obj.release()); | 43 *out = linked_ptr<T>(obj.release()); |
| 44 return true; | 44 return true; |
| 45 } | 45 } |
| 46 | 46 |
| 47 // This is used for getting an enum out of a ListValue, which will happen if an |
| 48 // array of enums is a parameter to a function. |
| 49 template<class T> |
| 50 bool GetItemFromList(const ListValue& from, int index, T* out) { |
| 51 int value; |
| 52 if (!from.GetInteger(index, &value)) |
| 53 return false; |
| 54 *out = static_cast<T>(value); |
| 55 return true; |
| 56 } |
| 57 |
| 47 // 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 |
| 48 // specified key or if the list has anything other than |T|. | 59 // specified key or if the list has anything other than |T|. |
| 49 template <class T> | 60 template <class T> |
| 50 bool PopulateArrayFromList( | 61 bool PopulateArrayFromList( |
| 51 const base::ListValue& list, std::vector<T>* out) { | 62 const base::ListValue& list, std::vector<T>* out) { |
| 52 out->clear(); | 63 out->clear(); |
| 53 T value; | 64 T value; |
| 54 for (size_t i = 0; i < list.GetSize(); ++i) { | 65 for (size_t i = 0; i < list.GetSize(); ++i) { |
| 55 if (!GetItemFromList(list, i, &value)) | 66 if (!GetItemFromList(list, i, &value)) |
| 56 return false; | 67 return false; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 const scoped_ptr<std::vector<T> >& from) { | 182 const scoped_ptr<std::vector<T> >& from) { |
| 172 if (from.get()) | 183 if (from.get()) |
| 173 return CreateValueFromArray(*from); | 184 return CreateValueFromArray(*from); |
| 174 return scoped_ptr<Value>(); | 185 return scoped_ptr<Value>(); |
| 175 } | 186 } |
| 176 | 187 |
| 177 } // namespace util | 188 } // namespace util |
| 178 } // namespace json_schema_compiler | 189 } // namespace json_schema_compiler |
| 179 | 190 |
| 180 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ | 191 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |
| OLD | NEW |