From a36e5e7c2f7902fc09869b984aea51a41d8b8848 Mon Sep 17 00:00:00 2001 From: Tim Rakowski Date: Fri, 20 Oct 2017 20:04:35 +0200 Subject: [PATCH] Enabled rosbag::Bag move operations if compiler support is available (#1189) * Enabled rosbag::Bag move operations if compiler support is available * include order --- tools/rosbag_storage/include/rosbag/bag.h | 10 ++++ tools/rosbag_storage/src/bag.cpp | 63 +++++++++++++---------- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/tools/rosbag_storage/include/rosbag/bag.h b/tools/rosbag_storage/include/rosbag/bag.h index 526c1dab03..cb78104c85 100644 --- a/tools/rosbag_storage/include/rosbag/bag.h +++ b/tools/rosbag_storage/include/rosbag/bag.h @@ -57,6 +57,7 @@ #include #include +#include #include #include @@ -111,6 +112,12 @@ class ROSBAG_DECL Bag ~Bag(); +#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES + Bag(Bag&& other); + + Bag& operator=(Bag&& other); +#endif // BOOST_NO_CXX11_RVALUE_REFERENCES + //! Open a bag file. /*! * \param filename The bag file to open @@ -186,9 +193,12 @@ class ROSBAG_DECL Bag void swap(Bag&); private: + // disable copying Bag(const Bag&); Bag& operator=(const Bag&); + void init(); + // This helper function actually does the write with an arbitrary serializable message template void doWrite(std::string const& topic, ros::Time const& time, T const& msg, boost::shared_ptr const& connection_header); diff --git a/tools/rosbag_storage/src/bag.cpp b/tools/rosbag_storage/src/bag.cpp index b030852cfe..09bfd94cbc 100644 --- a/tools/rosbag_storage/src/bag.cpp +++ b/tools/rosbag_storage/src/bag.cpp @@ -57,45 +57,52 @@ using ros::Time; namespace rosbag { -Bag::Bag() : - mode_(bagmode::Write), - version_(0), - compression_(compression::Uncompressed), - chunk_threshold_(768 * 1024), // 768KB chunks - bag_revision_(0), - file_size_(0), - file_header_pos_(0), - index_data_pos_(0), - connection_count_(0), - chunk_count_(0), - chunk_open_(false), - curr_chunk_data_pos_(0), - current_buffer_(0), - decompressed_chunk_(0) +Bag::Bag() { + init(); } -Bag::Bag(string const& filename, uint32_t mode) : - compression_(compression::Uncompressed), - chunk_threshold_(768 * 1024), // 768KB chunks - bag_revision_(0), - file_size_(0), - file_header_pos_(0), - index_data_pos_(0), - connection_count_(0), - chunk_count_(0), - chunk_open_(false), - curr_chunk_data_pos_(0), - current_buffer_(0), - decompressed_chunk_(0) +Bag::Bag(string const& filename, uint32_t mode) { + init(); open(filename, mode); } +#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES + +Bag::Bag(Bag&& other) { + init(); + swap(other); +} + +Bag& Bag::operator=(Bag&& other) { + swap(other); + return *this; +} + +#endif // BOOST_NO_CXX11_RVALUE_REFERENCES + Bag::~Bag() { close(); } +void Bag::init() { + mode_ = bagmode::Write; + version_ = 0; + compression_ = compression::Uncompressed; + chunk_threshold_ = 768 * 1024; // 768KB chunks + bag_revision_ = 0; + file_size_ = 0; + file_header_pos_ = 0; + index_data_pos_ = 0; + connection_count_ = 0; + chunk_count_ = 0; + chunk_open_ = false; + curr_chunk_data_pos_ = 0; + current_buffer_ = 0; + decompressed_chunk_ = 0; +} + void Bag::open(string const& filename, uint32_t mode) { mode_ = (BagMode) mode;