mn_linked_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_ee040f46_b480_4f16_a0f8_c6765774ff00_H_
19 #define MINLIB_ee040f46_b480_4f16_a0f8_c6765774ff00_H_
20 
21 #include "../mn_config.hpp"
22 
23 #include "../mn_def.hpp"
24 
25 namespace mn {
26  namespace pointer {
27 
35  };
36 
44  template <typename T >
46  template <typename U> friend class basic_linked_ptr;
47  public:
49  using value_type = T;
50  using element_type = T;
51  using reference = T&;
52  using pointer = T*;
54 
59  : m_pValue(nullptr) { Prev = Next = this; }
64  explicit basic_linked_ptr(pointer pValue)
65  : m_pValue(pValue) { Prev = Next = this; }
66 
68  if(m_pValue) link(_pPtr);
69  else Prev = Next = this;
70  }
71 
72  template <typename U>
74  : m_pValue(_pPtr.m_pValue) {
75  if(m_pValue) link(_pPtr);
76  else Prev = Next = this;
77  }
79 
80 
81  void reset() { reset(nullptr); }
82  pointer get() { return m_pValue; }
83 
84  void force_delete() { pointer _pValue = detach(); delete _pValue; }
85 
86 
92  int count() const {
93  int _count = 1;
94 
95  for(const base_type* _pCurrent = static_cast<const base_type*>(this);
96  _pCurrent->Next != static_cast<const base_type*>(this);
97  _pCurrent = _pCurrent->Next) {
98  ++_count;
99  }
100 
101  return _count;
102  }
103 
105  T* _pValue = m_pValue;
106  base_type* _p = this;
107 
108  do {
109  base_type* _pNext = _p->Next;
110  static_cast<self_type*>(_p)->m_pValue = 0;
111 
112  _p->Next = _p->Prev = _p;
113  _p = _pNext;
114  }
115  while(_p != this);
116 
117  return _pValue;
118  }
119 
120 
127  bool unique() { return (Next == static_cast<const base_type*>(this)); }
128 
130  if(_pPtr.m_pValue != m_pValue) {
131  reset(_pPtr.m_pValue);
132  if(_pPtr.m_pValue)
133  link(_pPtr);
134  }
135  return *this;
136  }
137 
138  reference operator*() { return *m_pValue; }
140  operator bool () { return (m_pValue != 0); }
141 
142  public:
143  template <typename U>
144  void reset(U* pValue) {
145  if(pValue != m_pValue) {
146  if(unique()) {
147  delete m_pValue;
148  }
149  else {
150  Prev->Next = Next;
151  Next->Prev = Prev;
152  Prev = Next = this;
153  }
154  m_pValue = pValue;
155  }
156  }
157 
158  template <typename U>
160  if(_pPtr.m_pValue != m_pValue) {
161  reset(_pPtr.m_pValue);
162  if(_pPtr.m_pValue)
163  link(_pPtr);
164  }
165  return *this;
166  }
167  template <typename U>
169  reset(pValue);
170  return *this;
171  }
172  protected:
173  template <typename U>
174  void link(const basic_linked_ptr<U>& pOther) {
175  Next = pOther.mpNext;
176  Prev = this;
177  Prev = const_cast<basic_linked_ptr<U>*>(&pOther);
178  pOther.Next = this;
179  }
180  protected:
182  };
183 
189  template <typename T >
191  return basic_linked_ptr<T>(value);
192  }
193 
199  template <typename T, typename U >
201  return basic_linked_ptr<T>(value);
202  }
203 
209  template <typename T>
210  inline basic_linked_ptr<T> make_link(T* value) {
211  return basic_linked_ptr<T>(value);
212  }
213 
214 
222  template <typename T>
223  inline T* get(const basic_linked_ptr<T>& _pPtr) {
224  return _pPtr.get();
225  }
230  template <typename T, typename U>
231  inline bool operator==(const basic_linked_ptr<T>& a, const basic_linked_ptr<U>& b) {
232  return (a.get() == b.get());
233  }
234 
238  template <typename T, typename U>
239  inline bool operator!=(const basic_linked_ptr<T>& a, const basic_linked_ptr<U>& b) {
240  return (a.get() != b.get());
241  }
242 
243  template <typename T>
245 
246  }
247 }
248 
249 #endif
This class implements a basic_linked_ptr template that it allows sharing of pointers between instance...
Definition: mn_linked_ptr.hpp:45
T element_type
Definition: mn_linked_ptr.hpp:50
T value_type
Definition: mn_linked_ptr.hpp:49
T * pointer
Definition: mn_linked_ptr.hpp:52
pointer operator->()
Definition: mn_linked_ptr.hpp:139
void link(const basic_linked_ptr< U > &pOther)
Definition: mn_linked_ptr.hpp:174
void reset(U *pValue)
Definition: mn_linked_ptr.hpp:144
pointer detach()
Definition: mn_linked_ptr.hpp:104
pointer m_pValue
Definition: mn_linked_ptr.hpp:181
basic_linked_ptr(const basic_linked_ptr< U > &_pPtr)
Definition: mn_linked_ptr.hpp:73
~basic_linked_ptr()
Definition: mn_linked_ptr.hpp:78
basic_linked_ptr(pointer pValue)
Construct,.
Definition: mn_linked_ptr.hpp:64
pointer get()
Definition: mn_linked_ptr.hpp:82
basic_linked_ptr()
Construct a new basic linked ptr object.
Definition: mn_linked_ptr.hpp:58
T & reference
Definition: mn_linked_ptr.hpp:51
void reset()
Definition: mn_linked_ptr.hpp:81
basic_linked_ptr & operator=(const basic_linked_ptr &_pPtr)
Definition: mn_linked_ptr.hpp:129
reference operator*()
Definition: mn_linked_ptr.hpp:138
basic_linked_ptr(const basic_linked_ptr &_pPtr)
Definition: mn_linked_ptr.hpp:67
bool unique()
If the use count of the owned pointer.
Definition: mn_linked_ptr.hpp:127
int count() const
Returns the use count of the shared pointer.
Definition: mn_linked_ptr.hpp:92
void force_delete()
Definition: mn_linked_ptr.hpp:84
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
bool operator!=(const basic_linked_ptr< T > &a, const basic_linked_ptr< U > &b)
Compares two basic_linked_ptr objects for inequality.
Definition: mn_linked_ptr.hpp:239
T * get(const basic_linked_ptr< T > &_pPtr)
Get the pointer object.
Definition: mn_linked_ptr.hpp:223
bool operator==(const basic_linked_ptr< T > &a, const basic_linked_ptr< U > &b)
Compares two basic_linked_ptr objects for equality. Equality is defined as being true when the pointe...
Definition: mn_linked_ptr.hpp:231
basic_linked_ptr< T > make_link(const basic_linked_ptr< T > &value)
Make a linked pointer.
Definition: mn_linked_ptr.hpp:190
Definition: mn_allocator_typetraits.hpp:25
This class allows basic_linked_ptr<T> and basic_linked_ptr to share the same base nodes.
Definition: mn_linked_ptr.hpp:32
basic_linked_ptr_node * Prev
Definition: mn_linked_ptr.hpp:33
basic_linked_ptr_node * Next
Definition: mn_linked_ptr.hpp:34