| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 | 15 |
| 16 namespace json_schema_compiler { | 16 namespace json_schema_compiler { |
| 17 |
| 18 namespace any { |
| 19 class Any; |
| 20 } |
| 21 |
| 17 namespace util { | 22 namespace util { |
| 18 | 23 |
| 19 // Creates a new item at |out| from |from|[|index|]. These are used by template | 24 // Creates a new item at |out| from |from|[|index|]. These are used by template |
| 20 // specializations of |Get(Optional)ArrayFromList|. | 25 // specializations of |Get(Optional)ArrayFromList|. |
| 21 bool GetItemFromList(const ListValue& from, int index, int* out); | 26 bool GetItemFromList(const ListValue& from, int index, int* out); |
| 22 bool GetItemFromList(const ListValue& from, int index, bool* out); | 27 bool GetItemFromList(const ListValue& from, int index, bool* out); |
| 23 bool GetItemFromList(const ListValue& from, int index, double* out); | 28 bool GetItemFromList(const ListValue& from, int index, double* out); |
| 24 bool GetItemFromList(const ListValue& from, int index, std::string* out); | 29 bool GetItemFromList(const ListValue& from, int index, std::string* out); |
| 25 bool GetItemFromList(const ListValue& from, int index, | 30 bool GetItemFromList(const ListValue& from, int index, |
| 26 linked_ptr<base::DictionaryValue>* out); | 31 linked_ptr<base::DictionaryValue>* out); |
| 32 bool GetItemFromList(const ListValue& from, int index, |
| 33 linked_ptr<any::Any>* out); |
| 27 | 34 |
| 28 // This template is used for types generated by tools/json_schema_compiler. | 35 // This template is used for types generated by tools/json_schema_compiler. |
| 29 template<class T> | 36 template<class T> |
| 30 bool GetItemFromList(const ListValue& from, int index, linked_ptr<T>* out) { | 37 bool GetItemFromList(const ListValue& from, int index, linked_ptr<T>* out) { |
| 31 DictionaryValue* dict; | 38 DictionaryValue* dict; |
| 32 if (!from.GetDictionary(index, &dict)) | 39 if (!from.GetDictionary(index, &dict)) |
| 33 return false; | 40 return false; |
| 34 scoped_ptr<T> obj(new T()); | 41 scoped_ptr<T> obj(new T()); |
| 35 if (!T::Populate(*dict, obj.get())) | 42 if (!T::Populate(*dict, obj.get())) |
| 36 return false; | 43 return false; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 } | 119 } |
| 113 | 120 |
| 114 // Appends a Value newly created from |from| to |out|. These used by template | 121 // Appends a Value newly created from |from| to |out|. These used by template |
| 115 // specializations of |Set(Optional)ArrayToList|. | 122 // specializations of |Set(Optional)ArrayToList|. |
| 116 void AddItemToList(const int from, base::ListValue* out); | 123 void AddItemToList(const int from, base::ListValue* out); |
| 117 void AddItemToList(const bool from, base::ListValue* out); | 124 void AddItemToList(const bool from, base::ListValue* out); |
| 118 void AddItemToList(const double from, base::ListValue* out); | 125 void AddItemToList(const double from, base::ListValue* out); |
| 119 void AddItemToList(const std::string& from, base::ListValue* out); | 126 void AddItemToList(const std::string& from, base::ListValue* out); |
| 120 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, | 127 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, |
| 121 base::ListValue* out); | 128 base::ListValue* out); |
| 129 void AddItemToList(const linked_ptr<any::Any>& from, |
| 130 base::ListValue* out); |
| 122 | 131 |
| 123 // This template is used for types generated by tools/json_schema_compiler. | 132 // This template is used for types generated by tools/json_schema_compiler. |
| 124 template<class T> | 133 template<class T> |
| 125 void AddItemToList(const linked_ptr<T>& from, ListValue* out) { | 134 void AddItemToList(const linked_ptr<T>& from, ListValue* out) { |
| 126 out->Append(from->ToValue().release()); | 135 out->Append(from->ToValue().release()); |
| 127 } | 136 } |
| 128 | 137 |
| 129 // Set |out| to the the contents of |from|. Requires GetItemFromList to be | 138 // Set |out| to the the contents of |from|. Requires GetItemFromList to be |
| 130 // implemented for |T|. | 139 // implemented for |T|. |
| 131 template <class T> | 140 template <class T> |
| (...skipping 27 matching lines...) Expand all Loading... |
| 159 } | 168 } |
| 160 | 169 |
| 161 template <class T> | 170 template <class T> |
| 162 scoped_ptr<Value> CreateValueFromOptionalArray( | 171 scoped_ptr<Value> CreateValueFromOptionalArray( |
| 163 const scoped_ptr<std::vector<T> >& from) { | 172 const scoped_ptr<std::vector<T> >& from) { |
| 164 if (from.get()) | 173 if (from.get()) |
| 165 return CreateValueFromArray(*from); | 174 return CreateValueFromArray(*from); |
| 166 return scoped_ptr<Value>(); | 175 return scoped_ptr<Value>(); |
| 167 } | 176 } |
| 168 | 177 |
| 169 } // namespace api_util | 178 } // namespace util |
| 170 } // namespace extensions | 179 } // namespace json_schema_compiler |
| 171 | 180 |
| 172 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ | 181 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |
| OLD | NEW |