Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion rosidl_generator_cpp/resource/msg__struct.hpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,15 @@ non_defaulted_zero_initialized_members = [
@[ else]@
static constexpr @(MSG_TYPE_TO_CPP[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 == 'float']@
@(constant.value)f@
@[ elif constant.type.typename == OCTET_TYPE]@
std::byte{@(constant.value)}@
@[ else]@
@(constant.value)@
@[ end if];
Expand Down
8 changes: 4 additions & 4 deletions rosidl_generator_cpp/resource/msg__traits.hpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ 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);
@[ else]@
rosidl_generator_traits::value_to_yaml(msg.@(member.name), out);
Expand All @@ -112,7 +112,7 @@ 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);
@[ else]@
rosidl_generator_traits::value_to_yaml(item, out);
Expand Down Expand Up @@ -158,7 +158,7 @@ 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);
@[ else]@
rosidl_generator_traits::value_to_yaml(msg.@(member.name), out);
Expand All @@ -182,7 +182,7 @@ 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);
@[ else]@
rosidl_generator_traits::value_to_yaml(item, out);
Expand Down
6 changes: 4 additions & 2 deletions rosidl_generator_cpp/rosidl_generator_cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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',
'wchar': 'char16_t',
'float': 'float',
Expand Down Expand Up @@ -196,11 +196,13 @@ def primitive_value_to_cpp(type_, value):
if type_.typename == 'boolean':
return 'true' if value else 'false'

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

if type_.typename in [
'short', 'unsigned short',
'char', 'wchar',
'double', 'long double',
'octet',
'int8', 'uint8',
'int16', 'uint16',
]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#define ROSIDL_GENERATOR_CPP__TEST_ARRAY_GENERATOR_HPP_

#include <climits>
#include <cstddef>
#include <cstdint>
#include <random>
#include <string>
#include <type_traits>
Expand Down Expand Up @@ -44,6 +46,37 @@ void test_vector_fill(
}
}

/**
* Helper function to generate a test pattern for byte type.
* Minimum and maximum values for the type and distributed values in the middle.
* @param C Container (vector, array, etc) to be filled
* @param size How many elements to fill in. Must size<=container_size
* @param min Minimum value in the range to fill.
* @param max Maximum value in the range to fill.
*/
template<
typename C,
typename std::enable_if<
std::is_same<typename C::value_type, std::byte>::value
>::type * = nullptr
>
void test_vector_fill(
C * container, size_t size,
typename C::value_type min, typename C::value_type max)
{
if (size > 0 && min < max) {
int step = (std::to_integer<int>(max) - std::to_integer<int>(min)) / static_cast<int>(size);

(*container)[0] = min;
for (size_t i = 1; i < size - 1; i++) {
int val = std::to_integer<int>(min) + i * step;
(*container)[i] = static_cast<std::byte>(val);
}
(*container)[size - 1] = max;
}
}


