Skip to content

Commit

Permalink
libcore|EventLoop: API usability improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 21daea0 commit abdb215
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions doomsday/libs/core/include/de/core/coreevent.h
Expand Up @@ -34,6 +34,7 @@ class DE_PUBLIC CoreEvent : public Event
CoreEvent(int type);
CoreEvent(int type, const Value &value);
CoreEvent(int type, const std::function<void()> &callback);
CoreEvent(const std::function<void()> &callback);

const Value &value() const;
inline int valuei() const { return value().asInt(); }
Expand Down
3 changes: 3 additions & 0 deletions doomsday/libs/core/include/de/core/eventloop.h
Expand Up @@ -78,6 +78,9 @@ class DE_PUBLIC EventLoop

virtual void processEvent(const Event &event);

public:
static void post(Event *event);

private:
DE_PRIVATE(d)
};
Expand Down
5 changes: 5 additions & 0 deletions doomsday/libs/core/src/core/coreevent.cpp
Expand Up @@ -35,6 +35,11 @@ CoreEvent::CoreEvent(int type, const std::function<void()> &callback)
, _callback(callback)
{}

CoreEvent::CoreEvent(const std::function<void()> &callback)
: Event(Callback)
, _callback(callback)
{}

const Value &CoreEvent::value() const
{
if (!_value)
Expand Down
13 changes: 13 additions & 0 deletions doomsday/libs/core/src/core/eventloop.cpp
Expand Up @@ -157,6 +157,19 @@ void EventLoop::processEvent(const Event &event)
}
}

void EventLoop::post(Event *event)
{
if (auto *evloop = get())
{
evloop->postEvent(event);
}
else
{
delete event;
warning("[EventLoop] Posted event was discarded because no event loop is running");
}
}

EventLoop *EventLoop::get()
{
using namespace internal;
Expand Down

0 comments on commit abdb215

Please sign in to comment.