mn_weak_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_4fa7b6b8_4e0e_4bd2_817c_08c6775c3ec2_H_
19 #define _MINLIB_4fa7b6b8_4e0e_4bd2_817c_08c6775c3ec2_H_
20 
21 #include "../mn_config.hpp"
22 
23 
24 #include "mn_shared_ptr.hpp"
25 
26 namespace mn {
27  namespace pointer {
28  template <typename T, typename TRefType = atomic_size_t >
30  public:
31  using value_type = T;
32  using element_type = T;
33  using reference = T&;
35  using pointer = value_type*;
36  using count_type = TRefType;
37 
38 
41 
42 
43  explicit basic_weak_ptr(value_type pValue = 0)
44  : m_ptr(pValue), m_ref(1) { }
45 
47  : m_ptr(r.m_ptr), m_ref(r.m_ref) { }
48 
50  : m_ptr(pShrd.get()), m_ref( pShrd.ref() ) {}
51 
52  template<class U, typename YRefType = count_type>
54  : m_ptr(r.m_ptr), m_ref(r.m_ref) { }
55 
56  template<class U, typename YRefType = count_type>
58  : m_ptr(pShrd.get()), m_ref(pShrd.ref() ) {}
59 
61  bool expired() { return m_ref.get() == 0; }
62  void reset() { self_type(0).swap(*this); }
63 
64  count_type use_count() { return m_ref.get(); }
65 
66  void swap(self_type& other) {
67  mn::swap<pointer>(m_ptr, other.m_ptr);
68  mn::swap<count_type >(m_ref, other.m_ref);
69  }
70  template<class Y, typename YRefType = atomic_size_t>
72  return m_ref < rhs.m_ref;
73  }
74  template<class Y, typename YRefType = atomic_size_t>
76  return m_ref < rhs.ref();
77  }
78 
79  pointer get() const {
80  return static_cast<T*>(m_ptr);
81  }
82 
83  pointer operator->() const {
84  assert(get() != 0);
85  return this->get();
86  }
88  assert(get() != 0);
89  return *this->get();
90  }
91 
92  operator bool() {
93  return m_ptr != 0;
94  }
95 
96  self_type& operator=( const self_type& r ) {
97  m_ptr = r.m_ptr;
98  m_ref = r.m_ref;
99  return *this;
100  }
101  private:
104  };
105 
106  template < typename T >
108 
109  template < typename T >
111 
117  template<typename T, typename... Args >
118  inline weak_ptr<T> make_weak(Args&&... args) {
119  return weak_ptr<T>(new T (mn::forward<Args>(args)...) );
120  }
121 
127  template<typename T, typename... Args >
128  inline weak_atomic_ptr<T> make_atomic_weak(Args&&... args) {
129  return weak_atomic_ptr<T>(new T (mn::forward<Args>(args)...) );
130  }
131  }
132 }
133 
134 #endif
Definition: mn_shared_ptr.hpp:29
ref_type ref()
Definition: mn_shared_ptr.hpp:62
Definition: mn_weak_ptr.hpp:29
basic_weak_ptr(const basic_weak_ptr< U, YRefType > &r)
Definition: mn_weak_ptr.hpp:53
bool expired()
Definition: mn_weak_ptr.hpp:61
const_value_type & operator*()
Definition: mn_weak_ptr.hpp:87
void reset()
Definition: mn_weak_ptr.hpp:62
const value_type const_value_type
Definition: mn_weak_ptr.hpp:34
count_type m_ref
Definition: mn_weak_ptr.hpp:103
T value_type
Definition: mn_weak_ptr.hpp:31
basic_shared_ptr< value_type, count_type > shared_type
Definition: mn_weak_ptr.hpp:40
bool owner_before(const basic_shared_ptr< Y, YRefType > &rhs)
Definition: mn_weak_ptr.hpp:75
pointer m_ptr
Definition: mn_weak_ptr.hpp:102
count_type use_count()
Definition: mn_weak_ptr.hpp:64
basic_weak_ptr(value_type pValue=0)
Definition: mn_weak_ptr.hpp:43
basic_weak_ptr(const self_type &r)
Definition: mn_weak_ptr.hpp:46
shared_type lock()
Definition: mn_weak_ptr.hpp:60
basic_weak_ptr(const basic_shared_ptr< U, YRefType > &pShrd)
Definition: mn_weak_ptr.hpp:57
basic_weak_ptr< value_type, count_type > self_type
Definition: mn_weak_ptr.hpp:39
value_type * pointer
Definition: mn_weak_ptr.hpp:35
bool owner_before(const basic_weak_ptr< Y, YRefType > &rhs)
Definition: mn_weak_ptr.hpp:71
TRefType count_type
Definition: mn_weak_ptr.hpp:36
void swap(self_type &other)
Definition: mn_weak_ptr.hpp:66
T element_type
Definition: mn_weak_ptr.hpp:32
T & reference
Definition: mn_weak_ptr.hpp:33
basic_weak_ptr(shared_type &pShrd)
Definition: mn_weak_ptr.hpp:49
self_type & operator=(const self_type &r)
Definition: mn_weak_ptr.hpp:96
pointer get() const
Definition: mn_weak_ptr.hpp:79
pointer operator->() const
Definition: mn_weak_ptr.hpp:83
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
weak_ptr< T > make_weak(Args &&... args)
Make a weak pointer.
Definition: mn_weak_ptr.hpp:118
weak_atomic_ptr< T > make_atomic_weak(Args &&... args)
Make a weak atomic pointer.
Definition: mn_weak_ptr.hpp:128
Definition: mn_allocator_typetraits.hpp:25