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 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 if type_.description: | 223 if type_.description: |
222 c.Comment(type_.description) | 224 c.Comment(type_.description) |
223 (c.Sblock('struct %(classname)s {') | 225 (c.Sblock('struct %(classname)s {') |
224 .Append('%(classname)s();') | 226 .Append('%(classname)s();') |
225 .Append('~%(classname)s();') | 227 .Append('~%(classname)s();') |
226 ) | 228 ) |
227 if type_.origin.from_json: | 229 if type_.origin.from_json: |
228 (c.Append() | 230 (c.Append() |
229 .Comment('Populates a %s object from a base::Value. Returns' | 231 .Comment('Populates a %s object from a base::Value. Returns' |
230 ' whether |out| was successfully populated.' % classname) | 232 ' whether |out| was successfully populated.' % classname) |
231 .Append('static bool Populate(const base::Value& value, ' | 233 .Append('static bool Populate(%s);' % self._GenerateParams( |
232 '%(classname)s* out);') | 234 ('const base::Value& value', '%s* out' % classname))) |
233 ) | 235 ) |
234 if is_toplevel: | 236 if is_toplevel: |
235 (c.Append() | 237 (c.Append() |
236 .Comment('Creates a %s object from a base::Value, or NULL on ' | 238 .Comment('Creates a %s object from a base::Value, or NULL on ' |
237 'failure.' % classname) | 239 'failure.' % classname) |
238 .Append('static scoped_ptr<%(classname)s> ' | 240 .Append('static scoped_ptr<%s> FromValue(%s);' % ( |
239 'FromValue(const base::Value& value);') | 241 classname, self._GenerateParams(('const base::Value& value',)))) |
240 ) | 242 ) |
241 if type_.origin.from_client: | 243 if type_.origin.from_client: |
242 value_type = ('base::Value' | 244 value_type = ('base::Value' |
243 if type_.property_type is PropertyType.CHOICES else | 245 if type_.property_type is PropertyType.CHOICES else |
244 'base::DictionaryValue') | 246 'base::DictionaryValue') |
245 (c.Append() | 247 (c.Append() |
246 .Comment('Returns a new %s representing the serialized form of this ' | 248 .Comment('Returns a new %s representing the serialized form of this ' |
247 '%s object.' % (value_type, classname)) | 249 '%s object.' % (value_type, classname)) |
248 .Append('scoped_ptr<%s> ToValue() const;' % value_type) | 250 .Append('scoped_ptr<%s> ToValue() const;' % value_type) |
249 ) | 251 ) |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 return c | 318 return c |
317 | 319 |
318 def _GenerateFunctionParams(self, function): | 320 def _GenerateFunctionParams(self, function): |
319 """Generates the struct for passing parameters from JSON to a function. | 321 """Generates the struct for passing parameters from JSON to a function. |
320 """ | 322 """ |
321 if not function.params: | 323 if not function.params: |
322 return Code() | 324 return Code() |
323 | 325 |
324 c = Code() | 326 c = Code() |
325 (c.Sblock('struct Params {') | 327 (c.Sblock('struct Params {') |
326 .Append('static scoped_ptr<Params> Create(const base::ListValue& args);') | 328 .Append('static scoped_ptr<Params> Create(%s);' % self._GenerateParams( |
| 329 ('const base::ListValue& args',))) |
327 .Append('~Params();') | 330 .Append('~Params();') |
328 .Append() | 331 .Append() |
329 .Cblock(self._GenerateTypes(p.type_ for p in function.params)) | 332 .Cblock(self._GenerateTypes(p.type_ for p in function.params)) |
330 .Cblock(self._GenerateFields(function.params)) | 333 .Cblock(self._GenerateFields(function.params)) |
331 .Eblock() | 334 .Eblock() |
332 .Append() | 335 .Append() |
333 .Sblock(' private:') | 336 .Sblock(' private:') |
334 .Append('Params();') | 337 .Append('Params();') |
335 .Append() | 338 .Append() |
336 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') | 339 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 def _GenerateFunctionResults(self, callback): | 381 def _GenerateFunctionResults(self, callback): |
379 """Generates namespace for passing a function's result back. | 382 """Generates namespace for passing a function's result back. |
380 """ | 383 """ |
381 c = Code() | 384 c = Code() |
382 (c.Append('namespace Results {') | 385 (c.Append('namespace Results {') |
383 .Append() | 386 .Append() |
384 .Concat(self._GenerateCreateCallbackArguments(callback)) | 387 .Concat(self._GenerateCreateCallbackArguments(callback)) |
385 .Append('} // namespace Results') | 388 .Append('} // namespace Results') |
386 ) | 389 ) |
387 return c | 390 return c |
| 391 |
| 392 def _GenerateParams(self, params): |
| 393 """Builds the parameter list for a function, given an array of parameters. |
| 394 """ |
| 395 if self._generate_error_messages: |
| 396 params += ('std::string* error = NULL',) |
| 397 return ', '.join(str(p) for p in params) |
OLD | NEW |