LCM
lcm-cpp.hpp
1 #ifndef __lcm_cpp_hpp__
2 #define __lcm_cpp_hpp__
3 
4 #include <string>
5 #include <vector>
6 #include <cstdio> /* needed for FILE* */
7 #include "lcm.h"
8 
9 namespace lcm {
10 
22 class Subscription;
23 
24 struct ReceiveBuffer;
25 
31 class LCM {
32  public:
42  inline LCM(std::string lcm_url="");
43 
51  inline LCM(lcm_t * lcm_in);
52 
59  inline ~LCM();
60 
68  inline bool good() const;
69 
79  inline int publish(const std::string& channel, const void *data,
80  unsigned int datalen);
81 
93  template<class MessageType>
94  inline int publish(const std::string& channel, const MessageType* msg);
95 
112  inline int getFileno();
113 
120  inline int handle();
131  inline int handleTimeout(int timeout_millis);
132 
187  template <class MessageType, class MessageHandlerClass>
188  Subscription* subscribe(const std::string& channel,
189  void (MessageHandlerClass::*handlerMethod)(const ReceiveBuffer* rbuf, const std::string& channel, const MessageType* msg),
190  MessageHandlerClass* handler);
191 
239  template <class MessageHandlerClass>
240  Subscription* subscribe(const std::string& channel,
241  void (MessageHandlerClass::*handlerMethod)(const ReceiveBuffer* rbuf, const std::string& channel),
242  MessageHandlerClass* handler);
243 
301  template <class MessageType, class ContextClass>
302  Subscription* subscribeFunction(const std::string& channel,
303  void (*handler)(const ReceiveBuffer* rbuf,
304  const std::string& channel,
305  const MessageType *msg,
306  ContextClass context),
307  ContextClass context);
308 
349  template <class ContextClass>
350  Subscription* subscribeFunction(const std::string& channel,
351  void (*handler)(const ReceiveBuffer* rbuf,
352  const std::string& channel,
353  ContextClass context),
354  ContextClass context);
355 
370  inline int unsubscribe(Subscription* subscription);
371 
383  inline lcm_t* getUnderlyingLCM();
384 
385  private:
386  lcm_t *lcm;
387  bool owns_lcm;
388 
389  std::vector<Subscription*> subscriptions;
390 };
391 
401  void *data;
405  uint32_t data_size;
410  int64_t recv_utime;
411 };
412 
427  public:
428  virtual ~Subscription() {}
429 
444  inline int setQueueCapacity(int num_messages);
445 
446  friend class LCM;
447  protected:
454 };
455 
467 struct LogEvent {
472  int64_t eventnum;
477  int64_t timestamp;
481  std::string channel;
485  int32_t datalen;
489  void* data;
490 };
491 
501 class LogFile {
502  public:
510  inline LogFile(const std::string & path, const std::string & mode);
511 
515  inline ~LogFile();
516 
520  inline bool good() const;
521 
531  inline const LogEvent* readNextEvent();
532 
542  inline int seekToTimestamp(int64_t timestamp);
543 
554  inline int writeEvent(LogEvent* event);
555 
566  inline FILE* getFilePtr();
567 
568  private:
569  LogEvent curEvent;
570  lcm_eventlog_t* eventlog;
571  lcm_eventlog_event_t* last_event;
572 };
573 
578 #define __lcm_cpp_impl_ok__
579 #include "lcm-cpp-impl.hpp"
580 #undef __lcm_cpp_impl_ok__
581 
582 }
583 
584 #endif
int writeEvent(LogEvent *event)
Definition: lcm-cpp.hpp:349
const LogEvent * readNextEvent()
Definition: lcm-cpp.hpp:326
struct _lcm_subscription_t lcm_subscription_t
Definition: lcm.h:53
Stores the raw bytes and timestamp of a received message.
Definition: lcm-cpp.hpp:397
int64_t eventnum
Definition: lcm-cpp.hpp:472
LogFile(const std::string &path, const std::string &mode)
Definition: lcm-cpp.hpp:303
bool good() const
Definition: lcm-cpp.hpp:320
int handle()
Waits for and dispatches messages.
Definition: lcm-cpp.hpp:195
lcm_t * getUnderlyingLCM()
retrives the lcm_t C data structure wrapped by this class.
Definition: lcm-cpp.hpp:298
int64_t timestamp
Definition: lcm-cpp.hpp:477
FILE * getFilePtr()
retrives the underlying FILE* wrapped by this class.
Definition: lcm-cpp.hpp:362
~LogFile()
Definition: lcm-cpp.hpp:309
int publish(const std::string &channel, const void *data, unsigned int datalen)
Publishes a raw data message.
Definition: lcm-cpp.hpp:144
Core communications class for the C++ API.
Definition: lcm-cpp.hpp:31
Represents a channel subscription, and can be used to unsubscribe and set options.
Definition: lcm-cpp.hpp:426
int getFileno()
Returns a file descriptor or socket that can be used with select(), poll(), or other event loops for ...
Definition: lcm-cpp.hpp:185
Definition: lcm-cpp.hpp:9
Represents a single event (message) in a log file.
Definition: lcm-cpp.hpp:467
int32_t datalen
Definition: lcm-cpp.hpp:485
Subscription * subscribeFunction(const std::string &channel, void(*handler)(const ReceiveBuffer *rbuf, const std::string &channel, const MessageType *msg, ContextClass context), ContextClass context)
Subscribe a function callback to a channel, with automatic message decoding.
Definition: lcm-cpp.hpp:258
void * data
Definition: lcm-cpp.hpp:401
int handleTimeout(int timeout_millis)
Waits for and dispatches messages, with a timeout.
Definition: lcm-cpp.hpp:205
Read and write LCM log files.
Definition: lcm-cpp.hpp:501
int64_t recv_utime
Definition: lcm-cpp.hpp:410
Subscription * subscribe(const std::string &channel, void(MessageHandlerClass::*handlerMethod)(const ReceiveBuffer *rbuf, const std::string &channel, const MessageType *msg), MessageHandlerClass *handler)
Subscribes a callback method of an object to a channel, with automatic message decoding.
Definition: lcm-cpp.hpp:216
int seekToTimestamp(int64_t timestamp)
Definition: lcm-cpp.hpp:343
int setQueueCapacity(int num_messages)
Adjusts the maximum number of received messages that can be queued up for this subscription.
Definition: lcm-cpp.hpp:11
struct _lcm_t lcm_t
Definition: lcm.h:48
uint32_t data_size
Definition: lcm-cpp.hpp:405
~LCM()
Destructor.
Definition: lcm-cpp.hpp:134
bool good() const
Checks if initialization succeeded during object construction.
Definition: lcm-cpp.hpp:128
std::string channel
Definition: lcm-cpp.hpp:481
lcm_subscription_t * c_subs
Definition: lcm-cpp.hpp:448
LCM(std::string lcm_url="")
Constructor.
Definition: lcm-cpp.hpp:114
int unsubscribe(Subscription *subscription)
Unsubscribes a message handler.
Definition: lcm-cpp.hpp:165
void * data
Definition: lcm-cpp.hpp:489