Chromium Code Reviews| Index: tools/json_schema_compiler/cpp_type_generator.py |
| diff --git a/tools/json_schema_compiler/cpp_type_generator.py b/tools/json_schema_compiler/cpp_type_generator.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4b22702c3d2342e4bcc7227f71f925084f3cc70c |
| --- /dev/null |
| +++ b/tools/json_schema_compiler/cpp_type_generator.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 CppTypeGenerator(object): |
| + """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 |
| + |
| + # TODO(calamity): Handle ANY |
| + def get_type(self, prop, pad_for_generics=False): |
| + """Translates a model.Property into its C++ type. |
| + |
| + If REF types from different namespaces are referenced, will resolve |
| + using self.__types. |
| + |
| + Use pad_for_generics when using as a generic to avoid operator ambiguity. |
| + """ |
| + cpp_type = None |
| + if prop.type == PropertyType.REF: |
| + ref_type = self.__types.get(prop.ref_type) |
| + if not ref_type: |
| + raise KeyError('Cannot find referenced type: %s' % prop.ref_type) |
| + if self.__namespace != ref_type: |
| + cpp_type = '%s::%s' % (ref_type.target_namespace, prop.ref_type) |
| + else: |
| + cpp_type = '%s' % prop.ref_type |
| + elif prop.type == PropertyType.BOOLEAN: |
| + cpp_type = 'bool' |
|
Yoyo Zhou
2012/01/19 02:19:40
too many spaces
calamity
2012/01/20 01:10:25
Done.
|
| + elif prop.type == PropertyType.INTEGER: |
| + cpp_type = 'int' |
| + elif prop.type == PropertyType.DOUBLE: |
| + cpp_type = 'double' |
| + elif prop.type == PropertyType.STRING: |
| + cpp_type = 'std::string' |
| + elif prop.type == PropertyType.ARRAY: |
| + cpp_type = 'std::vector<%s>' % self.get_type(prop.item_type) |
|
Yoyo Zhou
2012/01/19 02:19:40
Did you mean to call it with pad_for_generics=True
calamity
2012/01/20 01:10:25
Done.
|
| + 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"' % ( |
|
Yoyo Zhou
2012/01/19 02:19:40
If you had 2 dependencies from the same namespace,
calamity
2012/01/20 01:10:25
Fixed and added a test.
|
| + dependency_namespace.source_file_dir, |
| + dependency_namespace.target_namespace)) |
| + return includes |
| + |
| + def __type_dependencies(self, prop): |
| + """Recursively gets all the type dependencies of a property. |
| + """ |
| + deps = set() |
| + if prop: |
| + if prop.type == PropertyType.REF: |
| + deps.add(prop.ref_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 |