inline void serialize_field<std::string>( const rosidl_typesupport_introspection_c__MessageMember * member, void * field, eprosima::fastcdr::Cdr & ser) { using CStringHelper = StringHelper<rosidl_typesupport_introspection_c__MessageMembers>; if (!member->is_array_) { auto && str = CStringHelper::convert_to_std_string(field); // Control maximum length. if (member->string_upper_bound_ && str.length() > member->string_upper_bound_ + 1) { throw std::runtime_error("string overcomes the maximum length"); } ser << str; } else { // First, cast field to rosidl_generator_c // Then convert to a std::string using StringHelper and serialize the std::string if (member->array_size_ && !member->is_upper_bound_) { // tmpstring is defined here and not below to avoid // memory allocation in every iteration of the for loop std::string tmpstring; auto string_field = static_cast<rosidl_generator_c__String *>(field); for (size_t i = 0; i < member->array_size_; ++i) { tmpstring = string_field[i].data; ser.serialize(tmpstring); } } else { auto & string_sequence_field = *reinterpret_cast<rosidl_generator_c__String__Sequence *>(field); std::vector<std::string> cpp_string_vector; for (size_t i = 0; i < string_sequence_field.size; ++i) { cpp_string_vector.push_back( CStringHelper::convert_to_std_string(string_sequence_field.data[i])); } ser << cpp_string_vector; } } }