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

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

Issue 10701012: JSON Schema Compiler: Added event compilation. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Tested GetAllPossibleParameterLists better. 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
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 schema_util 8 import schema_util
9 9
10 class HGenerator(object): 10 class HGenerator(object):
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 if self._namespace.functions: 82 if self._namespace.functions:
83 (c.Append('//') 83 (c.Append('//')
84 .Append('// Functions') 84 .Append('// Functions')
85 .Append('//') 85 .Append('//')
86 .Append() 86 .Append()
87 ) 87 )
88 for function in self._namespace.functions.values(): 88 for function in self._namespace.functions.values():
89 (c.Concat(self._GenerateFunction(function)) 89 (c.Concat(self._GenerateFunction(function))
90 .Append() 90 .Append()
91 ) 91 )
92 if self._namespace.events:
93 (c.Append('//')
94 .Append('// Events')
95 .Append('//')
96 .Append()
97 )
98 for event in self._namespace.events.values():
99 (c.Concat(self._GenerateEvent(event))
100 .Append()
101 )
92 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) 102 (c.Concat(self._cpp_type_generator.GetNamespaceEnd())
93 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) 103 .Concat(self._cpp_type_generator.GetRootNamespaceEnd())
94 .Append() 104 .Append()
95 .Append('#endif // %s' % ifndef_name) 105 .Append('#endif // %s' % ifndef_name)
96 .Append() 106 .Append()
97 ) 107 )
98 return c 108 return c
99 109
100 def _FieldDependencyOrder(self): 110 def _FieldDependencyOrder(self):
101 """Generates the list of types in the current namespace in an order in which 111 """Generates the list of types in the current namespace in an order in which
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 def _GenerateFields(self, props): 146 def _GenerateFields(self, props):
137 """Generates the field declarations when declaring a type. 147 """Generates the field declarations when declaring a type.
138 """ 148 """
139 c = Code() 149 c = Code()
140 # Generate the enums needed for any fields with "choices" 150 # Generate the enums needed for any fields with "choices"
141 for prop in props: 151 for prop in props:
142 if prop.type_ == PropertyType.CHOICES: 152 if prop.type_ == PropertyType.CHOICES:
143 enum_name = self._cpp_type_generator.GetChoicesEnumType(prop) 153 enum_name = self._cpp_type_generator.GetChoicesEnumType(prop)
144 c.Append('%s %s_type;' % (enum_name, prop.unix_name)) 154 c.Append('%s %s_type;' % (enum_name, prop.unix_name))
145 c.Append() 155 c.Append()
146 for prop in self._cpp_type_generator.GetExpandedChoicesInParams(props): 156
157 for prop in self._cpp_type_generator.ExpandParams(props):
147 if prop.description: 158 if prop.description:
148 c.Comment(prop.description) 159 c.Comment(prop.description)
149 c.Append('%s %s;' % ( 160 c.Append('%s %s;' % (
150 self._cpp_type_generator.GetType(prop, wrap_optional=True), 161 self._cpp_type_generator.GetType(prop, wrap_optional=True),
151 prop.unix_name)) 162 prop.unix_name))
152 c.Append() 163 c.Append()
153 return c 164 return c
154 165
155 def _GenerateType(self, type_): 166 def _GenerateType(self, type_):
156 """Generates a struct for a type. 167 """Generates a struct for a type.
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 ) 213 )
203 214
204 (c.Eblock() 215 (c.Eblock()
205 .Sblock(' private:') 216 .Sblock(' private:')
206 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') 217 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);')
207 .Eblock('};') 218 .Eblock('};')
208 ) 219 )
209 c.Substitute({'classname': classname}) 220 c.Substitute({'classname': classname})
210 return c 221 return c
211 222
223 def _GenerateEvent(self, event):
224 """Generates the namespaces for an event.
225 """
226 c = Code()
227 (c.Sblock('namespace %s {' % cpp_util.Classname(event.name))
228 .Concat(self._GenerateCreateCallbackArguments(event))
229 .Eblock('};')
230 )
231 return c
232
212 def _GenerateFunction(self, function): 233 def _GenerateFunction(self, function):
213 """Generates the structs for a function. 234 """Generates the namespaces and structs for a function.
214 """ 235 """
215 c = Code() 236 c = Code()
216 (c.Sblock('namespace %s {' % cpp_util.Classname(function.name)) 237 (c.Sblock('namespace %s {' % cpp_util.Classname(function.name))
217 .Concat(self._GenerateFunctionParams(function)) 238 .Concat(self._GenerateFunctionParams(function))
218 .Append() 239 .Append()
219 ) 240 )
220 if function.callback: 241 if function.callback:
221 (c.Concat(self._GenerateFunctionResult(function)) 242 (c.Concat(self._GenerateFunctionResult(function.callback))
222 .Append() 243 .Append()
223 ) 244 )
224 c.Eblock('};') 245 c.Eblock('};')
225 246
226 return c 247 return c
227 248
228 def _GenerateFunctionParams(self, function): 249 def _GenerateFunctionParams(self, function):
229 """Generates the struct for passing parameters into a function. 250 """Generates the struct for passing parameters from JSON to a function.
230 """ 251 """
231 c = Code() 252 c = Code()
232 253
233 if function.params: 254 if function.params:
234 (c.Sblock('struct Params {') 255 (c.Sblock('struct Params {')
235 .Concat(self._GeneratePropertyStructures(function.params)) 256 .Concat(self._GeneratePropertyStructures(function.params))
236 .Concat(self._GenerateFields(function.params)) 257 .Concat(self._GenerateFields(function.params))
237 .Append('~Params();') 258 .Append('~Params();')
238 .Append() 259 .Append()
239 .Append('static scoped_ptr<Params> Create(' 260 .Append('static scoped_ptr<Params> Create('
(...skipping 25 matching lines...) Expand all
265 self._cpp_type_generator.GetChoicesEnumType(prop), 286 self._cpp_type_generator.GetChoicesEnumType(prop),
266 prop, 287 prop,
267 [choice.type_.name for choice in prop.choices.values()])) 288 [choice.type_.name for choice in prop.choices.values()]))
268 c.Concat(self._GeneratePropertyStructures(prop.choices.values())) 289 c.Concat(self._GeneratePropertyStructures(prop.choices.values()))
269 elif prop.type_ == PropertyType.ENUM: 290 elif prop.type_ == PropertyType.ENUM:
270 enum_name = self._cpp_type_generator.GetType(prop) 291 enum_name = self._cpp_type_generator.GetType(prop)
271 c.Concat(self._GenerateEnumDeclaration( 292 c.Concat(self._GenerateEnumDeclaration(
272 enum_name, 293 enum_name,
273 prop, 294 prop,
274 prop.enum_values)) 295 prop.enum_values))
275 c.Append('static scoped_ptr<base::Value> CreateEnumValue(%s %s);' % 296 create_enum_value = ('scoped_ptr<base::Value> CreateEnumValue(%s %s);' %
276 (enum_name, prop.unix_name)) 297 (enum_name, prop.unix_name))
298 # If the property is from the UI then we're in a struct so this function
299 # should be static. If it's from the client, then we're just in a
300 # namespace so we can't have the static keyword.
301 if prop.from_json:
302 create_enum_value = 'static ' + create_enum_value
303 c.Append(create_enum_value)
277 return c 304 return c
278 305
279 def _GenerateFunctionResult(self, function): 306 def _GenerateCreateCallbackArguments(self, function):
280 """Generates functions for passing a function's result back. 307 """Generates functions for passing paramaters to a callback.
281 """ 308 """
282 c = Code() 309 c = Code()
310 params = function.params
311 c.Concat(self._GeneratePropertyStructures(params))
283 312
284 c.Sblock('namespace Result {') 313 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params)
285 params = function.callback.params 314 for param_list in param_lists:
286 if not params: 315 declaration_list = []
287 c.Append('base::Value* Create();') 316 for param in param_list:
288 else:
289 c.Concat(self._GeneratePropertyStructures(params))
290
291 # If there is a single parameter, this is straightforward. However, if
292 # the callback parameter is of 'choices', this generates a Create method
293 # for each choice. This works because only 1 choice can be returned at a
294 # time.
295 for param in self._cpp_type_generator.GetExpandedChoicesInParams(params):
296 if param.description: 317 if param.description:
297 c.Comment(param.description) 318 c.Comment(param.description)
298 if param.type_ == PropertyType.ANY: 319 declaration_list.append('const %s' % cpp_util.GetParameterDeclaration(
299 c.Comment("base::Value* Result::Create(base::Value*) not generated " 320 param, self._cpp_type_generator.GetType(param)))
300 "because it's redundant.") 321 c.Append('base::ListValue* Create(%s);' % ', '.join(declaration_list))
301 continue 322 return c
302 c.Append('base::Value* Create(const %s);' %
303 cpp_util.GetParameterDeclaration(
304 param, self._cpp_type_generator.GetType(param)))
305 c.Eblock('};')
306 323
324 def _GenerateFunctionResult(self, callback):
325 """Generates namespace for passing a function's result back.
326 """
327 c = Code()
328 (c.Sblock('namespace Result {')
329 .Concat(self._GenerateCreateCallbackArguments(callback))
330 .Eblock('};')
331 )
307 return c 332 return c
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698