16 template<
typename T,
typename mutex_t = std::recursive_mutex,
typename x_lock_t =
17 std::unique_lock<mutex_t>,
typename s_lock_t = std::unique_lock<mutex_t >>
19 typedef mutex_t mtx_t;
20 const std::shared_ptr<T> ptr;
21 std::shared_ptr<mutex_t> mtx_ptr;
23 template<
typename req_lock>
28 auto_lock_t(auto_lock_t &&o) : ptr(std::move(o.ptr)), lock(std::move(o.lock)) {}
30 auto_lock_t(T *
const _ptr, mutex_t &_mtx) : ptr(_ptr), lock(_mtx) {}
32 T *operator->() {
return ptr; }
34 const T *operator->()
const {
return ptr; }
37 template<
typename req_lock>
38 class auto_lock_obj_t {
42 auto_lock_obj_t(auto_lock_obj_t &&o) :
43 ptr(std::move(o.ptr)), lock(std::move(o.lock)) {}
45 auto_lock_obj_t(T *
const _ptr, mutex_t &_mtx) : ptr(_ptr), lock(_mtx) {}
47 template<
typename arg_t>
48 auto operator[](arg_t arg) -> decltype((*ptr)[arg]) {
return (*ptr)[arg]; }
51 void lock() { mtx_ptr->lock(); }
53 void unlock() { mtx_ptr->unlock(); }
55 friend struct link_safe_ptrs;
57 template<
typename mutex_type>
friend
58 class std::lock_guard;
62 const std::shared_ptr<T> &getPtr()
const {
return ptr; }
64 template<
typename... Args>
65 safe_ptr(Args... args) : ptr(std::make_shared<T>(args...)), mtx_ptr(std::make_shared<mutex_t>()) {}
67 auto_lock_t<x_lock_t> operator->() {
return auto_lock_t<x_lock_t>(ptr.get(), *mtx_ptr); }
69 auto_lock_obj_t<x_lock_t> operator*() {
return auto_lock_obj_t<x_lock_t>(ptr.get(), *mtx_ptr); }
71 const auto_lock_t<s_lock_t> operator->()
const {
return auto_lock_t<s_lock_t>(ptr.get(), *mtx_ptr); }
73 const auto_lock_obj_t<s_lock_t> operator*()
const {
return auto_lock_obj_t<s_lock_t>(ptr.get(), *mtx_ptr); }