/**
* Helper function to generate a test pattern for integer number types.
* The template type parameter must be an integer number type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <array>
#include <cfloat>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
Expand Down Expand Up @@ -168,7 +169,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 @@ -211,8 +212,8 @@ void test_message_bounded(rosidl_generator_tests::msg::BoundedSequences message)
message, char_values, unsigned char, SEQUENCE_SIZE, \
0, UINT8_MAX)
TEST_BOUNDED_SEQUENCE_TYPES(
message, byte_values, uint8_t, SEQUENCE_SIZE, \
0, UINT8_MAX)
message, byte_values, std::byte, SEQUENCE_SIZE, \
std::byte{0}, std::byte{UINT8_MAX})
TEST_BOUNDED_SEQUENCE_TYPES(
message, float32_values, float, SEQUENCE_SIZE, \
FLT_MIN, FLT_MAX)
Expand Down Expand Up @@ -277,8 +278,8 @@ void test_message_unbounded(rosidl_generator_tests::msg::UnboundedSequences mess
message, char_values, unsigned char, SEQUENCE_SIZE, \
0, UINT8_MAX)
TEST_UNBOUNDED_SEQUENCE_TYPES(
message, byte_values, uint8_t, SEQUENCE_SIZE, \
0, UINT8_MAX)
message, byte_values, std::byte, SEQUENCE_SIZE, \
std::byte{0}, std::byte{UINT8_MAX})
TEST_UNBOUNDED_SEQUENCE_TYPES(
message, float32_values, float, SEQUENCE_SIZE, \
FLT_MIN, FLT_MAX)
Expand Down Expand Up @@ -331,8 +332,8 @@ void test_message_arrays(rosidl_generator_tests::msg::Arrays message)
message, char_values, unsigned char, ARRAY_SIZE, \
0, UINT8_MAX)
TEST_ARRAY_TYPES(
message, byte_values, uint8_t, ARRAY_SIZE, \
0, UINT8_MAX)
message, byte_values, std::byte, ARRAY_SIZE, \
std::byte{0}, std::byte{UINT8_MAX})
TEST_ARRAY_TYPES(
message, float32_values, float, ARRAY_SIZE, \
FLT_MIN, FLT_MAX)
Expand Down Expand Up @@ -463,7 +464,7 @@ TEST(Test_messages, unbounded_sequence_unbounded) {
TEST(Test_messages, constants) {
rosidl_generator_tests::msg::Constants message;
ASSERT_EQ(true, message.BOOL_CONST);
ASSERT_EQ(50, message.BYTE_CONST);
ASSERT_EQ(std::byte{50}, message.BYTE_CONST);
ASSERT_EQ(100, message.CHAR_CONST);
ASSERT_EQ(1.125f, message.FLOAT32_CONST);
ASSERT_EQ(1.125, message.FLOAT64_CONST);
Expand All @@ -487,7 +488,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 @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <cstddef>
#include <gtest/gtest.h>

#include "rosidl_generator_tests/msg/detail/arrays__builder.hpp"
Expand All @@ -24,7 +25,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 All @@ -38,7 +39,7 @@ TEST(Test_msg_initialization, build) {
.uint64_value(5000000ULL);

ASSERT_TRUE(basic.bool_value);
ASSERT_EQ(5, basic.byte_value);
ASSERT_EQ(std::byte{5}, basic.byte_value);
ASSERT_EQ(10, basic.char_value);
ASSERT_EQ(0.1125f, basic.float32_value);
ASSERT_EQ(0.01125, basic.float64_value);
Expand All @@ -57,7 +58,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 All @@ -71,7 +72,7 @@ TEST(Test_msg_initialization, build) {
.uint64_value(10000000ULL)});

ASSERT_FALSE(nested.basic_types_value.bool_value);
ASSERT_EQ(10, nested.basic_types_value.byte_value);
ASSERT_EQ(std::byte{10}, nested.basic_types_value.byte_value);
ASSERT_EQ(20, nested.basic_types_value.char_value);
ASSERT_EQ(0.225f, nested.basic_types_value.float32_value);
ASSERT_EQ(0.0225, nested.basic_types_value.float64_value);
Expand All @@ -90,7 +91,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 +108,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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <gtest/gtest.h>

#include <cstddef>
#include <cstring>

#include <string>
Expand Down Expand Up @@ -47,7 +48,7 @@ make_scope_exit(Callable callable)
TEST(Test_msg_initialization, no_arg_constructor) {
rosidl_generator_tests::msg::Defaults def;
ASSERT_TRUE(def.bool_value);
ASSERT_EQ(50, def.byte_value);
ASSERT_EQ(std::byte{50}, def.byte_value);
ASSERT_EQ(100, def.char_value);
ASSERT_EQ(1.125f, def.float32_value);
ASSERT_EQ(1.125, def.float64_value);
Expand All @@ -62,7 +63,7 @@ TEST(Test_msg_initialization, no_arg_constructor) {

rosidl_generator_tests::msg::BasicTypes basic;
ASSERT_FALSE(basic.bool_value);
ASSERT_EQ(0, basic.byte_value);
ASSERT_EQ(std::byte{0}, basic.byte_value);
ASSERT_EQ(0, basic.char_value);
ASSERT_EQ(0.0f, basic.float32_value);
ASSERT_EQ(0.0, basic.float64_value);
Expand All @@ -79,7 +80,7 @@ TEST(Test_msg_initialization, all_constructor) {
rosidl_generator_tests::msg::Defaults def(
rosidl_runtime_cpp::MessageInitialization::ALL);
ASSERT_TRUE(def.bool_value);
ASSERT_EQ(50, def.byte_value);
ASSERT_EQ(std::byte{50}, def.byte_value);
ASSERT_EQ(100, def.char_value);
ASSERT_EQ(1.125f, def.float32_value);
ASSERT_EQ(1.125, def.float64_value);
Expand All @@ -94,7 +95,7 @@ TEST(Test_msg_initialization, all_constructor) {

rosidl_generator_tests::msg::BasicTypes basic;
ASSERT_FALSE(basic.bool_value);
ASSERT_EQ(0, basic.byte_value);
ASSERT_EQ(std::byte{0}, basic.byte_value);
ASSERT_EQ(0, basic.char_value);
ASSERT_EQ(0.0f, basic.float32_value);
ASSERT_EQ(0.0, basic.float64_value);
Expand All @@ -111,7 +112,7 @@ TEST(Test_msg_initialization, zero_constructor) {
rosidl_generator_tests::msg::Defaults def(
rosidl_runtime_cpp::MessageInitialization::ZERO);
ASSERT_FALSE(def.bool_value);
ASSERT_EQ(0, def.byte_value);
ASSERT_EQ(std::byte{0}, def.byte_value);
ASSERT_EQ(0, def.char_value);
ASSERT_EQ(0.0f, def.float32_value);
ASSERT_EQ(0.0, def.float64_value);
Expand All @@ -126,7 +127,7 @@ TEST(Test_msg_initialization, zero_constructor) {

rosidl_generator_tests::msg::BasicTypes basic;
ASSERT_FALSE(basic.bool_value);
ASSERT_EQ(0, basic.byte_value);
ASSERT_EQ(std::byte{0}, basic.byte_value);
ASSERT_EQ(0, basic.char_value);
ASSERT_EQ(0.0f, basic.float32_value);
ASSERT_EQ(0.0, basic.float64_value);
Expand All @@ -143,7 +144,7 @@ TEST(Test_msg_initialization, defaults_only_constructor) {
rosidl_generator_tests::msg::Defaults def(
rosidl_runtime_cpp::MessageInitialization::DEFAULTS_ONLY);
ASSERT_TRUE(def.bool_value);
ASSERT_EQ(50, def.byte_value);
ASSERT_EQ(std::byte{50}, def.byte_value);
ASSERT_EQ(100, def.char_value);
ASSERT_EQ(1.125f, def.float32_value);
ASSERT_EQ(1.125, def.float64_value);
Expand All @@ -158,7 +159,7 @@ TEST(Test_msg_initialization, defaults_only_constructor) {

rosidl_generator_tests::msg::BasicTypes basic;
ASSERT_FALSE(basic.bool_value);
ASSERT_EQ(0, basic.byte_value);
ASSERT_EQ(std::byte{0}, basic.byte_value);
ASSERT_EQ(0, basic.char_value);
ASSERT_EQ(0.0f, basic.float32_value);
ASSERT_EQ(0.0, basic.float64_value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <cstddef>
#include <gtest/gtest.h>

#include "rosidl_generator_tests/srv/basic_types.hpp"
Expand All @@ -22,7 +23,7 @@ TEST(Test_srv_initialization, no_arg_request_constructor) {

rosidl_generator_tests::srv::BasicTypes::Request basic_types;
EXPECT_EQ(false, basic_types.bool_value);
EXPECT_EQ(0, basic_types.byte_value);
EXPECT_EQ(std::byte{0}, basic_types.byte_value);
EXPECT_EQ(0, basic_types.char_value);
EXPECT_EQ(0.0F, basic_types.float32_value);
EXPECT_EQ(0.0, basic_types.float64_value);
Expand All @@ -42,7 +43,7 @@ TEST(Test_srv_initialization, no_arg_response_constructor) {

rosidl_generator_tests::srv::BasicTypes::Response basic_types;
EXPECT_EQ(false, basic_types.bool_value);
EXPECT_EQ(0, basic_types.byte_value);
EXPECT_EQ(std::byte{0}, basic_types.byte_value);
EXPECT_EQ(0, basic_types.char_value);
EXPECT_EQ(0.0f, basic_types.float32_value);
EXPECT_EQ(0.0, basic_types.float64_value);
Expand Down
9 changes: 9 additions & 0 deletions rosidl_runtime_cpp/include/rosidl_runtime_cpp/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define ROSIDL_RUNTIME_CPP__TRAITS_HPP_

#include <codecvt>
#include <cstddef>
#include <cstdint>
#include <iomanip>
#include <iosfwd>
Expand Down Expand Up @@ -46,6 +47,14 @@ inline void character_value_to_yaml(char16_t value, std::ostream & out)
out.flags(flags);
}

inline void value_to_yaml(std::byte value, std::ostream & out)
{
auto flags = out.flags();
out << "0x" << std::hex << std::setw(2) << std::setfill('0') << \
std::to_integer<int>(value);
out.flags(flags);
}

inline void value_to_yaml(float value, std::ostream & out)
{
auto flags = out.flags();
Expand Down
Loading