CsPointer  2.0.0
cs_weak_pointer.h
1 
19 #ifndef LIB_CS_WEAK_POINTER_H
20 #define LIB_CS_WEAK_POINTER_H
21 
22 #include <cs_shared_pointer.h>
23 
24 #include <memory>
25 
26 namespace CsPointer {
27 
28 template <typename T>
29 class CsWeakPointer
30 {
31  public:
32  using element_type = typename std::weak_ptr<T>::element_type;
33  using pointer = element_type *;
34 
35  using Pointer = pointer;
37 
39  {
40  }
41 
42  template <typename U>
43  CsWeakPointer(const CsWeakPointer<U> &p) noexcept
44  : m_ptr(p.m_ptr)
45  {
46  }
47 
48  template <typename U>
49  CsWeakPointer(const CsSharedPointer<U> &p) noexcept
50  : m_ptr(p.m_ptr)
51  {
52  }
53 
54  ~CsWeakPointer() = default;
55 
56  CsWeakPointer(const CsWeakPointer &other) = default;
57  CsWeakPointer &operator=(const CsWeakPointer &other) = default;
58 
59  CsWeakPointer(CsWeakPointer &&other) = default;
60  CsWeakPointer &operator=(CsWeakPointer && other) = default;
61 
62  template <typename U>
64  {
65  m_ptr = p.m_ptr;
66  return *this;
67  }
68 
69  bool operator !() const {
70  return is_null();
71  }
72 
73  explicit operator bool() const noexcept {
74  return ! is_null();
75  }
76 
77  void clear() noexcept {
78  m_ptr.reset();
79  }
80 
81  bool expired() const noexcept {
82  return m_ptr.expired();
83  }
84 
85  CsSharedPointer<T> lock() const noexcept {
86  return m_ptr.lock();
87  }
88 
89  bool is_null() const noexcept {
90  return m_ptr.expired();
91  }
92 
93  template <typename U>
94  bool owner_before(const CsSharedPointer<U> &p) const noexcept {
95  return m_ptr.owner_before(p.m_ptr);
96  }
97 
98  template <typename U>
99  bool owner_before(const CsWeakPointer<U> &p) const noexcept {
100  return m_ptr.owner_before(p.m_ptr);
101  }
102 
103  void reset() noexcept {
104  m_ptr.reset();
105  }
106 
107  void swap(CsWeakPointer &other) noexcept {
108  std::swap(m_ptr, other.m_ptr);
109  }
110 
111  CsSharedPointer<T> toStrongRef() const noexcept {
112  return m_ptr.lock();
113  }
114 
115  long use_count() const noexcept {
116  return m_ptr.use_count();
117  }
118 
119  template <typename U>
120  bool operator==(const CsWeakPointer<U> &ptr) const noexcept {
121  return this->owner_before(ptr) == false && ptr.owner_before(*this) == false;
122  }
123 
124  template <typename U>
125  bool operator==(const CsSharedPointer<U> &ptr) const noexcept {
126  return this->owner_before(ptr) == false && ptr.owner_before(*this) == false;
127  }
128 
129  bool operator==(std::nullptr_t) const noexcept {
130  return this->expired();
131  }
132 
133  private:
134  std::weak_ptr<T> m_ptr;
135 
136  template <typename U>
137  friend class CsSharedPointer;
138 
139  template <typename U>
140  friend class CsWeakPointer;
141 };
142 
143 template <typename T>
144 void swap(CsWeakPointer<T> &ptr1, CsWeakPointer<T> &ptr2) noexcept
145 {
146  ptr1.swap(ptr2);
147 }
148 
149 } // end namespace
150 
151 #endif
long use_count() const noexcept
Definition: cs_weak_pointer.h:115
bool operator==(const CsWeakPointer< T1 > &ptr1, const CsSharedPointer< T2 > &ptr2)
CsSharedPointer< T > toStrongRef() const noexcept
Definition: cs_weak_pointer.h:111
void swap(CsWeakPointer &other) noexcept
Definition: cs_weak_pointer.h:107
void reset() noexcept
Definition: cs_weak_pointer.h:103
bool owner_before(const CsWeakPointer< U > &p) const noexcept
Definition: cs_weak_pointer.h:99
CsSharedPointer< T > lock() const noexcept
Definition: cs_weak_pointer.h:85
bool expired() const noexcept
Definition: cs_weak_pointer.h:81
void clear() noexcept
Definition: cs_weak_pointer.h:77
bool is_null() const noexcept
Definition: cs_weak_pointer.h:89
CsWeakPointer & operator=(const CsSharedPointer< U > &p) noexcept
Definition: cs_weak_pointer.h:63
CsWeakPointer & operator=(const CsWeakPointer &other) = default
bool operator!() const
Definition: cs_weak_pointer.h:69
Stores a pointer to a potentially shared object.
Definition: cs_shared_pointer.h:34
CsWeakPointer(const CsSharedPointer< U > &p) noexcept
Definition: cs_weak_pointer.h:49
Stores a weak pointer to a potentially shared object.
Definition: cs_shared_pointer.h:31
CsWeakPointer(const CsWeakPointer< U > &p) noexcept
Definition: cs_weak_pointer.h:43
CsWeakPointer()
Definition: cs_weak_pointer.h:38
element_type ElementType
Definition: cs_weak_pointer.h:36
pointer Pointer
Definition: cs_weak_pointer.h:35
bool owner_before(const CsSharedPointer< U > &p) const noexcept
Definition: cs_weak_pointer.h:94
element_type * pointer
Definition: cs_weak_pointer.h:33
typename std::weak_ptr< T >::element_type element_type
Definition: cs_weak_pointer.h:32
Namespace for the CsPointer library.