kakoune/src/event_manager.hh

90 lines
1.8 KiB
C++
Raw Normal View History

#ifndef event_manager_hh_INCLUDED
#define event_manager_hh_INCLUDED
#include "utils.hh"
#include "flags.hh"
2013-01-14 19:07:38 +01:00
#include <chrono>
2013-01-31 18:58:25 +01:00
#include <unordered_set>
2013-01-14 19:07:38 +01:00
namespace Kakoune
{
enum class EventMode
{
Normal = 1 << 0,
Urgent = 1 << 1
};
template<> struct WithBitOps<EventMode> : std::true_type {};
class FDWatcher
{
public:
using Callback = std::function<void (FDWatcher& watcher, EventMode mode)>;
FDWatcher(int fd, Callback callback);
~FDWatcher();
int fd() const { return m_fd; }
void run(EventMode mode);
private:
FDWatcher(const FDWatcher&) = delete;
int m_fd;
Callback m_callback;
};
2013-01-14 19:07:38 +01:00
using Clock = std::chrono::steady_clock;
using TimePoint = Clock::time_point;
class Timer
{
public:
using Callback = std::function<void (Timer& timer)>;
Timer(TimePoint date, Callback callback,
EventMode mode = EventMode::Normal);
2013-01-14 19:07:38 +01:00
~Timer();
TimePoint next_date() const { return m_date; }
void set_next_date(TimePoint date) { m_date = date; }
void run(EventMode mode);
2013-01-14 19:07:38 +01:00
private:
TimePoint m_date;
EventMode m_mode;
2013-01-14 19:07:38 +01:00
Callback m_callback;
};
// The EventManager provides an interface to file descriptor
// based event handling.
//
// The program main loop should call handle_next_events()
// until it's time to quit.
class EventManager : public Singleton<EventManager>
{
public:
EventManager();
~EventManager();
void handle_next_events(EventMode mode);
// force the watchers associated with fd to be executed
// on next handle_next_events call.
void force_signal(int fd);
private:
friend class FDWatcher;
2013-01-14 19:07:38 +01:00
friend class Timer;
2013-01-31 18:58:25 +01:00
std::unordered_set<FDWatcher*> m_fd_watchers;
std::unordered_set<Timer*> m_timers;
std::vector<int> m_forced_fd;
2013-01-14 19:07:38 +01:00
TimePoint m_last;
};
}
#endif // event_manager_hh_INCLUDED