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 #include "lcm.h"
16 
17 #if LCM_CXX_11_ENABLED
18 #include <functional>
19 #endif
20 
21 namespace lcm {
22 
34 class Subscription;
35 
36 struct ReceiveBuffer;
37 
43 class LCM {
44  public:
54  inline LCM(std::string lcm_url = "");
55 
63  inline LCM(lcm_t *lcm_in);
64 
71  inline ~LCM();
72 
80  inline bool good() const;
81 
91  inline int publish(const std::string &channel, const void *data, unsigned int datalen);
92 
104  template <class MessageType>
105  inline int publish(const std::string &channel, const MessageType *msg);
106 
123  inline int getFileno();
124 
131  inline int handle();
132 
142  inline int handleTimeout(int timeout_millis);
198  template <class MessageType, class MessageHandlerClass>
199  Subscription *subscribe(const std::string &channel,
200  void (MessageHandlerClass::*handlerMethod)(const ReceiveBuffer *rbuf,
201  const std::string &channel,
202  const MessageType *msg),
203  MessageHandlerClass *handler);
204 
252  template <class MessageHandlerClass>
253  Subscription *subscribe(const std::string &channel,
254  void (MessageHandlerClass::*handlerMethod)(const ReceiveBuffer *rbuf,
255  const std::string &channel),
256  MessageHandlerClass *handler);
257 
316  template <class MessageType, class ContextClass>
317  Subscription *subscribeFunction(const std::string &channel,
318  void (*handler)(const ReceiveBuffer *rbuf,
319  const std::string &channel,
320  const MessageType *msg, ContextClass context),
321  ContextClass context);
322 
363  template <class ContextClass>
364  Subscription *subscribeFunction(const std::string &channel,
365  void (*handler)(const ReceiveBuffer *rbuf,
366  const std::string &channel,
367  ContextClass context),
368  ContextClass context);
369 
370 #if LCM_CXX_11_ENABLED
371 
374  template <class MessageType>
375  using HandlerFunction = std::function<void(const ReceiveBuffer *rbuf,
376  const std::string &channel, const MessageType *msg)>;
425  template <class MessageType>
426  Subscription *subscribe(const std::string &channel, HandlerFunction<MessageType> handler);
427 #endif
428 
443  inline int unsubscribe(Subscription *subscription);
444 
456  inline lcm_t *getUnderlyingLCM();
457 
458  private:
459  lcm_t *lcm;
460  bool owns_lcm;
461 
462  std::vector<Subscription *> subscriptions;
463 };
464 
474  void *data;
478  uint32_t data_size;
483  int64_t recv_utime;
484 };
485 
500  public:
501  virtual ~Subscription() {}
516  inline int setQueueCapacity(int num_messages);
517 
522  inline int getQueueSize() const;
523 
524  friend class LCM;
525 
526  protected:
527  Subscription() { channel_buf.reserve(LCM_MAX_CHANNEL_NAME_LENGTH); };
528 
534 
535  // A "workspace" string that is overwritten with the channel name during
536  // message handling. This string serves to eliminate a heap allocation that
537  // would otherwise occur and which could preclude use in real-time
538  // applications.
539  std::string channel_buf;
540 };
541 
553 struct LogEvent {
558  int64_t eventnum;
563  int64_t timestamp;
567  std::string channel;
571  int32_t datalen;
575  void *data;
576 };
577 
587 class LogFile {
588  public:
596  inline LogFile(const std::string &path, const std::string &mode);
597 
601  inline ~LogFile();
602 
606  inline bool good() const;
607 
617  inline const LogEvent *readNextEvent();
618 
628  inline int seekToTimestamp(int64_t timestamp);
629 
640  inline int writeEvent(LogEvent *event);
641 
652  inline FILE *getFilePtr();
653 
654  private:
655  LogEvent curEvent;
656  lcm_eventlog_t *eventlog;
657  lcm_eventlog_event_t *last_event;
658 };
659 
664 #define __lcm_cpp_impl_ok__
665 #include "lcm-cpp-impl.hpp"
666 #undef __lcm_cpp_impl_ok__
667 }
668 
669 #endif
struct _lcm_subscription_t lcm_subscription_t
Definition: lcm.h:55
Stores the raw bytes and timestamp of a received message.
Definition: lcm-cpp.hpp:470
int64_t eventnum
Definition: lcm-cpp.hpp:558
int handle()
Waits for and dispatches messages.
Definition: lcm-cpp.hpp:201
lcm_t * getUnderlyingLCM()
retrives the lcm_t C data structure wrapped by this class.
Definition: lcm-cpp.hpp:318
int64_t timestamp
Definition: lcm-cpp.hpp:563
int publish(const std::string &channel, const void *data, unsigned int datalen)
Publishes a raw data message.
Definition: lcm-cpp.hpp:153
Core communications class for the C++ API.
Definition: lcm-cpp.hpp:43
Represents a channel subscription, and can be used to unsubscribe and set options.
Definition: lcm-cpp.hpp:499
std::function< void(const ReceiveBuffer *rbuf, const std::string &channel, const MessageType *msg)> HandlerFunction
Definition: lcm-cpp.hpp:376
int getFileno()
Returns a file descriptor or socket that can be used with select(), poll(), or other event loops for ...
Definition: lcm-cpp.hpp:192
Definition: lcm-cpp.hpp:21
Represents a single event (message) in a log file.
Definition: lcm-cpp.hpp:553
int32_t datalen
Definition: lcm-cpp.hpp:571
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:262
void * data
Definition: lcm-cpp.hpp:474
int handleTimeout(int timeout_millis)
Waits for and dispatches messages, with a timeout.
Definition: lcm-cpp.hpp:210
Read and write LCM log files.
Definition: lcm-cpp.hpp:587
int64_t recv_utime
Definition: lcm-cpp.hpp:483
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:220
bool good() const
Checks if initialization succeeded during object construction.
Definition: lcm-cpp.hpp:138
struct _lcm_t lcm_t
Definition: lcm.h:50
uint32_t data_size
Definition: lcm-cpp.hpp:478
~LCM()
Destructor.
Definition: lcm-cpp.hpp:143
std::string channel
Definition: lcm-cpp.hpp:567
lcm_subscription_t * c_subs
Definition: lcm-cpp.hpp:527
LCM(std::string lcm_url="")
Constructor.
Definition: lcm-cpp.hpp:128
int unsubscribe(Subscription *subscription)
Unsubscribes a message handler.
Definition: lcm-cpp.hpp:173
void * data
Definition: lcm-cpp.hpp:575