Chromium Code Reviews| 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..2bc0369f8f1e817da9c7efa3ff2432c0411c73ce |
| --- /dev/null |
| +++ b/tools/json_schema_compiler/h_generator.py |
| @@ -0,0 +1,212 @@ |
| +# 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_generator |
| +import cpp_util |
| + |
| +class HGenerator(object): |
| + """A .h generator for a namespace. |
| + """ |
| + def __init__(self, namespace, model, root_namespace): |
| + self.__cpp_type_generator = cpp_type_generator.CppTypeGenerator( |
| + namespace, model) |
| + self.__namespace = namespace |
| + self.__root_namespace = root_namespace |
| + |
| + def generate(self): |
| + """Generates a code.Code object with the .h for a single namespace. |
| + """ |
| + target_namespace = self.__namespace.target_namespace |
| + c = code.Code() |
| + (c.append(cpp_util.CHROMIUM_LICENSE).append() |
| + .append(cpp_util.GENERATED_FILE_MESSAGE % self.__namespace.source_file) |
| + .append() |
| + ) |
| + |
| + ifndef_name = self.__generate_ifndef_name() |
| + (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_generator.generate_cpp_includes() |
| + if includes.is_empty(): |
|
Yoyo Zhou
2012/01/19 02:19:40
This looks incorrect.
calamity
2012/01/20 01:10:25
Done.
|
| + (c.concat(includes) |
| + .append() |
| + ) |
| + |
| + (c.append('namespace %s {' % self.__root_namespace) |
| + .append('namespace %s {' % target_namespace) |
| + .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.__root_namespace) |
|
Yoyo Zhou
2012/01/19 02:19:40
2 spaces before inline comments
calamity
2012/01/20 01:10:25
Done.
|
| + .append('} // namespace %s' % target_namespace) |
| + .append() |
| + .append('#endif // %s' % ifndef_name) |
| + .append() |
| + ) |
| + 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() |
| + ) |
| + |
| + 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_generator.get_type(prop, pad_for_generics=True), |
| + prop.name)) |
| + else: |
| + c.append('%s %s;' % |
| + (self.__cpp_type_generator.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() |
| + .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 {') |
|
Yoyo Zhou
2012/01/19 23:19:11
Seems like this should be inside the
if function.p
calamity
2012/01/20 01:10:25
Originally, I had one class that generated the .cc
|
| + 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_generator.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_generator.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(self): |
| + """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__' % |
| + (self.__namespace.source_file_dir, self.__namespace.target_namespace)) |
| + .upper().replace('/', '_')) |
| + |