LCM
lcm-cpp.hpp
1 #ifndef __lcm_cpp_hpp__
2 #define __lcm_cpp_hpp__
3 
4 #ifndef LCM_CXX_11_ENABLED
5 #if __cplusplus >= 201103L
6 #define LCM_CXX_11_ENABLED 1
7 #else
8 #define LCM_CXX_11_ENABLED 0
9 #endif
10 #endif
11 
12 #include <cstdio> /* needed for FILE* */
13 #include <string>
14 #include <vector>
15 
16 #include "lcm.h"
17 
18 #if LCM_CXX_11_ENABLED
19 #include <functional>
20 #endif
21 
22 namespace lcm {
23 
35 class Subscription;
36 
37 struct ReceiveBuffer;
38 
44 class LCM {
45  public:
55  inline LCM(std::string lcm_url = "");
56 
64  inline LCM(lcm_t *lcm_in);
65 
72  inline ~LCM();
73 
81  inline bool good() const;
82 
92  inline int publish(const std::string &channel, const void *data, unsigned int datalen);
93 
105  template <class MessageType>
106  inline int publish(const std::string &channel, const MessageType *msg);
107 
124  inline int getFileno();
125 
132  inline int handle();
133 
143  inline int handleTimeout(int timeout_millis);
144 
199  template <class MessageType, class MessageHandlerClass>
200  Subscription *subscribe(const std::string &channel,
201  void (MessageHandlerClass::*handlerMethod)(const ReceiveBuffer *rbuf,
202  const std::string &channel,
203  const MessageType *msg),
204  MessageHandlerClass *handler);
205 
253  template <class MessageHandlerClass>
254  Subscription *subscribe(const std::string &channel,
255  void (MessageHandlerClass::*handlerMethod)(const ReceiveBuffer *rbuf,
256  const std::string &channel),
257  MessageHandlerClass *handler);
258 
317  template <class MessageType, class ContextClass>
318  Subscription *subscribeFunction(const std::string &channel,
319  void (*handler)(const ReceiveBuffer *rbuf,
320  const std::string &channel,
321  const MessageType *msg, ContextClass context),
322  ContextClass context);
323 
364  template <class ContextClass>
365  Subscription *subscribeFunction(const std::string &channel,
366  void (*handler)(const ReceiveBuffer *rbuf,
367  const std::string &channel,
368  ContextClass context),
369  ContextClass context);
370 
371 #if LCM_CXX_11_ENABLED
375  template <class MessageType>
376  using HandlerFunction = std::function<void(const ReceiveBuffer *rbuf,
377  const std::string &channel, const MessageType *msg)>;
426  template <class MessageType>
427  Subscription *subscribe(const std::string &channel, HandlerFunction<MessageType> handler);
428 #endif
429 
444  inline int unsubscribe(Subscription *subscription);
445 
458 
459  private:
460  lcm_t *lcm;
461  bool owns_lcm;
462 
463  std::vector<Subscription *> subscriptions;
464 };
465 
475  void *data;
479  uint32_t data_size;
484  int64_t recv_utime;
485 };
486 
501  public:
502  virtual ~Subscription() {}
517  inline int setQueueCapacity(int num_messages);
518 
523  inline int getQueueSize() const;
524 
525  friend class LCM;
526 
527  protected:
528  Subscription() { channel_buf.reserve(LCM_MAX_CHANNEL_NAME_LENGTH); };
529 
535 
536  // A "workspace" string that is overwritten with the channel name during
537  // message handling. This string serves to eliminate a heap allocation that
538  // would otherwise occur and which could preclude use in real-time
539  // applications.
540  std::string channel_buf;
541 };
542 
554 struct LogEvent {
559  int64_t eventnum;
564  int64_t timestamp;
568  std::string channel;
572  int32_t datalen;
576  void *data;
577 };
578 
588 class LogFile {
589  public:
597  inline LogFile(const std::string &path, const std::string &mode);
598 
602  inline ~LogFile();
603 
607  inline bool good() const;
608 
618  inline const LogEvent *readNextEvent();
619 
629  inline int seekToTimestamp(int64_t timestamp);
630 
641  inline int writeEvent(LogEvent *event);
642 
653  inline FILE *getFilePtr();
654 
655  private:
656  LogEvent curEvent;
657  lcm_eventlog_t *eventlog;
658  lcm_eventlog_event_t *last_event;
659 };
660 
665 #define __lcm_cpp_impl_ok__
666 #include "lcm-cpp-impl.hpp"
667 #undef __lcm_cpp_impl_ok__
668 } // namespace lcm
669 
670 #endif
Core communications class for the C++ API.
Definition: lcm-cpp.hpp:44
int publish(const std::string &channel, const MessageType *msg)
Publishes a message with automatic message encoding.
Subscription * subscribe(const std::string &channel, void(MessageHandlerClass::*handlerMethod)(const ReceiveBuffer *rbuf, const std::string &channel), MessageHandlerClass *handler)
Subscribe a callback method of an object to a channel, without automatic message decoding.
LCM(std::string lcm_url="")
Constructor.
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.
~LCM()
Destructor.
bool good() const
Checks if initialization succeeded during object construction.
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.
LCM(lcm_t *lcm_in)
Constructor.
int publish(const std::string &channel, const void *data, unsigned int datalen)
Publishes a raw data message.
int handleTimeout(int timeout_millis)
Waits for and dispatches messages, with a timeout.
Subscription * subscribe(const std::string &channel, HandlerFunction< MessageType > handler)
Subscribes a callback function to a channel, with automatic message decoding.
lcm_t * getUnderlyingLCM()
retrives the lcm_t C data structure wrapped by this class.
int handle()
Waits for and dispatches messages.
int getFileno()
Returns a file descriptor or socket that can be used with select(), poll(), or other event loops for ...
Subscription * subscribeFunction(const std::string &channel, void(*handler)(const ReceiveBuffer *rbuf, const std::string &channel, ContextClass context), ContextClass context)
Subscribe a function callback to a channel, without automatic message decoding.
std::function< void(const ReceiveBuffer *rbuf, const std::string &channel, const MessageType *msg)> HandlerFunction
Definition: lcm-cpp.hpp:377
int unsubscribe(Subscription *subscription)
Unsubscribes a message handler.
Read and write LCM log files.
Definition: lcm-cpp.hpp:588
FILE * getFilePtr()
retrives the underlying FILE* wrapped by this class.
bool good() const
const LogEvent * readNextEvent()
int writeEvent(LogEvent *event)
int seekToTimestamp(int64_t timestamp)
LogFile(const std::string &path, const std::string &mode)
Represents a channel subscription, and can be used to unsubscribe and set options.
Definition: lcm-cpp.hpp:500
int getQueueSize() const
Query the current number of unhandled messages queued up for this subscription.
lcm_subscription_t * c_subs
Definition: lcm-cpp.hpp:528
int setQueueCapacity(int num_messages)
Adjusts the maximum number of received messages that can be queued up for this subscription.
struct _lcm_subscription_t lcm_subscription_t
Definition: lcm.h:55
struct _lcm_t lcm_t
Definition: lcm.h:50
Represents a single event (message) in a log file.
Definition: lcm-cpp.hpp:554
void * data
Definition: lcm-cpp.hpp:576
int64_t eventnum
Definition: lcm-cpp.hpp:559
std::string channel
Definition: lcm-cpp.hpp:568
int64_t timestamp
Definition: lcm-cpp.hpp:564
int32_t datalen
Definition: lcm-cpp.hpp:572
Stores the raw bytes and timestamp of a received message.
Definition: lcm-cpp.hpp:471
void * data
Definition: lcm-cpp.hpp:475
uint32_t data_size
Definition: lcm-cpp.hpp:479
int64_t recv_utime
Definition: lcm-cpp.hpp:484