Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: tools/json_schema_compiler/h_generator.py

Issue 10639020: Switch the downloads API over to IDL/json_schema_compiler (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 from code import Code 5 from code import Code
6 from model import PropertyType 6 from model import PropertyType
7 import cpp_util 7 import cpp_util
8 import model
9 import os
10 import schema_util 8 import schema_util
11 9
12 class HGenerator(object): 10 class HGenerator(object):
13 """A .h generator for a namespace. 11 """A .h generator for a namespace.
14 """ 12 """
15 def __init__(self, namespace, cpp_type_generator): 13 def __init__(self, namespace, cpp_type_generator):
16 self._cpp_type_generator = cpp_type_generator 14 self._cpp_type_generator = cpp_type_generator
17 self._namespace = namespace 15 self._namespace = namespace
18 self._target_namespace = ( 16 self._target_namespace = (
19 self._cpp_type_generator.GetCppNamespaceName(self._namespace)) 17 self._cpp_type_generator.GetCppNamespaceName(self._namespace))
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 """Generates the list of types in the current namespace in an order in which 102 """Generates the list of types in the current namespace in an order in which
105 depended-upon types appear before types which depend on them. 103 depended-upon types appear before types which depend on them.
106 """ 104 """
107 dependency_order = [] 105 dependency_order = []
108 106
109 def ExpandType(path, type_): 107 def ExpandType(path, type_):
110 if type_ in path: 108 if type_ in path:
111 raise ValueError("Illegal circular dependency via cycle " + 109 raise ValueError("Illegal circular dependency via cycle " +
112 ", ".join(map(lambda x: x.name, path + [type_]))) 110 ", ".join(map(lambda x: x.name, path + [type_])))
113 for prop in type_.properties.values(): 111 for prop in type_.properties.values():
114 if not prop.optional and prop.type_ == PropertyType.REF: 112 if (prop.type_ == PropertyType.REF and
113 schema_util.GetNamespace(prop.ref_type) == self._namespace.name):
115 ExpandType(path + [type_], self._namespace.types[prop.ref_type]) 114 ExpandType(path + [type_], self._namespace.types[prop.ref_type])
116 if not type_ in dependency_order: 115 if not type_ in dependency_order:
117 dependency_order.append(type_) 116 dependency_order.append(type_)
118 117
119 for type_ in self._namespace.types.values(): 118 for type_ in self._namespace.types.values():
120 ExpandType([], type_) 119 ExpandType([], type_)
121 return dependency_order 120 return dependency_order
122 121
123 def _GenerateEnumDeclaration(self, enum_name, prop, values): 122 def _GenerateEnumDeclaration(self, enum_name, prop, values):
124 """Generate the declaration of a C++ enum for the given property and 123 """Generate the declaration of a C++ enum for the given property and
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 if type_.description: 170 if type_.description:
172 c.Comment(type_.description) 171 c.Comment(type_.description)
173 c.Append('typedef std::vector<%(item_type)s> %(classname)s;') 172 c.Append('typedef std::vector<%(item_type)s> %(classname)s;')
174 c.Substitute({'classname': classname, 'item_type': 173 c.Substitute({'classname': classname, 'item_type':
175 self._cpp_type_generator.GetType(type_.item_type, 174 self._cpp_type_generator.GetType(type_.item_type,
176 wrap_optional=True)}) 175 wrap_optional=True)})
177 elif type_.type_ == PropertyType.STRING: 176 elif type_.type_ == PropertyType.STRING:
178 if type_.description: 177 if type_.description:
179 c.Comment(type_.description) 178 c.Comment(type_.description)
180 c.Append('typedef std::string %(classname)s;') 179 c.Append('typedef std::string %(classname)s;')
181 c.Substitute({'classname': classname})
182 else: 180 else:
183 if type_.description: 181 if type_.description:
184 c.Comment(type_.description) 182 c.Comment(type_.description)
185 (c.Sblock('struct %(classname)s {') 183 (c.Sblock('struct %(classname)s {')
186 .Append('~%(classname)s();') 184 .Append('~%(classname)s();')
187 .Append('%(classname)s();') 185 .Append('%(classname)s();')
188 .Append() 186 .Append()
189 .Concat(self._GeneratePropertyStructures(type_.properties.values())) 187 .Concat(self._GeneratePropertyStructures(type_.properties.values()))
190 .Concat(self._GenerateFields(type_.properties.values())) 188 .Concat(self._GenerateFields(type_.properties.values()))
191 ) 189 )
192 if type_.from_json: 190 if type_.from_json:
193 (c.Comment('Populates a %s object from a Value. Returns' 191 (c.Comment('Populates a %s object from a base::Value. Returns'
194 ' whether |out| was successfully populated.' % classname) 192 ' whether |out| was successfully populated.' % classname)
195 .Append( 193 .Append('static bool Populate(const base::Value& value, '
196 'static bool Populate(const Value& value, %(classname)s* out);') 194 '%(classname)s* out);')
197 .Append() 195 .Append()
198 ) 196 )
199 197
200 if type_.from_client: 198 if type_.from_client:
201 (c.Comment('Returns a new DictionaryValue representing the' 199 (c.Comment('Returns a new base::DictionaryValue representing the'
202 ' serialized form of this %s object. Passes ' 200 ' serialized form of this %s object. Passes '
203 'ownership to caller.' % classname) 201 'ownership to caller.' % classname)
204 .Append('scoped_ptr<DictionaryValue> ToValue() const;') 202 .Append('scoped_ptr<base::DictionaryValue> ToValue() const;')
205 ) 203 )
206 204
207 (c.Eblock() 205 (c.Eblock()
208 .Sblock(' private:') 206 .Sblock(' private:')
209 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') 207 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);')
210 .Eblock('};') 208 .Eblock('};')
211 ) 209 )
212 c.Substitute({'classname': classname}) 210 c.Substitute({'classname': classname})
213 return c 211 return c
214 212
(...skipping 17 matching lines...) Expand all
232 """Generates the struct for passing parameters into a function. 230 """Generates the struct for passing parameters into a function.
233 """ 231 """
234 c = Code() 232 c = Code()
235 233
236 if function.params: 234 if function.params:
237 (c.Sblock('struct Params {') 235 (c.Sblock('struct Params {')
238 .Concat(self._GeneratePropertyStructures(function.params)) 236 .Concat(self._GeneratePropertyStructures(function.params))
239 .Concat(self._GenerateFields(function.params)) 237 .Concat(self._GenerateFields(function.params))
240 .Append('~Params();') 238 .Append('~Params();')
241 .Append() 239 .Append()
242 .Append('static scoped_ptr<Params> Create(const ListValue& args);') 240 .Append('static scoped_ptr<Params> Create('
241 'const base::ListValue& args);')
243 .Eblock() 242 .Eblock()
244 .Sblock(' private:') 243 .Sblock(' private:')
245 .Append('Params();') 244 .Append('Params();')
246 .Append() 245 .Append()
247 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') 246 .Append('DISALLOW_COPY_AND_ASSIGN(Params);')
248 .Eblock('};') 247 .Eblock('};')
249 ) 248 )
250 249
251 return c 250 return c
252 251
(...skipping 14 matching lines...) Expand all
267 self._cpp_type_generator.GetChoicesEnumType(prop), 266 self._cpp_type_generator.GetChoicesEnumType(prop),
268 prop, 267 prop,
269 [choice.type_.name for choice in prop.choices.values()])) 268 [choice.type_.name for choice in prop.choices.values()]))
270 c.Concat(self._GeneratePropertyStructures(prop.choices.values())) 269 c.Concat(self._GeneratePropertyStructures(prop.choices.values()))
271 elif prop.type_ == PropertyType.ENUM: 270 elif prop.type_ == PropertyType.ENUM:
272 enum_name = self._cpp_type_generator.GetType(prop) 271 enum_name = self._cpp_type_generator.GetType(prop)
273 c.Concat(self._GenerateEnumDeclaration( 272 c.Concat(self._GenerateEnumDeclaration(
274 enum_name, 273 enum_name,
275 prop, 274 prop,
276 prop.enum_values)) 275 prop.enum_values))
277 c.Append('static scoped_ptr<Value> CreateEnumValue(%s %s);' % 276 c.Append('static scoped_ptr<base::Value> CreateEnumValue(%s %s);' %
278 (enum_name, prop.unix_name)) 277 (enum_name, prop.unix_name))
279 return c 278 return c
280 279
281 def _GenerateFunctionResult(self, function): 280 def _GenerateFunctionResult(self, function):
282 """Generates functions for passing a function's result back. 281 """Generates functions for passing a function's result back.
283 """ 282 """
284 c = Code() 283 c = Code()
285 284
286 c.Sblock('namespace Result {') 285 c.Sblock('namespace Result {')
287 params = function.callback.params 286 params = function.callback.params
288 if not params: 287 if not params:
289 c.Append('Value* Create();') 288 c.Append('base::Value* Create();')
290 else: 289 else:
291 c.Concat(self._GeneratePropertyStructures(params)) 290 c.Concat(self._GeneratePropertyStructures(params))
292 291
293 # If there is a single parameter, this is straightforward. However, if 292 # If there is a single parameter, this is straightforward. However, if
294 # the callback parameter is of 'choices', this generates a Create method 293 # the callback parameter is of 'choices', this generates a Create method
295 # for each choice. This works because only 1 choice can be returned at a 294 # for each choice. This works because only 1 choice can be returned at a
296 # time. 295 # time.
297 for param in self._cpp_type_generator.GetExpandedChoicesInParams(params): 296 for param in self._cpp_type_generator.GetExpandedChoicesInParams(params):
298 if param.description: 297 if param.description:
299 c.Comment(param.description) 298 c.Comment(param.description)
300 if param.type_ == PropertyType.ANY: 299 if param.type_ == PropertyType.ANY:
301 c.Comment("Value* Result::Create(Value*) not generated " 300 c.Comment("base::Value* Result::Create(base::Value*) not generated "
302 "because it's redundant.") 301 "because it's redundant.")
303 continue 302 continue
304 c.Append('Value* Create(const %s);' % cpp_util.GetParameterDeclaration( 303 c.Append('base::Value* Create(const %s);' %
305 param, self._cpp_type_generator.GetType(param))) 304 cpp_util.GetParameterDeclaration(
305 param, self._cpp_type_generator.GetType(param)))
306 c.Eblock('};') 306 c.Eblock('};')
307 307
308 return c 308 return c
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/cpp_type_generator_test.py ('k') | tools/json_schema_compiler/idl_schema.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698