mn_shared_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_831159bd_3f35_4a00_8d46_f3fd737a5797_H_
19 #define _MINLIB_831159bd_3f35_4a00_8d46_f3fd737a5797_H_
20 
21 #include "../mn_config.hpp"
22 
23 #include "../mn_def.hpp"
24 
25 namespace mn {
26  namespace pointer {
27 
28  template < typename T, typename TRefType >
30  public:
31  using value_type = T;
32  using element_type = T;
33  using reference = T&;
35  using pointer = value_type*;
36  using ref_type = TRefType;
37 
39 
40 
41  explicit basic_shared_ptr(pointer ptr )
42  : m_ref(1), m_ptr(ptr) { }
43 
45  assert( (++m_ref != 0) );
46  m_ptr = sp.m_ptr;
47  }
48 
50  if (--m_ref == 0) delete m_ptr;
51  }
52 
54  pointer __px = this->get();
55  if (--m_ref == 0) delete m_ptr;
56 
57  return __px;
58  }
59  void reset( pointer pValue = 0)
60  { self_type(pValue).swap(*this); }
61 
63  return m_ref;
64  }
65  void swap(self_type& b) {
66  mn::swap<pointer>(m_ptr, b.m_ptr);
67  mn::swap<ref_type >(m_ref, b.m_ref);
68  }
69 
70  pointer get() const {
71  return static_cast<T*>(m_ptr);
72  }
73 
74  pointer operator->() const {
75  assert(get() != 0);
76  return this->get();
77  }
79  assert(get() != 0);
80  return *this->get();
81  }
82  operator bool() {
83  return m_ptr != 0;
84  }
85 
87  release();
88  m_ptr = sp.m_ptr;
89  m_ref = sp.m_ref;
90  return *this;
91  }
92  private:
95  };
96 
97  template < typename T, typename TRefType >
99  a.swap(b);
100  }
101 
102  template < typename T >
104 
105  template < typename T >
107 
113  template<typename T, typename... Args >
114  inline shared_ptr<T> make_shared(Args&&... args) {
115  return shared_ptr<T>(new T (mn::forward<Args>(args)...) );
116  }
117 
123  template<typename T, typename... Args >
124  inline shared_atomic_ptr<T> make_atomic_shared(Args&&... args) {
125  return shared_atomic_ptr<T>(new T (mn::forward<Args>(args)...) );
126  }
127  }
128 }
129 
130 #endif
Definition: mn_shared_ptr.hpp:29
T element_type
Definition: mn_shared_ptr.hpp:32
void reset(pointer pValue=0)
Definition: mn_shared_ptr.hpp:59
T value_type
Definition: mn_shared_ptr.hpp:31
basic_shared_ptr(const self_type &sp)
Definition: mn_shared_ptr.hpp:44
basic_shared_ptr(pointer ptr)
Definition: mn_shared_ptr.hpp:41
~basic_shared_ptr()
Definition: mn_shared_ptr.hpp:49
ref_type ref()
Definition: mn_shared_ptr.hpp:62
TRefType ref_type
Definition: mn_shared_ptr.hpp:36
const value_type const_value_type
Definition: mn_shared_ptr.hpp:34
pointer m_ptr
Definition: mn_shared_ptr.hpp:94
ref_type m_ref
Definition: mn_shared_ptr.hpp:93
self_type & operator=(self_type &sp)
Definition: mn_shared_ptr.hpp:86
T & reference
Definition: mn_shared_ptr.hpp:33
pointer get() const
Definition: mn_shared_ptr.hpp:70
void swap(self_type &b)
Definition: mn_shared_ptr.hpp:65
basic_shared_ptr< value_type, ref_type > self_type
Definition: mn_shared_ptr.hpp:38
pointer release()
Definition: mn_shared_ptr.hpp:53
const_value_type & operator*()
Definition: mn_shared_ptr.hpp:78
pointer operator->() const
Definition: mn_shared_ptr.hpp:74
value_type * pointer
Definition: mn_shared_ptr.hpp:35
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
shared_ptr< T > make_shared(Args &&... args)
Make a shared pointer.
Definition: mn_shared_ptr.hpp:114
void swap(basic_auto_ptr< T > &a, basic_auto_ptr< T > &b)
Definition: mn_auto_ptr.hpp:188
shared_atomic_ptr< T > make_atomic_shared(Args &&... args)
Make a shared atomic pointer.
Definition: mn_shared_ptr.hpp:124
Definition: mn_allocator_typetraits.hpp:25