Chromium Code Reviews| Index: tools/json_schema_compiler/generate_h.py |
| diff --git a/tools/json_schema_compiler/generate_h.py b/tools/json_schema_compiler/generate_h.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..132c4989034051ec9afb8afbed7465cba001964a |
| --- /dev/null |
| +++ b/tools/json_schema_compiler/generate_h.py |
| @@ -0,0 +1,182 @@ |
| +# Copyright (c) 2011 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 type_manager |
| + |
| +class H_Generator(object): |
|
not at google - send to devlin
2012/01/13 02:14:09
Same comment applies here as to CCGenerator.
calamity
2012/01/16 04:01:06
Done.
|
| + """A .h generator for a namespace.""" |
| + |
| + def __init__(self, namespace, model): |
| + self.type_manager = type_manager.TypeManager(namespace, model) |
| + self.namespace = namespace |
| + |
| + def generate(self): |
| + """Generates the .h code for self.namespace. |
|
not at google - send to devlin
2012/01/13 02:14:09
ditto
calamity
2012/01/16 04:01:06
Done.
|
| + |
| + Returns a code.Code object. |
| + """ |
| + include_path = self.namespace.parent_dir |
| + filename = self.namespace.filename |
| + c = code.Code() |
| + c.append(code.CHROMIUM_LICENSE).append() |
| + c.append(code.WARNING_MESSAGE % self.namespace.parent_path) |
| + c.append() |
| + |
| + ifndef_name = generate_ifndef_name(include_path, filename) |
| + c.append('#ifndef %s' % ifndef_name) |
| + c.append('#define %s' % ifndef_name) |
| + c.append('#pragma once') |
| + c.append() |
| + c.append('#include <string>') |
| + c.append('#include <vector>') |
| + c.append() |
| + c.append('#include "base/basictypes.h"') |
| + c.append('#include "base/memory/scoped_ptr.h"') |
| + c.append('#include "base/values.h"') |
| + c.append() |
| + c.append('using base::Value;') |
| + c.append('using base::DictionaryValue;') |
| + c.append('using base::ListValue;') |
| + c.append() |
| + |
| + includes = self.type_manager.resolve_generated_includes() |
| + if includes.code: |
| + c.add(includes) |
| + c.append() |
| + |
| + c.append('namespace extensions {') |
|
not at google - send to devlin
2012/01/13 02:14:09
ditto
calamity
2012/01/16 04:01:06
Done.
|
| + c.append('namespace %s {' % filename) |
| + c.append() |
| + c.append('//') |
| + c.append('// Types') |
| + c.append('//') |
| + c.append() |
| + for tipe in self.namespace.types.values(): |
| + c.add(self.generate_type(tipe)) |
| + c.append() |
| + c.append('//') |
| + c.append('// Functions') |
| + c.append('//') |
| + c.append() |
| + for function in self.namespace.functions.values(): |
| + c.add(self.generate_function(function)) |
| + c.append() |
| + c.append() |
| + c.append() |
| + c.append('} // namespace extensions') |
| + c.append('} // namespace %s' % filename) |
| + c.append() |
| + c.append('#endif // %s' % ifndef_name) |
| + return c |
| + |
| + def generate_type(self, tipe, serializable=True): |
| + """Generates a struct for a type.""" |
| + classname = code.cpp_name(tipe.name) |
| + c = code.Code() |
| + if tipe.description: |
| + c.comment(tipe.description) |
| + c.sblock('struct %(classname)s {') |
| + c.append('~%(classname)s();') |
| + c.append('%(classname)s();') |
| + c.append() |
| + for prop in tipe.properties.values(): |
| + if prop.description: |
| + c.comment(prop.description) |
| + if prop.optional: |
| + c.append('scoped_ptr<%s> %s;' % ( |
| + self.type_manager.get_generic_type(prop), prop.name)) |
| + else: |
| + c.append('%s %s;' % (self.type_manager.get_type(prop), prop.name)) |
| + c.append() |
| + |
| + c.append('// Populates a %(classname)s object from a Value. Returns') |
| + c.append('// whether |out| was successfully populated.') |
| + c.append('static bool Populate(const Value& value, %(classname)s* out);') |
| + c.append() |
| + if serializable: |
| + c.append('// Returns a new DictionaryValue representing the serialized form') |
|
not at google - send to devlin
2012/01/13 02:14:09
line too long
calamity
2012/01/16 04:01:06
Done.
|
| + c.append('// of this %(classname)s object.') |
| + c.append('DictionaryValue* ToValue() const;') |
| + c.eblock() |
| + c.sblock(' private:') |
| + c.append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') |
| + |
| + c.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 {' % code.cpp_name(function.name)) |
| + c.add(self.generate_function_params(function)) |
| + c.add(self.generate_function_result(function)) |
| + c.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.add(self.generate_type(param, serializable=False)) |
| + c.append() |
| + for param in function.params: |
| + c.append('%s %s;' % (self.type_manager.get_type(param), param.name)) |
| + |
| + if function.params: |
| + c.append() |
| + c.append('Params();') |
| + c.append('~Params();') |
| + |
| + c.append() |
| + c.append('static bool Populate(const ListValue& args, Params* out);') |
| + c.eblock() |
| + c.sblock(' private:') |
| + c.append('DISALLOW_COPY_AND_ASSIGN(Params);') |
| + |
| + c.eblock('};') |
| + c.append() |
| + |
| + 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() |
| + |
| + # TODO(calamity): Put this comment in less stupid place |
| + if function.callback.param.description: |
| + c.comment(function.callback.param.description) |
| + c.append('class Result {') |
| + c.sblock(' public:') |
| + arg = '' |
| + # TODO(calamity): handle object |
| + if function.callback.param: |
| + arg = 'const ' + self.type_manager.parameter_declaration( |
| + function.callback.param, |
| + type_modifiers={PropertyType.REF: type_manager.ParamFormat.REFERENCE}) |
| + c.append('static Value* Create(%s);' % arg) |
| + c.eblock() |
| + c.sblock(' private:') |
| + c.append('Result() {};') |
| + c.append('DISALLOW_COPY_AND_ASSIGN(Result);') |
| + c.eblock('};') |
| + c.append() |
| + |
| + 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('/', '_') |
| + |