From 3fb5e5f4caf78926ffb65ed4800adf6ae36eb09c Mon Sep 17 00:00:00 2001 From: Sam Privett Date: Tue, 9 Jan 2024 07:07:10 -0800 Subject: [PATCH] Add wchar support (#349) * Add wchar support and .idl example * Undo automatic IDE formatting noise * Added back unused imports to see if this fixes the build * More attempts to fix the weird build failure * Removed the linter tests for auto-generated message source files in `rclrs_example_msgs`. Re-applied some changes removed when root causing. --------- Co-authored-by: Sam Privett --- examples/message_demo/src/message_demo.rs | 9 ++ rclrs_example_msgs/CMakeLists.txt | 3 +- rclrs_example_msgs/msg/MyMessage.idl | 86 +++++++++++++++++++ .../rosidl_generator_rs/__init__.py | 9 +- 4 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 rclrs_example_msgs/msg/MyMessage.idl diff --git a/examples/message_demo/src/message_demo.rs b/examples/message_demo/src/message_demo.rs index 76d46f67a..8f3bb2a63 100644 --- a/examples/message_demo/src/message_demo.rs +++ b/examples/message_demo/src/message_demo.rs @@ -68,6 +68,14 @@ fn check_default_values() { ); } +fn check_default_idl_values() { + let idiomatic_msg = rclrs_example_msgs::msg::MyMessage::default(); + let rmw_msg = rclrs_example_msgs::msg::rmw::MyMessage::default(); + + assert_eq!(idiomatic_msg.wchar_value, 0u16); + assert_eq!(rmw_msg.wchar_value, 0u16); +} + fn demonstrate_printing() { let default_msg = rclrs_example_msgs::msg::VariousTypes::default(); println!("================== Compact debug representation =================="); @@ -170,6 +178,7 @@ fn demonstrate_pubsub() -> Result<(), Error> { fn main() -> Result<(), Error> { check_default_values(); + check_default_idl_values(); demonstrate_printing(); demonstrate_serde()?; demonstrate_sequences(); diff --git a/rclrs_example_msgs/CMakeLists.txt b/rclrs_example_msgs/CMakeLists.txt index aa7cff63e..f5f76cc41 100644 --- a/rclrs_example_msgs/CMakeLists.txt +++ b/rclrs_example_msgs/CMakeLists.txt @@ -16,10 +16,11 @@ find_package(rosidl_default_generators REQUIRED) set(msg_files "msg/NestedType.msg" "msg/VariousTypes.msg" + "msg/MyMessage.idl" ) + rosidl_generate_interfaces(${PROJECT_NAME} ${msg_files} - ADD_LINTER_TESTS ) ament_export_dependencies(rosidl_default_runtime) diff --git a/rclrs_example_msgs/msg/MyMessage.idl b/rclrs_example_msgs/msg/MyMessage.idl new file mode 100644 index 000000000..d33f9bbac --- /dev/null +++ b/rclrs_example_msgs/msg/MyMessage.idl @@ -0,0 +1,86 @@ +// Taken and slightly adapted from +// https://github.com/ros2/rosidl/blob/iron/rosidl_parser/test/msg/MyMessage.idl + +module rclrs_example_msgs { + module msg { + module MyMessage_Constants { + const short SHORT_CONSTANT = -23; + const unsigned long UNSIGNED_LONG_CONSTANT = 42; + const float FLOAT_CONSTANT = 1.25; + const boolean BOOLEAN_CONSTANT = TRUE; + const string STRING_CONSTANT = "string_value"; + const wstring WSTRING_CONSTANT = "wstring_value_\u2122"; + const string EMPTY_STRING_CONSTANT = ""; + }; + + @verbatim ( language="comment", text="Documentation of MyMessage." "Adjacent string literal." ) + @transfer_mode(SHMEM_REF) + struct MyMessage { + short short_value, short_value2; + @default ( value=123 ) + unsigned short unsigned_short_value; + @key + @range ( min=-10, max=10 ) + long long_value; + @verbatim (language="comment", text="") + unsigned long unsigned_long_value; + long long long_long_value; + unsigned long long unsigned_long_long_value; + float float_value; + double double_value; +// long double long_double_value; + char char_value; + wchar wchar_value; + boolean boolean_value; + octet octet_value; + int8 int8_value; + uint8 uint8_value; + int16 int16_value; + uint16 uint16_value; + int32 int32_value; + uint32 uint32_value; + int64 int64_value; + uint64 uint64_value; + string string_value; + string<5> bounded_string_value; + wstring wstring_value; + wstring<23> bounded_wstring_value; +// wstring constant_bounded_wstring_value; + sequence unbounded_short_values; + sequence bounded_short_values; + sequence> unbounded_values_of_bounded_strings; + sequence, 4> bounded_values_of_bounded_strings; + short array_short_values[23]; + + // Tests of the floating point parser (7.2.6.4) + @default ( value=1.9e10 ) + float int_and_frac_with_positive_scientific; + @default ( value=1.9e+10 ) + float int_and_frac_with_explicit_positive_scientific; + @default ( value=1.1e-10) + float int_and_frac_with_negative_scientific; + @default ( value=0.00009 ) + float int_and_frac; + @default ( value = 1. ) + float int_with_empty_frac; + @default ( value = .1 ) + float frac_only; + @default ( value=9e05 ) + float int_with_positive_scientific; + @default ( value=9e+05 ) + float int_with_explicit_positive_scientific; + @default ( value=9e-05 ) + float int_with_negative_scientific; + + // Tests of the fixed point parser (7.2.6.5) + @default ( value=8.7d ) + float fixed_int_and_frac; + @default ( value=4.d ) + float fixed_int_with_dot_only; + @default ( value=.3d ) + float fixed_frac_only; + @default ( value=7d ) + float fixed_int_only; + }; + }; +}; diff --git a/rosidl_generator_rs/rosidl_generator_rs/__init__.py b/rosidl_generator_rs/rosidl_generator_rs/__init__.py index 762fd4884..502d1d34d 100644 --- a/rosidl_generator_rs/rosidl_generator_rs/__init__.py +++ b/rosidl_generator_rs/rosidl_generator_rs/__init__.py @@ -14,7 +14,6 @@ import os import pathlib -import subprocess from pathlib import Path @@ -24,12 +23,7 @@ import rosidl_pycommon from rosidl_parser.definition import AbstractGenericString -from rosidl_parser.definition import AbstractNestedType -from rosidl_parser.definition import AbstractSequence -from rosidl_parser.definition import AbstractString -from rosidl_parser.definition import AbstractWString from rosidl_parser.definition import Array -from rosidl_parser.definition import BASIC_TYPES from rosidl_parser.definition import BasicType from rosidl_parser.definition import BoundedSequence from rosidl_parser.definition import BoundedString @@ -219,6 +213,7 @@ def primitive_value_to_rs(type_, value): if type_.type in [ 'byte', 'char', + 'wchar', 'int8', 'uint8', 'int16', @@ -311,6 +306,8 @@ def get_rmw_rs_type(type_): return 'u8' elif type_.typename == 'char': return 'u8' + elif type_.typename == 'wchar': + return 'u16' elif type_.typename == 'float': return 'f32' elif type_.typename == 'double':