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 from code import Code | 5 from code import Code |
6 from model import PropertyType, Type | 6 from model import PropertyType, Type |
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): |
11 def __init__(self, type_generator, cpp_namespace): | 11 def __init__(self, type_generator, cpp_namespace): |
12 self._type_generator = type_generator | 12 self._type_generator = type_generator |
13 self._cpp_namespace = cpp_namespace | 13 self._cpp_namespace = cpp_namespace |
14 | 14 |
15 def Generate(self, namespace): | 15 def Generate(self, namespace): |
16 return _Generator(namespace, | 16 return _Generator(namespace, |
17 self._type_generator, | 17 self._type_generator, |
18 self._cpp_namespace).Generate() | 18 self._cpp_namespace).Generate() |
19 | 19 |
20 class _Generator(object): | 20 class _Generator(object): |
21 """A .h generator for a namespace. | 21 """A .h generator for a namespace. |
22 """ | 22 """ |
23 def __init__(self, namespace, cpp_type_generator, cpp_namespace): | 23 def __init__(self, namespace, cpp_type_generator, cpp_namespace): |
24 self._namespace = namespace | 24 self._namespace = namespace |
25 self._type_helper = cpp_type_generator | 25 self._type_helper = cpp_type_generator |
26 self._cpp_namespace = cpp_namespace | 26 self._cpp_namespace = cpp_namespace |
27 self._target_namespace = ( | 27 self._target_namespace = ( |
28 self._type_helper.GetCppNamespaceName(self._namespace)) | 28 self._type_helper.GetCppNamespaceName(self._namespace)) |
29 self._generate_error_messages = namespace.compiler_options.get( | |
30 'generate_error_messages', False) | |
29 | 31 |
30 def Generate(self): | 32 def Generate(self): |
31 """Generates a Code object with the .h for a single namespace. | 33 """Generates a Code object with the .h for a single namespace. |
32 """ | 34 """ |
33 c = Code() | 35 c = Code() |
34 (c.Append(cpp_util.CHROMIUM_LICENSE) | 36 (c.Append(cpp_util.CHROMIUM_LICENSE) |
35 .Append() | 37 .Append() |
36 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) | 38 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) |
37 .Append() | 39 .Append() |
38 ) | 40 ) |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 if type_.description: | 222 if type_.description: |
221 c.Comment(type_.description) | 223 c.Comment(type_.description) |
222 (c.Sblock('struct %(classname)s {') | 224 (c.Sblock('struct %(classname)s {') |
223 .Append('%(classname)s();') | 225 .Append('%(classname)s();') |
224 .Append('~%(classname)s();') | 226 .Append('~%(classname)s();') |
225 ) | 227 ) |
226 if type_.origin.from_json: | 228 if type_.origin.from_json: |
227 (c.Append() | 229 (c.Append() |
228 .Comment('Populates a %s object from a base::Value. Returns' | 230 .Comment('Populates a %s object from a base::Value. Returns' |
229 ' whether |out| was successfully populated.' % classname) | 231 ' whether |out| was successfully populated.' % classname) |
230 .Append('static bool Populate(const base::Value& value, ' | 232 .Append('static bool Populate(%s);' % self._GenerateParams( |
231 '%(classname)s* out);') | 233 ('const base::Value& value', '%s* out' % classname))) |
232 ) | 234 ) |
233 if is_toplevel: | 235 if is_toplevel: |
234 (c.Append() | 236 (c.Append() |
235 .Comment('Creates a %s object from a base::Value, or NULL on ' | 237 .Comment('Creates a %s object from a base::Value, or NULL on ' |
236 'failure.' % classname) | 238 'failure.' % classname) |
237 .Append('static scoped_ptr<%(classname)s> ' | 239 .Append('static scoped_ptr<%s> FromValue(%s);' % ( |
238 'FromValue(const base::Value& value);') | 240 classname, self._GenerateParams(('const base::Value& value',)))) |
239 ) | 241 ) |
240 if type_.origin.from_client: | 242 if type_.origin.from_client: |
241 (c.Append() | 243 (c.Append() |
242 .Comment('Returns a new base::DictionaryValue representing the' | 244 .Comment('Returns a new base::DictionaryValue representing the' |
243 ' serialized form of this %s object.' % classname) | 245 ' serialized form of this %s object.' % classname) |
244 .Append('scoped_ptr<base::DictionaryValue> ToValue() const;') | 246 .Append('scoped_ptr<base::DictionaryValue> ToValue() const;') |
245 ) | 247 ) |
246 properties = type_.properties.values() | 248 properties = type_.properties.values() |
247 (c.Append() | 249 (c.Append() |
248 .Cblock(self._GenerateTypes(p.type_ for p in properties)) | 250 .Cblock(self._GenerateTypes(p.type_ for p in properties)) |
(...skipping 22 matching lines...) Expand all Loading... | |
271 # Choices are modelled with optional fields for each choice. Exactly one | 273 # Choices are modelled with optional fields for each choice. Exactly one |
272 # field of the choice is guaranteed to be set by the compiler. | 274 # field of the choice is guaranteed to be set by the compiler. |
273 (c.Sblock('struct %(classname)s {') | 275 (c.Sblock('struct %(classname)s {') |
274 .Append('%(classname)s();') | 276 .Append('%(classname)s();') |
275 .Append('~%(classname)s();') | 277 .Append('~%(classname)s();') |
276 .Append()) | 278 .Append()) |
277 c.Cblock(self._GenerateTypes(type_.choices)) | 279 c.Cblock(self._GenerateTypes(type_.choices)) |
278 if type_.origin.from_json: | 280 if type_.origin.from_json: |
279 (c.Comment('Populates a %s object from a base::Value. Returns' | 281 (c.Comment('Populates a %s object from a base::Value. Returns' |
280 ' whether |out| was successfully populated.' % classname) | 282 ' whether |out| was successfully populated.' % classname) |
281 .Append('static bool Populate(const base::Value& value, ' | 283 .Append('static bool Populate(%s);' % self._GenerateParams( |
282 '%(classname)s* out);') | 284 ('const base::Value& value', '%s* out' % classname))) |
283 .Append() | 285 .Append() |
284 ) | 286 ) |
285 if type_.origin.from_client: | 287 if type_.origin.from_client: |
286 (c.Comment('Returns a new base::Value representing the' | 288 (c.Comment('Returns a new base::Value representing the' |
287 ' serialized form of this %s object.' % classname) | 289 ' serialized form of this %s object.' % classname) |
288 .Append('scoped_ptr<base::Value> ToValue() const;') | 290 .Append('scoped_ptr<base::Value> ToValue() const;') |
289 .Append() | 291 .Append() |
290 ) | 292 ) |
291 c.Append('// Choices:') | 293 c.Append('// Choices:') |
292 for choice_type in type_.choices: | 294 for choice_type in type_.choices: |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
331 return c | 333 return c |
332 | 334 |
333 def _GenerateFunctionParams(self, function): | 335 def _GenerateFunctionParams(self, function): |
334 """Generates the struct for passing parameters from JSON to a function. | 336 """Generates the struct for passing parameters from JSON to a function. |
335 """ | 337 """ |
336 if not function.params: | 338 if not function.params: |
337 return Code() | 339 return Code() |
338 | 340 |
339 c = Code() | 341 c = Code() |
340 (c.Sblock('struct Params {') | 342 (c.Sblock('struct Params {') |
341 .Append('static scoped_ptr<Params> Create(const base::ListValue& args);') | 343 .Append('static scoped_ptr<Params> Create(%s);' % self._GenerateParams( |
344 ('const base::ListValue& args',))) | |
342 .Append('~Params();') | 345 .Append('~Params();') |
343 .Append() | 346 .Append() |
344 .Cblock(self._GenerateTypes(p.type_ for p in function.params)) | 347 .Cblock(self._GenerateTypes(p.type_ for p in function.params)) |
345 .Cblock(self._GenerateFields(function.params)) | 348 .Cblock(self._GenerateFields(function.params)) |
346 .Eblock() | 349 .Eblock() |
347 .Append() | 350 .Append() |
348 .Sblock(' private:') | 351 .Sblock(' private:') |
349 .Append('Params();') | 352 .Append('Params();') |
350 .Append() | 353 .Append() |
351 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') | 354 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
384 def _GenerateFunctionResults(self, callback): | 387 def _GenerateFunctionResults(self, callback): |
385 """Generates namespace for passing a function's result back. | 388 """Generates namespace for passing a function's result back. |
386 """ | 389 """ |
387 c = Code() | 390 c = Code() |
388 (c.Append('namespace Results {') | 391 (c.Append('namespace Results {') |
389 .Append() | 392 .Append() |
390 .Concat(self._GenerateCreateCallbackArguments(callback)) | 393 .Concat(self._GenerateCreateCallbackArguments(callback)) |
391 .Append('} // namespace Results') | 394 .Append('} // namespace Results') |
392 ) | 395 ) |
393 return c | 396 return c |
397 | |
398 def _GenerateParams(self, params): | |
399 """Builds the parameter list for a function, given an array of parameters. | |
400 """ | |
401 if self._generate_error_messages: | |
402 params += ('std::string* error = NULL',) | |
403 return ', '.join(str(p) for p in params) | |
not at google - send to devlin
2013/06/19 17:02:08
see list thing again
| |
OLD | NEW |