Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rosbag::View::iterator copy assignment operator #1017

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions test/test_rosbag_storage/src/create_and_iterate_bag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
#include "boost/foreach.hpp"
#include <gtest/gtest.h>


TEST(rosbag_storage, create_and_iterate_bag)
{
const char* bag_filename = "/tmp/rosbag_storage_create_and_iterate_bag.bag";
void create_test_bag(const std::string &filename)
{
rosbag::Bag bag;
bag.open(bag_filename, rosbag::bagmode::Write);
bag.open(filename, rosbag::bagmode::Write);

std_msgs::String str;
str.data = std::string("foo");
Expand All @@ -30,6 +27,42 @@ TEST(rosbag_storage, create_and_iterate_bag)
bag.close();
}

const char* bag_filename = "/tmp/rosbag_storage_create_and_iterate_bag.bag";

TEST(rosbag_storage, iterator_copy_constructor)
{
// copy ctor
rosbag::Bag bag;
bag.open(bag_filename, rosbag::bagmode::Read);
rosbag::View view(bag, rosbag::TopicQuery("numbers"));
rosbag::View::const_iterator it0 = view.begin();
EXPECT_EQ(42, it0->instantiate<std_msgs::Int32>()->data);
rosbag::View::const_iterator it1(it0);
EXPECT_EQ(it0, it1);
EXPECT_EQ(42, it1->instantiate<std_msgs::Int32>()->data);
++it1;
EXPECT_NE(it0, it1);
EXPECT_EQ(42, it0->instantiate<std_msgs::Int32>()->data);
}

TEST(rosbag_storage, iterator_copy_assignment)
{
// copy assignment
rosbag::Bag bag;
bag.open(bag_filename, rosbag::bagmode::Read);
rosbag::View view(bag, rosbag::TopicQuery("numbers"));
rosbag::View::const_iterator it0 = view.begin();
EXPECT_EQ(42, it0->instantiate<std_msgs::Int32>()->data);
rosbag::View::const_iterator it1;
it1 = it0;
EXPECT_EQ(it0, it1);
EXPECT_EQ(42, it1->instantiate<std_msgs::Int32>()->data);
++it1;
EXPECT_NE(it0, it1);
EXPECT_EQ(42, it0->instantiate<std_msgs::Int32>()->data);
}

TEST(rosbag_storage, iterate_bag)
{
rosbag::Bag bag;
bag.open(bag_filename, rosbag::bagmode::Read);
Expand Down Expand Up @@ -71,10 +104,10 @@ TEST(rosbag_storage, create_and_iterate_bag)

bag.close();
}
}

int main(int argc, char **argv) {
ros::Time::init();
create_test_bag(bag_filename);

testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
Expand Down
1 change: 1 addition & 0 deletions tools/rosbag_storage/include/rosbag/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class ROSBAG_DECL View
{
public:
iterator(iterator const& i);
iterator &operator=(iterator const& i);
iterator();
~iterator();

Expand Down
13 changes: 13 additions & 0 deletions tools/rosbag_storage/src/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ View::iterator::iterator(View* view, bool end) : view_(view), view_revision_(0),

View::iterator::iterator(const iterator& i) : view_(i.view_), iters_(i.iters_), view_revision_(i.view_revision_), message_instance_(NULL) { }

View::iterator &View::iterator::operator=(iterator const& i) {
if (this != &i) {
view_ = i.view_;
iters_ = i.iters_;
view_revision_ = i.view_revision_;
if (message_instance_ != NULL) {
delete message_instance_;
message_instance_ = NULL;
}
}
return *this;
}

void View::iterator::populate() {
assert(view_ != NULL);

Expand Down