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..9913977b236fa4dadffa55938f013fa8394921e6 |
--- /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. |
+ """ |
+ simple_c_types = { |
+ PropertyType.BOOLEAN: 'bool', |
+ PropertyType.INTEGER: 'int', |
+ PropertyType.DOUBLE: 'double', |
+ PropertyType.STRING: 'std::string', |
+ } |
+ 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 PropertyType.is_fundamental(prop): |
+ cpp_type = simple_c_types[prop.type] |
not at google - send to devlin
2012/01/18 06:57:28
Just be explicit, and don't have the simple_c_type
calamity
2012/01/18 07:30:54
Are you adverse to the python-style switch case?
not at google - send to devlin
2012/01/18 07:49:37
No, though then you'd also need to have PropertyTy
calamity
2012/01/18 09:35:18
Apparently you can use your classes as keys of a d
|
+ 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.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 |