-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (67 loc) · 2.26 KB
/
main.cpp
File metadata and controls
80 lines (67 loc) · 2.26 KB
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
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) December 2025 Félix-Olivier Dumas. All rights reserved.
// Licensed under the terms described in the LICENSE file
#include <iostream>
#include <concepts>
#include <vector>
#include <print>
template<typename Hooked>
struct IEventHookable {
protected:
IEventHookable() {
if constexpr (requires(Hooked h) { h.onCreated(); })
static_cast<Hooked*>(this)->onCreated();
else onCreatedDefault();
}
~IEventHookable() {
if constexpr (requires(Hooked h) { h.onDestroyed(); })
static_cast<Hooked*>(this)->onDestroyed();
else onDestroyedDefault();
}
protected:
void invokePreUpdate() {
if constexpr (requires(Hooked h) { h.onPreUpdate(); })
static_cast<Hooked*>(this)->onPreUpdate();
else onPreUpdateDefault();
}
void invokeUpdate() {
invokePreUpdate();
if constexpr (requires(Hooked h) { h.onUpdate(); })
static_cast<Hooked*>(this)->onUpdate();
else onUpdateDefault();
invokePostUpdate();
}
void invokePostUpdate() {
if constexpr (requires(Hooked h) { h.onPostUpdate(); })
static_cast<Hooked*>(this)->onPostUpdate();
else onPostUpdateDefault();
}
protected:
void onCreatedDefault() { std::cout << "No 'onCreated' using default.\n"; }
void onDestroyedDefault() { std::cout << "No 'onDestroyed' using default.\n"; }
void onUpdateDefault() { std::cout << "No 'onUpdate' using default.\n"; }
void onPreUpdateDefault() { std::cout << "No 'onPreUpdate' using default.\n"; }
void onPostUpdateDefault() { std::cout << "No 'onPostUpdate' using default.\n"; }
};
template<typename Derived>
struct IUpdatable {
void update()
requires std::is_base_of_v<IEventHookable<Derived>, Derived>
{ static_cast<Derived*>(this)->invokeUpdate(); }
};
struct System : public IEventHookable<System>, public IUpdatable<System> {
public: friend IEventHookable<System>; friend IUpdatable<System>;
private:
void onCreated() {
std::cout << "Creating new system...\n";
}
void onUpdate() {
std::cout << "Updating System...\n";
}
void onDestroyed() {
std::cout << "Destroying system...\n";
}
};
int main() {
System sys;
sys.update();
}