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

Unified Diff: tools/json_schema_compiler/h_generator.py

Issue 9114036: Code generation for extensions api (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: more rework Created 8 years, 11 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
new file mode 100644
index 0000000000000000000000000000000000000000..a78c3e61757e1068222537e8ab303f2fe7480ed9
--- /dev/null
+++ b/tools/json_schema_compiler/h_generator.py
@@ -0,0 +1,204 @@
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+from model import PropertyType
+import code
+import cpp_type_manager
+import cpp_util
+
+class HGenerator(object):
+ """A .h generator for a namespace.
+ """
+ def __init__(self, namespace, model):
+ self.__cpp_type_manager = cpp_type_manager.CppTypeManager(namespace, model)
+ self.__namespace = namespace
+
+ def generate(self):
+ """Generates a code.Code object with the .h for a single namespace.
+ """
+ include_path = self.__namespace.parent_dir
+ filename = self.__namespace.filename
+ c = code.Code()
+ (c.append(cpp_util.CHROMIUM_LICENSE).append()
+ .append(cpp_util.GENERATED_FILE_MESSAGE % self.__namespace.parent_path)
+ .append()
+ )
+
+ ifndef_name = generate_ifndef_name(include_path, filename)
+ (c.append('#ifndef %s' % ifndef_name)
+ .append('#define %s' % ifndef_name)
+ .append('#pragma once')
+ .append()
+ .append('#include <string>')
+ .append('#include <vector>')
+ .append()
+ .append('#include "base/basictypes.h"')
+ .append('#include "base/memory/scoped_ptr.h"')
+ .append('#include "base/values.h"')
+ .append()
+ .append('using base::Value;')
+ .append('using base::DictionaryValue;')
+ .append('using base::ListValue;')
+ .append()
+ )
+
+ includes = self.__cpp_type_manager.generate_cpp_includes()
+ if includes.render():
not at google - send to devlin 2012/01/17 05:42:32 seems weird to render() this here then not use the
calamity 2012/01/18 05:43:08 Done.
+ (c.concat(includes)
+ .append()
+ )
+
+ (c.append('namespace %s {' % self.__namespace.root_namespace)
+ .append('namespace %s {' % filename)
+ .append()
+ .append('//')
+ .append('// Types')
+ .append('//')
+ .append()
+ )
+ for tipe in self.__namespace.types.values():
+ (c.concat(self.generate_type(tipe))
+ .append()
+ )
+ (c.append('//')
+ .append('// Functions')
+ .append('//')
+ .append()
+ )
+ for function in self.__namespace.functions.values():
+ (c.concat(self.generate_function(function))
+ .append()
+ )
+ (c.append()
+ .append()
+ .append('} // namespace %s' % self.__namespace.root_namespace)
+ .append('} // namespace %s' % filename)
+ .append()
+ .append('#endif // %s' % ifndef_name)
+ )
+ return c
+
+ def generate_type(self, tipe, serializable=True):
+ """Generates a struct for a type.
+ """
+ classname = cpp_util.cpp_name(tipe.name)
+ c = code.Code()
+ if tipe.description:
+ c.comment(tipe.description)
+ (c.sblock('struct %(classname)s {')
+ .append('~%(classname)s();')
+ .append('%(classname)s();')
+ .append()
+ )
not at google - send to devlin 2012/01/17 05:42:32 blank line here perhaps?
calamity 2012/01/18 05:43:08 Done.
+ for prop in tipe.properties.values():
+ if prop.description:
+ c.comment(prop.description)
+ if prop.optional:
+ c.append('scoped_ptr<%s> %s;' %
+ (self.__cpp_type_manager.get_type(prop, pad_for_generics=True),
+ prop.name))
+ else:
+ c.append('%s %s;' % (self.__cpp_type_manager.get_type(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()
+ )
+ if serializable:
+ (c.comment('Returns a new DictionaryValue representing the'
+ ' serialized form of this %(classname)s object.')
+ .append('DictionaryValue* ToValue() const;')
+ )
+ (c.eblock()
not at google - send to devlin 2012/01/17 05:42:32 blank line here perhaps?
calamity 2012/01/18 05:43:08 Done.
+ .sblock(' private:')
+ .append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);')
+ .eblock('};')
+ )
+ c.substitute({'classname': classname})
+ return c
+
+ def generate_function(self, function):
+ """Generates the structs for a function.
+ """
+ c = code.Code()
+ (c.sblock('struct %s {' % cpp_util.cpp_name(function.name))
+ .concat(self.generate_function_params(function))
+ .append()
+ .concat(self.generate_function_result(function))
+ .append()
+ .eblock('};')
+ )
+ return c
+
+ def generate_function_params(self, function):
+ """Generates the struct for passing parameters into a function.
+ """
+ c = code.Code()
+
+ c.sblock('struct Params {')
+ for param in function.params:
+ if param.description:
+ c.comment(param.description)
+ if param.type == PropertyType.OBJECT:
+ c.concat(self.generate_type(param, serializable=False))
+ c.append()
+ for param in function.params:
+ c.append('%s %s;' % (self.__cpp_type_manager.get_type(param), param.name))
+
+ if function.params:
+ (c.append()
+ .append('Params();')
+ .append('~Params();')
+ .append()
+ .append('static bool Populate(const ListValue& args, Params* out);')
+ .eblock()
+ .sblock(' private:')
+ .append('DISALLOW_COPY_AND_ASSIGN(Params);')
+ )
+
+ c.eblock('};')
+
+ return c
+
+ def generate_function_result(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 % {'type': self.__cpp_type_manager.get_type(param),
+ 'name': param.name}
+ (c.append('static Value* Create(%s);' % arg)
+ .eblock()
+ .sblock(' private:')
+ .append('Result() {};')
+ .append('DISALLOW_COPY_AND_ASSIGN(Result);')
+ .eblock('};')
+ )
+
+ return c
+
+def generate_ifndef_name(path, filename):
+ """Formats a path and filename as a #define name.
+
+ e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__.
+ """
+ return ('%s/%s_H__' % (path, filename)).upper().replace('/', '_')
+

Powered by Google App Engine
This is Rietveld 408576698