LCM
|
Core communications class for the C++ API. More...
#include <lcm/lcm-cpp.hpp>
Public Types | |
template<class MessageType > | |
using | HandlerFunction = std::function< void(const ReceiveBuffer *rbuf, const std::string &channel, const MessageType *msg)> |
Public Member Functions | |
LCM (std::string lcm_url="") | |
Constructor. More... | |
LCM (lcm_t *lcm_in) | |
Constructor. More... | |
~LCM () | |
Destructor. More... | |
bool | good () const |
Checks if initialization succeeded during object construction. More... | |
int | publish (const std::string &channel, const void *data, unsigned int datalen) |
Publishes a raw data message. More... | |
template<class MessageType > | |
int | publish (const std::string &channel, const MessageType *msg) |
Publishes a message with automatic message encoding. More... | |
int | getFileno () |
Returns a file descriptor or socket that can be used with select() , poll() , or other event loops for asynchronous notification of incoming messages. More... | |
int | handle () |
Waits for and dispatches messages. More... | |
int | handleTimeout (int timeout_millis) |
Waits for and dispatches messages, with a timeout. More... | |
template<class MessageType , class MessageHandlerClass > | |
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. More... | |
template<class MessageHandlerClass > | |
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. More... | |
template<class MessageType , class ContextClass > | |
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. More... | |
template<class ContextClass > | |
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. More... | |
template<class MessageType > | |
Subscription * | subscribe (const std::string &channel, HandlerFunction< MessageType > handler) |
Subscribes a callback function to a channel, with automatic message decoding. More... | |
int | unsubscribe (Subscription *subscription) |
Unsubscribes a message handler. More... | |
lcm_t * | getUnderlyingLCM () |
retrives the lcm_t C data structure wrapped by this class. More... | |
Core communications class for the C++ API.
using lcm::LCM::HandlerFunction = std::function<void(const ReceiveBuffer *rbuf, const std::string &channel, const MessageType *msg)> |
Type alias for the handler function type.
|
inline |
Constructor.
Initializes the LCM instance and connects it to the specified LCM network. See the documentation on lcm_create() in the C API for details on how lcm_url is formatted.
|
inline |
|
inline |
Destructor.
Disconnects from the LCM network, and destroys all outstanding Subscription objects.
|
inline |
Returns a file descriptor or socket that can be used with select()
, poll()
, or other event loops for asynchronous notification of incoming messages.
This method is useful when integrating LCM into another event loop, such as the Qt event loop (via QSocketNotifier), the GLib event loop (via GIOChannel), a custom select()
-
or poll()
-based
event loop, or any other event loop that supports file descriptors.
|
inline |
retrives the lcm_t C data structure wrapped by this class.
This method should be used carefully and sparingly. An example use case would be extending the subscription mechanism to Boost Function objects.
|
inline |
Checks if initialization succeeded during object construction.
|
inline |
Waits for and dispatches messages.
|
inline |
Waits for and dispatches messages, with a timeout.
New in LCM 1.1.0.
|
inline |
Publishes a message with automatic message encoding.
This template method is designed for use with C++ classes generated by lcm-gen.
channel | the channel to publish the message on. |
msg | the message to publish. |
|
inline |
Publishes a raw data message.
channel | the channel to publish the message on. |
data | data buffer containing the message to publish |
datalen | length of the message, in bytes. |
Subscription* lcm::LCM::subscribe | ( | const std::string & | channel, |
HandlerFunction< MessageType > | handler | ||
) |
Subscribes a callback function to a channel, with automatic message decoding.
This method is designed for use with C++ classes generated by lcm-gen
.
The callback function will be invoked on the object when a message arrives on the specified channel. Prior to method invocation, LCM will attempt to automatically decode the message to the specified message type MessageType
, which should be a class generated by
lcm-gen
. If message decoding fails, the callback function is not invoked and an error message is printed to stderr.
The callback function is invoked during calls to LCM::handle(). Callback methods are invoked by the same thread that invokes LCM::handle(), in the order that they were subscribed.
For example:
channel | The channel to subscribe to. This is treated as a regular expression implicitly surrounded by '^' and '$'. |
handler | A handler function, for example a lambda. |
Subscription* lcm::LCM::subscribe | ( | const std::string & | channel, |
void(MessageHandlerClass::*)(const ReceiveBuffer *rbuf, const std::string &channel) | handlerMethod, | ||
MessageHandlerClass * | handler | ||
) |
Subscribe a callback method of an object to a channel, without automatic message decoding.
This method is designed for use when automatic message decoding is not desired.
The callback method will be invoked on the object when a message arrives on the specified channel. Callback methods are invoked during calls to LCM::handle(), by the same thread that calls LCM::handle(). Callbacks are invoked in the order that they were subscribed.
For example:
channel | The channel to subscribe to. This is treated as a regular expression implicitly surrounded by '^' and '$'. |
handlerMethod | A class method pointer identifying the callback method. |
handler | A class instance that the callback method will be invoked on. |
Subscription* lcm::LCM::subscribe | ( | const std::string & | channel, |
void(MessageHandlerClass::*)(const ReceiveBuffer *rbuf, const std::string &channel, const MessageType *msg) | handlerMethod, | ||
MessageHandlerClass * | handler | ||
) |
Subscribes a callback method of an object to a channel, with automatic message decoding.
This method is designed for use with C++ classes generated by lcm-gen
.
The callback method will be invoked on the object when a message arrives on the specified channel. Prior to method invocation, LCM will attempt to automatically decode the message to the specified message type MessageType
, which should be a class generated by
lcm-gen
. If message decoding fails, the callback method is not invoked and an error message is printed to stderr.
The callback method is invoked during calls to LCM::handle(). Callback methods are invoked by the same thread that invokes LCM::handle(), in the order that they were subscribed.
For example:
channel | The channel to subscribe to. This is treated as a regular expression implicitly surrounded by '^' and '$'. |
handlerMethod | A class method pointer identifying the callback method. |
handler | A class instance that the callback method will be invoked on. |
Subscription* lcm::LCM::subscribeFunction | ( | const std::string & | channel, |
void(*)(const ReceiveBuffer *rbuf, const std::string &channel, const MessageType *msg, ContextClass context) | handler, | ||
ContextClass | context | ||
) |
Subscribe a function callback to a channel, with automatic message decoding.
This method is designed for use with static member functions and C-style functions.
The callback function will be invoked on the object when a message arrives on the specified channel. Prior to callback invocation, LCM will attempt to automatically decode the message to the specified message type MessageType
, which should be a class generated by
lcm-gen
. If message decoding fails, the callback function is not invoked and an error message is printed to stderr.
The callback function is invoked during calls to LCM::handle(). Callbacks are invoked by the same thread that invokes LCM::handle(), in the order that they were subscribed.
For example:
channel | The channel to subscribe to. This is treated as a regular expression implicitly surrounded by '^' and '$'. |
handler | A function pointer identifying the callback function. |
context | A context variable that will be passed to the callback function. This can be used to pass state or other information to the callback function. If not needed, then ContextClass can be set to void*, and this argument set to NULL. |
Subscription* lcm::LCM::subscribeFunction | ( | const std::string & | channel, |
void(*)(const ReceiveBuffer *rbuf, const std::string &channel, ContextClass context) | handler, | ||
ContextClass | context | ||
) |
Subscribe a function callback to a channel, without automatic message decoding.
This method is designed for use when automatic message decoding is not desired.
For example:
channel | The channel to subscribe to. This is treated as a regular expression implicitly surrounded by '^' and '$'. |
handler | A function pointer identifying the callback function. |
context | A context variable that will be passed to the callback function. This can be used to pass state or other information to the callback function. If not needed, then ContextClass can be set to void*, and this argument set to NULL. |
|
inline |
Unsubscribes a message handler.
After unsubscription, the callback registered by the original call to subscribe() or subscribeFunction() will no longer be invoked when messages are received. The Subscription object is destroyed by this method.
subscription | a Subscription object previously returned by a call to subscribe() or subscribeFunction(). |
subscription
is not a valid subscription.