Robowflex  v0.1
Making MoveIt Easy
bag.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef ROBOWFLEX_IO_BAG_
4 #define ROBOWFLEX_IO_BAG_
5 
7 
8 // clang-format off
10 // clang-format on
11 #include <rosbag/bag.h>
13 
14 #include <rosbag/view.h>
15 
16 namespace robowflex
17 {
18  namespace IO
19  {
20  /** \brief `rosbag` management class to ease message saving and loading.
21  */
22  class Bag
23  {
24  public:
25  /** \brief File modes
26  */
27  enum Mode
28  {
29  READ, ///< Read-only
30  WRITE ///< Write-only
31  };
32 
33  /** \brief Constructor.
34  * \param[in] file File to open or create.
35  * \param[in] mode Mode to open file in.
36  */
37  Bag(const std::string &file, Mode mode = WRITE);
38 
39  /** \brief Destructor.
40  * Closes opened bag.
41  */
42  ~Bag();
43 
44  /** \brief Adds a message to the bag under \a topic.
45  * \param[in] topic Topic to save message under.
46  * \param[in] msg Message to write.
47  * \tparam T Type of message.
48  */
49  template <typename T>
50  bool addMessage(const std::string &topic, const T &msg)
51  {
52  if (mode_ == WRITE)
53  {
54  bag_.write(topic, ros::Time::now(), msg);
55  return true;
56  }
57 
58  return false;
59  }
60 
61  /** \brief Gets messages from an opened bag. Returns all messages of type \a T from a list of
62  * topics \a topics.
63  * \param[in] topics List of topics to load messages from.
64  * \tparam T type of messages to load from topics.
65  */
66  template <typename T>
68  {
69  std::vector<T> msgs;
70 
71  if (mode_ != READ)
72  return msgs;
73 
74  rosbag::View view(bag_, rosbag::TopicQuery(topics));
75  for (auto &msg : view)
76  {
77  typename T::ConstPtr ptr = msg.instantiate<T>();
78  if (ptr != nullptr)
79  msgs.emplace_back(*ptr);
80  }
81 
82  return msgs;
83  }
84 
85  private:
86  const Mode mode_; ///< Mode to open file in.
87  const std::string file_; ///< File opened.
88  rosbag::Bag bag_; ///< `rosbag` opened.
89  };
90  } // namespace IO
91 } // namespace robowflex
92 
93 #endif
rosbag management class to ease message saving and loading.
Definition: bag.h:23
~Bag()
Destructor. Closes opened bag.
Mode
File modes.
Definition: bag.h:28
@ WRITE
Write-only.
Definition: bag.h:30
@ READ
Read-only.
Definition: bag.h:29
const std::string file_
File opened.
Definition: bag.h:87
const Mode mode_
Mode to open file in.
Definition: bag.h:86
Bag(const std::string &file, Mode mode=WRITE)
Constructor.
rosbag::Bag bag_
rosbag opened.
Definition: bag.h:88
bool addMessage(const std::string &topic, const T &msg)
Adds a message to the bag under topic.
Definition: bag.h:50
std::vector< T > getMessages(const std::vector< std::string > &topics)
Gets messages from an opened bag. Returns all messages of type T from a list of topics topics.
Definition: bag.h:67
T emplace_back(T... args)
#define ROBOWFLEX_PUSH_DISABLE_GCC_WARNING(warning)
Definition: macros.h:77
#define ROBOWFLEX_POP_GCC
Definition: macros.h:80
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25