From 2090d067c1acf65ff63e46f311f19c9965ac48ca Mon Sep 17 00:00:00 2001 From: Erik Boasson Date: Fri, 7 Jun 2019 11:16:37 +0200 Subject: [PATCH] Specialize deserializer for strings (#3) As std::string is not a basic type, "new" must sometimes be called to initialze the memory before passing it as a reference. --- .../rmw_cyclonedds_cpp/TypeSupport_impl.hpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/TypeSupport_impl.hpp b/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/TypeSupport_impl.hpp index 298351a..513ffdb 100644 --- a/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/TypeSupport_impl.hpp +++ b/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/TypeSupport_impl.hpp @@ -421,6 +421,38 @@ void deserialize_field( } } +template<> +inline void deserialize_field( + const rosidl_typesupport_introspection_cpp::MessageMember * member, + void * field, + cycdeser & deser, + bool call_new) +{ + if (!member->is_array_) { + if (call_new) { + // Because std::string is a complex datatype, we need to make sure that + // the memory is initialized to something reasonable before eventually + // passing it as a reference. + new(field) std::string(); + } + deser >> *static_cast(field); + } else if (member->array_size_ && !member->is_upper_bound_) { + std::string * array = static_cast(field); + if (call_new) { + for (size_t i = 0; i < member->array_size_; ++i) { + new(&array[i]) std::string(); + } + } + deser.deserializeA(array, member->array_size_); + } else { + auto & vector = *reinterpret_cast *>(field); + if (call_new) { + new(&vector) std::vector; + } + deser >> vector; + } +} + template void deserialize_field( const rosidl_typesupport_introspection_c__MessageMember * member,