Support Class Library
A set of tools providing classes and utility
Mutexed.h
Go to the documentation of this file.
1 #pragma once
2 #include <mutex>
4 
5 namespace scl{
6  namespace async{
13  template <class T, class Lock = std::mutex, template<class> class Guard = std::lock_guard>
14  class Mutexed{
15  public:
16  using lock_type = Lock;
17  using guard_type = Guard<lock_type>;
18  using value_type = T;
19 
20  protected:
22  mutable std::unique_lock<lock_type> lock{lock_type{}, std::defer_lock};
23 
24  public:
25  template <class... Args>
26  explicit Mutexed(Args&&... args) : value{std::forward<Args>(args)...} {
27  }
28 
29  Mutexed(Mutexed&&) noexcept = default;
30  Mutexed& operator=(Mutexed&&) noexcept = default;
31 
32  Mutexed(const Mutexed&) = delete;
33  Mutexed& operator=(const Mutexed&) = delete;
34 
35  template <class F>
37  {
38  guard_type _{lock};
39  std::forward<F>(f)(static_cast<value_type&>(this->value));
40  }
41  return *this;
42  }
43  };
44 
45  template <class T>
46  struct with_traits<Mutexed<T>>{
47  template <class F>
48  auto operator()(Mutexed<T>& mutex, F&& delegate) -> META::return_t<F> {
49  return mutex.transaction(std::forward<F&&>(delegate));
50  }
51  };
52  }
53 }
std::unique_lock< lock_type > lock
Definition: Mutexed.h:22
A wrapper class that protects a value with a lock.
Definition: Mutexed.h:14
value_type value
Definition: Mutexed.h:21
Mutexed(Args &&... args)
Definition: Mutexed.h:26
Global namespace of the SCL.
Definition: alias.hpp:3
Mutexed & transaction(F &&f)
Definition: Mutexed.h:36
constexpr scl::utils::Placeholder _
Definition: Placeholder.h:39
Mutexed & operator=(Mutexed &&) noexcept=default
auto operator()(Mutexed< T > &mutex, F &&delegate) -> scl::tools::meta ::return_t< F >
Definition: Mutexed.h:48
Traits for use with the scl::async::with function.
Definition: with_traits.h:15