Skip to content

Commit d383778

Browse files
committed
[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 <[email protected]>
1 parent bf30ace commit d383778

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

rosidl_generator_cpp/rosidl_generator_cpp/__init__.py

+37-8
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,44 @@ def prefix_with_bom_if_necessary(content):
6767
'int32': 'int32_t',
6868
'uint64': 'uint64_t',
6969
'int64': 'int64_t',
70-
'string': 'std::basic_string<char, std::char_traits<char>, ' +
71-
'typename std::allocator_traits<ContainerAllocator>::template rebind_alloc<char>>',
72-
'wstring': 'std::basic_string<char16_t, std::char_traits<char16_t>, typename ' +
73-
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char16_t>>',
7470
}
7571

7672

73+
def resolve_string_type(type_):
74+
"""
75+
Convert a string type into the C++ declaration,
76+
respecting character width and string boundedness.
77+
78+
Example input: TODO
79+
Example output: TODO
80+
81+
@param type_: The message type
82+
@type type_: rosidl_parser.Type
83+
"""
84+
if isinstance(type_, AbstractString):
85+
if isinstance(type_, BoundedString):
86+
return \
87+
('rosidl_runtime_cpp::bounded_basic_string<char, %u, std::char_traits<char>, ' +
88+
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char>>') \
89+
% (type_.maximum_size)
90+
elif isinstance(type_, UnboundedString):
91+
return \
92+
('std::basic_string<char, std::char_traits<char>, ' +
93+
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char>>')
94+
elif isinstance(type_, AbstractWString):
95+
if isinstance(type_, BoundedWString):
96+
return \
97+
('rosidl_runtime_cpp::bounded_basic_string<char16_t, %u, ' +
98+
'std::char_traits<char16_t>, ' +
99+
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char16_t>>') \
100+
% (type_.maximum_size)
101+
elif isinstance(type_, UnboundedWString):
102+
return \
103+
('std::basic_string<char16_t, std::char_traits<char16_t>, ' +
104+
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char16_t>>')
105+
assert False, type_
106+
107+
77108
def msg_type_only_to_cpp(type_):
78109
"""
79110
Convert a message type into the C++ declaration, ignoring array types.
@@ -88,10 +119,8 @@ def msg_type_only_to_cpp(type_):
88119
type_ = type_.value_type
89120
if isinstance(type_, BasicType):
90121
cpp_type = MSG_TYPE_TO_CPP[type_.typename]
91-
elif isinstance(type_, AbstractString):
92-
cpp_type = MSG_TYPE_TO_CPP['string']
93-
elif isinstance(type_, AbstractWString):
94-
cpp_type = MSG_TYPE_TO_CPP['wstring']
122+
elif isinstance(type_, AbstractGenericString):
123+
cpp_type = resolve_string_type(type_)
95124
elif isinstance(type_, NamespacedType):
96125
typename = '::'.join(type_.namespaced_name())
97126
cpp_type = typename + '_<ContainerAllocator>'

0 commit comments

Comments
 (0)