mn_auto_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) 2018 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 _MINILIB_a66be7cc_ab50_11eb_b276_b7700b99df9f_H_
19 #define _MINILIB_a66be7cc_ab50_11eb_b276_b7700b99df9f_H_
20 
21 #include "../mn_config.hpp"
22 
23 #include "../mn_algorithm.hpp"
24 #include "../mn_def.hpp"
25 
26 namespace mn {
27  namespace pointer {
28 
34  template <typename T>
36  public:
37  using value_type = T;
38  using element_type = T;
40  using pointer = T*;
41  using reference = T&;
43 
45  : m_ptr(nullptr) { }
47  : m_ptr(ptr) { }
48  basic_auto_ptr(pointer ptr, bool shared)
49  : m_ptr(ptr) { }
50  basic_auto_ptr(const self_type& other )
51  : m_ptr(other.m_ptr) { }
52  basic_auto_ptr(basic_auto_ptr&& other) noexcept
53  : m_ptr(mn::move(other.m_ptr)) { other.m_ptr = nullptr; }
54 
55  template <class TO>
57  : m_ptr(const_cast<TO*>(other.get())) { if (m_ptr) m_ptr->duplicate(); }
58 
59  ~basic_auto_ptr() { if (m_ptr) m_ptr->release(); }
60 
61  template <class TO>
62  void reset(const basic_auto_ptr<TO>& p) { assign<TO>(p); }
63  void reset() { if (m_ptr) { m_ptr->release(); m_ptr = nullptr; } }
64  void reset(pointer ptr) { assign(ptr); }
65  void reset(pointer ptr, bool shared) { assign(ptr, shared); }
66  void reset(const self_type& ptr) { assign(ptr); }
67 
68 
69  void swap(self_type& ptr) { mn::swap(m_ptr, ptr.m_ptr); }
70  pointer get() { return m_ptr; }
71  bool isNull() const { return m_ptr == nullptr; }
72 
73  pointer duplicate() { if (m_ptr) m_ptr->duplicate(); return m_ptr; }
74 
75 
76  operator pointer() { return m_ptr; }
77  pointer operator ->() { return m_ptr; }
78  bool operator ! () const { return m_ptr == nullptr; }
79 
80  template <class TO>
81  self_type& operator = (const basic_auto_ptr<TO>& ptr) { return assign<TO>(ptr); }
82  self_type& operator = (pointer ptr) { return assign(ptr); }
83  self_type& operator = (const self_type& ptr) { return assign(ptr); }
84 
85  self_type& operator = (self_type&& other) noexcept {
86  if (m_ptr) m_ptr->release();
87  m_ptr = other.m_ptr;
88  other.m_ptr = nullptr;
89  return *this;
90  }
91 
92  bool operator == (const self_type& other) const {
93  return m_ptr == other.m_ptr;
94  }
95  bool operator == (const pointer other) const {
96  return m_ptr == other;
97  }
98  bool operator != (const self_type& other) const {
99  return m_ptr != other.m_ptr;
100  }
101  bool operator != (const pointer other) const {
102  return m_ptr != other;
103  }
104  bool operator <= (const self_type& other) const {
105  return m_ptr <= other.m_ptr;
106  }
107  bool operator <= (const pointer other) const {
108  return m_ptr <= other;
109  }
110  bool operator >= (const self_type& other) const {
111  return m_ptr >= other.m_ptr;
112  }
113  bool operator >= (const pointer other) const {
114  return m_ptr >= other;
115  }
116  bool operator < (const self_type& other) const {
117  return m_ptr < other.m_ptr;
118  }
119  bool operator < (const pointer other) const {
120  return m_ptr < other;
121  }
122  bool operator > (const self_type& other) const {
123  return m_ptr > other.m_ptr;
124  }
125  bool operator > (const pointer other) const {
126  return m_ptr > other;
127  }
128 
129  template <class TO>
131 
132  TO* pOther = dynamic_cast<TO*>(m_ptr);
133  return basic_auto_ptr<TO>(pOther, true);
134 
135  }
136  template <class TO>
138 
139  TO* pOther = static_cast<TO*>(m_ptr);
140  return basic_auto_ptr<TO>(pOther, true);
141 
142  }
143  private:
145  if (m_ptr != other) {
146  if (m_ptr) m_ptr->release();
147  m_ptr = other;
148  }
149  return *this;
150  }
151 
152  self_type& assign(pointer other, bool shared) {
153 
154  if (m_ptr != other) {
155  if (m_ptr) m_ptr->release();
156  m_ptr = other;
157  if (shared && m_ptr) m_ptr->duplicate();
158  }
159  return *this;
160  }
161 
162  self_type& assign(const self_type& other) {
163 
164  if (&other != this) {
165  if (m_ptr) m_ptr->release();
166  m_ptr = other.m_ptr;
167  if (m_ptr) m_ptr->duplicate();
168  }
169  return *this;
170  }
171 
172  template <class TO>
174 
175  if (other.get() != m_ptr) {
176  if (m_ptr) m_ptr->release();
177  m_ptr = const_cast<TO*>(other.get());
178  if (m_ptr) m_ptr->duplicate();
179  }
180  return *this;
181  }
182 
183  private:
185  };
186 
187  template <typename T>
189  a.swap(b);
190  }
191 
192  template <typename T>
194 
200  template<typename T, typename... Args >
201  inline auto_ptr<T> make_auto(Args&&... args) {
202  return auto_ptr<T>(new T (mn::forward<Args>(args)...) );
203  }
204 
205  }
206 }
207 
208 #endif // _MINILIB_a66be7cc_ab50_11eb_b276_b7700b99df9f_H_
AutoPtr is a "smart" pointer for classes implementing reference counting based garbage collection.
Definition: mn_auto_ptr.hpp:35
pointer operator->()
Definition: mn_auto_ptr.hpp:77
basic_auto_ptr(basic_auto_ptr &&other) noexcept
Definition: mn_auto_ptr.hpp:52
void reset(pointer ptr)
Definition: mn_auto_ptr.hpp:64
basic_auto_ptr(const basic_auto_ptr< TO > &other)
Definition: mn_auto_ptr.hpp:56
basic_auto_ptr(pointer ptr)
Definition: mn_auto_ptr.hpp:46
basic_auto_ptr()
Definition: mn_auto_ptr.hpp:44
bool operator==(const self_type &other) const
Definition: mn_auto_ptr.hpp:92
self_type & assign(const basic_auto_ptr< TO > &other)
Definition: mn_auto_ptr.hpp:173
basic_auto_ptr< TO > scast() const
Definition: mn_auto_ptr.hpp:137
self_type & assign(pointer other, bool shared)
Definition: mn_auto_ptr.hpp:152
pointer duplicate()
Definition: mn_auto_ptr.hpp:73
pointer get()
Definition: mn_auto_ptr.hpp:70
const value_type const_value_type
Definition: mn_auto_ptr.hpp:39
void reset(const self_type &ptr)
Definition: mn_auto_ptr.hpp:66
void reset(const basic_auto_ptr< TO > &p)
Definition: mn_auto_ptr.hpp:62
bool operator<(const self_type &other) const
Definition: mn_auto_ptr.hpp:116
bool operator>(const self_type &other) const
Definition: mn_auto_ptr.hpp:122
basic_auto_ptr(pointer ptr, bool shared)
Definition: mn_auto_ptr.hpp:48
void swap(self_type &ptr)
Definition: mn_auto_ptr.hpp:69
T value_type
Definition: mn_auto_ptr.hpp:37
void reset(pointer ptr, bool shared)
Definition: mn_auto_ptr.hpp:65
self_type & assign(pointer other)
Definition: mn_auto_ptr.hpp:144
pointer m_ptr
Definition: mn_auto_ptr.hpp:184
bool isNull() const
Definition: mn_auto_ptr.hpp:71
bool operator!() const
Definition: mn_auto_ptr.hpp:78
T * pointer
Definition: mn_auto_ptr.hpp:40
bool operator<=(const self_type &other) const
Definition: mn_auto_ptr.hpp:104
self_type & operator=(const basic_auto_ptr< TO > &ptr)
Definition: mn_auto_ptr.hpp:81
basic_auto_ptr< TO > dycast() const
Definition: mn_auto_ptr.hpp:130
bool operator>=(const self_type &other) const
Definition: mn_auto_ptr.hpp:110
~basic_auto_ptr()
Definition: mn_auto_ptr.hpp:59
self_type & assign(const self_type &other)
Definition: mn_auto_ptr.hpp:162
void reset()
Definition: mn_auto_ptr.hpp:63
T element_type
Definition: mn_auto_ptr.hpp:38
basic_auto_ptr(const self_type &other)
Definition: mn_auto_ptr.hpp:50
bool operator!=(const self_type &other) const
Definition: mn_auto_ptr.hpp:98
T & reference
Definition: mn_auto_ptr.hpp:41
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
auto_ptr< T > make_auto(Args &&... args)
Make a auto pointer.
Definition: mn_auto_ptr.hpp:201
void swap(basic_auto_ptr< T > &a, basic_auto_ptr< T > &b)
Definition: mn_auto_ptr.hpp:188
Definition: mn_allocator_typetraits.hpp:25
void swap(TAssignable &a, TAssignable &b)
Definition: mn_algorithm.hpp:312
void move(const T *src, const T *last, T *dest)
Definition: mn_algorithm.hpp:100