From de8aa3fd1c943a6b4717dc549cd4945771cc601e Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Thu, 2 May 2019 11:48:39 -0700 Subject: [PATCH] add WString support (#22) * add WString support Signed-off-by: Dirk Thomas * fix type of string literal Signed-off-by: Dirk Thomas * check wstring_to_u16_string function Signed-off-by: Dirk Thomas * fix style Signed-off-by: Dirk Thomas * fix dll linkage warnings Signed-off-by: Dirk Thomas --- rosidl_typesupport_fastrtps_c/CMakeLists.txt | 4 +- .../wstring_conversion.hpp | 36 ++++++++ .../resource/msg__type_support_c.cpp.em | 86 +++++++++++++++---- .../src/wstring_conversion.cpp | 42 +++++++++ .../CMakeLists.txt | 4 +- .../wstring_conversion.hpp | 33 +++++++ .../resource/msg__type_support.cpp.em | 78 +++++++++++++++-- .../src/wstring_conversion.cpp | 41 +++++++++ 8 files changed, 294 insertions(+), 30 deletions(-) create mode 100644 rosidl_typesupport_fastrtps_c/include/rosidl_typesupport_fastrtps_c/wstring_conversion.hpp create mode 100644 rosidl_typesupport_fastrtps_c/src/wstring_conversion.cpp create mode 100644 rosidl_typesupport_fastrtps_cpp/include/rosidl_typesupport_fastrtps_cpp/wstring_conversion.hpp create mode 100644 rosidl_typesupport_fastrtps_cpp/src/wstring_conversion.cpp diff --git a/rosidl_typesupport_fastrtps_c/CMakeLists.txt b/rosidl_typesupport_fastrtps_c/CMakeLists.txt index fb4f492..6c3b956 100644 --- a/rosidl_typesupport_fastrtps_c/CMakeLists.txt +++ b/rosidl_typesupport_fastrtps_c/CMakeLists.txt @@ -47,7 +47,9 @@ ament_export_include_directories(include) ament_python_install_package(${PROJECT_NAME}) -add_library(${PROJECT_NAME} SHARED src/identifier.cpp) +add_library(${PROJECT_NAME} SHARED + src/identifier.cpp + src/wstring_conversion.cpp) if(WIN32) target_compile_definitions(${PROJECT_NAME} PRIVATE "ROSIDL_TYPESUPPORT_FASTRTPS_C_BUILDING_DLL") diff --git a/rosidl_typesupport_fastrtps_c/include/rosidl_typesupport_fastrtps_c/wstring_conversion.hpp b/rosidl_typesupport_fastrtps_c/include/rosidl_typesupport_fastrtps_c/wstring_conversion.hpp new file mode 100644 index 0000000..64cfbfc --- /dev/null +++ b/rosidl_typesupport_fastrtps_c/include/rosidl_typesupport_fastrtps_c/wstring_conversion.hpp @@ -0,0 +1,36 @@ +// Copyright 2019 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef ROSIDL_TYPESUPPORT_FASTRTPS_C__WSTRING_CONVERSION_HPP_ +#define ROSIDL_TYPESUPPORT_FASTRTPS_C__WSTRING_CONVERSION_HPP_ + +#include + +#include "rosidl_generator_c/u16string.h" +#include "rosidl_typesupport_fastrtps_c/visibility_control.h" + +namespace rosidl_typesupport_fastrtps_c +{ + +ROSIDL_TYPESUPPORT_FASTRTPS_C_PUBLIC +void u16string_to_wstring( + const rosidl_generator_c__U16String & u16str, std::wstring & wstr); + +ROSIDL_TYPESUPPORT_FASTRTPS_C_PUBLIC +bool wstring_to_u16string( + const std::wstring & wstr, rosidl_generator_c__U16String & u16str); + +} // namespace rosidl_typesupport_fastrtps_c + +#endif // ROSIDL_TYPESUPPORT_FASTRTPS_C__WSTRING_CONVERSION_HPP_ diff --git a/rosidl_typesupport_fastrtps_c/resource/msg__type_support_c.cpp.em b/rosidl_typesupport_fastrtps_c/resource/msg__type_support_c.cpp.em index b0ef8d9..3f738cf 100644 --- a/rosidl_typesupport_fastrtps_c/resource/msg__type_support_c.cpp.em +++ b/rosidl_typesupport_fastrtps_c/resource/msg__type_support_c.cpp.em @@ -24,6 +24,7 @@ header_files = [ 'string', # Provides the rosidl_typesupport_fastrtps_c__identifier symbol declaration. 'rosidl_typesupport_fastrtps_c/identifier.h', + 'rosidl_typesupport_fastrtps_c/wstring_conversion.hpp', # Provides the definition of the message_type_support_callbacks_t struct. 'rosidl_typesupport_fastrtps_cpp/message_type_support.h', package_name + '/msg/rosidl_typesupport_fastrtps_c__visibility_control.h', @@ -197,6 +198,21 @@ if isinstance(type_, AbstractNestedType): } cdr << str->data; } +@[ elif isinstance(member.type.value_type, AbstractWString)]@ + std::wstring wstr; + for (size_t i = 0; i < size; ++i) { + const rosidl_generator_c__U16String * str = &array_ptr[i]; + if (str->capacity == 0 || str->capacity <= str->size) { + fprintf(stderr, "string capacity not greater than size\n"); + return false; + } + if (str->data[str->size] != u'\0') { + fprintf(stderr, "string not null-terminated\n"); + return false; + } + rosidl_typesupport_fastrtps_c::u16string_to_wstring(*str, wstr); + cdr << wstr; + } @[ elif isinstance(member.type.value_type, BasicType)]@ cdr.serializeArray(array_ptr, size); @[ else]@ @@ -219,6 +235,10 @@ if isinstance(type_, AbstractNestedType): return false; } cdr << str->data; +@[ elif isinstance(member.type, AbstractWString)]@ + std::wstring wstr; + rosidl_typesupport_fastrtps_c::u16string_to_wstring(ros_message->@(member.name), wstr); + cdr << wstr; @[ elif isinstance(member.type, BasicType) and member.type.typename == 'boolean']@ cdr << (ros_message->@(member.name) ? true : false); @[ elif isinstance(member.type, BasicType)]@ @@ -291,19 +311,15 @@ else: } auto array_ptr = ros_message->@(member.name).data; @[ end if]@ -@[ if isinstance(member.type.value_type, AbstractGenericString)]@ -@{ -string_type = 'std::string' if isinstance(member.type.value_type, AbstractString) else 'std::u16string' -string_typename = 'rosidl_generator_c__String' if isinstance(member.type.value_type, AbstractString) else 'rosidl_generator_c__U16string' -}@ +@[ if isinstance(member.type.value_type, AbstractString)]@ for (size_t i = 0; i < size; ++i) { - @(string_type) tmp; + std::string tmp; cdr >> tmp; auto & ros_i = array_ptr[i]; if (!ros_i.data) { - @(string_typename)__init(&ros_i); + rosidl_generator_c__String__init(&ros_i); } - bool succeeded = @(string_typename)__assign( + bool succeeded = rosidl_generator_c__String__assign( &ros_i, tmp.c_str()); if (!succeeded) { @@ -311,6 +327,21 @@ string_typename = 'rosidl_generator_c__String' if isinstance(member.type.value_t return false; } } +@[ elif isinstance(member.type.value_type, AbstractWString)]@ + std::wstring wstr; + for (size_t i = 0; i < size; ++i) { + auto & ros_i = array_ptr[i]; + if (!ros_i.data) { + rosidl_generator_c__U16String__init(&ros_i); + } + cdr >> wstr; + bool succeeded = rosidl_typesupport_fastrtps_c::wstring_to_u16string(wstr, ros_i); + if (!succeeded) { + fprintf(stderr, "failed to create wstring from u16string\n"); + rosidl_generator_c__U16String__fini(&ros_i); + return false; + } + } @[ elif isinstance(member.type.value_type, BasicType)]@ cdr.deserializeArray(array_ptr, size); @[ else]@ @@ -322,23 +353,31 @@ string_typename = 'rosidl_generator_c__String' if isinstance(member.type.value_t } } @[ end if]@ -@[ elif isinstance(member.type, AbstractGenericString)]@ -@{ -string_type = 'std::string' if isinstance(member.type, AbstractString) else 'std::u16string' -string_typename = 'rosidl_generator_c__String' if isinstance(member.type, AbstractString) else 'rosidl_generator_c__U16string' -}@ - @(string_type) tmp; +@[ elif isinstance(member.type, AbstractString)]@ + std::string tmp; cdr >> tmp; if (!ros_message->@(member.name).data) { - @(string_typename)__init(&ros_message->@(member.name)); + rosidl_generator_c__String__init(&ros_message->@(member.name)); } - bool succeeded = @(string_typename)__assign( + bool succeeded = rosidl_generator_c__String__assign( &ros_message->@(member.name), tmp.c_str()); if (!succeeded) { fprintf(stderr, "failed to assign string into field '@(member.name)'\n"); return false; } +@[ elif isinstance(member.type, AbstractWString)]@ + if (!ros_message->@(member.name).data) { + rosidl_generator_c__U16String__init(&ros_message->@(member.name)); + } + std::wstring wstr; + cdr >> wstr; + bool succeeded = rosidl_typesupport_fastrtps_c::wstring_to_u16string(wstr, ros_message->@(member.name)); + if (!succeeded) { + fprintf(stderr, "failed to create wstring from u16string\n"); + rosidl_generator_c__U16String__fini(&ros_message->@(member.name)); + return false; + } @[ elif isinstance(member.type, BasicType)]@ cdr >> ros_message->@(member.name); @[ else]@ @@ -379,10 +418,13 @@ size_t get_serialized_size_@('__'.join([package_name] + list(interface_path.pare current_alignment += padding + eprosima::fastcdr::Cdr::alignment(current_alignment, padding); @[ end if]@ -@[ if isinstance(member.type.value_type, AbstractString)]@ +@[ if isinstance(member.type.value_type, AbstractGenericString)]@ for (size_t index = 0; index < array_size; ++index) { current_alignment += padding + eprosima::fastcdr::Cdr::alignment(current_alignment, padding) + +@[ if isinstance(member.type.value_type, AbstractWString)]@ + 2 * +@[ end if]@ array_ptr[index].size + 1; } @[ elif isinstance(member.type.value_type, BasicType)]@ @@ -398,9 +440,12 @@ size_t get_serialized_size_@('__'.join([package_name] + list(interface_path.pare @[ end if]@ } @[ else]@ -@[ if isinstance(member.type, AbstractString)]@ +@[ if isinstance(member.type, AbstractGenericString)]@ current_alignment += padding + eprosima::fastcdr::Cdr::alignment(current_alignment, padding) + +@[ if isinstance(member.type, AbstractWString)]@ + 2 * +@[ end if]@ ros_message->@(member.name).size + 1; @[ elif isinstance(member.type, BasicType)]@ { @@ -461,12 +506,15 @@ type_ = member.type if isinstance(type_, AbstractNestedType): type_ = type_.value_type }@ -@[ if isinstance(type_, AbstractString)]@ +@[ if isinstance(type_, AbstractGenericString)]@ full_bounded = false; for (size_t index = 0; index < array_size; ++index) { current_alignment += padding + @[ if type_.has_maximum_size()]@ eprosima::fastcdr::Cdr::alignment(current_alignment, padding) + +@[ if isinstance(type_, AbstractWString)]@ + 2 * +@[ end if]@ @(type_.maximum_size) + 1; @[ else]@ eprosima::fastcdr::Cdr::alignment(current_alignment, padding) + 1; diff --git a/rosidl_typesupport_fastrtps_c/src/wstring_conversion.cpp b/rosidl_typesupport_fastrtps_c/src/wstring_conversion.cpp new file mode 100644 index 0000000..4a6dba5 --- /dev/null +++ b/rosidl_typesupport_fastrtps_c/src/wstring_conversion.cpp @@ -0,0 +1,42 @@ +// Copyright 2019 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "rosidl_generator_c/u16string_functions.h" + +namespace rosidl_typesupport_fastrtps_c +{ + +void u16string_to_wstring(const rosidl_generator_c__U16String & u16str, std::wstring & wstr) +{ + wstr.resize(u16str.size); + for (size_t i = 0; i < u16str.size; ++i) { + wstr[i] = static_cast(u16str.data[i]); + } +} + +bool wstring_to_u16string(const std::wstring & wstr, rosidl_generator_c__U16String & u16str) +{ + bool succeeded = rosidl_generator_c__U16String__resize(&u16str, wstr.size()); + if (!succeeded) { + return false; + } + for (size_t i = 0; i < wstr.size(); ++i) { + u16str.data[i] = static_cast(wstr[i]); + } + return true; +} + +} // namespace rosidl_typesupport_fastrtps_c diff --git a/rosidl_typesupport_fastrtps_cpp/CMakeLists.txt b/rosidl_typesupport_fastrtps_cpp/CMakeLists.txt index a0ac9d3..e69d2c6 100644 --- a/rosidl_typesupport_fastrtps_cpp/CMakeLists.txt +++ b/rosidl_typesupport_fastrtps_cpp/CMakeLists.txt @@ -44,7 +44,9 @@ ament_export_include_directories(include) ament_python_install_package(${PROJECT_NAME}) -add_library(${PROJECT_NAME} SHARED src/identifier.cpp) +add_library(${PROJECT_NAME} SHARED + src/identifier.cpp + src/wstring_conversion.cpp) if(WIN32) target_compile_definitions(${PROJECT_NAME} PRIVATE "ROSIDL_TYPESUPPORT_FASTRTPS_CPP_BUILDING_DLL") diff --git a/rosidl_typesupport_fastrtps_cpp/include/rosidl_typesupport_fastrtps_cpp/wstring_conversion.hpp b/rosidl_typesupport_fastrtps_cpp/include/rosidl_typesupport_fastrtps_cpp/wstring_conversion.hpp new file mode 100644 index 0000000..703e4fb --- /dev/null +++ b/rosidl_typesupport_fastrtps_cpp/include/rosidl_typesupport_fastrtps_cpp/wstring_conversion.hpp @@ -0,0 +1,33 @@ +// Copyright 2019 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef ROSIDL_TYPESUPPORT_FASTRTPS_CPP__WSTRING_CONVERSION_HPP_ +#define ROSIDL_TYPESUPPORT_FASTRTPS_CPP__WSTRING_CONVERSION_HPP_ + +#include + +#include + +namespace rosidl_typesupport_fastrtps_cpp +{ + +ROSIDL_TYPESUPPORT_FASTRTPS_CPP_PUBLIC +void u16string_to_wstring(const std::u16string & u16str, std::wstring & wstr); + +ROSIDL_TYPESUPPORT_FASTRTPS_CPP_PUBLIC +bool wstring_to_u16string(const std::wstring & wstr, std::u16string & u16str); + +} // namespace rosidl_typesupport_fastrtps_cpp + +#endif // ROSIDL_TYPESUPPORT_FASTRTPS_CPP__WSTRING_CONVERSION_HPP_ diff --git a/rosidl_typesupport_fastrtps_cpp/resource/msg__type_support.cpp.em b/rosidl_typesupport_fastrtps_cpp/resource/msg__type_support.cpp.em index 02ab4ec..7c53266 100644 --- a/rosidl_typesupport_fastrtps_cpp/resource/msg__type_support.cpp.em +++ b/rosidl_typesupport_fastrtps_cpp/resource/msg__type_support.cpp.em @@ -1,8 +1,9 @@ @# Included from rosidl_typesupport_fastrtps_cpp/resource/idl__type_support.cpp.em @{ +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 BasicType from rosidl_parser.definition import BoundedSequence @@ -16,6 +17,7 @@ header_files = [ 'rosidl_typesupport_fastrtps_cpp/identifier.hpp', 'rosidl_typesupport_fastrtps_cpp/message_type_support.h', 'rosidl_typesupport_fastrtps_cpp/message_type_support_decl.hpp', + 'rosidl_typesupport_fastrtps_cpp/wstring_conversion.hpp', 'fastcdr/Cdr.h', ] }@ @@ -89,17 +91,25 @@ cdr_serialize( @[ if isinstance(member.type, AbstractNestedType)]@ { @[ if isinstance(member.type, Array)]@ -@[ if not isinstance(member.type.value_type, NamespacedType)]@ +@[ if not isinstance(member.type.value_type, (NamespacedType, AbstractWString))]@ cdr << ros_message.@(member.name); @[ else]@ +@[ if isinstance(member.type.value_type, AbstractWString)]@ + std::wstring wstr; +@[ end if]@ for (size_t i = 0; i < @(member.type.size); i++) { +@[ if isinstance(member.type.value_type, NamespacedType)]@ @('::'.join(member.type.value_type.namespaces))::typesupport_fastrtps_cpp::cdr_serialize( ros_message.@(member.name)[i], cdr); +@[ else]@ + rosidl_typesupport_fastrtps_cpp::u16string_to_wstring(ros_message.@(member.name)[i], wstr); + cdr << wstr; +@[ end if]@ } @[ end if]@ @[ else]@ -@[ if isinstance(member.type, BoundedSequence) or isinstance(member.type.value_type, NamespacedType)]@ +@[ if isinstance(member.type, BoundedSequence) or isinstance(member.type.value_type, (NamespacedType, AbstractWString))]@ size_t size = ros_message.@(member.name).size(); @[ if isinstance(member.type, BoundedSequence)]@ if (size > @(member.type.maximum_size)) { @@ -107,13 +117,19 @@ cdr_serialize( } @[ end if]@ @[ end if]@ -@[ if not isinstance(member.type.value_type, NamespacedType) and not isinstance(member.type, BoundedSequence)]@ +@[ if not isinstance(member.type.value_type, (NamespacedType, AbstractWString)) and not isinstance(member.type, BoundedSequence)]@ cdr << ros_message.@(member.name); @[ else]@ cdr << static_cast(size); +@[ if isinstance(member.type.value_type, AbstractWString)]@ + std::wstring wstr; +@[ end if]@ for (size_t i = 0; i < size; i++) { @[ if isinstance(member.type.value_type, BasicType) and member.type.value_type.typename == 'boolean']@ cdr << (ros_message.@(member.name)[i] ? true : false); +@[ elif isinstance(member.type.value_type, AbstractWString)]@ + rosidl_typesupport_fastrtps_cpp::u16string_to_wstring(ros_message.@(member.name)[i], wstr); + cdr << wstr; @[ elif not isinstance(member.type.value_type, NamespacedType)]@ cdr << ros_message.@(member.name)[i]; @[ else]@ @@ -127,6 +143,12 @@ cdr_serialize( } @[ elif isinstance(member.type, BasicType) and member.type.typename == 'boolean']@ cdr << (ros_message.@(member.name) ? true : false); +@[ elif isinstance(member.type, AbstractWString)]@ + { + std::wstring wstr; + rosidl_typesupport_fastrtps_cpp::u16string_to_wstring(ros_message.@(member.name), wstr); + cdr << wstr; + } @[ elif not isinstance(member.type, NamespacedType)]@ cdr << ros_message.@(member.name); @[ else]@ @@ -149,28 +171,50 @@ cdr_deserialize( @[ if isinstance(member.type, AbstractNestedType)]@ { @[ if isinstance(member.type, Array)]@ -@[ if not isinstance(member.type.value_type, NamespacedType)]@ +@[ if not isinstance(member.type.value_type, (NamespacedType, AbstractWString))]@ cdr >> ros_message.@(member.name); @[ else]@ +@[ if isinstance(member.type.value_type, AbstractWString)]@ + std::wstring wstr; +@[ end if]@ for (size_t i = 0; i < @(member.type.size); i++) { +@[ if isinstance(member.type.value_type, NamespacedType)]@ @('::'.join(member.type.value_type.namespaces))::typesupport_fastrtps_cpp::cdr_deserialize( cdr, ros_message.@(member.name)[i]); +@[ else]@ + cdr >> wstr; + bool succeeded = rosidl_typesupport_fastrtps_cpp::wstring_to_u16string(wstr, ros_message.@(member.name)[i]); + if (!succeeded) { + fprintf(stderr, "failed to create wstring from u16string\n"); + return false; + } +@[ end if]@ } @[ end if]@ @[ else]@ -@[ if not isinstance(member.type.value_type, NamespacedType) and not isinstance(member.type, BoundedSequence)]@ +@[ if not isinstance(member.type.value_type, (NamespacedType, AbstractWString)) and not isinstance(member.type, BoundedSequence)]@ cdr >> ros_message.@(member.name); @[ else]@ uint32_t cdrSize; cdr >> cdrSize; size_t size = static_cast(cdrSize); ros_message.@(member.name).resize(size); +@[ if isinstance(member.type.value_type, AbstractWString)]@ + std::wstring wstr; +@[ end if]@ for (size_t i = 0; i < size; i++) { @[ if isinstance(member.type.value_type, BasicType) and member.type.value_type.typename == 'boolean']@ uint8_t tmp; cdr >> tmp; ros_message.@(member.name)[i] = tmp ? true : false; +@[ elif isinstance(member.type.value_type, AbstractWString)]@ + cdr >> wstr; + bool succeeded = rosidl_typesupport_fastrtps_cpp::wstring_to_u16string(wstr, ros_message.@(member.name)[i]); + if (!succeeded) { + fprintf(stderr, "failed to create wstring from u16string\n"); + return false; + } @[ elif not isinstance(member.type.value_type, NamespacedType)]@ cdr >> ros_message.@(member.name)[i]; @[ else]@ @@ -187,6 +231,16 @@ cdr_deserialize( cdr >> tmp; ros_message.@(member.name) = tmp ? true : false; } +@[ elif isinstance(member.type, AbstractWString)]@ + { + std::wstring wstr; + cdr >> wstr; + bool succeeded = rosidl_typesupport_fastrtps_cpp::wstring_to_u16string(wstr, ros_message.@(member.name)); + if (!succeeded) { + fprintf(stderr, "failed to create wstring from u16string\n"); + return false; + } + } @[ elif not isinstance(member.type, NamespacedType)]@ cdr >> ros_message.@(member.name); @[ else]@ @@ -226,10 +280,13 @@ get_serialized_size( current_alignment += padding + eprosima::fastcdr::Cdr::alignment(current_alignment, padding); @[ end if]@ -@[ if isinstance(member.type.value_type, AbstractString)]@ +@[ if isinstance(member.type.value_type, AbstractGenericString)]@ for (size_t index = 0; index < array_size; ++index) { current_alignment += padding + eprosima::fastcdr::Cdr::alignment(current_alignment, padding) + +@[ if isinstance(member.type.value_type, AbstractWString)]@ + 2 * +@[ end if]@ ros_message.@(member.name)[index].size() + 1; } @[ elif isinstance(member.type.value_type, BasicType)]@ @@ -245,9 +302,12 @@ get_serialized_size( @[ end if]@ } @[ else]@ -@[ if isinstance(member.type, AbstractString)]@ +@[ if isinstance(member.type, AbstractGenericString)]@ current_alignment += padding + eprosima::fastcdr::Cdr::alignment(current_alignment, padding) + +@[ if isinstance(member.type, AbstractWString)]@ + 2 * +@[ end if]@ ros_message.@(member.name).size() + 1; @[ elif isinstance(member.type, BasicType)]@ { @@ -303,7 +363,7 @@ type_ = member.type if isinstance(type_, AbstractNestedType): type_ = type_.value_type }@ -@[ if isinstance(type_, AbstractString)]@ +@[ if isinstance(type_, AbstractGenericString)]@ full_bounded = false; for (size_t index = 0; index < array_size; ++index) { current_alignment += padding + diff --git a/rosidl_typesupport_fastrtps_cpp/src/wstring_conversion.cpp b/rosidl_typesupport_fastrtps_cpp/src/wstring_conversion.cpp new file mode 100644 index 0000000..387359d --- /dev/null +++ b/rosidl_typesupport_fastrtps_cpp/src/wstring_conversion.cpp @@ -0,0 +1,41 @@ +// Copyright 2019 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +namespace rosidl_typesupport_fastrtps_cpp +{ + +void u16string_to_wstring(const std::u16string & u16str, std::wstring & wstr) +{ + wstr.resize(u16str.size()); + for (size_t i = 0; i < u16str.size(); ++i) { + wstr[i] = static_cast(u16str[i]); + } +} + +bool wstring_to_u16string(const std::wstring & wstr, std::u16string & u16str) +{ + try { + u16str.resize(wstr.size()); + } catch (...) { + return false; + } + for (size_t i = 0; i < wstr.size(); ++i) { + u16str[i] = static_cast(wstr[i]); + } + return true; +} + +} // namespace rosidl_typesupport_fastrtps_cpp