##

Event.hh
Go to the documentation of this file.
/*
* Copyright (C) 2012 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef GAZEBO_COMMON_EVENT_HH_
#define GAZEBO_COMMON_EVENT_HH_
#include <atomic>
#include <functional>
#include <list>
#include <map>
#include <memory>
#include <mutex>
#include <gazebo/gazebo_config.h>
#include <gazebo/common/Time.hh>
#include <gazebo/common/CommonTypes.hh>
#include "gazebo/util/system.hh"
namespace gazebo
{
namespace event
{
class GZ_COMMON_VISIBLE Event
{
public: Event();
public: virtual ~Event();
public: virtual void Disconnect(int _id) = 0;
public: bool Signaled() const;
public: void SetSignaled(const bool _sig);
private: bool signaled;
};
class GZ_COMMON_VISIBLE Connection
{
public: Connection(Event *_e, const int _i);
public: ~Connection();
public: int Id() const;
private: Event *event = nullptr;
private: int id = -1;
private: common::Time creationTime;
public: template<typename T> friend class EventT;
};
template<typename T>
class EventT : public Event
{
public: EventT();
public: virtual ~EventT();
public: ConnectionPtr Connect(const std::function<T> &_subscriber);
public: virtual void Disconnect(int _id);
public: unsigned int ConnectionCount() const;
public: void operator()()
{this->Signal();}
public: template< typename P >
void operator()(const P &_p)
{
this->Signal(_p);
}
public: template< typename P1, typename P2 >
void operator()(const P1 &_p1, const P2 &_p2)
{
this->Signal(_p1, _p2);
}
public: template< typename P1, typename P2, typename P3 >
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3)
{
this->Signal(_p1, _p2, _p3);
}
public: template< typename P1, typename P2, typename P3, typename P4 >
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3,
const P4 &_p4)
{
this->Signal(_p1, _p2, _p3, _p4);
}
public: template< typename P1, typename P2, typename P3, typename P4,
typename P5 >
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3,
const P4 &_p4, const P5 &_p5)
{
this->Signal(_p1, _p2, _p3, _p4, _p5);
}
public: template< typename P1, typename P2, typename P3, typename P4,
typename P5, typename P6 >
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3,
const P4 &_p4, const P5 &_p5, const P6 &_p6)
{
this->Signal(_p1, _p2, _p3, _p4, _p5, _p6);
}
public: template< typename P1, typename P2, typename P3, typename P4,
typename P5, typename P6, typename P7 >
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3,
const P4 &_p4, const P5 &_p5, const P6 &_p6,
const P7 &_p7)
{
this->Signal(_p1, _p2, _p3, _p4, _p5, _p6, _p7);
}
public: template< typename P1, typename P2, typename P3, typename P4,
typename P5, typename P6, typename P7, typename P8 >
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)
{
this->Signal(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8);
}
public: template< typename P1, typename P2, typename P3, typename P4,
typename P5, typename P6, typename P7, typename P8,
typename P9 >
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)
{
this->Signal(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9);
}
public: template< typename P1, typename P2, typename P3, typename P4,
typename P5, typename P6, typename P7, typename P8,
typename P9, typename P10 >
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)
{
this->Signal(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9, _p10);
}
public: void Signal()
{
this->Cleanup();
this->SetSignaled(true);
for (const auto &iter: this->connections)
{
if (iter.second->on)
iter.second->callback();
}
}
public: template< typename P >
void Signal(const P &_p)
{
this->Cleanup();
this->SetSignaled(true);
for (const auto &iter: this->connections)
{
if (iter.second->on)
iter.second->callback(_p);
}
}
public: template< typename P1, typename P2 >
void Signal(const P1 &_p1, const P2 &_p2)
{
this->Cleanup();
this->SetSignaled(true);
for (const auto &iter: this->connections)
{
if (iter.second->on)
iter.second->callback(_p1, _p2);
}
}
public: template< typename P1, typename P2, typename P3 >
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3)
{
this->Cleanup();
this->SetSignaled(true);
for (const auto &iter: this->connections)
{
if (iter.second->on)
iter.second->callback(_p1, _p2, _p3);
}
}
public: template<typename P1, typename P2, typename P3, typename P4>
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3,
const P4 &_p4)
{
this->Cleanup();
this->SetSignaled(true);
for (const auto &iter: this->connections)
{
if (iter.second->on)
iter.second->callback(_p1, _p2, _p3, _p4);
}
}
public: template<typename P1, typename P2, typename P3, typename P4,
typename P5>
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3,
const P4 &_p4, const P5 &_p5)
{
this->Cleanup();
this->SetSignaled(true);
for (const auto &iter: this->connections)
{
if (iter.second->on)
iter.second->callback(_p1, _p2, _p3, _p4, _p5);
}
}
public: template<typename P1, typename P2, typename P3, typename P4,
typename P5, typename P6>
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3,
const P4 &_p4, const P5 &_p5, const P6 &_p6)
{
this->Cleanup();
this->SetSignaled(true);
for (const auto &iter: this->connections)
{
if (iter.second->on)
iter.second->callback(_p1, _p2, _p3, _p4, _p5, _p6);
}
}
public: template<typename P1, typename P2, typename P3, typename P4,
typename P5, typename P6, typename P7>
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3,
const P4 &_p4, const P5 &_p5, const P6 &_p6, const P7 &_p7)
{
this->Cleanup();
this->SetSignaled(true);
for (const auto &iter: this->connections)
{
if (iter.second->on)
iter.second->callback(_p1, _p2, _p3, _p4, _p5, _p6, _p7);
}
}
public: template<typename P1, typename P2, typename P3, typename P4,
typename P5, typename P6, typename P7, typename P8>
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)
{
this->Cleanup();
this->SetSignaled(true);
for (const auto &iter: this->connections)
{
if (iter.second->on)
{
iter.second->callback(_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8);
}
}
}
public: template< typename P1, typename P2, typename P3, typename P4,
typename P5, typename P6, typename P7, typename P8,
typename P9 >
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)
{
this->Cleanup();
this->SetSignaled(true);
for (const auto &iter: this->connections)
{
if (iter.second->on)
{
iter.second->callback(
_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9);
}
}
}
public: template< typename P1, typename P2, typename P3, typename P4,
typename P5, typename P6, typename P7, typename P8,
typename P9, typename P10 >
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)
{
this->Cleanup();
this->SetSignaled(true);
for (const auto &iter: this->connections)
{
if (iter.second->on)
{
iter.second->callback(
_p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9, _p10);
}
}
}
private: void Cleanup();
private: class EventConnection
{
public: EventConnection(const bool _on, const std::function<T> &_cb)
: callback(_cb)
{
// Windows Visual Studio 2012 does not have atomic_bool constructor,
// so we have to set "on" using operator=
this->on = _on;
}
public: std::atomic_bool on;
public: std::function<T> callback;
};
typedef std::map<int, std::unique_ptr<EventConnection>> EvtConnectionMap;
private: EvtConnectionMap connections;
private: std::mutex mutex;
private: std::list<typename EvtConnectionMap::const_iterator>
connectionsToRemove;
};
template<typename T>
EventT<T>::EventT()
: Event()
{
}
template<typename T>
EventT<T>::~EventT()
{
this->connections.clear();
}
template<typename T>
ConnectionPtr EventT<T>::Connect(const std::function<T> &_subscriber)
{
int index = 0;
if (!this->connections.empty())
{
auto const &iter = this->connections.rbegin();
index = iter->first + 1;
}
this->connections[index].reset(new EventConnection(true, _subscriber));
return ConnectionPtr(new Connection(this, index));
}
template<typename T>
unsigned int EventT<T>::ConnectionCount() const
{
return this->connections.size();
}
template<typename T>
void EventT<T>::Disconnect(int _id)
{
// Find the connection
auto const &it = this->connections.find(_id);
if (it != this->connections.end())
{
it->second->on = false;
this->connectionsToRemove.push_back(it);
}
}
template<typename T>
void EventT<T>::Cleanup()
{
std::lock_guard<std::mutex> lock(this->mutex);
// Remove all queue connections.
for (auto &conn : this->connectionsToRemove)
this->connections.erase(conn);
this->connectionsToRemove.clear();
}
}
}
#endif
gazebo::event::Connection::Id
int Id() const
Get the id of this connection.
gazebo::event::EventT::operator()
void operator()(const P &_p)
Signal the event with one parameter.
Definition: Event.hh:126
Time.hh
gazebo::event::EventT::operator()
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:170
gazebo::event::EventT::operator()
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:219
gazebo::event::EventT::operator()
void operator()(const P1 &_p1, const P2 &_p2)
Signal the event with two parameters.
Definition: Event.hh:135
gazebo
Forward declarations for the common classes.
Definition: Animation.hh:26
gazebo::common::Time
A Time class, can be used to hold wall- or sim-time. stored as sec and nano-sec.
Definition: Time.hh:47
gazebo::event::EventT::Signal
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:401
gazebo::event::Event
Base class for all events.
Definition: Event.hh:43
gazebo::event::EventT::Signal
void Signal()
Signal the event for all subscribers.
Definition: Event.hh:269
gazebo::event::Connection::~Connection
~Connection()
Destructor.
system.hh
gazebo::event::EventT::Signal
void Signal(const P1 &_p1, const P2 &_p2)
Signal the event with two parameter.
Definition: Event.hh:300
CommonTypes.hh
gazebo::event::EventT::Signal
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:425
gazebo::event::EventT::Signal
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:454
gazebo::event::EventT::Signal
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:485
gazebo::event::EventT::Signal
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3)
Signal the event with three parameter.
Definition: Event.hh:317
gazebo::event::Connection
A class that encapsulates a connection.
Definition: Event.hh:68
gazebo::event::EventT
A class for event processing.
Definition: Event.hh:97
gazebo::event::EventT::operator()
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4)
Signal the event with four parameters.
Definition: Event.hh:156
gazebo::event::EventT::Signal
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:378
gazebo::event::Event::Signaled
bool Signaled() const
Get whether this event has been signaled.
gazebo::event::EventT::Connect
ConnectionPtr Connect(const std::function< T > &_subscriber)
Connect a callback to this event.
Definition: Event.hh:558
gazebo::event::EventT::~EventT
virtual ~EventT()
Destructor.
Definition: Event.hh:550
gazebo::event::EventT::operator()
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:185
gazebo::event::EventT::operator()
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:239
gazebo::event::ConnectionPtr
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:134
gazebo::event::Event::Event
Event()
Constructor.
gazebo::event::EventT::operator()
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:260
gazebo::event::Event::~Event
virtual ~Event()
Destructor.
gazebo::event::Connection::Connection
Connection(Event *_e, const int _i)
Constructor.
gazebo::event::EventT::Signal
void Signal(const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4)
Signal the event with four parameter.
Definition: Event.hh:335
gazebo::event::EventT::Signal
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:356
gazebo::event::Event::Disconnect
virtual void Disconnect(int _id)=0
Disconnect.
gazebo::event::EventT::Disconnect
virtual void Disconnect(int _id)
Disconnect a callback to this event.
Definition: Event.hh:581
gazebo::event::EventT::operator()
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:201
gazebo::event::EventT::operator()
void operator()()
Access the signal.
Definition: Event.hh:120
gazebo::event::Event::SetSignaled
void SetSignaled(const bool _sig)
Set whether this event has been signaled.
gazebo::event::EventT::EventT
EventT()
Constructor.
Definition: Event.hh:543
gazebo::event::EventT::Signal
void Signal(const P &_p)
Signal the event with one parameter.
Definition: Event.hh:284
gazebo::event::EventT::ConnectionCount
unsigned int ConnectionCount() const
Get the number of connections.
Definition: Event.hh:573
gazebo::event::EventT::operator()
void operator()(const P1 &_p1, const P2 &_p2, const P3 &_p3)
Signal the event with three parameters.
Definition: Event.hh:145