Aller au contenu
Bienvenue sur la nouvelle documentation de CppUtils !

RAII accessor providing exclusive read-only access to a const-qualified value.

Holds an exclusive lock (std::unique_lock) preventing concurrent access by other threads, but restricts operations on the protected value to const member functions and read-only inspection.

Paramètre templateDescription
TThe type of the value being accessed (const-qualified).
MutexThe mutex type used for synchronization.
explicit constexpr Accessor::Accessor(Mutex& mutex, const T& value);

Constructs a const accessor by acquiring a unique lock on the provided mutex.

ParamètreDescription
mutexMutex to lock.
valueConst reference to the value to protect.

[[nodiscard]] constexpr auto Accessor::value(this auto&& self [[clang::lifetimebound, msvc::lifetimebound]]) noexcept -> decltype(auto);

Returns a const reference to the protected value.


[[nodiscard]] constexpr auto Accessor::operator->(this auto&& self [[clang::lifetimebound, msvc::lifetimebound]]) noexcept -> auto*;

Accesses const member functions or fields of the protected value.


[[nodiscard]] constexpr auto Accessor::operator*(this auto&& self [[clang::lifetimebound, msvc::lifetimebound]]) noexcept -> decltype(auto);

Dereferences to obtain the protected const value (equivalent to calling value()).


[[nodiscard]] constexpr auto Accessor::getLockGuard(this auto&& self [[clang::lifetimebound, msvc::lifetimebound]]) noexcept -> auto&;

Returns a reference to the underlying std::unique_lock guard.

Provides direct access to the lock guard, allowing it to be used with synchronization primitives requiring a std::unique_lock (such as std::condition_variable::wait).

When used with std::condition_variable::wait, the wait function atomically releases the mutex while placing the thread to sleep, and re-acquires the lock upon waking before returning once the predicate evaluates to true:

auto accessor = state.access();
condition.wait(accessor.getLockGuard(), [&] {
return accessor->isReady();
});

Retourne : Reference to the internal std::unique_lock<Mutex>.