Debian Codegraph @

plugins/dm.gameconnection/DiffStatus.h

raw
1 #pragma once
2
3 #include <map>
4
5 namespace gameconn
6 {
7
8 /**
9 * Represents the change of status of some object.
10 * Used mainly to write map diff for hot reload feature.
11 */
12 class DiffStatus
13 {
14 //change in presence:
15 // -1: removed
16 // 1: added
17 // 0: otherwise
18 short _delta = 0;
19 //true if object was modified in any way
20 //added/removed objects count as modified too
21 bool _modified = false;
22 //true if object should be respawned afresh
23 bool _respawn = false;
24
25 public:
26 inline bool isAdded() const { return _delta > 0; }
27 inline bool isRemoved() const { return _delta < 0; }
28 inline bool isModified() const { return _modified; }
29 inline bool needsRespawn() const { return _respawn; }
30
31 inline static DiffStatus added() {
32 DiffStatus res;
33 res._delta = 1;
34 res._modified = true;
35 //res._respawn = true; //TODO: add + remove forces respawn?
36 return res;
37 }
38 inline static DiffStatus removed() {
39 DiffStatus res;
40 res._delta = -1;
41 res._modified = true;
42 return res;
43 }
44 inline static DiffStatus modified() {
45 DiffStatus res;
46 res._modified = true;
47 return res;
48 }
49 inline static DiffStatus forceRespawn() {
50 DiffStatus res;
51 res._modified = true;
52 res._respawn = true;
53 return res;
54 }
55
56 DiffStatus combine(const DiffStatus& laterChange) const {
57 DiffStatus res;
58 res._delta = _delta + laterChange._delta;
59 if (std::abs(res._delta) > 1) {
60 assert(false); //double addition without removal?...
61 res._delta = (res._delta < 0 ? -1 : 1);
62 }
63 res._modified = _modified || laterChange._modified;
64 res._respawn = _respawn || laterChange._respawn;
65 return res;
66 }
67 };
68
69 typedef std::map<std::string, DiffStatus> DiffEntityStatuses;
70
71 }