]> git.tdb.fi Git - libs/game.git/blob - source/game/accessguard.cpp
Enforce no creation or destruction of objects during tick
[libs/game.git] / source / game / accessguard.cpp
1 #include "accessguard.h"
2
3 namespace Msp::Game {
4
5 thread_local AccessGuard *AccessGuard::instance = nullptr;
6
7 AccessGuard::AccessGuard()
8 {
9         if(!instance)
10                 instance = this;
11 }
12
13 AccessGuard::AccessGuard(AccessGuard &&other):
14         flags(std::move(other.flags))
15 {
16         if(&other==instance)
17                 instance = this;
18 }
19
20 AccessGuard &AccessGuard::operator=(AccessGuard &&other)
21 {
22         flags = std::move(other.flags);
23         if(&other==instance)
24                 instance = this;
25         return *this;
26 }
27
28 AccessGuard::~AccessGuard()
29 {
30         if(this==instance)
31                 instance = nullptr;
32 }
33
34 AccessGuard &AccessGuard::get_instance()
35 {
36         if(!instance)
37                 throw std::logic_error("no AccessGuard instance");
38         return *instance;
39 }
40
41 void AccessGuard::block_all()
42 {
43         default_flag = BLOCKED;
44         for(uint8_t &f: flags)
45                 f = BLOCKED;
46 }
47
48 void AccessGuard::unblock_all()
49 {
50         default_flag = UNBLOCKED;
51         for(uint8_t &f: flags)
52                 f = UNBLOCKED;
53 }
54
55 unsigned AccessGuard::get_next_index()
56 {
57         static unsigned next_index = 0;
58         return next_index++;
59 }
60
61 } // namespace Msp::Game