Skip to content

Commit

Permalink
Refactor EventLoop class and introduce a new virtual-base class Event…
Browse files Browse the repository at this point in the history
…LoopInterface (#1175)
  • Loading branch information
wengxt authored Nov 12, 2024
1 parent 717034c commit 0c415a1
Show file tree
Hide file tree
Showing 9 changed files with 645 additions and 385 deletions.
3 changes: 2 additions & 1 deletion src/lib/fcitx-utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ set(FCITX_UTILS_SOURCES
cutf8.cpp
color.cpp
i18nstring.cpp
event_common.cpp
eventdispatcher.cpp
library.cpp
fs.cpp
Expand All @@ -60,6 +59,8 @@ set(FCITX_UTILS_SOURCES
misc.cpp
semver.cpp
keydata.cpp
event.cpp
eventloopinterface.cpp
)

set(FCITX_UTILS_HEADERS
Expand Down
86 changes: 86 additions & 0 deletions src/lib/fcitx-utils/event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

/*
* SPDX-FileCopyrightText: 2015-2015 CSSlayer <[email protected]>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
*/

#include "event.h"
#include <cstdint>
#include <cstring>
#include <ctime>
#include <memory>
#include <utility>
#include "event_p.h"
#include "eventloopinterface.h"
#include "macros.h"

namespace fcitx {

class EventLoopPrivate {
public:
EventLoopPrivate(std::unique_ptr<EventLoopInterface> impl)
: impl_(std::move(impl)) {}

std::unique_ptr<EventLoopInterface> impl_;
};

EventLoop::EventLoop() : EventLoop(createDefaultEventLoop()) {}

EventLoop::EventLoop(std::unique_ptr<EventLoopInterface> impl)
: d_ptr(std::make_unique<EventLoopPrivate>(std::move(impl))) {}

EventLoop::~EventLoop() = default;

const char *EventLoop::impl() { return defaultEventLoopImplementation(); }

const char *EventLoop::implementation() const {
FCITX_D();
return d->impl_->implementation();
}

void *EventLoop::nativeHandle() {
FCITX_D();
return d->impl_->nativeHandle();
}

bool EventLoop::exec() {
FCITX_D();
return d->impl_->exec();
}

void EventLoop::exit() {
FCITX_D();
return d->impl_->exit();
}

std::unique_ptr<EventSourceIO> EventLoop::addIOEvent(int fd, IOEventFlags flags,
IOCallback callback) {
FCITX_D();
return d->impl_->addIOEvent(fd, flags, std::move(callback));
}

std::unique_ptr<EventSourceTime>
EventLoop::addTimeEvent(clockid_t clock, uint64_t usec, uint64_t accuracy,
TimeCallback callback) {
FCITX_D();
return d->impl_->addTimeEvent(clock, usec, accuracy, std::move(callback));
}

std::unique_ptr<EventSource> EventLoop::addExitEvent(EventCallback callback) {
FCITX_D();
return d->impl_->addExitEvent(std::move(callback));
}

std::unique_ptr<EventSource> EventLoop::addDeferEvent(EventCallback callback) {
FCITX_D();
return d->impl_->addDeferEvent(std::move(callback));
}

std::unique_ptr<EventSource> EventLoop::addPostEvent(EventCallback callback) {
FCITX_D();
return d->impl_->addPostEvent(std::move(callback));
}

} // namespace fcitx
63 changes: 8 additions & 55 deletions src/lib/fcitx-utils/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,76 +8,29 @@
#define _FCITX_UTILS_EVENT_H_

#include <cstdint>
#include <functional>
#include <memory>
#include <stdexcept>
#include <fcitx-utils/eventloopinterface.h>
#include <fcitx-utils/flags.h>
#include <fcitx-utils/macros.h>
#include "fcitxutils_export.h"

namespace fcitx {

enum class IOEventFlag {
In = (1 << 0),
Out = (1 << 1),
Err = (1 << 2),
Hup = (1 << 3),
EdgeTrigger = (1 << 4),
};

using IOEventFlags = Flags<IOEventFlag>;

class FCITXUTILS_EXPORT EventLoopException : public std::runtime_error {
public:
EventLoopException(int error);

FCITX_NODISCARD int error() const { return errno_; }

private:
int errno_;
};

struct FCITXUTILS_EXPORT EventSource {
virtual ~EventSource();
FCITX_NODISCARD virtual bool isEnabled() const = 0;
virtual void setEnabled(bool enabled) = 0;
FCITX_NODISCARD virtual bool isOneShot() const = 0;
virtual void setOneShot() = 0;
};

struct FCITXUTILS_EXPORT EventSourceIO : public EventSource {
FCITX_NODISCARD virtual int fd() const = 0;
virtual void setFd(int fd) = 0;
FCITX_NODISCARD virtual IOEventFlags events() const = 0;
virtual void setEvents(IOEventFlags flags) = 0;
FCITX_NODISCARD virtual IOEventFlags revents() const = 0;
};

struct FCITXUTILS_EXPORT EventSourceTime : public EventSource {
virtual void setNextInterval(uint64_t time);
FCITX_NODISCARD virtual uint64_t time() const = 0;
virtual void setTime(uint64_t time) = 0;
FCITX_NODISCARD virtual uint64_t accuracy() const = 0;
virtual void setAccuracy(uint64_t accuracy) = 0;
FCITX_NODISCARD virtual clockid_t clock() const = 0;
};

using IOCallback =
std::function<bool(EventSourceIO *, int fd, IOEventFlags flags)>;
using TimeCallback = std::function<bool(EventSourceTime *, uint64_t usec)>;
using EventCallback = std::function<bool(EventSource *)>;

FCITXUTILS_EXPORT uint64_t now(clockid_t clock);

class EventLoopPrivate;
class FCITXUTILS_EXPORT EventLoop {
public:
EventLoop();
EventLoop(std::unique_ptr<EventLoopInterface> impl);
virtual ~EventLoop();
bool exec();
void exit();

static const char *impl();
/**
* Return the default implementation name.
*/
FCITXUTILS_DEPRECATED static const char *impl();

const char *implementation() const;
void *nativeHandle();

FCITX_NODISCARD std::unique_ptr<EventSourceIO>
Expand Down
Loading

0 comments on commit 0c415a1

Please sign in to comment.