Chromium Code Reviews| Index: tools/json_schema_compiler/cpp_type_manager.py |
| diff --git a/tools/json_schema_compiler/cpp_type_manager.py b/tools/json_schema_compiler/cpp_type_manager.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..53c718b3de2c146e26d68b0002f7ecd9f1e75d9d |
| --- /dev/null |
| +++ b/tools/json_schema_compiler/cpp_type_manager.py |
| @@ -0,0 +1,91 @@ |
| +# 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 cpp_util |
| +from code import Code |
| + |
| +class CppTypeManager(object): |
|
not at google - send to devlin
2012/01/17 05:42:32
I don't think Manager describes this class properl
|
| + """Manages the types of properties and provides utilities for getting the |
| + C++ type out of a model.Property |
| + """ |
| + def __init__(self, namespace, model): |
| + self.model = model |
| + self.types = model.types |
| + self.namespace = namespace |
|
not at google - send to devlin
2012/01/17 05:42:32
these should all be private
|
| + |
| + # TODO(calamity): Handle ANY |
| + def get_type(self, prop, pad_for_generics=False): |
| + """Translates a json_type into its C++ equivalent. |
|
not at google - send to devlin
2012/01/17 05:42:32
Translates a Property's type into its C++ equivale
|
| + |
| + 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', |
| + } |
| + cpp_type = None |
| + 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: |
| + cpp_type = '%s::%s' % (ref_type.filename, prop.json_type) |
| + else: |
| + cpp_type = '%s' % prop.json_type |
| + elif prop.type == PropertyType.FUNDAMENTAL: |
| + cpp_type = simple_c_types[prop.json_type] |
| + elif prop.type == PropertyType.ARRAY: |
| + cpp_type = 'std::vector<%s>' % self.get_type(prop.item_type) |
| + elif prop.type == PropertyType.OBJECT: |
| + cpp_type = cpp_util.cpp_name(prop.name) |
| + # TODO(calamity): choices |
| + else: |
| + raise NotImplementedError |
| + |
| + # Add a space to prevent operator ambiguity |
| + if pad_for_generics and cpp_type[-1] == '>': |
| + return '%s ' % cpp_type |
| + return '%s' % cpp_type |
| + |
| + def generate_cpp_includes(self): |
| + """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): |
| + """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 |