|
|
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:
13: #include "device.h"
14:
15: // ユーザ定義リテラルを使ったサフィックス
16:
1.1.1.2 root 17: // ナノ秒
1.1.1.7 root 18: static inline constexpr uint64 operator"" _nsec(unsigned long long ns)
1.1.1.2 root 19: {
20: return ns;
21: }
1.1.1.7 root 22: static inline constexpr uint64 operator"" _nsec(long double ns)
1.1.1.2 root 23: {
24: return ns;
25: }
26:
1.1 root 27: // マイクロ秒
1.1.1.7 root 28: static inline constexpr uint64 operator"" _usec(unsigned long long us)
1.1 root 29: {
1.1.1.5 root 30: return us * 1000_nsec;
1.1 root 31: }
1.1.1.7 root 32: static inline constexpr uint64 operator"" _usec(long double us)
1.1 root 33: {
1.1.1.5 root 34: return us * 1000_nsec;
1.1 root 35: }
36:
37: // ミリ秒
1.1.1.7 root 38: static inline constexpr uint64 operator"" _msec(unsigned long long ms)
1.1 root 39: {
40: return ms * 1000_usec;
41: }
1.1.1.7 root 42: static inline constexpr uint64 operator"" _msec(long double ms)
1.1 root 43: {
44: return ms * 1000_usec;
45: }
46:
47: // 秒
1.1.1.7 root 48: static inline constexpr uint64 operator"" _sec(unsigned long long sec)
1.1 root 49: {
50: return sec * 1000_msec;
51: }
1.1.1.7 root 52: static inline constexpr uint64 operator"" _sec(long double sec)
1.1 root 53: {
54: return sec * 1000_msec;
55: }
56:
57: // コールバック関数の型
1.1.1.5 root 58: class Event;
1.1.1.9 ! root 59: using EventCallback_t = void (Device::*)(Event&);
! 60: #define ToEventCallback(f) static_cast<EventCallback_t>(f)
! 61:
1.1 root 62:
63: // イベント
64: //
65: // イベントは必要なデバイスごとに用意する。おそらく最初から個数が決まって
66: // いることが多いはずなので、デバイスクラス内のメンバ変数(実体) として
67: // 宣言し、デバイスのコンストラクタ等で初期化するという流れになるはず。
1.1.1.5 root 68: //
69: // VM リセットによるイベントタイマーの停止処理は各デバイスの責任で行うこと。
1.1 root 70: class Event
71: {
72: public:
73: Event();
1.1.1.8 root 74: Event(Device *dev_);
1.1.1.9 ! root 75: ~Event();
1.1 root 76:
1.1.1.9 ! root 77: // このイベントを登録する
! 78: void Regist();
! 79: // このイベントを登録する (名前を同時に設定する)
! 80: void Regist(const std::string& name_);
1.1.1.3 root 81:
1.1.1.7 root 82: // このイベントが動作中なら true を返す
83: bool IsRunning() const {
84: return active;
85: }
86:
1.1 root 87: // 以下はイベントを呼び出す側がセットする
88:
1.1.1.6 root 89: // イベントを起こすまでの仮想時刻での相対時間 [nsec]
1.1 root 90: // 例えば今から 1msec 後にイベントを起こしたい場合は 1000000 となる。
1.1.1.5 root 91: uint64 time {};
1.1 root 92:
1.1.1.5 root 93: Device *dev {}; // デバイス
1.1.1.9 ! root 94: EventCallback_t func {}; // コールバック関数
1.1.1.5 root 95: int code {}; // コールバック関数への引数
1.1.1.9 ! root 96: const std::string GetName() const { return name; }
1.1.1.2 root 97: void SetName(const std::string& val);
1.1 root 98:
99: // スケジューラが内部で使う
1.1.1.5 root 100: bool active {}; // スケジューラにセットされていれば真
1.1.1.6 root 101: uint64 vtime {}; // イベントを起こす仮想時刻
1.1.1.2 root 102:
1.1.1.9 ! root 103: uint64 count {}; // コールバックを呼び出した回数
! 104: uint64 last_count {}; // (表示用)前回表示したときの count
! 105:
1.1.1.2 root 106: private:
107: // イベント名(表示用)
108: // '\0' 含まず25文字まで。デバイス名も必要なら含めること。
109: std::string name {};
1.1 root 110: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.