Skip to content

Commit 8cab193

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 8cab193

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

rosidl_generator_cpp/rosidl_generator_cpp/__init__.py

+39-8
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
from rosidl_parser.definition import Array
2424
from rosidl_parser.definition import BasicType
2525
from rosidl_parser.definition import BoundedSequence
26+
from rosidl_parser.definition import BoundedString
2627
from rosidl_parser.definition import FLOATING_POINT_TYPES
2728
from rosidl_parser.definition import NamespacedType
2829
from rosidl_parser.definition import UnboundedSequence
30+
from rosidl_parser.definition import UnboundedString
2931

3032

3133
def generate_cpp(generator_arguments_file):
@@ -67,13 +69,44 @@ def prefix_with_bom_if_necessary(content):
6769
'int32': 'int32_t',
6870
'uint64': 'uint64_t',
6971
'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>>',
7472
}
7573

7674

75+
def resolve_string_type(type_):
76+
"""
77+
Convert a string type into the C++ declaration,
78+
respecting character width and string boundedness.
79+
80+
Example input: TODO
81+
Example output: TODO
82+
83+
@param type_: The message type
84+
@type type_: rosidl_parser.Type
85+
"""
86+
if isinstance(type_, AbstractString):
87+
if isinstance(type_, BoundedString):
88+
return \
89+
('rosidl_runtime_cpp::bounded_basic_string<char, %u, std::char_traits<char>, ' +
90+
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char>>') \
91+
% (type_.maximum_size)
92+
elif isinstance(type_, UnboundedString):
93+
return \
94+
('std::basic_string<char, std::char_traits<char>, ' +
95+
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char>>')
96+
elif isinstance(type_, AbstractWString):
97+
if isinstance(type_, BoundedWString):
98+
return \
99+
('rosidl_runtime_cpp::bounded_basic_string<char16_t, %u, ' +
100+
'std::char_traits<char16_t>, ' +
101+
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char16_t>>') \
102+
% (type_.maximum_size)
103+
elif isinstance(type_, UnboundedWString):
104+
return \
105+
('std::basic_string<char16_t, std::char_traits<char16_t>, ' +
106+
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char16_t>>')
107+
assert False, type_
108+
109+
77110
def msg_type_only_to_cpp(type_):
78111
"""
79112
Convert a message type into the C++ declaration, ignoring array types.
@@ -88,10 +121,8 @@ def msg_type_only_to_cpp(type_):
88121
type_ = type_.value_type
89122
if isinstance(type_, BasicType):
90123
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']
124+
elif isinstance(type_, AbstractGenericString):
125+
cpp_type = resolve_string_type(type_)
95126
elif isinstance(type_, NamespacedType):
96127
typename = '::'.join(type_.namespaced_name())
97128
cpp_type = typename + '_<ContainerAllocator>'

0 commit comments

Comments
 (0)