StormByte C++ Library 0.0.9999
StormByte is a comprehensive, cross-platform C++ library aimed at easing system programming, configuration management, logging, and database handling tasks. This library provides a unified API that abstracts away the complexities and inconsistencies of different platforms (Windows, Linux).
Loading...
Searching...
No Matches
mutexed.hxx
1#pragma once
2
3#include <mutex>
4
12namespace StormByte {
13 template<typename T>
14 class Mutexed {
15 public:
20
25 Mutexed(const T& value) noexcept : m_value(value) {}
26
31 Mutexed(const Mutexed<T>& other) noexcept {
32 std::lock_guard<std::mutex> lock(m_mutex);
33 std::lock_guard<std::mutex> lock_other(other.m_mutex);
34 m_value = other.m_value;
35 }
36
41 Mutexed(Mutexed<T>&& other) noexcept {
42 std::lock_guard<std::mutex> lock(m_mutex);
43 std::lock_guard<std::mutex> lock_other(other.m_mutex);
44 m_value = std::move(other.m_value);
45 }
46
53 if (this != &other) {
54 std::lock_guard<std::mutex> lock(m_mutex);
55 std::lock_guard<std::mutex> lock_other(other.m_mutex);
56 m_value = other.m_value;
57 }
58 return *this;
59 }
60
67 if (this != &other) {
68 std::lock_guard<std::mutex> lock(m_mutex);
69 std::lock_guard<std::mutex> lock_other(other.m_mutex);
70 m_value = std::move(other.m_value);
71 }
72 return *this;
73 }
74
80 virtual Mutexed<T>& operator=(const T& value) noexcept {
81 std::lock_guard<std::mutex> lock(m_mutex);
82 m_value = value;
83 return *this;
84 }
85
91 virtual Mutexed<T>& operator=(T&& value) noexcept {
92 std::lock_guard<std::mutex> lock(m_mutex);
93 m_value = std::move(value);
94 return *this;
95 }
96
101
108 // Lock both mutexes for thread-safe comparison
109 std::lock_guard<std::mutex> lock_this(m_mutex);
110 std::lock_guard<std::mutex> lock_other(other.m_mutex);
111
112 // Compare values manually to return strong ordering
113 if (m_value == other.m_value) {
114 return std::strong_ordering::equal;
115 } else if (m_value < other.m_value) {
116 return std::strong_ordering::less;
117 } else {
118 return std::strong_ordering::greater;
119 }
120 }
121
127 bool operator!=(const Mutexed& other) const noexcept {
128 return operator<=>(other) != std::strong_ordering::equal;
129 }
130
135 virtual T& operator*() noexcept {
136 return m_value;
137 }
138
143 virtual const T& operator*() const noexcept {
144 return m_value;
145 }
146
151 virtual T* operator->() noexcept {
152 return &m_value;
153 }
154
159 virtual const T* operator->() const noexcept {
160 return &m_value;
161 }
162
166 void Lock() const {
167 m_mutex.lock();
168 }
169
173 void Unlock() const {
174 m_mutex.unlock();
175 }
176
177 protected:
179 mutable std::mutex m_mutex;
180 };
181}
Definition mutexed.hxx:14
Mutexed(const Mutexed< T > &other) noexcept
Copy constructor.
Definition mutexed.hxx:31
void Unlock() const
Unlocks the mutex.
Definition mutexed.hxx:173
virtual Mutexed< T > & operator=(T &&value) noexcept
Assignment move operator.
Definition mutexed.hxx:91
virtual Mutexed< T > & operator=(const T &value) noexcept
Assignment operator.
Definition mutexed.hxx:80
virtual T & operator*() noexcept
Dereference operator (use with explicit locking).
Definition mutexed.hxx:135
virtual const T & operator*() const noexcept
Dereference operator (use with explicit locking).
Definition mutexed.hxx:143
Mutexed< T > & operator=(const Mutexed< T > &other) noexcept
Copy assignment operator.
Definition mutexed.hxx:52
Mutexed(Mutexed< T > &&other) noexcept
Move constructor.
Definition mutexed.hxx:41
void Lock() const
Locks the mutex.
Definition mutexed.hxx:166
bool operator!=(const Mutexed &other) const noexcept
Inequality operator.
Definition mutexed.hxx:127
Mutexed< T > & operator=(Mutexed< T > &&other) noexcept
Move assignment operator.
Definition mutexed.hxx:66
virtual ~Mutexed() noexcept=default
Destructor.
virtual T * operator->() noexcept
Pointer dereference operator (use with explicit locking).
Definition mutexed.hxx:151
std::mutex m_mutex
Mutex.
Definition mutexed.hxx:179
virtual const T * operator->() const noexcept
Pointer dereference operator (use with explicit locking).
Definition mutexed.hxx:159
std::strong_ordering operator<=>(const Mutexed &other) const noexcept
Spaceship operator for comparison.
Definition mutexed.hxx:107
Mutexed() noexcept=default
Default constructor.
T m_value
Value.
Definition mutexed.hxx:178
Main namespace for the StormByte library.
std::conditional_t< is_reference< T >::value, std::expected< std::reference_wrapper< std::remove_reference_t< T > >, std::shared_ptr< E > >, std::expected< T, std::shared_ptr< E > > > Expected
Expected type with support for reference types.
Definition expected.hxx:32