forked from ros2/rosidl_typesupport_fastrtps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve wide string (de)serialization (ros2#113)
* Added serialization helpers for char16_t strings in rosidl_typesupport_fastrtps_c. * Change generated code in rosidl_typesupport_fastrtps_c * Added serialization helpers for char16_t strings in rosidl_typesupport_fastrtps_cpp. * Change generated code in rosidl_typesupport_fastrtps_cpp * Throw overflow_error when size does not fit. * Throw exception when deserialized char is too big. * Deprecate unused methods * Fix dependency on fastcdr Signed-off-by: Miguel Company <[email protected]>
- Loading branch information
1 parent
0d4b6d6
commit b4f63c0
Showing
13 changed files
with
301 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...dl_typesupport_fastrtps_c/include/rosidl_typesupport_fastrtps_c/serialization_helpers.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// 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__SERIALIZATION_HELPERS_HPP_ | ||
#define ROSIDL_TYPESUPPORT_FASTRTPS_C__SERIALIZATION_HELPERS_HPP_ | ||
|
||
#include <limits> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
#include "fastcdr/Cdr.h" | ||
#include "fastcdr/exceptions/BadParamException.h" | ||
|
||
#include "rosidl_runtime_c/u16string.h" | ||
#include "rosidl_runtime_c/u16string_functions.h" | ||
|
||
#include "rosidl_typesupport_fastrtps_c/visibility_control.h" | ||
|
||
namespace rosidl_typesupport_fastrtps_c | ||
{ | ||
|
||
inline void cdr_serialize( | ||
eprosima::fastcdr::Cdr & cdr, | ||
const rosidl_runtime_c__U16String & u16str) | ||
{ | ||
if (u16str.size > std::numeric_limits<uint32_t>::max()) { | ||
throw std::overflow_error("String length exceeds does not fit in CDR serialization format"); | ||
} | ||
auto len = static_cast<uint32_t>(u16str.size); | ||
cdr << len; | ||
for (uint32_t i = 0; i < len; ++i) { | ||
// We are serializing a uint32_t for interoperability with other DDS-based implementations. | ||
// We might change this to a uint16_t in the future if we don't mind breaking backward | ||
// compatibility and we make the change for all the supported DDS implementations at the same | ||
// time. | ||
uint32_t c = static_cast<uint32_t>(u16str.data[i]); | ||
cdr << c; | ||
} | ||
} | ||
|
||
inline bool cdr_deserialize( | ||
eprosima::fastcdr::Cdr & cdr, | ||
rosidl_runtime_c__U16String & u16str) | ||
{ | ||
uint32_t len; | ||
cdr >> len; | ||
bool succeeded = rosidl_runtime_c__U16String__resize(&u16str, len); | ||
if (!succeeded) { | ||
return false; | ||
} | ||
|
||
for (uint32_t i = 0; i < len; ++i) { | ||
// We are serializing a uint32_t for interoperability with other DDS-based implementations. | ||
// If we change this to a uint16_t in the future, we could remove the check below, since all | ||
// serialized values would fit in the destination type. | ||
uint32_t c; | ||
cdr >> c; | ||
if (c > std::numeric_limits<uint_least16_t>::max()) { | ||
throw eprosima::fastcdr::exception::BadParamException( | ||
"Character value exceeds maximum value"); | ||
} | ||
u16str.data[i] = static_cast<uint_least16_t>(c); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} // namespace rosidl_typesupport_fastrtps_c | ||
|
||
#endif // ROSIDL_TYPESUPPORT_FASTRTPS_C__SERIALIZATION_HELPERS_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.