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 any { | |
18 class Any; | |
19 } | |
20 | |
21 namespace util { | 17 namespace util { |
22 | 18 |
23 // 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 |
24 // specializations of |Get(Optional)ArrayFromList|. | 20 // specializations of |Get(Optional)ArrayFromList|. |
25 bool GetItemFromList(const ListValue& from, int index, int* out); | 21 bool GetItemFromList(const ListValue& from, int index, int* out); |
26 bool GetItemFromList(const ListValue& from, int index, bool* out); | 22 bool GetItemFromList(const ListValue& from, int index, bool* out); |
27 bool GetItemFromList(const ListValue& from, int index, double* out); | 23 bool GetItemFromList(const ListValue& from, int index, double* out); |
28 bool GetItemFromList(const ListValue& from, int index, std::string* out); | 24 bool GetItemFromList(const ListValue& from, int index, std::string* out); |
29 bool GetItemFromList(const ListValue& from, int index, | 25 bool GetItemFromList(const ListValue& from, |
30 linked_ptr<base::DictionaryValue>* out); | 26 int index, |
31 bool GetItemFromList(const ListValue& from, int index, | 27 linked_ptr<base::Value>* out); |
32 linked_ptr<any::Any>* out); | 28 bool GetItemFromList(const ListValue& from, |
| 29 int index, |
| 30 linked_ptr<base::DictionaryValue>* out); |
33 | 31 |
34 // 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. |
35 template<class T> | 33 template<class T> |
36 bool GetItemFromList(const ListValue& from, int index, linked_ptr<T>* out) { | 34 bool GetItemFromList(const ListValue& from, int index, linked_ptr<T>* out) { |
37 const DictionaryValue* dict; | 35 const DictionaryValue* dict; |
38 if (!from.GetDictionary(index, &dict)) | 36 if (!from.GetDictionary(index, &dict)) |
39 return false; | 37 return false; |
40 scoped_ptr<T> obj(new T()); | 38 scoped_ptr<T> obj(new T()); |
41 if (!T::Populate(*dict, obj.get())) | 39 if (!T::Populate(*dict, obj.get())) |
42 return false; | 40 return false; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 125 |
128 return PopulateOptionalArrayFromList(*list, out); | 126 return PopulateOptionalArrayFromList(*list, out); |
129 } | 127 } |
130 | 128 |
131 // Appends a Value newly created from |from| to |out|. These used by template | 129 // Appends a Value newly created from |from| to |out|. These used by template |
132 // specializations of |Set(Optional)ArrayToList|. | 130 // specializations of |Set(Optional)ArrayToList|. |
133 void AddItemToList(const int from, base::ListValue* out); | 131 void AddItemToList(const int from, base::ListValue* out); |
134 void AddItemToList(const bool from, base::ListValue* out); | 132 void AddItemToList(const bool from, base::ListValue* out); |
135 void AddItemToList(const double from, base::ListValue* out); | 133 void AddItemToList(const double from, base::ListValue* out); |
136 void AddItemToList(const std::string& from, base::ListValue* out); | 134 void AddItemToList(const std::string& from, base::ListValue* out); |
| 135 void AddItemToList(const linked_ptr<base::Value>& from, |
| 136 base::ListValue* out); |
137 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, | 137 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, |
138 base::ListValue* out); | 138 base::ListValue* out); |
139 void AddItemToList(const linked_ptr<any::Any>& from, | |
140 base::ListValue* out); | |
141 | 139 |
142 // This template is used for types generated by tools/json_schema_compiler. | 140 // This template is used for types generated by tools/json_schema_compiler. |
143 template<class T> | 141 template<class T> |
144 void AddItemToList(const linked_ptr<T>& from, ListValue* out) { | 142 void AddItemToList(const linked_ptr<T>& from, ListValue* out) { |
145 out->Append(from->ToValue().release()); | 143 out->Append(from->ToValue().release()); |
146 } | 144 } |
147 | 145 |
148 // Set |out| to the the contents of |from|. Requires GetItemFromList to be | 146 // Set |out| to the the contents of |from|. Requires GetItemFromList to be |
149 // implemented for |T|. | 147 // implemented for |T|. |
150 template <class T> | 148 template <class T> |
(...skipping 12 matching lines...) Expand all Loading... |
163 template <class T> | 161 template <class T> |
164 void PopulateListFromOptionalArray( | 162 void PopulateListFromOptionalArray( |
165 const scoped_ptr<std::vector<T> >& from, | 163 const scoped_ptr<std::vector<T> >& from, |
166 base::ListValue* out) { | 164 base::ListValue* out) { |
167 if (from.get()) | 165 if (from.get()) |
168 PopulateListFromArray(*from, out); | 166 PopulateListFromArray(*from, out); |
169 | 167 |
170 } | 168 } |
171 | 169 |
172 template <class T> | 170 template <class T> |
173 scoped_ptr<Value> CreateValueFromArray( | 171 scoped_ptr<Value> CreateValueFromArray(const std::vector<T>& from) { |
174 const std::vector<T>& from) { | |
175 base::ListValue* list = new base::ListValue(); | 172 base::ListValue* list = new base::ListValue(); |
176 PopulateListFromArray(from, list); | 173 PopulateListFromArray(from, list); |
177 return scoped_ptr<Value>(list); | 174 return scoped_ptr<Value>(list); |
178 } | 175 } |
179 | 176 |
180 template <class T> | 177 template <class T> |
181 scoped_ptr<Value> CreateValueFromOptionalArray( | 178 scoped_ptr<Value> CreateValueFromOptionalArray( |
182 const scoped_ptr<std::vector<T> >& from) { | 179 const scoped_ptr<std::vector<T> >& from) { |
183 if (from.get()) | 180 if (from.get()) |
184 return CreateValueFromArray(*from); | 181 return CreateValueFromArray(*from); |
185 return scoped_ptr<Value>(); | 182 return scoped_ptr<Value>(); |
186 } | 183 } |
187 | 184 |
188 } // namespace util | 185 } // namespace util |
189 } // namespace json_schema_compiler | 186 } // namespace json_schema_compiler |
190 | 187 |
191 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ | 188 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |
OLD | NEW |