Skip to content

Add std::vector compatible swap() API to rosidl::Buffer#959

Open
nvcyc wants to merge 1 commit intorollingfrom
nvcyc/rosidl_buffer_apis
Open

Add std::vector compatible swap() API to rosidl::Buffer#959
nvcyc wants to merge 1 commit intorollingfrom
nvcyc/rosidl_buffer_apis

Conversation

@nvcyc
Copy link
Copy Markdown
Contributor

@nvcyc nvcyc commented Apr 22, 2026

Description

Adds the missing swap() APIs to rosidl::Buffer so that it remains a true drop-in replacement for std::vector in CPU-backed message fields.

Specifically the following functions are added for rosidl::Buffer (src/ros2/rosidl/rosidl_buffer/include/rosidl_buffer/buffer.hpp):

  • Member swap(Buffer &)
  • Member swap(std::vector<T, Allocator> &)
  • Non-member rosidl::swap(Buffer&, Buffer&)
  • Non-member rosidl::swap(Buffer&, std::vector&)
  • Non-member rosidl::swap(std::vector&, Buffer&)

Is this user-facing behavior change?

Yes. rosidl::Buffer now supports all the swap forms that std::vector does, including the cross-type cases with std::vector.

Additional Information

This is to fix the issue discovered from ros-perception/perception_pcl#529.

Signed-off-by: CY Chen <cyc@nvidia.com>
EXPECT_EQ(2, buffers[1][0]);
EXPECT_EQ(3, buffers[2][0]);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add tests checking that calling swap on non-CPU buffers throws an exception.

@wentasah
Copy link
Copy Markdown

Confirming that this fixes the problem reported in ros-perception/perception_pcl#529. But note that even with this change, some packages would still need to be updated. Besides PCL, I discovered wep21/bag2_to_image#8, where the compatibility with OpenCV was broken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants