From d3837789f4f05c8eff8223ecc5f10cdf6e078fb5 Mon Sep 17 00:00:00 2001 From: Abrar Rahman Protyasha Date: Thu, 13 Jan 2022 06:10:28 +0600 Subject: [PATCH] [rosidl_generator_cpp] Fix string type resolution This commit correctly maps Bounded string/wstring IDL types to `rosidl_runtime_cpp::bounded_basic_string` types, rather than `std::basic_string` types. Signed-off-by: Abrar Rahman Protyasha --- .../rosidl_generator_cpp/__init__.py | 45 +++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/rosidl_generator_cpp/rosidl_generator_cpp/__init__.py b/rosidl_generator_cpp/rosidl_generator_cpp/__init__.py index 9fbbf62f0..cf1e7faf2 100644 --- a/rosidl_generator_cpp/rosidl_generator_cpp/__init__.py +++ b/rosidl_generator_cpp/rosidl_generator_cpp/__init__.py @@ -67,13 +67,44 @@ def prefix_with_bom_if_necessary(content): 'int32': 'int32_t', 'uint64': 'uint64_t', 'int64': 'int64_t', - 'string': 'std::basic_string, ' + - 'typename std::allocator_traits::template rebind_alloc>', - 'wstring': 'std::basic_string, typename ' + - 'std::allocator_traits::template rebind_alloc>', } +def resolve_string_type(type_): + """ + Convert a string type into the C++ declaration, + respecting character width and string boundedness. + + Example input: TODO + Example output: TODO + + @param type_: The message type + @type type_: rosidl_parser.Type + """ + if isinstance(type_, AbstractString): + if isinstance(type_, BoundedString): + return \ + ('rosidl_runtime_cpp::bounded_basic_string, ' + + 'std::allocator_traits::template rebind_alloc>') \ + % (type_.maximum_size) + elif isinstance(type_, UnboundedString): + return \ + ('std::basic_string, ' + + 'std::allocator_traits::template rebind_alloc>') + elif isinstance(type_, AbstractWString): + if isinstance(type_, BoundedWString): + return \ + ('rosidl_runtime_cpp::bounded_basic_string, ' + + 'std::allocator_traits::template rebind_alloc>') \ + % (type_.maximum_size) + elif isinstance(type_, UnboundedWString): + return \ + ('std::basic_string, ' + + 'std::allocator_traits::template rebind_alloc>') + assert False, type_ + + def msg_type_only_to_cpp(type_): """ Convert a message type into the C++ declaration, ignoring array types. @@ -88,10 +119,8 @@ def msg_type_only_to_cpp(type_): type_ = type_.value_type if isinstance(type_, BasicType): cpp_type = MSG_TYPE_TO_CPP[type_.typename] - elif isinstance(type_, AbstractString): - cpp_type = MSG_TYPE_TO_CPP['string'] - elif isinstance(type_, AbstractWString): - cpp_type = MSG_TYPE_TO_CPP['wstring'] + elif isinstance(type_, AbstractGenericString): + cpp_type = resolve_string_type(type_) elif isinstance(type_, NamespacedType): typename = '::'.join(type_.namespaced_name()) cpp_type = typename + '_'