opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
synchronized_value.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <concepts>
6#include <mutex>
7#include <type_traits>
8#include <utility>
9
10namespace osc
11{
12 // Represents a value, plus an associated mutex, where the value can only be accessed via the mutex.
13 template<
14 typename T,
15 typename Mutex = std::mutex,
16 typename MutexGuard = std::lock_guard<Mutex>
17 >
19 public:
20 using value_type = T;
23
24 // Value-constructs an instance of `value_type` with an associated instance of `mutex_type`.
25 SynchronizedValue() = default;
26
27 // In-place constructs an instance of `value_type` from `Args`, along with an associated instance of `mutex_type`.
28 template<typename... Args>
29 requires std::constructible_from<value_type, Args&&...>
30 explicit SynchronizedValue(Args&&... args) :
31 value_{std::forward<Args>(args)...}
32 {}
33
35 value_{*other.lock()}
36 {}
37
39 value_{std::move(tmp).value()}
40 {}
41
43 {
44 if (&other != this) {
45 *this->lock() = *other.lock();
46 }
47 return *this;
48 }
49
51 {
52 if (&tmp != this) {
53 *this->lock() = std::move(tmp).value();
54 }
55 return *this;
56 }
57
59
61 {
62 const auto guard = mutex_guard_type{mutex_};
63 return std::move(value_);
64 }
65
66 template<typename TGuard = MutexGuard>
71
72 template<typename TGuard = MutexGuard>
77
78 template<
79 typename U,
80 typename Getter,
81 typename TGuard = MutexGuard
82 >
84 requires std::is_same_v<decltype(f(std::declval<value_type>())), const U&>
85 {
87 }
88
89 private:
90 mutable mutex_type mutex_;
91 value_type value_{};
92 };
93}
Definition synchronized_value_guard.h:13
Definition synchronized_value.h:18
SynchronizedValue & operator=(const SynchronizedValue &other)
Definition synchronized_value.h:42
T value_type
Definition synchronized_value.h:20
SynchronizedValue & operator=(SynchronizedValue &&tmp) noexcept
Definition synchronized_value.h:50
~SynchronizedValue() noexcept=default
SynchronizedValueGuard< value_type, mutex_type, TGuard > lock()
Definition synchronized_value.h:67
SynchronizedValueGuard< const value_type, mutex_type, TGuard > lock() const
Definition synchronized_value.h:73
SynchronizedValue(SynchronizedValue &&tmp) noexcept
Definition synchronized_value.h:38
Mutex mutex_type
Definition synchronized_value.h:21
SynchronizedValue(const SynchronizedValue &other)
Definition synchronized_value.h:34
SynchronizedValueGuard< const U, mutex_type, TGuard > lock_child(Getter f) const
Definition synchronized_value.h:83
value_type value() &&
Definition synchronized_value.h:60
MutexGuard mutex_guard_type
Definition synchronized_value.h:22
SynchronizedValue(Args &&... args)
Definition synchronized_value.h:30
Definition custom_decoration_generator.h:5
constexpr U to(T &&value)
Definition conversion.h:56