|
|
1.1 root 1: //
2: // nono
1.1.1.4 root 3: // Copyright (C) 2020 nono project
4: // Licensed under nono-license.txt
1.1 root 5: //
6:
1.1.1.9 root 7: //
8: // イベント
9: //
10:
1.1 root 11: #pragma once
12:
1.1.1.12 root 13: #include "device.h"
1.1 root 14:
1.1.1.13! root 15: template <typename T>
! 16: EventCallback_t ToEventCallback(T f) {
! 17: return static_cast<EventCallback_t>(f);
! 18: }
1.1.1.9 root 19:
1.1 root 20:
21: // イベント
22: //
23: // イベントは必要なデバイスごとに用意する。おそらく最初から個数が決まって
24: // いることが多いはずなので、デバイスクラス内のメンバ変数(実体) として
25: // 宣言し、デバイスのコンストラクタ等で初期化するという流れになるはず。
1.1.1.5 root 26: //
27: // VM リセットによるイベントタイマーの停止処理は各デバイスの責任で行うこと。
1.1 root 28: class Event
29: {
30: public:
1.1.1.12 root 31: Event(Device *, int code_, EventCallback_t, const std::string&);
1.1.1.9 root 32: ~Event();
1.1 root 33:
1.1.1.7 root 34: // このイベントが動作中なら true を返す
35: bool IsRunning() const {
36: return active;
37: }
38:
1.1.1.13! root 39: Device *GetOwner() const noexcept { return dev; }
! 40: EventCallback_t GetCallback() const noexcept { return func; }
! 41:
1.1 root 42: // 以下はイベントを呼び出す側がセットする
43:
1.1.1.13! root 44: // コールバック関数をセットする。
! 45: template <typename T>
! 46: void SetCallback(void (T::*func_)(Event *)) {
! 47: func = static_cast<EventCallback_t>(func_);
! 48: }
! 49:
1.1.1.6 root 50: // イベントを起こすまでの仮想時刻での相対時間 [nsec]
1.1 root 51: // 例えば今から 1msec 後にイベントを起こしたい場合は 1000000 となる。
1.1.1.5 root 52: uint64 time {};
1.1 root 53:
1.1.1.5 root 54: int code {}; // コールバック関数への引数
1.1.1.9 root 55: const std::string GetName() const { return name; }
1.1.1.2 root 56: void SetName(const std::string& val);
1.1 root 57:
58: // スケジューラが内部で使う
1.1.1.5 root 59: bool active {}; // スケジューラにセットされていれば真
1.1.1.6 root 60: uint64 vtime {}; // イベントを起こす仮想時刻
1.1.1.2 root 61:
1.1.1.9 root 62: uint64 count {}; // コールバックを呼び出した回数
63: uint64 last_count {}; // (表示用)前回表示したときの count
64:
1.1.1.2 root 65: private:
1.1.1.13! root 66: Device *dev {}; // デバイス
! 67: EventCallback_t func {}; // コールバック関数
! 68:
1.1.1.2 root 69: // イベント名(表示用)
70: // '\0' 含まず25文字まで。デバイス名も必要なら含めること。
71: std::string name {};
1.1 root 72: };
1.1.1.12 root 73:
74: //
75: // イベントマネージャ
76: //
77: class EventManager : public Object
78: {
79: using inherited = Object;
80: friend class Scheduler;
81: public:
82: EventManager();
83: ~EventManager() override;
84:
85: // イベントを登録する。
86: Event *Regist(Device *d) {
87: return Regist(d, 0);
88: }
89: Event *Regist(Device *d, EventCallback_t f) {
90: return Regist(d, 0, f);
91: }
92: Event *Regist(Device *d, EventCallback_t f, const std::string& n) {
93: return Regist(d, 0, f, n);
94: }
95: Event *Regist(Device *d, int code) {
96: return Regist(d, code, NULL);
97: }
98: Event *Regist(Device *d, int code, EventCallback_t f) {
99: return Regist(d, code, f, "");
100: }
101: Event *Regist(Device *d, int code, EventCallback_t f, const std::string& n);
102:
103: private:
104: std::vector<Event *> all {};
105: };
106:
1.1.1.13! root 107: inline EventManager* GetEventManager() {
1.1.1.12 root 108: return Object::GetObject<EventManager>(OBJ_EVENT_MANAGER);
109: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.