FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_accessLock.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_ACCESSLOCK_HPP_INCLUDED
18#define _FGE_C_ACCESSLOCK_HPP_INCLUDED
19
20#include "FastEngine/fge_except.hpp"
21#include <mutex>
22
23namespace fge
24{
25
36template<class TMutex>
38{
39public:
40 explicit AccessLock(TMutex& mutex) :
41 g_mutex(&mutex)
42 {
43 this->g_mutex->lock();
44 }
45 ~AccessLock() { this->g_mutex->unlock(); }
46
47 AccessLock(AccessLock const&) = delete;
48 AccessLock(AccessLock&&) noexcept = delete;
49
50 AccessLock& operator=(AccessLock const&) = delete;
51 AccessLock& operator=(AccessLock&&) noexcept = delete;
52
53 [[nodiscard]] inline bool operator==(TMutex const& r) const { return this->g_mutex == &r; }
54 [[nodiscard]] inline bool operator!=(TMutex const& r) const { return this->g_mutex != &r; }
55
56 inline void throwIfDifferent(TMutex const& r) const
57 {
58 if (this->g_mutex != &r)
59 {
60 throw fge::Exception("AccessLock: provided mutex is different");
61 }
62 }
63
64 [[nodiscard]] inline AccessLock<TMutex>* operator&() = delete;
65
66private:
67 TMutex* g_mutex;
68};
69
70} // namespace fge
71
72#endif // _FGE_C_ACCESSLOCK_HPP_INCLUDED
Class that lock a mutex and unlock it only when the object is destroyed.
Definition C_accessLock.hpp:38
Definition fge_except.hpp:28