utility-event: Fix compile errors

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2019-09-04 20:30:16 +02:00
parent 29ab00633c
commit f61cdeb582
2 changed files with 71 additions and 28 deletions

View file

@ -19,15 +19,30 @@
#include "utility-event.hpp"
utility::event::event()
{
on.add = std::make_shared<utility::event>();
on.remove = std::make_shared<utility::event>();
on.empty = std::make_shared<utility::event>();
}
utility::event::~event()
{
clear();
on.add.reset();
on.remove.reset();
on.empty.reset();
}
size_t utility::event::add(listener_t callback)
{
_listeners.push_back(callback);
{
arguments_t args;
event_args_t args;
args.emplace("event", this);
args.emplace("listener", &callback);
on.add(args);
on.add->call(args);
}
return _listeners.size();
@ -38,16 +53,16 @@ size_t utility::event::remove(listener_t callback)
_listeners.remove(callback);
{
arguments_t args;
event_args_t args;
args.emplace("event", this);
args.emplace("listener", &callback);
on.remove(args);
on.remove->call(args);
}
if (_listeners.empty()) {
arguments_t args;
event_args_t args;
args.emplace("event", this);
on.empty(args);
on.empty->call(args);
}
return _listeners.size();
@ -65,22 +80,26 @@ bool utility::event::empty()
void utility::event::clear()
{
return _listeners.clear();
_listeners.clear();
event_args_t args;
args.emplace("event", this);
on.empty->call(args);
}
size_t utility::event::call(arguments_t& arguments)
size_t utility::event::call(event_args_t& arguments)
{
size_t idx = 0;
for (auto const& listener : _listeners) {
if (listener.second(listener.first, arguments)) {
break;
}
/*if (listener.first.expired()) {
// Lifeline has expired, so remove it.
_listeners.remove(listener);
continue;
}*/
//if (listener.second(listener.first, arguments)) {
// break;
// }
idx++;
}
return idx;
}
size_t utility::event::operator()(arguments_t& arguments)
{
return call(arguments);
}

View file

@ -29,15 +29,38 @@
#include <string>
namespace utility {
typedef std::map<std::string, void*> arguments_t;
typedef std::shared_ptr<void> lifeline_t;
typedef std::function<bool(lifeline_t, arguments_t&)> callback_t;
typedef std::pair<lifeline_t, callback_t> listener_t;
typedef std::weak_ptr<void> lifeline_t;
typedef std::map<std::string, void*> event_args_t;
#define D_EVENT_FUNC_T void(std::weak_ptr<void>, event_args_t)
typedef std::function<D_EVENT_FUNC_T> event_cb_t;
struct event_pair {
lifeline_t lifeline;
event_cb_t callback;
event_pair(lifeline_t _lifeline, event_cb_t _callback)
{
lifeline = _lifeline;
callback = _callback;
}
bool operator==(const event_pair& rhs)
{
return (!lifeline.owner_before(rhs.lifeline) && !rhs.lifeline.owner_before(lifeline))
&& (lifeline.lock() == rhs.lifeline.lock())
&& (callback.target<D_EVENT_FUNC_T>() == rhs.callback.target<D_EVENT_FUNC_T>());
}
};
typedef event_pair listener_t;
class event {
std::list<listener_t> _listeners;
public:
event();
virtual ~event();
size_t add(listener_t listener);
inline event operator+=(listener_t listener)
@ -65,28 +88,29 @@ namespace utility {
void clear();
size_t call(arguments_t& arguments);
size_t call(event_args_t& arguments);
inline size_t operator()(arguments_t& arguments)
inline size_t operator()(event_args_t& arguments)
{
return call(arguments);
}
inline size_t call()
{
return call(arguments_t{});
event_args_t args{};
return call(args);
}
inline size_t operator()()
{
return call(arguments_t{});
return call();
}
public:
struct {
utility::event add;
utility::event remove;
utility::event empty;
std::shared_ptr<utility::event> add;
std::shared_ptr<utility::event> remove;
std::shared_ptr<utility::event> empty;
} on;
};
} // namespace utility