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 |
| index f398c752ec04e4348e1188e6cc89d6730a55b93b..56d8a66d459bb85f4f8dc09ce3843f246130d40f 100644 |
| --- a/tools/json_schema_compiler/cpp_type_generator.py |
| +++ b/tools/json_schema_compiler/cpp_type_generator.py |
| @@ -115,7 +115,8 @@ class CppTypeGenerator(object): |
| """ |
| return cpp_util.Classname(prop.name) + 'Type' |
| - def GetType(self, prop, pad_for_generics=False, wrap_optional=False): |
| + def GetType(self, prop, pad_for_generics=False, wrap_optional=False, |
| + serialized=False): |
| """Translates a model.Property into its C++ type. |
| If REF types from different namespaces are referenced, will resolve |
| @@ -125,9 +126,15 @@ class CppTypeGenerator(object): |
| Use wrap_optional to wrap the type in a scoped_ptr<T> if the Property is |
| optional. |
| + |
| + Use serialized when getting the serialized type (prop.serialized_type). |
| """ |
| cpp_type = None |
| - if prop.type_ == PropertyType.REF: |
| + type_ = prop.type_ |
| + if serialized and prop.serialized_type is not None: |
| + type_ = prop.serialized_type |
| + |
| + if type_ == PropertyType.REF: |
| dependency_namespace = self._ResolveTypeNamespace(prop.ref_type) |
| if not dependency_namespace: |
| raise KeyError('Cannot find referenced type: %s' % prop.ref_type) |
| @@ -136,23 +143,25 @@ class CppTypeGenerator(object): |
| schema_util.StripSchemaNamespace(prop.ref_type)) |
| else: |
| cpp_type = schema_util.StripSchemaNamespace(prop.ref_type) |
| - elif prop.type_ == PropertyType.BOOLEAN: |
| + elif type_ == PropertyType.BOOLEAN: |
| cpp_type = 'bool' |
| - elif prop.type_ == PropertyType.INTEGER: |
| + elif type_ == PropertyType.INTEGER: |
| cpp_type = 'int' |
| - elif prop.type_ == PropertyType.DOUBLE: |
| + elif type_ == PropertyType.INT64: |
| + cpp_type = 'int64' |
| + elif type_ == PropertyType.DOUBLE: |
| cpp_type = 'double' |
| - elif prop.type_ == PropertyType.STRING: |
| + elif type_ == PropertyType.STRING: |
| cpp_type = 'std::string' |
| - elif prop.type_ == PropertyType.ENUM: |
| + elif type_ == PropertyType.ENUM: |
| cpp_type = cpp_util.Classname(prop.name) |
| - elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: |
| + elif type_ == PropertyType.ADDITIONAL_PROPERTIES: |
| cpp_type = 'base::DictionaryValue' |
| - elif prop.type_ == PropertyType.ANY: |
| + elif type_ == PropertyType.ANY: |
| cpp_type = any_helper.ANY_CLASS |
| - elif prop.type_ == PropertyType.OBJECT: |
| + elif type_ == PropertyType.OBJECT: |
| cpp_type = cpp_util.Classname(prop.name) |
| - elif prop.type_ == PropertyType.ARRAY: |
| + elif type_ == PropertyType.ARRAY: |
| item_type = prop.item_type |
| if item_type.type_ == PropertyType.REF: |
| item_type = self.GetReferencedProperty(item_type) |
| @@ -163,14 +172,14 @@ class CppTypeGenerator(object): |
| cpp_type = 'std::vector<%s> ' |
| cpp_type = cpp_type % self.GetType( |
| prop.item_type, pad_for_generics=True) |
| - elif prop.type_ == PropertyType.BINARY: |
| + elif type_ == PropertyType.BINARY: |
| cpp_type = 'std::string' |
| else: |
| - raise NotImplementedError(prop.type_) |
| + raise NotImplementedError(type_) |
| # Enums aren't wrapped because C++ won't allow it. Optional enums have a |
| # NONE value generated instead. |
| - if wrap_optional and prop.optional and prop.type_ != PropertyType.ENUM: |
| + if wrap_optional and prop.optional and type_ != PropertyType.ENUM: |
| cpp_type = 'scoped_ptr<%s> ' % cpp_type |
| if pad_for_generics: |
| return cpp_type |
| @@ -217,8 +226,27 @@ class CppTypeGenerator(object): |
| self._cpp_namespaces[dependency]) |
| for dependency in self._NamespaceTypeDependencies().keys()]): |
| c.Append('#include "%s"' % header) |
| + |
| + # TODO(mwrosen): Add support for more conversions as necessary. |
| + if self._ShouldIncludeBaseNumberConversions(): |
| + c.Append('#include "base/string_number_conversions.h"') |
|
not at google - send to devlin
2012/07/26 04:51:42
bleh, just include it unconditionally. Too much of
mitchellwrosen
2012/07/26 20:00:27
I thought so too =)
|
| + |
| return c |
| + def _ShouldIncludeBaseNumberConversions(self): |
| + """Returns true if base/string_number_conversions.h should be included. |
| + """ |
| + for function in self._namespace.functions.values(): |
| + for param in function.params: |
| + if (param.serialized_type is not None and |
| + ((param.type_ == PropertyType.STRING and |
| + (param.serialized_type == PropertyType.INTEGER or |
| + param.serialized_type == PropertyType.INT64)) or |
| + (param.serialized_type == PropertyType.STRING and |
| + param.type_ == PropertyType.INTEGER))): |
| + return True |
| + return False |
| + |
| def _ResolveTypeNamespace(self, ref_type): |
| """Resolves a type, which must be explicitly qualified, to its enclosing |
| namespace. |