Chromium Code Reviews| Index: tools/json_schema_compiler/type_manager.py |
| diff --git a/tools/json_schema_compiler/type_manager.py b/tools/json_schema_compiler/type_manager.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f2fde4ab35975036f8a5c369d589204700ee2bf4 |
| --- /dev/null |
| +++ b/tools/json_schema_compiler/type_manager.py |
| @@ -0,0 +1,117 @@ |
| +# 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 model |
| +from code import Code |
| +import code |
| + |
| +class TypeManager(object): |
|
not at google - send to devlin
2012/01/13 06:45:55
this class is specific enough to C to warrant it b
calamity
2012/01/16 04:01:06
Done.
|
| + """Manages the types of properties and provides utlities for getting the |
|
not at google - send to devlin
2012/01/13 06:45:55
s/utlities/utilities/
calamity
2012/01/16 04:01:06
Done.
|
| + C++ type out of a model.Property""" |
| + def __init__(self, namespace, model): |
| + self.model = model |
| + self.types = model.types |
| + self.namespace = namespace |
| + |
| + # TODO(calamity): Handle ANY |
| + def get_type(self, prop): |
|
not at google - send to devlin
2012/01/13 06:45:55
get_cpp_type
calamity
2012/01/16 04:01:06
Changed class to CppTypeManager.
|
| + """Translates a json_type into its C++ equivalent. |
| + |
| + If REF types from different namespaces are referenced, will resolve |
| + using self.types. |
| + """ |
| + simple_c_types = { |
| + 'boolean': 'bool', |
| + 'integer': 'int', |
| + 'double': 'double', |
| + 'string': 'std::string', |
| + } |
| + if prop.type == PropertyType.REF: |
| + ref_type = self.types.get(prop.json_type) |
| + if not ref_type: |
| + raise KeyError('Cannot find referenced type: %s' % prop.json_type) |
| + if self.namespace != ref_type: |
| + return '%s::%s' % (ref_type.filename, prop.json_type) |
| + else: |
| + return '%s' % prop.json_type |
| + elif prop.type == PropertyType.FUNDAMENTAL: |
| + return simple_c_types[prop.json_type] |
| + elif prop.type == PropertyType.ARRAY: |
| + return 'std::vector<%s>' % self.get_type(prop.item_type) |
| + elif prop.type == PropertyType.OBJECT: |
| + return code.cpp_name(prop.name) |
| + # TODO(calamity): choices |
| + else: |
| + raise NotImplementedError |
| + |
| + def parameter_declaration(self, param, type_modifiers=None, |
|
not at google - send to devlin
2012/01/13 06:45:55
generate_cpp_parameter(...) perhaps?
though it se
calamity
2012/01/16 04:01:06
I'll remove it for now.
|
| + default_format='%(type)s %(name)s'): |
| + """Returns a string that declares a parameter. Used where the parameter type |
| + isn't clear and covering all cases would be ugly. |
| + |
| + Can be given a dictionary with PropertyType keys and format string values |
| + where %(type)s and %(name)s denote the C++ type and parameter |
| + name respectively. |
| + |
| + ParamFormat has some common formats. |
| + """ |
| + if not type_modifiers: |
| + type_modifiers = {} |
| + return (type_modifiers.get(param.type, default_format)) % { |
| + 'type': self.get_type(param), 'name': param.name} |
| + |
| + def get_generic_type(self, prop): |
|
not at google - send to devlin
2012/01/13 06:45:55
this should probably be merged into get_type, with
calamity
2012/01/16 04:01:06
Done.
|
| + """Returns the c_type of an object suitable for putting in <%s>. |
| + |
| + Will add a space to inner generics to prevent operator ambiguity. |
| + """ |
| + prop_type = self.get_type(prop) |
| + if prop_type[-1] == '>': |
| + return '%s ' % prop_type |
| + else: |
| + return '%s' % prop_type |
| + |
| + def resolve_generated_includes(self): |
|
not at google - send to devlin
2012/01/13 06:45:55
call this like, generate_cpp_includes
calamity
2012/01/16 04:01:06
Done.
|
| + """Returns the #include lines for self.namespace using the other |
| + namespaces in self.model. |
| + """ |
| + dependencies = set() |
| + for function in self.namespace.functions.values(): |
| + for param in function.params: |
| + dependencies |= self.type_dependencies(param) |
| + dependencies |= self.type_dependencies(function.callback.param) |
| + for tipe in self.namespace.types.values(): |
| + for prop in tipe.properties.values(): |
| + dependencies |= self.type_dependencies(prop) |
| + |
| + includes = Code() |
| + for dependency in dependencies: |
| + dependency_namespace = self.types[dependency] |
| + if dependency_namespace != self.namespace: |
| + includes.append('#include "%s/%s.h"' % ( |
| + dependency_namespace.parent_dir, |
| + dependency_namespace.filename)) |
| + return includes |
| + |
| + def type_dependencies(self, prop): |
|
not at google - send to devlin
2012/01/13 06:45:55
private, so __type_dependencies
calamity
2012/01/16 04:01:06
Done.
|
| + """Gets all the type dependencies of a single property. |
| + |
| + Will also get type dependencies from subproperties""" |
| + deps = set() |
| + if prop: |
| + if prop.type == PropertyType.REF: |
| + deps.add(prop.json_type) |
| + elif prop.type == PropertyType.ARRAY: |
| + deps = self.type_dependencies(prop.item_type) |
| + elif prop.type == PropertyType.OBJECT: |
| + for p in prop.properties.values(): |
| + deps |= self.type_dependencies(p) |
| + return deps |
| + |
| +class ParamFormat(object): |
| + """Common parameter modifier formats to be used with |
| + TypeManager.parameter_declaration""" |
| + POINTER = '%(type)s* %(name)s' |
| + REFERENCE = '%(type)s& %(name)s' |