Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions rosidl_generator_cpp/resource/idl__struct.hpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ include_directives = set()
#include <string>
#include <vector>

#include "rosidl_runtime_cpp/byte_helpers.hpp"
#include "rosidl_runtime_cpp/bounded_vector.hpp"
#include "rosidl_runtime_cpp/message_initialization.hpp"

Expand Down
18 changes: 10 additions & 8 deletions rosidl_generator_cpp/resource/msg__struct.hpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from rosidl_generator_cpp import create_init_alloc_and_member_lists
from rosidl_generator_cpp import escape_string
from rosidl_generator_cpp import escape_wstring
from rosidl_generator_cpp import msg_type_to_cpp
from rosidl_generator_cpp import MSG_TYPE_TO_CPP
from rosidl_generator_cpp import MSG_TYPE_TO_CPP_CONVERSION
from rosidl_generator_cpp import generate_zero_string
from rosidl_generator_cpp import generate_default_string
from rosidl_parser.definition import AbstractNestedType
Expand Down Expand Up @@ -235,17 +235,19 @@ non_defaulted_zero_initialized_members = [
#endif
@[ end if]@
@[ if isinstance(constant.type, AbstractString)]@
static const @(MSG_TYPE_TO_CPP['string']) @(constant.name);
static const @(MSG_TYPE_TO_CPP_CONVERSION['string']) @(constant.name);
@[ elif isinstance(constant.type, AbstractWString)]@
static const @(MSG_TYPE_TO_CPP['wstring']) @(constant.name);
static const @(MSG_TYPE_TO_CPP_CONVERSION['wstring']) @(constant.name);
@[ else]@
static constexpr @(MSG_TYPE_TO_CPP[constant.type.typename]) @(constant.name) =
static constexpr @(MSG_TYPE_TO_CPP_CONVERSION[constant.type.typename]) @(constant.name) =
@[ if isinstance(constant.type, BasicType)]@
@[ if constant.type.typename in (*INTEGER_TYPES, *CHARACTER_TYPES, BOOLEAN_TYPE, OCTET_TYPE)]@
@[ if constant.type.typename in (*INTEGER_TYPES, *CHARACTER_TYPES, BOOLEAN_TYPE)]@
@(int(constant.value))@
@[ if constant.type.typename in UNSIGNED_INTEGER_TYPES]@
u@
@[ end if]@
@[ elif constant.type.typename == OCTET_TYPE]@
std::byte{@(constant.value)}@
@[ elif constant.type.typename == 'float']@
@(constant.value)f@
@[ else]@
Expand Down Expand Up @@ -336,17 +338,17 @@ using @(message.structure.namespaced_type.name) =
@[ end if]@
@[ if isinstance(c.type, AbstractString)]@
template<typename ContainerAllocator>
const @(MSG_TYPE_TO_CPP['string'])
const @(MSG_TYPE_TO_CPP_CONVERSION['string'])
@(message.structure.namespaced_type.name)_<ContainerAllocator>::@(c.name) = "@(escape_string(c.value))";
@[ elif isinstance(c.type, AbstractWString)]@
template<typename ContainerAllocator>
const @(MSG_TYPE_TO_CPP['wstring'])
const @(MSG_TYPE_TO_CPP_CONVERSION['wstring'])
@(message.structure.namespaced_type.name)_<ContainerAllocator>::@(c.name) = u"@(escape_wstring(c.value))";
@[ else ]@
#if __cplusplus < 201703L
// static constexpr member variable definitions are only needed in C++14 and below, deprecated in C++17
template<typename ContainerAllocator>
constexpr @(MSG_TYPE_TO_CPP[c.type.typename]) @(message.structure.namespaced_type.name)_<ContainerAllocator>::@(c.name);
constexpr @(MSG_TYPE_TO_CPP_CONVERSION[c.type.typename]) @(message.structure.namespaced_type.name)_<ContainerAllocator>::@(c.name);
#endif // __cplusplus < 201703L
@[ end if]@
@[ if c.name in msvc_common_macros]@
Expand Down
16 changes: 12 additions & 4 deletions rosidl_generator_cpp/resource/msg__traits.hpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ inline void to_flow_style_yaml(
{
@[ if isinstance(member.type, BasicType)]@
out << "@(member.name): ";
@[ if member.type.typename in ('octet', 'char', 'wchar')]@
@[ if member.type.typename in ('char', 'wchar')]@
rosidl_generator_traits::character_value_to_yaml(msg.@(member.name), out);
@[ elif member.type.typename == 'octet']@
rosidl_generator_traits::value_to_yaml(static_cast<std::byte>(msg.@(member.name)), out);
@[ else]@
rosidl_generator_traits::value_to_yaml(msg.@(member.name), out);
@[ end if]@
Expand All @@ -112,8 +114,10 @@ inline void to_flow_style_yaml(
size_t pending_items = msg.@(member.name).size();
for (auto item : msg.@(member.name)) {
@[ if isinstance(member.type.value_type, BasicType)]@
@[ if member.type.value_type.typename in ('octet', 'char', 'wchar')]@
@[ if member.type.value_type.typename in ('char', 'wchar')]@
rosidl_generator_traits::character_value_to_yaml(item, out);
@[ elif member.type.value_type.typename == 'octet']@
rosidl_generator_traits::value_to_yaml(static_cast<std::byte>(item), out);
@[ else]@
rosidl_generator_traits::value_to_yaml(item, out);
@[ end if]@
Expand Down Expand Up @@ -158,8 +162,10 @@ inline void to_block_style_yaml(
}
@[ if isinstance(member.type, BasicType)]@
out << "@(member.name): ";
@[ if member.type.typename in ('octet', 'char', 'wchar')]@
@[ if member.type.typename in ('char', 'wchar')]@
rosidl_generator_traits::character_value_to_yaml(msg.@(member.name), out);
@[ elif member.type.typename == 'octet']@
rosidl_generator_traits::value_to_yaml(static_cast<std::byte>(msg.@(member.name)), out);
@[ else]@
rosidl_generator_traits::value_to_yaml(msg.@(member.name), out);
@[ end if]@
Expand All @@ -182,8 +188,10 @@ inline void to_block_style_yaml(
}
@[ if isinstance(member.type.value_type, BasicType)]@
out << "- ";
@[ if member.type.value_type.typename in ('octet', 'char', 'wchar')]@
@[ if member.type.value_type.typename in ('char', 'wchar')]@
rosidl_generator_traits::character_value_to_yaml(item, out);
@[ elif member.type.value_type.typename == 'octet']@
rosidl_generator_traits::value_to_yaml(static_cast<std::byte>(item), out);
@[ else]@
rosidl_generator_traits::value_to_yaml(item, out);
@[ end if]@
Expand Down
19 changes: 12 additions & 7 deletions rosidl_generator_cpp/rosidl_generator_cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from ast import literal_eval
from copy import copy
from typing import List

from rosidl_parser.definition import AbstractGenericString
Expand Down Expand Up @@ -55,7 +56,7 @@ def prefix_with_bom_if_necessary(content: str) -> str:

MSG_TYPE_TO_CPP = {
'boolean': 'bool',
'octet': 'unsigned char', # TODO change to std::byte with C++17
'octet': 'std::byte',
'char': 'unsigned char', # TODO change to char8_t with C++20
'wchar': 'char16_t',
'float': 'float',
Expand All @@ -75,6 +76,9 @@ def prefix_with_bom_if_necessary(content: str) -> str:
'std::allocator_traits<ContainerAllocator>::template rebind_alloc<char16_t>>',
}

MSG_TYPE_TO_CPP_CONVERSION = copy(MSG_TYPE_TO_CPP)
MSG_TYPE_TO_CPP_CONVERSION['octet'] = 'rosidl_runtime_cpp::ByteConverter'


def msg_type_only_to_cpp(type_):
"""
Expand All @@ -89,11 +93,11 @@ def msg_type_only_to_cpp(type_):
if isinstance(type_, AbstractNestedType):
type_ = type_.value_type
if isinstance(type_, BasicType):
cpp_type = MSG_TYPE_TO_CPP[type_.typename]
cpp_type = MSG_TYPE_TO_CPP_CONVERSION[type_.typename]
elif isinstance(type_, AbstractString):
cpp_type = MSG_TYPE_TO_CPP['string']
cpp_type = MSG_TYPE_TO_CPP_CONVERSION['string']
elif isinstance(type_, AbstractWString):
cpp_type = MSG_TYPE_TO_CPP['wstring']
cpp_type = MSG_TYPE_TO_CPP_CONVERSION['wstring']
elif isinstance(type_, NamespacedType):
typename = '::'.join(type_.namespaced_name())
cpp_type = typename + '_<ContainerAllocator>'
Expand Down Expand Up @@ -196,11 +200,12 @@ def primitive_value_to_cpp(type_, value):
if type_.typename == 'boolean':
return 'true' if value else 'false'

if type_.typename in [
'char', 'octet'
]:
if type_.typename == 'char':
return f'static_cast<unsigned char>({value})'

if type_.typename == 'octet':
return f'std::byte{{{value}}}'

if type_.typename in [
'short', 'unsigned short',
'wchar',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void test_message_basic_types(rosidl_generator_tests::msg::BasicTypes message)
#ifdef __linux__
#pragma GCC diagnostic pop
#endif
TEST_BASIC_TYPE_FIELD_ASSIGNMENT(message, byte_value, 0, 255)
TEST_BASIC_TYPE_FIELD_ASSIGNMENT(message, byte_value, std::byte{0}, std::byte{255})
TEST_BASIC_TYPE_FIELD_ASSIGNMENT(message, char_value, 0, UINT8_MAX)
TEST_BASIC_TYPE_FIELD_ASSIGNMENT(message, float32_value, FLT_MIN, FLT_MAX)
TEST_BASIC_TYPE_FIELD_ASSIGNMENT(message, float64_value, DBL_MIN, DBL_MAX)
Expand Down Expand Up @@ -487,7 +487,7 @@ TEST(Test_messages, constants_assign) {
TEST(Test_messages, defaults) {
rosidl_generator_tests::msg::Defaults message;
TEST_BASIC_TYPE_FIELD_ASSIGNMENT(message, bool_value, true, false);
TEST_BASIC_TYPE_FIELD_ASSIGNMENT(message, byte_value, 50, 255);
TEST_BASIC_TYPE_FIELD_ASSIGNMENT(message, byte_value, std::byte{50}, std::byte{255});
TEST_BASIC_TYPE_FIELD_ASSIGNMENT(message, char_value, 100, UINT8_MAX);
TEST_BASIC_TYPE_FIELD_ASSIGNMENT(message, float32_value, 1.125f, FLT_MAX);
TEST_BASIC_TYPE_FIELD_ASSIGNMENT(message, float64_value, 1.125, DBL_MAX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ TEST(Test_msg_initialization, build) {
::rosidl_generator_tests::msg::BasicTypes basic =
::rosidl_generator_tests::build<::rosidl_generator_tests::msg::BasicTypes>()
.bool_value(true)
.byte_value(5)
.byte_value(std::byte{5})
.char_value(10)
.float32_value(0.1125f)
.float64_value(0.01125)
Expand Down Expand Up @@ -57,7 +57,7 @@ TEST(Test_msg_initialization, build) {
{
rosidl_generator_tests::build<rosidl_generator_tests::msg::BasicTypes>()
.bool_value(false)
.byte_value(10)
.byte_value(std::byte{10})
.char_value(20)
.float32_value(0.225f)
.float64_value(0.0225)
Expand Down Expand Up @@ -90,7 +90,7 @@ TEST(Test_msg_initialization, build) {
rosidl_generator_tests::msg::Arrays arrays =
rosidl_generator_tests::build<rosidl_generator_tests::msg::Arrays>()
.bool_values({{true, false, true}})
.byte_values({{5, 10, 5}})
.byte_values({{std::byte{5}, std::byte{10}, std::byte{5}}})
.char_values({{10, 20, 10}})
.float32_values({{0.1125f, 0.225f, 0.1125f}})
.float64_values({{0.01125, 0.0225, 0.01125}})
Expand All @@ -107,7 +107,7 @@ TEST(Test_msg_initialization, build) {
.constants_values({{constants, constants, constants}})
.defaults_values({{defaults, defaults, defaults}})
.bool_values_default({{false, true, false}})
.byte_values_default({{10, 5, 10}})
.byte_values_default({{std::byte{10}, std::byte{5}, std::byte{10}}})
.char_values_default({{20, 10, 20}})
.float32_values_default({{0.225f, 0.1125f, 0.225f}})
.float64_values_default({{0.0225, 0.01125, 0.0225}})
Expand Down
Loading