|
|
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.10! root 13: #include "header.h"
1.1 root 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.10! root 58: class Device;
1.1.1.5 root 59: class Event;
1.1.1.9 root 60: using EventCallback_t = void (Device::*)(Event&);
61: #define ToEventCallback(f) static_cast<EventCallback_t>(f)
62:
1.1 root 63:
64: // イベント
65: //
66: // イベントは必要なデバイスごとに用意する。おそらく最初から個数が決まって
67: // いることが多いはずなので、デバイスクラス内のメンバ変数(実体) として
68: // 宣言し、デバイスのコンストラクタ等で初期化するという流れになるはず。
1.1.1.5 root 69: //
70: // VM リセットによるイベントタイマーの停止処理は各デバイスの責任で行うこと。
1.1 root 71: class Event
72: {
73: public:
74: Event();
1.1.1.8 root 75: Event(Device *dev_);
1.1.1.9 root 76: ~Event();
1.1 root 77:
1.1.1.9 root 78: // このイベントを登録する
79: void Regist();
80: // このイベントを登録する (名前を同時に設定する)
81: void Regist(const std::string& name_);
1.1.1.3 root 82:
1.1.1.7 root 83: // このイベントが動作中なら true を返す
84: bool IsRunning() const {
85: return active;
86: }
87:
1.1 root 88: // 以下はイベントを呼び出す側がセットする
89:
1.1.1.6 root 90: // イベントを起こすまでの仮想時刻での相対時間 [nsec]
1.1 root 91: // 例えば今から 1msec 後にイベントを起こしたい場合は 1000000 となる。
1.1.1.5 root 92: uint64 time {};
1.1 root 93:
1.1.1.5 root 94: Device *dev {}; // デバイス
1.1.1.9 root 95: EventCallback_t func {}; // コールバック関数
1.1.1.5 root 96: int code {}; // コールバック関数への引数
1.1.1.9 root 97: const std::string GetName() const { return name; }
1.1.1.2 root 98: void SetName(const std::string& val);
1.1 root 99:
100: // スケジューラが内部で使う
1.1.1.5 root 101: bool active {}; // スケジューラにセットされていれば真
1.1.1.6 root 102: uint64 vtime {}; // イベントを起こす仮想時刻
1.1.1.2 root 103:
1.1.1.9 root 104: uint64 count {}; // コールバックを呼び出した回数
105: uint64 last_count {}; // (表示用)前回表示したときの count
106:
1.1.1.2 root 107: private:
108: // イベント名(表示用)
109: // '\0' 含まず25文字まで。デバイス名も必要なら含めること。
110: std::string name {};
1.1 root 111: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.