mn_lock_ptr.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2021 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library is free software; you can redistribute it and/or modify
6 *it under the terms of the GNU Lesser General Public License as published by
7 *the Free Software Foundation, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library is distributed in the hope that it will be useful, but
10 *WITHOUT ANY WARRANTY; without even the implied warranty of
11 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 *General Public License for more details.
13 *
14 *You should have received a copy of the GNU Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 #ifndef MINLIB_e3c52c47_2600_40a8_bbff_29fa60eea6d2_H_
19 #define MINLIB_e3c52c47_2600_40a8_bbff_29fa60eea6d2_H_
20 
21 #include "../mn_config.hpp"
22 
23 #include "mn_algorithm.hpp"
24 #include "../mn_autolock.hpp"
25 #include "../mn_def.hpp"
26 
27 namespace mn {
28  namespace pointer {
35  template <typename T, class TLOCK = LockType_t>
37  public:
38  using value_type = T;
39  using element_type = T;
40  using const_value_type = const T;
41 
42  using lock_type = TLOCK;
43  using pointer = T*;
44  using const_pointer = T*;
45  using reference = T&;
46  using const_reference = const T&;
47 
49 
56  : m_ptr(static_cast<pointer>(&v)), m_lock(m) {
57  m_lock.lock();
58  }
59 
66  : m_ptr(v), m_lock(m) {
67  m_lock.lock();
68  }
69 
74  m_lock.unlock();
75  }
76 
81  const_pointer get() const { return m_ptr; }
86  pointer get() { return m_ptr; }
87 
92  const_reference operator *() const { return *m_ptr; }
97  reference operator *() { return *m_ptr; }
101  const_pointer operator->() const { return m_ptr; }
105  pointer operator->() { return m_ptr; }
106 
108  pointer tmp = m_ptr;
109  m_ptr = NULL;
110  return tmp;
111  }
112  void reset(pointer p = NULL) {
113  m_ptr = p;
114  }
116  return m_lock;
117  }
118 
119  basic_lock_ptr(const basic_lock_ptr&) = delete;
121  private:
122  volatile pointer m_ptr;
124  };
125 
126  template <typename T, class TLOCK = LockType_t>
127  inline basic_lock_ptr<T, TLOCK> make_lock_ref(volatile T& v, TLOCK& m) {
128  return basic_lock_ptr<T, TLOCK>(v, m);
129  }
130 
131  template <typename T, class TLOCK = LockType_t>
132  inline basic_lock_ptr<T, TLOCK> make_lock_ptr(volatile T* v, TLOCK& m) {
133  return basic_lock_ptr<T, TLOCK>(v, m);
134  }
135 
136  template <typename T, class TLOCK = LockType_t>
138  }
139 }
140 #endif
auto lock pointer.
Definition: mn_lock_ptr.hpp:36
T * const_pointer
Definition: mn_lock_ptr.hpp:44
void reset(pointer p=NULL)
Definition: mn_lock_ptr.hpp:112
const T & const_reference
Definition: mn_lock_ptr.hpp:46
const_pointer get() const
Get the pointer value.
Definition: mn_lock_ptr.hpp:81
const_reference operator*() const
Get the reference of the pointer.
Definition: mn_lock_ptr.hpp:92
basic_lock_ptr & operator=(const basic_lock_ptr &)=delete
basic_lock_ptr(volatile pointer v, lock_type &m)
constructor make the pointer and auto lock
Definition: mn_lock_ptr.hpp:65
lock_type & m_lock
Definition: mn_lock_ptr.hpp:123
const T const_value_type
Definition: mn_lock_ptr.hpp:40
const_pointer operator->() const
Helper to use this pointer as "native" pointer.
Definition: mn_lock_ptr.hpp:101
lock_type & get_lock()
Definition: mn_lock_ptr.hpp:115
T element_type
Definition: mn_lock_ptr.hpp:39
T * pointer
Definition: mn_lock_ptr.hpp:43
pointer operator->()
Helper to use this pointer as "native" pointer.
Definition: mn_lock_ptr.hpp:105
TLOCK lock_type
Definition: mn_lock_ptr.hpp:42
pointer get()
Get the pointer value.
Definition: mn_lock_ptr.hpp:86
basic_lock_ptr(const basic_lock_ptr &)=delete
~basic_lock_ptr()
decunstructor and auto unlock the pointer
Definition: mn_lock_ptr.hpp:73
basic_lock_ptr(volatile reference v, lock_type &m)
constructor make the pointer and auto lock
Definition: mn_lock_ptr.hpp:55
pointer release()
Definition: mn_lock_ptr.hpp:107
volatile pointer m_ptr
Definition: mn_lock_ptr.hpp:122
T & reference
Definition: mn_lock_ptr.hpp:45
T value_type
Definition: mn_lock_ptr.hpp:38
Basic algorithmens This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniT...
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
basic_lock_ptr< T, TLOCK > make_lock_ref(volatile T &v, TLOCK &m)
Definition: mn_lock_ptr.hpp:127
basic_lock_ptr< T, TLOCK > make_lock_ptr(volatile T *v, TLOCK &m)
Definition: mn_lock_ptr.hpp:132
Definition: mn_allocator_typetraits.hpp:25