17 #ifndef GAZEBO_COMMON_EVENT_HH_
18 #define GAZEBO_COMMON_EVENT_HH_
27 #include "gazebo/gazebo_config.h"
32 #include "ignition/common/Profiler.hh"
66 private:
bool signaled;
82 public:
int Id()
const;
85 private:
Event *
event =
nullptr;
94 public:
template<
typename T>
friend class EventT;
127 public:
template<
typename P >
136 public:
template<
typename P1,
typename P2 >
146 public:
template<
typename P1,
typename P2,
typename P3 >
149 this->
Signal(_p1, _p2, _p3);
157 public:
template<
typename P1,
typename P2,
typename P3,
typename P4 >
161 this->
Signal(_p1, _p2, _p3, _p4);
170 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
173 const P4 &_p4,
const P5 &_p5)
175 this->
Signal(_p1, _p2, _p3, _p4, _p5);
185 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
186 typename P5,
typename P6 >
188 const P4 &_p4,
const P5 &_p5,
const P6 &_p6)
190 this->
Signal(_p1, _p2, _p3, _p4, _p5, _p6);
201 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
202 typename P5,
typename P6,
typename P7 >
204 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
207 this->
Signal(_p1, _p2, _p3, _p4, _p5, _p6, _p7);
219 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
220 typename P5,
typename P6,
typename P7,
typename P8 >
222 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
223 const P7 &_p7,
const P8 &_p8)
225 this->
Signal(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8);
238 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
239 typename P5,
typename P6,
typename P7,
typename P8,
242 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
243 const P7 &_p7,
const P8 &_p8,
const P9 &_p9)
245 this->
Signal(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9);
259 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
260 typename P5,
typename P6,
typename P7,
typename P8,
261 typename P9,
typename P10 >
263 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
264 const P7 &_p7,
const P8 &_p8,
const P9 &_p9,
267 this->
Signal(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9, _p10);
273 IGN_PROFILE(
"Event::Signal");
278 for (
const auto &iter: this->connections)
282 IGN_PROFILE_BEGIN(
"callback0");
283 iter.second->callback();
291 public:
template<
typename P >
294 IGN_PROFILE(
"Event::Signal");
299 for (
const auto &iter: this->connections)
303 IGN_PROFILE_BEGIN(
"callback1");
304 iter.second->callback(_p);
313 public:
template<
typename P1,
typename P2 >
314 void Signal(
const P1 &_p1,
const P2 &_p2)
316 IGN_PROFILE(
"Event::Signal");
321 for (
const auto &iter: this->connections)
325 IGN_PROFILE_BEGIN(
"callback2");
326 iter.second->callback(_p1, _p2);
336 public:
template<
typename P1,
typename P2,
typename P3 >
337 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3)
339 IGN_PROFILE(
"Event::Signal");
344 for (
const auto &iter: this->connections)
348 IGN_PROFILE_BEGIN(
"callback3");
349 iter.second->callback(_p1, _p2, _p3);
360 public:
template<
typename P1,
typename P2,
typename P3,
typename P4>
361 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3,
364 IGN_PROFILE(
"Event::Signal");
369 for (
const auto &iter: this->connections)
373 IGN_PROFILE_BEGIN(
"callback4");
374 iter.second->callback(_p1, _p2, _p3, _p4);
386 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
388 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3,
389 const P4 &_p4,
const P5 &_p5)
391 IGN_PROFILE(
"Event::Signal");
396 for (
const auto &iter: this->connections)
400 IGN_PROFILE_BEGIN(
"callback5");
401 iter.second->callback(_p1, _p2, _p3, _p4, _p5);
414 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
415 typename P5,
typename P6>
416 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3,
417 const P4 &_p4,
const P5 &_p5,
const P6 &_p6)
419 IGN_PROFILE(
"Event::Signal");
424 for (
const auto &iter: this->connections)
428 IGN_PROFILE_BEGIN(
"callback6");
429 iter.second->callback(_p1, _p2, _p3, _p4, _p5, _p6);
443 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
444 typename P5,
typename P6,
typename P7>
445 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3,
446 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
const P7 &_p7)
448 IGN_PROFILE(
"Event::Signal");
453 for (
const auto &iter: this->connections)
457 IGN_PROFILE_BEGIN(
"callback7");
458 iter.second->callback(_p1, _p2, _p3, _p4, _p5, _p6, _p7);
473 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
474 typename P5,
typename P6,
typename P7,
typename P8>
475 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3,
476 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
const P7 &_p7,
479 IGN_PROFILE(
"Event::Signal");
484 for (
const auto &iter: this->connections)
488 IGN_PROFILE_BEGIN(
"callback8");
489 iter.second->callback(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8);
505 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
506 typename P5,
typename P6,
typename P7,
typename P8,
508 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3,
509 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
const P7 &_p7,
510 const P8 &_p8,
const P9 &_p9)
512 IGN_PROFILE(
"Event::Signal");
517 for (
const auto &iter: this->connections)
521 IGN_PROFILE_BEGIN(
"callback9");
522 iter.second->callback(
523 _p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9);
540 public:
template<
typename P1,
typename P2,
typename P3,
typename P4,
541 typename P5,
typename P6,
typename P7,
typename P8,
542 typename P9,
typename P10 >
543 void Signal(
const P1 &_p1,
const P2 &_p2,
const P3 &_p3,
544 const P4 &_p4,
const P5 &_p5,
const P6 &_p6,
const P7 &_p7,
545 const P8 &_p8,
const P9 &_p9,
const P10 &_p10)
550 for (
const auto &iter: this->connections)
552 IGN_PROFILE(
"Event::Signal");
556 IGN_PROFILE_BEGIN(
"callback10");
557 iter.second->callback(
558 _p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9, _p10);
567 private:
void Cleanup();
570 private:
class EventConnection
573 public: EventConnection(
const bool _on,
const std::function<T> &_cb)
582 public: std::atomic_bool on;
585 public: std::function<T> callback;
590 typedef std::map<int, std::unique_ptr<EventConnection>> EvtConnectionMap;
593 private: EvtConnectionMap connections;
596 private: std::mutex mutex;
599 private: std::list<typename EvtConnectionMap::const_iterator>
614 this->connections.clear();
623 if (!this->connections.empty())
625 auto const &iter = this->connections.rbegin();
626 index = iter->first + 1;
628 this->connections[index].reset(
new EventConnection(
true, _subscriber));
637 return this->connections.size();
646 auto const &it = this->connections.find(_id);
648 if (it != this->connections.end())
650 it->second->on =
false;
651 this->connectionsToRemove.push_back(it);
659 std::lock_guard<std::mutex> lock(this->mutex);
661 for (
auto &conn : this->connectionsToRemove)
662 this->connections.erase(conn);
663 this->connectionsToRemove.clear();
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:48
A class that encapsulates a connection.
Definition: Event.hh:71
int Id() const
Get the id of this connection.
Connection(Event *_e, const int _i)
Constructor.
A class for event processing.
Definition: Event.hh:100
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4)
Signal the event with four parameter.
Definition: Event.hh:361
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3)
Signal the event with three parameter.
Definition: Event.hh:337
void operator()(const P &_p)
Signal the event with one parameter.
Definition: Event.hh:128
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3)
Signal the event with three parameters.
Definition: Event.hh:147
void Signal(const P &_p)
Signal the event with one parameter.
Definition: Event.hh:292
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:203
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:543
void operator()()
Access the signal.
Definition: Event.hh:122
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:187
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:445
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:172
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:262
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:416
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:508
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:475
void Signal(const P1 &_p1, const P2 &_p2)
Signal the event with two parameter.
Definition: Event.hh:314
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:241
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:221
void operator()(const P1 &_p1, const P2 &_p2)
Signal the event with two parameters.
Definition: Event.hh:137
void Signal()
Signal the event for all subscribers.
Definition: Event.hh:271
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4)
Signal the event with four parameters.
Definition: Event.hh:158
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:388
Base class for all events.
Definition: Event.hh:46
virtual void Disconnect(int _id)=0
Disconnect.
virtual ~Event()
Destructor.
void SetSignaled(const bool _sig)
Set whether this event has been signaled.
bool Signaled() const
Get whether this event has been signaled.
unsigned int ConnectionCount() const
Get the number of connections.
Definition: Event.hh:635
EventT()
Constructor.
Definition: Event.hh:605
virtual void Disconnect(int _id)
Disconnect a callback to this event.
Definition: Event.hh:643
ConnectionPtr Connect(const std::function< T > &_subscriber)
Connect a callback to this event.
Definition: Event.hh:620
virtual ~EventT()
Destructor.
Definition: Event.hh:612
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:134
Forward declarations for the common classes.
Definition: Animation.hh:27