Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bounded string support in C++ #645

Draft
wants to merge 1 commit into
base: rolling
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions rosidl_generator_cpp/rosidl_generator_cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
from rosidl_parser.definition import Array
from rosidl_parser.definition import BasicType
from rosidl_parser.definition import BoundedSequence
from rosidl_parser.definition import BoundedString
from rosidl_parser.definition import FLOATING_POINT_TYPES
from rosidl_parser.definition import NamespacedType
from rosidl_parser.definition import UnboundedSequence
from rosidl_parser.definition import UnboundedString


def generate_cpp(generator_arguments_file):
Expand Down Expand Up @@ -67,13 +69,44 @@ def prefix_with_bom_if_necessary(content):
'int32': 'int32_t',
'uint64': 'uint64_t',
'int64': 'int64_t',
'string': 'std::basic_string<char, std::char_traits<char>, ' +
'typename std::allocator_traits<ContainerAllocator>::template rebind_alloc<char>>',
'wstring': 'std::basic_string<char16_t, std::char_traits<char16_t>, typename ' +
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char16_t>>',
}


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<char, %u, std::char_traits<char>, ' +
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char>>') \
% (type_.maximum_size)
elif isinstance(type_, UnboundedString):
return \
('std::basic_string<char, std::char_traits<char>, ' +
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char>>')
elif isinstance(type_, AbstractWString):
if isinstance(type_, BoundedWString):
return \
('rosidl_runtime_cpp::bounded_basic_string<char16_t, %u, ' +
'std::char_traits<char16_t>, ' +
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char16_t>>') \
% (type_.maximum_size)
elif isinstance(type_, UnboundedWString):
return \
('std::basic_string<char16_t, std::char_traits<char16_t>, ' +
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char16_t>>')
assert False, type_


def msg_type_only_to_cpp(type_):
"""
Convert a message type into the C++ declaration, ignoring array types.
Expand All @@ -88,10 +121,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 + '_<ContainerAllocator>'
Expand Down