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

Unified Diff: tools/json_schema_compiler/h_generator.py

Issue 9309044: Supporting more APIs with json_schema_compiler (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: a few comments, fixes Created 8 years, 10 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 side-by-side diff with in-line comments
Download patch
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..04e340eb990366da13a471bfa95eae82c4defa6e 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,8 +74,8 @@ 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()
@@ -82,7 +85,7 @@ class HGenerator(object):
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)
@@ -95,13 +98,9 @@ class HGenerator(object):
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('%s %s;' % (
+ self._cpp_type_generator.GetType(prop, wrap_optional=True),
+ prop.unix_name))
c.Append()
(c.Comment('Populates a %(classname)s object from a Value. Returns'
@@ -128,7 +127,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))
@@ -152,7 +151,8 @@ class HGenerator(object):
c.Append()
for param in function.params:
c.Append('%s %s;' %
- (self._cpp_type_generator.GetType(param), param.name))
+ (self._cpp_type_generator.GetType(param, wrap_optional=True),
+ param.unix_name))
(c.Append()
.Append('~Params();')
@@ -163,42 +163,40 @@ 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:
- arg = 'const %(type)s %(name)s'
- arg = arg % {
+ params = function.callback.params
+ if not params:
+ c.Append('static Value* Create();')
+ else:
+ for param in params:
not at google - send to devlin 2012/02/06 04:19:17 same; explain why generating a new Create() functi
calamity 2012/02/06 11:51:18 Done.
+ sub = {
'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('};')
+ 'name': param.unix_name,
+ }
+ arg = 'const %(type)s %(name)s'
+ if param.type_ == PropertyType.REF:
+ arg = 'const %(type)s& %(name)s'
+ arg = arg % sub
not at google - send to devlin 2012/02/06 04:19:17 same deal, arg %= { 'type': ... 'name': ... }
calamity 2012/02/06 11:51:18 Done.
+ 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 +206,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('/', '_'))

Powered by Google App Engine
This is Rietveld 408576698