/usr/include/lcm/lcm-cpp-impl.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 | // DO NOT EVER INCLUDE THIS HEADER FILE YOURSELF
#ifndef __lcm_cpp_impl_ok__
#error "Don't include this file"
#endif
// =============== implementation ===============
int
Subscription::setQueueCapacity(int num_messages)
{
return lcm_subscription_set_queue_capacity(c_subs, num_messages);
}
template <class MessageType, class ContextClass>
class LCMTypedSubscription : public Subscription {
friend class LCM;
private:
ContextClass context;
void (*handler)(const ReceiveBuffer *rbuf, const std::string& channel,
const MessageType*msg, ContextClass context);
static void cb_func(const lcm_recv_buf_t *rbuf, const char *channel,
void *user_data)
{
typedef LCMTypedSubscription<MessageType,ContextClass> SubsClass;
SubsClass *subs = static_cast<SubsClass *> (user_data);
MessageType msg;
int status = msg.decode(rbuf->data, 0, rbuf->data_size);
if (status < 0) {
fprintf (stderr, "error %d decoding %s!!!\n", status,
MessageType::getTypeName());
return;
}
const ReceiveBuffer rb = {
rbuf->data,
rbuf->data_size,
rbuf->recv_utime
};
subs->handler(&rb, channel, &msg, subs->context);
}
};
template <class ContextClass>
class LCMUntypedSubscription : public Subscription {
friend class LCM;
private:
ContextClass context;
void (*handler)(const ReceiveBuffer *rbuf, const std::string& channel,
ContextClass context);
static void cb_func(const lcm_recv_buf_t *rbuf, const char *channel,
void *user_data)
{
typedef LCMUntypedSubscription<ContextClass> SubsClass;
SubsClass *subs = static_cast<SubsClass *> (user_data);
const ReceiveBuffer rb = {
rbuf->data,
rbuf->data_size,
rbuf->recv_utime
};
subs->handler(&rb, channel, subs->context);
}
};
template <class MessageType, class MessageHandlerClass>
class LCMMHSubscription : public Subscription {
friend class LCM;
private:
MessageHandlerClass* handler;
void (MessageHandlerClass::*handlerMethod)(const ReceiveBuffer* rbuf, const std::string& channel, const MessageType* msg);
static void cb_func(const lcm_recv_buf_t *rbuf, const char *channel,
void *user_data)
{
LCMMHSubscription<MessageType,MessageHandlerClass> *subs =
static_cast<LCMMHSubscription<MessageType,MessageHandlerClass> *>(user_data);
MessageType msg;
int status = msg.decode(rbuf->data, 0, rbuf->data_size);
if (status < 0) {
fprintf (stderr, "error %d decoding %s!!!\n", status,
MessageType::getTypeName());
return;
}
const ReceiveBuffer rb = {
rbuf->data,
rbuf->data_size,
rbuf->recv_utime
};
std::string chan_str(channel);
(subs->handler->*subs->handlerMethod)(&rb, chan_str, &msg);
}
};
template<class MessageHandlerClass>
class LCMMHUntypedSubscription : public Subscription {
friend class LCM;
private:
MessageHandlerClass* handler;
void (MessageHandlerClass::*handlerMethod)(const ReceiveBuffer* rbuf, const std::string& channel);
static void cb_func(const lcm_recv_buf_t *rbuf, const char *channel, void *user_data)
{
LCMMHUntypedSubscription<MessageHandlerClass> *subs =
static_cast<LCMMHUntypedSubscription<MessageHandlerClass> *>(user_data);
const ReceiveBuffer rb = {
rbuf->data,
rbuf->data_size,
rbuf->recv_utime
};
std::string chan_str(channel);
(subs->handler->*subs->handlerMethod)(&rb, chan_str);
}
};
inline
LCM::LCM(std::string lcm_url):
owns_lcm(true)
{
this->lcm = lcm_create(lcm_url.c_str());
}
inline
LCM::LCM(lcm_t * lcm_in):
owns_lcm(false)
{
this->lcm = lcm_in;
}
inline bool
LCM::good() const
{
return this->lcm != NULL;
}
inline
LCM::~LCM() {
for(int i=0, n=subscriptions.size(); i<n; i++) {
delete subscriptions[i];
}
if(this->lcm && this->owns_lcm) {
lcm_destroy(this->lcm);
}
}
inline int
LCM::publish(const std::string& channel, const void *data, unsigned int datalen) {
if(!this->lcm) {
fprintf(stderr,
"LCM instance not initialized. Ignoring call to publish()\n");
return -1;
}
return lcm_publish(this->lcm, channel.c_str(), data, datalen);
}
template<class MessageType>
inline int
LCM::publish(const std::string& channel, const MessageType *msg) {
unsigned int datalen = msg->getEncodedSize();
uint8_t *buf = new uint8_t[datalen];
msg->encode(buf, 0, datalen);
int status = this->publish(channel, buf, datalen);
delete[] buf;
return status;
}
inline int
LCM::unsubscribe(Subscription *subscription) {
if(!this->lcm) {
fprintf(stderr,
"LCM instance not initialized. Ignoring call to unsubscribe()\n");
return -1;
}
std::vector<Subscription*>::iterator iter;
std::vector<Subscription*>::iterator eiter = subscriptions.end();
for(iter=subscriptions.begin(); iter!= eiter; ++iter) {
if(*iter == subscription) {
int status = lcm_unsubscribe(lcm, subscription->c_subs);
subscriptions.erase(iter);
delete subscription;
return status;
}
}
return -1;
}
inline int
LCM::getFileno() {
if(!this->lcm) {
fprintf(stderr,
"LCM instance not initialized. Ignoring call to fileno()\n");
return -1;
}
return lcm_get_fileno(this->lcm);
}
inline int
LCM::handle() {
if(!this->lcm) {
fprintf(stderr,
"LCM instance not initialized. Ignoring call to handle()\n");
return -1;
}
return lcm_handle(this->lcm);
}
inline int
LCM::handleTimeout(int timeout_millis) {
if(!this->lcm) {
fprintf(stderr,
"LCM instance not initialized. Ignoring call to handle()\n");
return -1;
}
return lcm_handle_timeout(this->lcm, timeout_millis);
}
template <class MessageType, class MessageHandlerClass>
Subscription*
LCM::subscribe(const std::string& channel,
void (MessageHandlerClass::*handlerMethod)(const ReceiveBuffer* rbuf, const std::string& channel, const MessageType* msg),
MessageHandlerClass* handler)
{
if(!this->lcm) {
fprintf(stderr,
"LCM instance not initialized. Ignoring call to subscribe()\n");
return NULL;
}
LCMMHSubscription<MessageType, MessageHandlerClass> *subs =
new LCMMHSubscription<MessageType, MessageHandlerClass>();
subs->handler = handler;
subs->handlerMethod = handlerMethod;
subs->c_subs = lcm_subscribe(this->lcm, channel.c_str(),
LCMMHSubscription<MessageType, MessageHandlerClass>::cb_func, subs);
subscriptions.push_back(subs);
return subs;
}
template <class MessageHandlerClass>
Subscription*
LCM::subscribe(const std::string& channel,
void (MessageHandlerClass::*handlerMethod)(const ReceiveBuffer* rbuf, const std::string& channel),
MessageHandlerClass* handler)
{
if(!this->lcm) {
fprintf(stderr,
"LCM instance not initialized. Ignoring call to subscribe()\n");
return NULL;
}
LCMMHUntypedSubscription<MessageHandlerClass> *subs =
new LCMMHUntypedSubscription<MessageHandlerClass>();
subs->handler = handler;
subs->handlerMethod = handlerMethod;
subs->c_subs = lcm_subscribe(this->lcm, channel.c_str(),
LCMMHUntypedSubscription<MessageHandlerClass>::cb_func, subs);
subscriptions.push_back(subs);
return subs;
}
template <class MessageType, class ContextClass>
Subscription*
LCM::subscribeFunction(const std::string& channel,
void (*handler)(const ReceiveBuffer *rbuf,
const std::string& channel,
const MessageType *msg, ContextClass context),
ContextClass context) {
if(!this->lcm) {
fprintf(stderr, "LCM instance not initialized. Ignoring call to subscribeFunction()\n");
return NULL;
}
typedef LCMTypedSubscription<MessageType, ContextClass> SubsClass;
SubsClass *sub = new SubsClass();
sub->c_subs = lcm_subscribe(lcm, channel.c_str(), SubsClass::cb_func, sub);
sub->handler = handler;
sub->context = context;
subscriptions.push_back(sub);
return sub;
}
template <class ContextClass>
Subscription*
LCM::subscribeFunction(const std::string& channel,
void (*handler)(const ReceiveBuffer *rbuf,
const std::string& channel,
ContextClass context),
ContextClass context) {
if(!this->lcm) {
fprintf(stderr, "LCM instance not initialized. Ignoring call to subscribeFunction()\n");
return NULL;
}
typedef LCMUntypedSubscription<ContextClass> SubsClass;
SubsClass *sub = new SubsClass();
sub->c_subs = lcm_subscribe(lcm, channel.c_str(), SubsClass::cb_func, sub);
sub->handler = handler;
sub->context = context;
subscriptions.push_back(sub);
return sub;
}
lcm_t*
LCM::getUnderlyingLCM()
{
return this->lcm;
}
LogFile::LogFile(const std::string & path, const std::string & mode) :
eventlog(lcm_eventlog_create(path.c_str(), mode.c_str())),
last_event(NULL)
{
}
LogFile::~LogFile()
{
if(eventlog)
lcm_eventlog_destroy(eventlog);
eventlog = NULL;
if(last_event)
lcm_eventlog_free_event(last_event);
last_event = NULL;
}
bool
LogFile::good() const
{
return eventlog != NULL;
}
const LogEvent*
LogFile::readNextEvent()
{
lcm_eventlog_event_t* evt = lcm_eventlog_read_next_event(eventlog);
if(last_event)
lcm_eventlog_free_event(last_event);
last_event = evt;
if(!evt)
return NULL;
curEvent.eventnum = evt->eventnum;
curEvent.timestamp = evt->timestamp;
curEvent.channel.assign(evt->channel, evt->channellen);
curEvent.datalen = evt->datalen;
curEvent.data = evt->data;
return &curEvent;
}
int
LogFile::seekToTimestamp(int64_t timestamp)
{
return lcm_eventlog_seek_to_timestamp(eventlog, timestamp);
}
int
LogFile::writeEvent(LogEvent* event)
{
lcm_eventlog_event_t evt;
evt.eventnum = event->eventnum;
evt.timestamp = event->timestamp;
evt.channellen = event->channel.size();
evt.datalen = event->datalen;
evt.channel = (char*) event->channel.c_str();
evt.data = event->data;
return lcm_eventlog_write_event(eventlog, &evt);
}
FILE*
LogFile::getFilePtr()
{
return eventlog->f;
}
|