Skip to content

Commit eb4ef90

Browse files
committed
make some new friends
1 parent ce2e68e commit eb4ef90

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

include/dice/template-library/mutex.hpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
namespace dice::template_library {
1010

11+
template<typename T, typename Mutex = std::mutex>
12+
struct mutex;
13+
1114
/**
1215
* An RAII guard for a value behind a mutex.
1316
* When this mutex_guard is dropped the lock is automatically released.
@@ -25,15 +28,17 @@ namespace dice::template_library {
2528
using mutex_type = Mutex;
2629

2730
private:
31+
friend struct mutex<T, Mutex>;
32+
2833
value_type *value_ptr_;
2934
std::unique_lock<mutex_type> lock_;
3035

31-
public:
3236
mutex_guard(std::unique_lock<mutex_type> &&lock, T &value) noexcept
3337
: value_ptr_{&value},
3438
lock_{std::move(lock)} {
3539
}
3640

41+
public:
3742
mutex_guard() = delete;
3843
mutex_guard(mutex_guard const &other) noexcept = delete;
3944
mutex_guard &operator=(mutex_guard const &other) noexcept = delete;
@@ -65,7 +70,7 @@ namespace dice::template_library {
6570
* @tparam T value type stored
6671
* @tparam Mutex the mutex type
6772
*/
68-
template<typename T, typename Mutex = std::mutex>
73+
template<typename T, typename Mutex>
6974
struct mutex {
7075
using value_type = T;
7176
using mutex_type = Mutex;

include/dice/template-library/shared_mutex.hpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
namespace dice::template_library {
1111

12+
template<typename T, typename Mutex = std::shared_mutex>
13+
struct shared_mutex;
14+
1215
/**
1316
* An RAII guard for a value behind a shared_mutex.
1417
* When this shared_mutex_guard is dropped the lock is automatically released.
@@ -27,15 +30,17 @@ namespace dice::template_library {
2730
using lock_type = std::conditional_t<std::is_const_v<T>, std::shared_lock<mutex_type>, std::unique_lock<mutex_type>>;
2831

2932
private:
33+
friend struct shared_mutex<std::remove_const_t<T>, Mutex>;
34+
3035
value_type *value_ptr_;
3136
lock_type lock_;
3237

33-
public:
3438
shared_mutex_guard(lock_type &&lock, T &value) noexcept
3539
: value_ptr_{&value},
3640
lock_{std::move(lock)} {
3741
}
3842

43+
public:
3944
shared_mutex_guard() = delete;
4045
shared_mutex_guard(shared_mutex_guard const &other) noexcept = delete;
4146
shared_mutex_guard &operator=(shared_mutex_guard const &other) noexcept = delete;
@@ -67,7 +72,7 @@ namespace dice::template_library {
6772
* @tparam T value type stored
6873
* @tparam Mutex the mutex type
6974
*/
70-
template<typename T, typename Mutex = std::shared_mutex>
75+
template<typename T, typename Mutex>
7176
struct shared_mutex {
7277
using value_type = T;
7378
using mutex_type = Mutex;

0 commit comments

Comments
 (0)