2017-08-06 00:14:35 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <ostream>
|
|
|
|
|
|
|
|
namespace Dexode
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename ... Args>
|
2017-08-06 11:22:59 +02:00
|
|
|
class Event
|
2017-08-06 00:14:35 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Callback = std::function<void(Args...)>;
|
|
|
|
|
2017-08-06 11:22:59 +02:00
|
|
|
constexpr explicit Event(const std::string& name)
|
2017-08-06 00:14:35 +02:00
|
|
|
: _key{std::hash<std::string>{}(name + typeid(Callback).name())}
|
|
|
|
, _name{name}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-06 11:22:59 +02:00
|
|
|
constexpr Event(const Event& other)
|
2017-08-06 00:14:35 +02:00
|
|
|
: _key{other._key}
|
|
|
|
, _name{other._name}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-06 11:22:59 +02:00
|
|
|
Event(Event&& other)
|
2017-08-06 00:14:35 +02:00
|
|
|
: _key{other._key}
|
|
|
|
, _name{other._name}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-06 11:22:59 +02:00
|
|
|
Event& operator=(Event&&) = delete;
|
2017-08-06 00:14:35 +02:00
|
|
|
|
2017-08-06 11:22:59 +02:00
|
|
|
Event& operator=(const Event&) = delete;
|
2017-08-06 00:14:35 +02:00
|
|
|
|
|
|
|
const size_t getKey() const
|
|
|
|
{
|
|
|
|
return _key;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& getName() const
|
|
|
|
{
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
|
2017-08-06 11:22:59 +02:00
|
|
|
bool operator==(const Event& rhs) const
|
2017-08-06 00:14:35 +02:00
|
|
|
{
|
|
|
|
return _key == rhs._key &&
|
|
|
|
_name == rhs._name;
|
|
|
|
}
|
|
|
|
|
2017-08-06 11:22:59 +02:00
|
|
|
bool operator!=(const Event& rhs) const
|
2017-08-06 00:14:35 +02:00
|
|
|
{
|
|
|
|
return !(rhs == *this);
|
|
|
|
}
|
|
|
|
|
2017-08-06 11:22:59 +02:00
|
|
|
friend std::ostream& operator<<(std::ostream& stream, const Event& notification)
|
2017-08-06 00:14:35 +02:00
|
|
|
{
|
2017-08-06 11:22:59 +02:00
|
|
|
stream << "Event{name: " << notification._name << " key: " << notification._key;
|
2017-08-06 00:14:35 +02:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::size_t _key;
|
|
|
|
const std::string _name;
|
|
|
|
};
|
|
|
|
|
2017-08-06 11:22:59 +02:00
|
|
|
template<typename EventType>
|
|
|
|
struct event_traits;
|
2017-08-06 00:14:35 +02:00
|
|
|
|
|
|
|
template<typename ... Args>
|
2017-08-06 11:22:59 +02:00
|
|
|
struct event_traits<Event<Args...>>
|
2017-08-06 00:14:35 +02:00
|
|
|
{
|
|
|
|
using callback_type = typename std::function<void(Args...)>;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|