--- nono/vm/event.h 2026/04/29 17:05:10 1.1.1.9 +++ nono/vm/event.h 2026/04/29 17:06:00 1.1.1.13 @@ -12,53 +12,11 @@ #include "device.h" -// ユーザ定義リテラルを使ったサフィックス - -// ナノ秒 -static inline constexpr uint64 operator"" _nsec(unsigned long long ns) -{ - return ns; -} -static inline constexpr uint64 operator"" _nsec(long double ns) -{ - return ns; +template +EventCallback_t ToEventCallback(T f) { + return static_cast(f); } -// マイクロ秒 -static inline constexpr uint64 operator"" _usec(unsigned long long us) -{ - return us * 1000_nsec; -} -static inline constexpr uint64 operator"" _usec(long double us) -{ - return us * 1000_nsec; -} - -// ミリ秒 -static inline constexpr uint64 operator"" _msec(unsigned long long ms) -{ - return ms * 1000_usec; -} -static inline constexpr uint64 operator"" _msec(long double ms) -{ - return ms * 1000_usec; -} - -// 秒 -static inline constexpr uint64 operator"" _sec(unsigned long long sec) -{ - return sec * 1000_msec; -} -static inline constexpr uint64 operator"" _sec(long double sec) -{ - return sec * 1000_msec; -} - -// コールバック関数の型 -class Event; -using EventCallback_t = void (Device::*)(Event&); -#define ToEventCallback(f) static_cast(f) - // イベント // @@ -70,28 +28,29 @@ using EventCallback_t = void (Device::*) class Event { public: - Event(); - Event(Device *dev_); + Event(Device *, int code_, EventCallback_t, const std::string&); ~Event(); - // このイベントを登録する - void Regist(); - // このイベントを登録する (名前を同時に設定する) - void Regist(const std::string& name_); - // このイベントが動作中なら true を返す bool IsRunning() const { return active; } + Device *GetOwner() const noexcept { return dev; } + EventCallback_t GetCallback() const noexcept { return func; } + // 以下はイベントを呼び出す側がセットする + // コールバック関数をセットする。 + template + void SetCallback(void (T::*func_)(Event *)) { + func = static_cast(func_); + } + // イベントを起こすまでの仮想時刻での相対時間 [nsec] // 例えば今から 1msec 後にイベントを起こしたい場合は 1000000 となる。 uint64 time {}; - Device *dev {}; // デバイス - EventCallback_t func {}; // コールバック関数 int code {}; // コールバック関数への引数 const std::string GetName() const { return name; } void SetName(const std::string& val); @@ -104,7 +63,47 @@ class Event uint64 last_count {}; // (表示用)前回表示したときの count private: + Device *dev {}; // デバイス + EventCallback_t func {}; // コールバック関数 + // イベント名(表示用) // '\0' 含まず25文字まで。デバイス名も必要なら含めること。 std::string name {}; }; + +// +// イベントマネージャ +// +class EventManager : public Object +{ + using inherited = Object; + friend class Scheduler; + public: + EventManager(); + ~EventManager() override; + + // イベントを登録する。 + Event *Regist(Device *d) { + return Regist(d, 0); + } + Event *Regist(Device *d, EventCallback_t f) { + return Regist(d, 0, f); + } + Event *Regist(Device *d, EventCallback_t f, const std::string& n) { + return Regist(d, 0, f, n); + } + Event *Regist(Device *d, int code) { + return Regist(d, code, NULL); + } + Event *Regist(Device *d, int code, EventCallback_t f) { + return Regist(d, code, f, ""); + } + Event *Regist(Device *d, int code, EventCallback_t f, const std::string& n); + + private: + std::vector all {}; +}; + +inline EventManager* GetEventManager() { + return Object::GetObject(OBJ_EVENT_MANAGER); +}