BRICKS
Small, useful blocks of code, to build bigger things.
Loading...
Searching...
No Matches
read_guard.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <shared_mutex>
4
5namespace bricks::detail {
6
7template <typename T>
8class read_guard {
9 public:
10 read_guard(const T& this_in, std::shared_mutex& mutex_in) noexcept
11 : this_(this_in), mutex_(mutex_in)
12 {
13 mutex_.lock_shared();
14 }
15
16 ~read_guard() noexcept { mutex_.unlock_shared(); }
17
18 read_guard(const read_guard&) = delete;
19 auto operator=(const read_guard&) -> read_guard& = delete;
20
21 read_guard(read_guard&&) noexcept = default;
22 auto operator=(read_guard&&) noexcept -> read_guard& = default;
23
24 auto operator->() const noexcept -> const typename T::value_type* { return &this_; }
25
26 auto operator*() const noexcept -> const typename T::value_type& { return this_; }
27
28 private:
29 const T& this_;
30 std::shared_mutex& mutex_;
31};
32
33} // namespace bricks::detail