Robowflex  v0.1
Making MoveIt Easy
robowflex::IO::Bag Class Reference

rosbag management class to ease message saving and loading. More...

#include <bag.h>

Public Types

enum  Mode {
  READ ,
  WRITE
}
 File modes. More...
 

Public Member Functions

 Bag (const std::string &file, Mode mode=WRITE)
 Constructor. More...
 
 ~Bag ()
 Destructor. Closes opened bag. More...
 
template<typename T >
bool addMessage (const std::string &topic, const T &msg)
 Adds a message to the bag under topic. More...
 
template<typename T >
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. More...
 

Private Attributes

const Mode mode_
 Mode to open file in. More...
 
const std::string file_
 File opened. More...
 
rosbag::Bag bag_
 rosbag opened. More...
 

Detailed Description

rosbag management class to ease message saving and loading.

Definition at line 22 of file bag.h.

Member Enumeration Documentation

◆ Mode

File modes.

Enumerator
READ 

Read-only.

WRITE 

Write-only.

Definition at line 27 of file bag.h.

28  {
29  READ, ///< Read-only
30  WRITE ///< Write-only
31  };
@ WRITE
Write-only.
Definition: bag.h:30
@ READ
Read-only.
Definition: bag.h:29

Constructor & Destructor Documentation

◆ Bag()

IO::Bag::Bag ( const std::string file,
Mode  mode = WRITE 
)

Constructor.

Parameters
[in]fileFile to open or create.
[in]modeMode to open file in.

IO::Bag

Definition at line 398 of file robowflex_library/src/io.cpp.

399  : mode_(mode)
400  , file_((mode_ == WRITE) ? file : IO::resolvePath(file))
401  , bag_(file_, (mode_ == WRITE) ? rosbag::bagmode::Write : rosbag::bagmode::Read)
402 {
403 }
const std::string file_
File opened.
Definition: bag.h:87
const Mode mode_
Mode to open file in.
Definition: bag.h:86
rosbag::Bag bag_
rosbag opened.
Definition: bag.h:88
std::string resolvePath(const std::string &path)
Resolves package:// URLs and relative file paths to their canonical form.

◆ ~Bag()

IO::Bag::~Bag ( )

Destructor. Closes opened bag.

Definition at line 405 of file robowflex_library/src/io.cpp.

406 {
407  bag_.close();
408 }

Member Function Documentation

◆ addMessage()

template<typename T >
bool robowflex::IO::Bag::addMessage ( const std::string topic,
const T &  msg 
)
inline

Adds a message to the bag under topic.

Parameters
[in]topicTopic to save message under.
[in]msgMessage to write.
Template Parameters
TType of message.

Definition at line 50 of file bag.h.

51  {
52  if (mode_ == WRITE)
53  {
54  bag_.write(topic, ros::Time::now(), msg);
55  return true;
56  }
57 
58  return false;
59  }

◆ getMessages()

template<typename T >
std::vector<T> robowflex::IO::Bag::getMessages ( const std::vector< std::string > &  topics)
inline

Gets messages from an opened bag. Returns all messages of type T from a list of topics topics.

Parameters
[in]topicsList of topics to load messages from.
Template Parameters
Ttype of messages to load from topics.

Definition at line 67 of file bag.h.

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  }
T emplace_back(T... args)

Member Data Documentation

◆ bag_

rosbag::Bag robowflex::IO::Bag::bag_
private

rosbag opened.

Definition at line 88 of file bag.h.

◆ file_

const std::string robowflex::IO::Bag::file_
private

File opened.

Definition at line 87 of file bag.h.

◆ mode_

const Mode robowflex::IO::Bag::mode_
private

Mode to open file in.

Definition at line 86 of file bag.h.


The documentation for this class was generated from the following files: