Index: tools/json_schema_compiler/h_generator.py |
diff --git a/tools/json_schema_compiler/h_generator.py b/tools/json_schema_compiler/h_generator.py |
index 619763b8b1676dbc8a97159ac72ad6c1c631f1cc..8ec299498bae718dd7c7a37a10d4bba0f5ff87ef 100644 |
--- a/tools/json_schema_compiler/h_generator.py |
+++ b/tools/json_schema_compiler/h_generator.py |
@@ -5,7 +5,6 @@ |
from model import PropertyType |
import code |
import cpp_util |
-import os |
class HGenerator(object): |
"""A .h generator for a namespace. |
@@ -35,6 +34,7 @@ class HGenerator(object): |
.Append('#include <vector>') |
.Append() |
.Append('#include "base/basictypes.h"') |
+ .Append('#include "base/memory/linked_ptr.h"') |
.Append('#include "base/memory/scoped_ptr.h"') |
.Append('#include "base/values.h"') |
.Append() |
@@ -44,13 +44,16 @@ class HGenerator(object): |
.Append() |
) |
- includes = self._cpp_type_generator.GenerateCppIncludes() |
- if not includes.IsEmpty(): |
- (c.Concat(includes) |
+ c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) |
+ forward_declarations = ( |
+ self._cpp_type_generator.GenerateForwardDeclarations()) |
+ if not forward_declarations.IsEmpty(): |
+ (c.Append() |
+ .Concat(forward_declarations) |
.Append() |
) |
- (c.Concat(self._cpp_type_generator.GetCppNamespaceStart()) |
+ (c.Concat(self._cpp_type_generator.GetNamespaceStart()) |
.Append() |
.Append('//') |
.Append('// Types') |
@@ -71,18 +74,45 @@ class HGenerator(object): |
.Append() |
) |
(c.Append() |
- .Append() |
- .Concat(self._cpp_type_generator.GetCppNamespaceEnd()) |
+ .Concat(self._cpp_type_generator.GetNamespaceEnd()) |
+ .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) |
.Append() |
.Append('#endif // %s' % ifndef_name) |
.Append() |
) |
return c |
+ def _GenerateFields(self, props): |
+ """Generates the field declarations when declaring a type. |
+ """ |
+ c = code.Code() |
+ for prop in self._cpp_type_generator.ExpandedChoicesInParams(props): |
+ if prop.description: |
+ c.Comment(prop.description) |
+ c.Append('%s %s;' % ( |
+ self._cpp_type_generator.GetType(prop, wrap_optional=True), |
+ prop.unix_name)) |
+ c.Append() |
+ for prop in [x for x in props if x.type_ == PropertyType.CHOICES]: |
not at google - send to devlin
2012/02/08 05:02:08
I think this needs a comment, like
# Generate the
calamity
2012/02/08 07:01:18
Done.
|
+ enum_name = self._cpp_type_generator.GetChoicesEnumName(prop) |
+ c.Sblock('enum %s {' % enum_name) |
+ enums = [] |
+ enums.append(self._cpp_type_generator.GetChoiceEnum(prop, None)) |
+ for choice in prop.choices.values(): |
+ enums.append( |
+ self._cpp_type_generator.GetChoiceEnum(prop, choice.type_)) |
+ for enum in ', '.join(enums).split(): |
+ c.Append(enum) |
not at google - send to devlin
2012/02/08 05:02:08
I don't understand why you've done it this way rat
calamity
2012/02/08 07:01:18
Hah. Didn't know that. My default assumption is th
|
+ (c.Eblock('};') |
+ .Append() |
+ .Append('%s %s_type;' % (enum_name, prop.unix_name)) |
+ ) |
+ return c |
+ |
def _GenerateType(self, type_, serializable=True): |
"""Generates a struct for a type. |
""" |
- classname = cpp_util.CppName(type_.name) |
+ classname = cpp_util.Classname(type_.name) |
c = code.Code() |
if type_.description: |
c.Comment(type_.description) |
@@ -90,24 +120,11 @@ class HGenerator(object): |
.Append('~%(classname)s();') |
.Append('%(classname)s();') |
.Append() |
- ) |
- |
- for prop in type_.properties.values(): |
- if prop.description: |
- c.Comment(prop.description) |
- if prop.optional: |
- c.Append('scoped_ptr<%s> %s;' % |
- (self._cpp_type_generator.GetType(prop, pad_for_generics=True), |
- prop.name)) |
- else: |
- c.Append('%s %s;' % |
- (self._cpp_type_generator.GetType(prop), prop.name)) |
- c.Append() |
- |
- (c.Comment('Populates a %(classname)s object from a Value. Returns' |
- ' whether |out| was successfully populated.') |
- .Append('static bool Populate(const Value& value, %(classname)s* out);') |
- .Append() |
+ .Concat(self._GenerateFields(type_.properties.values())) |
+ .Comment('Populates a %(classname)s object from a Value. Returns' |
+ ' whether |out| was successfully populated.') |
+ .Append('static bool Populate(const Value& value, %(classname)s* out);') |
+ .Append() |
) |
if serializable: |
@@ -128,7 +145,7 @@ class HGenerator(object): |
"""Generates the structs for a function. |
""" |
c = code.Code() |
- (c.Sblock('namespace %s {' % cpp_util.CppName(function.name)) |
+ (c.Sblock('namespace %s {' % cpp_util.Classname(function.name)) |
.Concat(self._GenerateFunctionParams(function)) |
.Append() |
.Concat(self._GenerateFunctionResult(function)) |
@@ -150,11 +167,8 @@ class HGenerator(object): |
if param.type_ == PropertyType.OBJECT: |
c.Concat(self._GenerateType(param, serializable=False)) |
c.Append() |
- for param in function.params: |
- c.Append('%s %s;' % |
- (self._cpp_type_generator.GetType(param), param.name)) |
- |
- (c.Append() |
+ (c.Concat(self._GenerateFields(function.params)) |
+ .Append() |
.Append('~Params();') |
.Append() |
.Append('static scoped_ptr<Params> Create(const ListValue& args);') |
@@ -163,42 +177,43 @@ class HGenerator(object): |
.Append('Params();') |
.Append() |
.Append('DISALLOW_COPY_AND_ASSIGN(Params);') |
+ .Eblock('};') |
) |
- c.Eblock('};') |
- |
return c |
def _GenerateFunctionResult(self, function): |
"""Generates the struct for passing a function's result back. |
""" |
- # TODO(calamity): Handle returning an object |
c = code.Code() |
- param = function.callback.param |
- # TODO(calamity): Put this description comment in less stupid place |
- if param.description: |
- c.Comment(param.description) |
(c.Append('class Result {') |
.Sblock(' public:') |
) |
- arg = '' |
- # TODO(calamity): handle object |
- if param: |
- if param.type_ == PropertyType.REF: |
- arg = 'const %(type)s& %(name)s' |
- else: |
+ params = function.callback.params |
+ if not params: |
+ c.Append('static Value* Create();') |
+ else: |
+ # If there is a single parameter, this is straightforward. However, if |
+ # the callback parameter is of 'choices', this generates a Create method |
+ # for each choice. This works because only 1 choice can be returned at a |
+ # time. |
+ for param in self._cpp_type_generator.ExpandedChoicesInParams(params): |
arg = 'const %(type)s %(name)s' |
- arg = arg % { |
- 'type': self._cpp_type_generator.GetType(param), |
- 'name': param.name |
- } |
- (c.Append('static Value* Create(%s);' % arg) |
- .Eblock() |
- .Sblock(' private:') |
- .Append('Result() {};') |
- .Append('DISALLOW_COPY_AND_ASSIGN(Result);') |
- .Eblock('};') |
+ if param.type_ == PropertyType.REF: |
+ arg = 'const %(type)s& %(name)s' |
+ arg %= { |
+ 'type': self._cpp_type_generator.GetType(param, wrap_optional=True), |
+ 'name': param.unix_name, |
+ } |
+ if param.description: |
+ c.Comment(param.description) |
+ c.Append('static Value* Create(%s);' % arg) |
+ (c.Eblock() |
+ .Sblock(' private:') |
+ .Append('Result() {};') |
+ .Append('DISALLOW_COPY_AND_ASSIGN(Result);') |
+ .Eblock('};') |
) |
return c |
@@ -208,7 +223,7 @@ class HGenerator(object): |
e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. |
""" |
- return (('%s_%s_H__' % |
+ return (('%s/%s_H__' % |
(self._namespace.source_file_dir, self._target_namespace)) |
- .upper().replace(os.sep, '_')) |
+ .upper().replace('/', '_')) |