AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AMutexWrapper.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14#include <AUI/Traits/values.h>
15#include <AUI/Traits/memory.h>
16
21template<typename T>
22class AMutexWrapper: public aui::noncopyable {
23public:
24 AMutexWrapper(T value = T()) noexcept: mValue(std::move(value)) {}
25
26
27 void lock() {
28 mMutex.lock();
29
30#if AUI_DEBUG
31 mOwnerThread = AThread::current();
32#endif
33 }
34
35 AMutexWrapper& operator=(const T& rhs) {
36 value() = rhs;
37 return *this;
38 }
39
40 AMutexWrapper& operator=(T&& rhs) noexcept {
41 value() = std::move(rhs);
42 return *this;
43 }
44
45 template<typename U, std::enable_if_t<std::is_constructible_v<U, T>>* = 0>
46 operator U() noexcept {
47 return { value() };
48 }
49
50 void unlock() {
51#if AUI_DEBUG
52 mOwnerThread = nullptr;
53#endif
54 mMutex.unlock();
55 }
56
57 T& value() noexcept {
58#if AUI_DEBUG
59 AUI_ASSERTX(mOwnerThread == AThread::current(), "AMutexWrapper should be locked by this thread in order to get access to the underlying object");
60#endif
61 return mValue;
62 }
63
64 T* operator->() noexcept {
65 return &value();
66 }
67
68private:
69 T mValue;
70 AMutex mMutex;
71
72#if AUI_DEBUG
73 _<AAbstractThread> mOwnerThread;
74#endif
75};
static _< AAbstractThread > current()
Definition AThread.cpp:197
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:178
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition Assert.h:74
Basic syscall-based synchronization primitive.
Definition AMutex.h:33
Forbids copy of your class.
Definition values.h:40