-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGroupedCalls.cpp
More file actions
75 lines (60 loc) · 1.34 KB
/
GroupedCalls.cpp
File metadata and controls
75 lines (60 loc) · 1.34 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
#include <iostream>
#include <set>
#include <utility>
typedef void (UpdateFn)(void*);
struct CompareUpdateFns {
bool operator()(const std::pair<UpdateFn*, void*>& a,
const std::pair<UpdateFn*, void*>& b) const {
if (a.first == b.first) {
return a.second < b.second;
}
return a.first < b.first;
}
};
class SceneObject {
public:
virtual ~SceneObject() {}
virtual void Update() {}
};
struct SceneObject_VTable {
void* dtor;
UpdateFn* update;
};
UpdateFn* GetUpdateFn(SceneObject* obj) {
SceneObject_VTable** vtable_ptr = (SceneObject_VTable**)obj;
return (*vtable_ptr)->update;
}
class SceneManager {
public:
void Add(SceneObject* obj) {
update_fns_.insert({ GetUpdateFn(obj), obj });
}
void UpdateAll() {
for (auto& update : update_fns_) {
update.first(update.second);
}
}
private:
std::set<std::pair<UpdateFn*, void*>, CompareUpdateFns> update_fns_;
};
class Obj1 : public SceneObject {
void Update() override {
std::cout << "## Object 1!" << std::endl;
}
};
class Obj2 : public SceneObject {
void Update() override {
std::cout << "** Object 2!" << std::endl;
}
};
int main() {
SceneManager scene_manager;
Obj1 a;
Obj2 b;
Obj1 c;
scene_manager.Add(&a);
scene_manager.Add(&b);
scene_manager.Add(&c);
scene_manager.UpdateAll();
return 0;
}