| 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._error_parameter = '' |
| 30 if self._namespace.generate_error_messages: |
| 31 self._error_parameter = ', std::string* error = NULL' |
| 29 | 32 |
| 30 def Generate(self): | 33 def Generate(self): |
| 31 """Generates a Code object with the .h for a single namespace. | 34 """Generates a Code object with the .h for a single namespace. |
| 32 """ | 35 """ |
| 33 c = Code() | 36 c = Code() |
| 34 (c.Append(cpp_util.CHROMIUM_LICENSE) | 37 (c.Append(cpp_util.CHROMIUM_LICENSE) |
| 35 .Append() | 38 .Append() |
| 36 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) | 39 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) |
| 37 .Append() | 40 .Append() |
| 38 ) | 41 ) |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 c.Comment(type_.description) | 224 c.Comment(type_.description) |
| 222 (c.Sblock('struct %(classname)s {') | 225 (c.Sblock('struct %(classname)s {') |
| 223 .Append('%(classname)s();') | 226 .Append('%(classname)s();') |
| 224 .Append('~%(classname)s();') | 227 .Append('~%(classname)s();') |
| 225 ) | 228 ) |
| 226 if type_.origin.from_json: | 229 if type_.origin.from_json: |
| 227 (c.Append() | 230 (c.Append() |
| 228 .Comment('Populates a %s object from a base::Value. Returns' | 231 .Comment('Populates a %s object from a base::Value. Returns' |
| 229 ' whether |out| was successfully populated.' % classname) | 232 ' whether |out| was successfully populated.' % classname) |
| 230 .Append('static bool Populate(const base::Value& value, ' | 233 .Append('static bool Populate(const base::Value& value, ' |
| 231 '%(classname)s* out);') | 234 '%s* out%s);' % (classname, self._error_parameter)) |
| 232 ) | 235 ) |
| 233 if is_toplevel: | 236 if is_toplevel: |
| 234 (c.Append() | 237 (c.Append() |
| 235 .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 ' |
| 236 'failure.' % classname) | 239 'failure.' % classname) |
| 237 .Append('static scoped_ptr<%(classname)s> ' | 240 .Append('static scoped_ptr<%s> FromValue(' |
| 238 'FromValue(const base::Value& value);') | 241 'const base::Value& value%s);' % |
| 242 (classname, self._error_parameter)) |
| 239 ) | 243 ) |
| 240 if type_.origin.from_client: | 244 if type_.origin.from_client: |
| 241 (c.Append() | 245 (c.Append() |
| 242 .Comment('Returns a new base::DictionaryValue representing the' | 246 .Comment('Returns a new base::DictionaryValue representing the' |
| 243 ' serialized form of this %s object.' % classname) | 247 ' serialized form of this %s object.' % classname) |
| 244 .Append('scoped_ptr<base::DictionaryValue> ToValue() const;') | 248 .Append('scoped_ptr<base::DictionaryValue> ToValue() const;') |
| 245 ) | 249 ) |
| 246 properties = type_.properties.values() | 250 properties = type_.properties.values() |
| 247 (c.Append() | 251 (c.Append() |
| 248 .Cblock(self._GenerateTypes(p.type_ for p in properties)) | 252 .Cblock(self._GenerateTypes(p.type_ for p in properties)) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 272 # field of the choice is guaranteed to be set by the compiler. | 276 # field of the choice is guaranteed to be set by the compiler. |
| 273 (c.Sblock('struct %(classname)s {') | 277 (c.Sblock('struct %(classname)s {') |
| 274 .Append('%(classname)s();') | 278 .Append('%(classname)s();') |
| 275 .Append('~%(classname)s();') | 279 .Append('~%(classname)s();') |
| 276 .Append()) | 280 .Append()) |
| 277 c.Cblock(self._GenerateTypes(type_.choices)) | 281 c.Cblock(self._GenerateTypes(type_.choices)) |
| 278 if type_.origin.from_json: | 282 if type_.origin.from_json: |
| 279 (c.Comment('Populates a %s object from a base::Value. Returns' | 283 (c.Comment('Populates a %s object from a base::Value. Returns' |
| 280 ' whether |out| was successfully populated.' % classname) | 284 ' whether |out| was successfully populated.' % classname) |
| 281 .Append('static bool Populate(const base::Value& value, ' | 285 .Append('static bool Populate(const base::Value& value, ' |
| 282 '%(classname)s* out);') | 286 '%s* out%s);' % (classname, self._error_parameter)) |
| 283 .Append() | 287 .Append() |
| 284 ) | 288 ) |
| 285 if type_.origin.from_client: | 289 if type_.origin.from_client: |
| 286 (c.Comment('Returns a new base::Value representing the' | 290 (c.Comment('Returns a new base::Value representing the' |
| 287 ' serialized form of this %s object.' % classname) | 291 ' serialized form of this %s object.' % classname) |
| 288 .Append('scoped_ptr<base::Value> ToValue() const;') | 292 .Append('scoped_ptr<base::Value> ToValue() const;') |
| 289 .Append() | 293 .Append() |
| 290 ) | 294 ) |
| 291 c.Append('// Choices:') | 295 c.Append('// Choices:') |
| 292 for choice_type in type_.choices: | 296 for choice_type in type_.choices: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 return c | 335 return c |
| 332 | 336 |
| 333 def _GenerateFunctionParams(self, function): | 337 def _GenerateFunctionParams(self, function): |
| 334 """Generates the struct for passing parameters from JSON to a function. | 338 """Generates the struct for passing parameters from JSON to a function. |
| 335 """ | 339 """ |
| 336 if not function.params: | 340 if not function.params: |
| 337 return Code() | 341 return Code() |
| 338 | 342 |
| 339 c = Code() | 343 c = Code() |
| 340 (c.Sblock('struct Params {') | 344 (c.Sblock('struct Params {') |
| 341 .Append('static scoped_ptr<Params> Create(const base::ListValue& args);') | 345 .Append('static scoped_ptr<Params> Create(' |
| 346 'const base::ListValue& args%s);' % self._error_parameter) |
| 342 .Append('~Params();') | 347 .Append('~Params();') |
| 343 .Append() | 348 .Append() |
| 344 .Cblock(self._GenerateTypes(p.type_ for p in function.params)) | 349 .Cblock(self._GenerateTypes(p.type_ for p in function.params)) |
| 345 .Cblock(self._GenerateFields(function.params)) | 350 .Cblock(self._GenerateFields(function.params)) |
| 346 .Eblock() | 351 .Eblock() |
| 347 .Append() | 352 .Append() |
| 348 .Sblock(' private:') | 353 .Sblock(' private:') |
| 349 .Append('Params();') | 354 .Append('Params();') |
| 350 .Append() | 355 .Append() |
| 351 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') | 356 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 def _GenerateFunctionResults(self, callback): | 389 def _GenerateFunctionResults(self, callback): |
| 385 """Generates namespace for passing a function's result back. | 390 """Generates namespace for passing a function's result back. |
| 386 """ | 391 """ |
| 387 c = Code() | 392 c = Code() |
| 388 (c.Append('namespace Results {') | 393 (c.Append('namespace Results {') |
| 389 .Append() | 394 .Append() |
| 390 .Concat(self._GenerateCreateCallbackArguments(callback)) | 395 .Concat(self._GenerateCreateCallbackArguments(callback)) |
| 391 .Append('} // namespace Results') | 396 .Append('} // namespace Results') |
| 392 ) | 397 ) |
| 393 return c | 398 return c |
| OLD | NEW |