/usr/include/lcm/lcm-cpp.hpp is in liblcm-dev 1.3.1+repack1-1+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | #ifndef __lcm_cpp_hpp__
#define __lcm_cpp_hpp__
#include <string>
#include <vector>
#include <cstdio> /* needed for FILE* */
#include "lcm.h"
namespace lcm {
/**
* @defgroup LcmCpp C++ API Reference
*
* THe %LCM C++ API provides classes and data structures for communicating with
* other %LCM clients, as well as reading and writing %LCM log files. It is a
* pure header wrapper around the C API, and has the same linking requirements
* as the C API.
*
* @{
*/
class Subscription;
struct ReceiveBuffer;
/**
* @brief Core communications class for the C++ API.
*
* @headerfile lcm/lcm-cpp.hpp
*/
class LCM {
public:
/**
* @brief 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.
*
* @sa lcm_create()
*/
inline LCM(std::string lcm_url="");
/**
* @brief Constructor.
*
* Initializes the c++ LCM instance from an existing C instance.
*
* @sa lcm_create()
*/
inline LCM(lcm_t * lcm_in);
/**
* @brief Destructor.
*
* Disconnects from the LCM network, and destroys all outstanding
* Subscription objects.
*/
inline ~LCM();
/**
* @brief Checks if initialization succeeded during object
* construction.
*
* @return true if initialization succeeded and the instance appears
* ready for communication, false if not.
*/
inline bool good() const;
/**
* @brief Publishes a raw data message.
*
* @param channel the channel to publish the message on.
* @param data data buffer containing the message to publish
* @param datalen length of the message, in bytes.
*
* @return 0 on success, -1 on failure.
*/
inline int publish(const std::string& channel, const void *data,
unsigned int datalen);
/**
* @brief Publishes a message with automatic message encoding.
*
* This template method is designed for use with C++ classes generated
* by lcm-gen.
*
* @param channel the channel to publish the message on.
* @param msg the message to publish.
*
* @return 0 on success, -1 on failure.
*/
template<class MessageType>
inline int publish(const std::string& channel, const MessageType* msg);
/**
* @brief Returns a file descriptor or socket that can be used with
* @c select(), @c 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 @c select() @c - or @c poll() @c -based event loop, or any other
* event loop that supports file descriptors.
*
* @todo link to example code.
*
* @return a non-negative file descriptor on success, or -1 if something
* is wrong.
* @sa lcm_get_fileno()
*/
inline int getFileno();
/**
* @brief Waits for and dispatches messages.
*
* @return 0 on success, -1 if something went wrong.
* @sa lcm_handle()
*/
inline int handle();
/**
* @brief Waits for and dispatches messages, with a timeout.
*
* New in LCM 1.1.0.
*
* @return >0 if a message was handled, 0 if the function timed out,
* and <0 if an error occured.
* @sa lcm_handle_timeout()
*/
inline int handleTimeout(int timeout_millis);
/**
* @brief 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
* @c lcm-gen @c .
*
* 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 @c MessageType @c , which should be a class generated
* by @c lcm-gen @c . 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:
*
* \code
* #include <exlcm/example_t.lcm>
* #include <lcm/lcm-cpp.hpp>
*
* class MyMessageHandler {
* void onMessage(const lcm::ReceiveBuffer* rbuf, const std::string& channel,
* const exlcm::example_t* msg) {
* // do something with the message
* }
* };
*
* int main(int argc, char** argv) {
* lcm::LCM lcm;
* MyMessageHandler handler;
* lcm.subscribe("CHANNEL", &MyMessageHandler::onMessage, &handler);
* while(true)
* lcm.handle();
* return 0;
* }
* \endcode
*
* @param channel The channel to subscribe to. This is treated as a
* regular expression implicitly surrounded by '^' and '$'.
* @param handlerMethod A class method pointer identifying the callback
* method.
* @param handler A class instance that the callback method will be
* invoked on.
*
* @return a Subscription object that can be used to adjust the
* subscription and unsubscribe. The Subscription object is managed by
* the LCM class, and is automatically destroyed when its LCM instance
* is destroyed.
*/
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);
/**
* @brief 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:
*
* \code
* #include <lcm/lcm-cpp.hpp>
*
* class MyMessageHandler {
* void onMessage(const lcm::ReceiveBuffer* rbuf, const std::string& channel) {
* // do something with the message. Raw message bytes are
* // accessible via rbuf->data
* }
* };
*
* int main(int argc, char** argv) {
* lcm::LCM lcm;
* MyMessageHandler handler;
* lcm.subscribe("CHANNEL", &MyMessageHandler::onMessage, &handler);
* while(true)
* lcm.handle();
* return 0;
* }
* \endcode
*
* @param channel The channel to subscribe to. This is treated as a
* regular expression implicitly surrounded by '^' and '$'.
* @param handlerMethod A class method pointer identifying the callback
* method.
* @param handler A class instance that the callback method will be
* invoked on.
*
* @return a Subscription object that can be used to adjust the
* subscription and unsubscribe. The Subscription object is managed by
* the LCM class, and is automatically destroyed when its LCM instance
* is destroyed.
*/
template <class MessageHandlerClass>
Subscription* subscribe(const std::string& channel,
void (MessageHandlerClass::*handlerMethod)(const ReceiveBuffer* rbuf, const std::string& channel),
MessageHandlerClass* handler);
/**
* @brief 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 @c MessageType @c , which should be a class generated
* by @c lcm-gen @c . 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:
*
* \code
* #include <lcm/lcm-cpp.hpp>
*
* class State {
* public:
* lcm::LCM lcm;
* int usefulVariable;
* };
*
* void onMessage(const lcm::ReceiveBuffer* rbuf, const std::string& channel, const MessageType* msg, State* state) {
* // do something with the message.
* }
*
* int main(int argc, char** argv) {
* State* state = new State;
* state->lcm.subscribe("CHANNEL", onMessage, state);
* while(true)
* state->lcm.handle();
* delete state;
* return 0;
* }
* \endcode
*
* @param channel The channel to subscribe to. This is treated as a
* regular expression implicitly surrounded by '^' and '$'.
* @param handler A function pointer identifying the callback
* function.
* @param 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 @c
* ContextClass @c can be set to void*, and this argument set to NULL.
*
* @return a Subscription object that can be used to adjust the
* subscription and unsubscribe. The Subscription object is managed by
* the LCM class, and is automatically destroyed when its LCM instance
* is destroyed.
*/
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);
/**
* @brief 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:
*
* \code
* #include <lcm/lcm-cpp.hpp>
*
* void onMessage(const lcm::ReceiveBuffer* rbuf, const std::string& channel, void*) {
* // do something with the message. Raw message bytes are
* // accessible via rbuf->data
* }
*
* int main(int argc, char** argv) {
* LCM::lcm lcm;
* lcm.subscribe("CHANNEL", onMessage, NULL);
* while(true)
* lcm.handle();
* return 0;
* }
* \endcode
*
* @param channel The channel to subscribe to. This is treated as a
* regular expression implicitly surrounded by '^' and '$'.
* @param handler A function pointer identifying the callback
* function.
* @param 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 @c
* ContextClass @c can be set to void*, and this argument set to NULL.
*
* @return a Subscription object that can be used to adjust the
* subscription and unsubscribe. The Subscription object is managed by
* the LCM class, and is automatically destroyed when its LCM instance
* is destroyed.
*/
template <class ContextClass>
Subscription* subscribeFunction(const std::string& channel,
void (*handler)(const ReceiveBuffer* rbuf,
const std::string& channel,
ContextClass context),
ContextClass context);
/**
* @brief 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.
*
* @param subscription a Subscription object previously returned by a
* call to subscribe() or subscribeFunction().
*
* @return 0 on success, -1 if @p subscription is not a valid
* subscription.
*/
inline int unsubscribe(Subscription* subscription);
/**
* @brief 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.
*
* @return the lcm_t instance wrapped by this object.
*
* @sa lcm_t
*/
inline lcm_t* getUnderlyingLCM();
private:
lcm_t *lcm;
bool owns_lcm;
std::vector<Subscription*> subscriptions;
};
/**
* @brief Stores the raw bytes and timestamp of a received message.
*
* @headerfile lcm/lcm-cpp.hpp
*/
struct ReceiveBuffer {
/**
* Message payload data, represented as a raw byte buffer.
*/
void *data;
/**
* Length of message payload, in bytes.
*/
uint32_t data_size;
/**
* Timestamp identifying when the message was received. Specified in
* microseconds since the UNIX epoch.
*/
int64_t recv_utime;
};
/**
* @brief Represents a channel subscription, and can be used to unsubscribe
* and set options.
*
* This class is not meant to be instantiated by the user, and instead is
* constructed and returned by a call to LCM::subscribe() or
* LCM::subscribeFunction().
*
* To unsubscribe, pass the instance to LCM::unsubscribe(). Once unsubscribed,
* the object is destroyed and can not be used anymore.
*
* @headerfile lcm/lcm-cpp.hpp
*/
class Subscription {
public:
virtual ~Subscription() {}
/**
* @brief Adjusts the maximum number of received messages that can be
* queued up for this subscription.
*
* @param num_messages the maximum queue size, in messages. The
* default is 30.
*
* Setting this to a low number may reduce
* overall latency at the expense of dropping more messages.
* Conversely, setting this to a high number may drop fewer messages at
* the expense of increased latency. A value of 0 indicates no limit,
* and should be used very carefully.
*
*/
inline int setQueueCapacity(int num_messages);
friend class LCM;
protected:
Subscription() {};
/**
* The underlying lcm_subscription_t object wrapped by this
* subscription.
*/
lcm_subscription_t *c_subs;
};
/**
* @brief Represents a single event (message) in a log file.
*
*
*
* This struct is the C++ counterpart for lcm_eventlog_event_t.
*
* @sa lcm_eventlog_event_t
*
* @headerfile lcm/lcm-cpp.hpp
*/
struct LogEvent {
/**
* Monotically increasing counter identifying the event number. This field
* is managed by LCM, and there should be no need to ever set it manually.
*/
int64_t eventnum;
/**
* Timestamp identifying when the event was received. Represented in
* microseconds since the UNIX epoch.
*/
int64_t timestamp;
/**
* The LCM channel on which the message was received.
*/
std::string channel;
/**
* The length of the message payload, in bytes
*/
int32_t datalen;
/**
* The message payload.
*/
void* data;
};
/**
* @brief Read and write %LCM log files.
*
* This class is the C++ counterpart for lcm_eventlog_t.
*
* @sa lcm_eventlog_t
*
* @headerfile lcm/lcm-cpp.hpp
*/
class LogFile {
public:
/**
* Constructor. Opens the specified log file for reading or writing.
* @param path the file to open
* @param mode "r" (read mode) or "w" (write mode)
*
* @sa lcm_eventlog_create()
*/
inline LogFile(const std::string & path, const std::string & mode);
/**
* Destructor. Closes the log file.
*/
inline ~LogFile();
/**
* @return true if the log file is ready for reading/writing.
*/
inline bool good() const;
/**
* Reads the next event in the log file. Valid in read mode only.
*
* The LogFile class manages the memory of the read event. The
* returned event is valid until the next call to this method.
*
* @return the next event, or NULL if the end of the log file has been
* reached.
*/
inline const LogEvent* readNextEvent();
/**
* Seek close to the specified timestamp in the log file. Valid
* in read mode only.
*
* @param timestamp the desired seek point in the log file.
*
* @return 0 on success, -1 on error.
* @sa lcm_eventlog_seek_to_timestamp()
*/
inline int seekToTimestamp(int64_t timestamp);
/**
* Writes an event to the log file. Valid in write mode only.
*
* @param event the event to write. The timestamp, channel, datalen,
* and data fields should be filled in. The eventnum field will be
* automatically filled in.
*
* @return 0 on success, -1 on error.
* @sa lcm_eventlog_write_event()
*/
inline int writeEvent(LogEvent* event);
/**
* @brief retrives the underlying FILE* wrapped by this class.
*
* This method should be used carefully and sparingly.
* An example use-case is borrowing to tweak the behavior of the I/O.
* Calls of interest include fflush(), fileno(), setvbuf(), etc
* It is a bad idea to attempt reading or writing on the raw FILE*
*
* @return the FILE* wrapped by this object.
*/
inline FILE* getFilePtr();
private:
LogEvent curEvent;
lcm_eventlog_t* eventlog;
lcm_eventlog_event_t* last_event;
};
/**
* @}
*/
#define __lcm_cpp_impl_ok__
#include "lcm-cpp-impl.hpp"
#undef __lcm_cpp_impl_ok__
}
#endif
|