18 #ifndef _GAZEBO_EVENT_HH_
19 #define _GAZEBO_EVENT_HH_
29 #include <boost/function.hpp>
30 #include <boost/bind.hpp>
32 #include <gazebo/gazebo_config.h>
52 class GZ_COMMON_VISIBLE EventPrivate
55 public: EventPrivate();
58 public:
bool signaled;
70 public:
virtual ~
Event();
78 public:
virtual void Disconnect(
int _id) = 0;
82 public:
bool GetSignaled()
const;
86 protected:
Event(EventPrivate &_d);
94 class GZ_COMMON_VISIBLE ConnectionPrivate
97 public: ConnectionPrivate();
102 public: ConnectionPrivate(
Event *_e,
int _i);
105 public:
Event *event;
131 public:
int GetId()
const;
134 private: ConnectionPrivate *dataPtr;
137 public:
template<
typename T>
friend class EventT;
146 boost::function<T> *_cb)
155 public: std::atomic_bool
on;
158 public: std::shared_ptr<boost::function<T> >
callback;
163 template<
typename T>
164 class EventTPrivate :
public EventPrivate
168 typedef std::map<int, std::shared_ptr<EventConnection<T> > >
172 public: EvtConnectionMap connections;
175 public: std::mutex mutex;
178 public: std::list<typename EvtConnectionMap::const_iterator>
185 template<
typename T>
218 public:
template<
typename P >
227 public:
template<
typename P1,
typename P2 >
237 public:
template<
typename P1,
typename P2,
typename P3 >
240 this->
Signal(_p1, _p2, _p3);
248 public:
template<
typename P1,
typename P2,
typename P3,
typename P4 >
252 this->
Signal(_p1, _p2, _p3, _p4);
261 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
264 const P4 &_p4,
const P5 &_p5)
266 this->
Signal(_p1, _p2, _p3, _p4, _p5);
276 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
277 typename P5,
typename P6 >
279 const P4 &_p4,
const P5 &_p5,
const P6 &_p6)
281 this->
Signal(_p1, _p2, _p3, _p4, _p5, _p6);
292 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
293 typename P5,
typename P6,
typename P7 >
295 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
298 this->
Signal(_p1, _p2, _p3, _p4, _p5, _p6, _p7);
310 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
311 typename P5,
typename P6,
typename P7,
typename P8 >
313 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
314 const P7 &_p7,
const P8 &_p8)
316 this->
Signal(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8);
329 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
330 typename P5,
typename P6,
typename P7,
typename P8,
333 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
334 const P7 &_p7,
const P8 &_p8,
const P9 &_p9)
336 this->
Signal(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9);
350 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
351 typename P5,
typename P6,
typename P7,
typename P8,
352 typename P9,
typename P10 >
354 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
355 const P7 &_p7,
const P8 &_p8,
const P9 &_p9,
358 this->
Signal(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9, _p10);
366 this->myDataPtr->signaled =
true;
367 for (
auto iter: this->myDataPtr->connections)
370 (*iter.second->callback)();
376 public:
template<
typename P >
381 this->myDataPtr->signaled =
true;
382 for (
auto iter: this->myDataPtr->connections)
385 (*iter.second->callback)(_p);
392 public:
template<
typename P1,
typename P2 >
393 void Signal(
const P1 &_p1,
const P2 &_p2)
397 this->myDataPtr->signaled =
true;
398 for (
auto iter: this->myDataPtr->connections)
401 (*iter.second->callback)(_p1, _p2);
409 public:
template<
typename P1,
typename P2,
typename P3 >
410 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3)
414 this->myDataPtr->signaled =
true;
415 for (
auto iter: this->myDataPtr->connections)
418 (*iter.second->callback)(_p1, _p2, _p3);
427 public:
template<
typename P1,
typename P2,
typename P3,
typename P4>
428 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3,
433 this->myDataPtr->signaled =
true;
434 for (
auto iter: this->myDataPtr->connections)
437 (*iter.second->callback)(_p1, _p2, _p3, _p4);
447 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
449 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3,
450 const P4 &_p4,
const P5 &_p5)
454 this->myDataPtr->signaled =
true;
455 for (
auto iter: this->myDataPtr->connections)
458 (*iter.second->callback)(_p1, _p2, _p3, _p4, _p5);
469 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
470 typename P5,
typename P6>
471 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3,
472 const P4 &_p4,
const P5 &_p5,
const P6 &_p6)
476 this->myDataPtr->signaled =
true;
477 for (
auto iter: this->myDataPtr->connections)
480 (*iter.second->callback)(_p1, _p2, _p3, _p4, _p5, _p6);
492 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
493 typename P5,
typename P6,
typename P7>
494 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3,
495 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
const P7 &_p7)
499 this->myDataPtr->signaled =
true;
500 for (
auto iter: this->myDataPtr->connections.begin())
503 (*iter.second->callback)(_p1, _p2, _p3, _p4, _p5, _p6, _p7);
516 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
517 typename P5,
typename P6,
typename P7,
typename P8>
518 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3,
519 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
const P7 &_p7,
524 this->myDataPtr->signaled =
true;
525 for (
auto iter: this->myDataPtr->connections)
529 (*iter.second->callback)(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8);
544 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
545 typename P5,
typename P6,
typename P7,
typename P8,
547 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3,
548 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
const P7 &_p7,
549 const P8 &_p8,
const P9 &_p9)
553 this->myDataPtr->signaled =
true;
554 for (
auto iter: this->myDataPtr->connections)
558 (*iter.second->callback)(
559 _p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9);
575 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
576 typename P5,
typename P6,
typename P7,
typename P8,
577 typename P9,
typename P10 >
578 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3,
579 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
const P7 &_p7,
580 const P8 &_p8,
const P9 &_p9,
const P10 &_p10)
584 this->myDataPtr->signaled =
true;
585 for (
auto iter: this->myDataPtr->connections)
589 (*iter.second->callback)(
590 _p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9, _p10);
598 private:
void Cleanup();
601 private: EventTPrivate<T> *myDataPtr;
607 :
Event(*(new EventTPrivate<T>()))
609 this->myDataPtr =
static_cast<EventTPrivate<T>*
>(this->
dataPtr);
616 this->myDataPtr->connections.clear();
625 if (!this->myDataPtr->connections.empty())
627 auto const &iter = this->myDataPtr->connections.rbegin();
628 index = iter->first + 1;
631 new boost::function<T>(_subscriber)));
643 this->Disconnect(_c->GetId());
644 _c->dataPtr->event =
NULL;
645 _c->dataPtr->id = -1;
653 return this->myDataPtr->connections.size();
662 auto const &it = this->myDataPtr->connections.find(_id);
664 if (it != this->myDataPtr->connections.end())
666 it->second->on =
false;
667 this->myDataPtr->connectionsToRemove.push_back(it);
675 std::lock_guard<std::mutex> lock(this->myDataPtr->mutex);
677 for (
auto &conn : this->myDataPtr->connectionsToRemove)
678 this->myDataPtr->connections.erase(conn);
679 this->myDataPtr->connectionsToRemove.clear();
unsigned int ConnectionCount() const
Get the number of connections.
Definition: Event.hh:651
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4)
Signal the event with four parameters.
Definition: Event.hh:249
virtual ~EventT()
Destructor.
Definition: Event.hh:614
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5)
Signal the event with five parameters.
Definition: Event.hh:263
void Signal(const P &_p)
Signal the event with one parameter.
Definition: Event.hh:377
Base class for all events.
Definition: Event.hh:64
A class that encapsulates a connection.
Definition: Event.hh:116
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5, const P6 &_p6)
Signal the event with six parameter.
Definition: Event.hh:471
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5, const P6 &_p6, const P7 &_p7, const P8 &_p8, const P9 &_p9)
Signal the event with nine parameter.
Definition: Event.hh:547
EventPrivate * dataPtr
Data pointer.
Definition: Event.hh:89
void operator()()
Access the signal.
Definition: Event.hh:213
void Signal(const P1 &_p1, const P2 &_p2)
Signal the event with two parameter.
Definition: Event.hh:393
EventT()
Constructor.
Definition: Event.hh:606
std::shared_ptr< boost::function< T > > callback
Callback function.
Definition: Event.hh:158
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5, const P6 &_p6, const P7 &_p7)
Signal the event with seven parameters.
Definition: Event.hh:294
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5, const P6 &_p6, const P7 &_p7, const P8 &_p8)
Signal the event with eight parameters.
Definition: Event.hh:312
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5, const P6 &_p6)
Signal the event with six parameters.
Definition: Event.hh:278
ConnectionPtr Connect(const boost::function< T > &_subscriber)
Connect a callback to this event.
Definition: Event.hh:622
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5, const P6 &_p6, const P7 &_p7, const P8 &_p8, const P9 &_p9, const P10 &_p10)
Signal the event with ten parameters.
Definition: Event.hh:353
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3)
Signal the event with three parameters.
Definition: Event.hh:238
void operator()(const P1 &_p1, const P2 &_p2)
Signal the event with two parameters.
Definition: Event.hh:228
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:153
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5)
Signal the event with five parameter.
Definition: Event.hh:449
#define NULL
Definition: CommonTypes.hh:31
std::atomic_bool on
On/off value for the event callback.
Definition: Event.hh:155
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3)
Signal the event with three parameter.
Definition: Event.hh:410
virtual void Disconnect(ConnectionPtr _c)
Disconnect a callback to this event.
Definition: Event.hh:638
A class for event processing.
Definition: Event.hh:186
void Signal()
Signal the event for all subscribers.
Definition: Event.hh:362
EventConnection(const bool _on, boost::function< T > *_cb)
Constructor.
Definition: Event.hh:145
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4)
Signal the event with four parameter.
Definition: Event.hh:428
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5, const P6 &_p6, const P7 &_p7)
Signal the event with seven parameter.
Definition: Event.hh:494
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5, const P6 &_p6, const P7 &_p7, const P8 &_p8, const P9 &_p9, const P10 &_p10)
Signal the event with ten parameter.
Definition: Event.hh:578
void operator()(const P &_p)
Signal the event with one parameter.
Definition: Event.hh:219
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5, const P6 &_p6, const P7 &_p7, const P8 &_p8, const P9 &_p9)
Signal the event with nine parameters.
Definition: Event.hh:332
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:44
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5, const P6 &_p6, const P7 &_p7, const P8 &_p8)
Signal the event with eight parameter.
Definition: Event.hh:518