79 lines
1.3 KiB
C
Raw Normal View History

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