-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib-events.h
69 lines (58 loc) · 2 KB
/
lib-events.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef LIB_EVENTS_H
#define LIB_EVENTS_H
#include <QString>
#include <cstdint>
#include "lib-api-common-def.h"
#include "xn.h"
/* This file provides storage & calling capabilities of callbacks from the
* library back to the hJOPserver.
*/
namespace Xn {
using TrkStdNotifyEvent = void CALL_CONV (*)(const void *sender, void *data);
using TrkStatusChangedEv = void CALL_CONV (*)(const void *sender, void *data, int trkStatus);
using TrkLogEv = void CALL_CONV (*)(const void *sender, void *data, int loglevel, const uint16_t *msg);
using TrkLocoEv = void CALL_CONV (*)(const void *sender, void *data, uint16_t addr);
using TrkMsgEv = void CALL_CONV (*)(const void *sender, void *data, const uint16_t *msg);
template <typename F>
struct EventData {
F func = nullptr;
void *data = nullptr;
bool defined() const { return this->func != nullptr; }
};
struct XnEvents {
EventData<TrkStdNotifyEvent> beforeOpen;
EventData<TrkStdNotifyEvent> afterOpen;
EventData<TrkStdNotifyEvent> beforeClose;
EventData<TrkStdNotifyEvent> afterClose;
EventData<TrkLogEv> onLog;
EventData<TrkStatusChangedEv> onTrkStatusChanged;
EventData<TrkLocoEv> onLocoStolen;
EventData<TrkMsgEv> onOpenError;
void call(const EventData<TrkStdNotifyEvent> &e) const {
if (e.defined())
e.func(this, e.data);
}
void call(const EventData<TrkLogEv> &e, LogLevel loglevel, const QString &msg) const {
if (e.defined())
e.func(this, e.data, static_cast<int>(loglevel), msg.utf16());
}
void call(const EventData<TrkStatusChangedEv> &e, TrkStatus status) const {
if (e.defined())
e.func(this, e.data, static_cast<int>(status));
}
void call(const EventData<TrkLocoEv> &e, LocoAddr addr) const {
if (e.defined())
e.func(this, e.data, addr.addr);
}
void call(const EventData<TrkMsgEv> &e, const QString &msg) const {
if (e.defined())
e.func(this, e.data, msg.utf16());
}
template <typename F>
static void bind(EventData<F> &event, const F &func, void *const data) {
event.func = func;
event.data = data;
}
};
} // namespace Xn
#endif