qxLib
link.h
Go to the documentation of this file.
1 /**
2 
3  @file link.h
4  @author Khrapov
5  @date 21.11.2021
6  @copyright © Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 #pragma once
10 
12 
13 #include <cstddef>
14 #include <memory>
15 
16 namespace qx
17 {
18 
19 /**
20 
21  @class link
22  @brief std::weak_ptr wrapper that do not allow you
23  to create strong pointer (std::shared_ptr)
24  @tparam T - pointer class type
25  @author Khrapov
26  @date 21.11.2021
27 
28 **/
29 template<class T>
30 class link
31 {
32  // friend templated link class
33  friend class link;
34 
35 public:
36  /**
37 
38  @class lock_ptr
39  @brief std::shared_ptr wrapper
40  @author Khrapov
41  @date 23.11.2021
42 
43  **/
44  class lock_ptr
45  {
46  // private constructor
47  friend class link;
48 
49  // friend templated lock_ptr class
50  friend class lock_ptr;
51 
52  public:
54 
55  T* operator->() const noexcept;
56  T& operator*() const noexcept;
57 
58  /**
59  @brief Get object pointer
60  @retval - T object pointer
61  **/
62  T* get() const noexcept;
63 
64  /**
65  @brief Is object presents
66  @retval - true if object presents
67  **/
68  explicit operator bool() const noexcept;
69 
70  /**
71  @brief operator==
72  @tparam U - lock_ptr with pointer convertible to T
73  @param other - other link_ptr
74  @retval - true, if objects are equal
75  **/
76  template<class U>
77  bool operator==(const U& other) const noexcept;
78 
79  /**
80  @brief operator<=>
81  @tparam U - lock_ptr with pointer convertible to T
82  @param other - other link_ptr
83  @retval - three-way comparison result
84  **/
85  template<class U>
86  std::strong_ordering operator<=>(const U& other) const noexcept;
87 
88  private:
89  /**
90  @brief lock_ptr object constructor
91  @param pShared - object strong pointer
92  **/
93  lock_ptr(std::shared_ptr<T> pShared) noexcept;
94 
95  private:
96  std::shared_ptr<T> m_pRaw;
97  };
98 
99 public:
100  link() noexcept = default;
101 
102  /**
103  @brief link object constructor
104  @tparam U - T or type convertible to T
105  @param pWeak - weak object pointer
106  **/
107  template<class U>
108  link(const std::weak_ptr<U>& pWeak) noexcept;
109 
110  /**
111  @brief link object constructor
112  @param pWeak - weak object pointer
113  **/
114  link(std::weak_ptr<T> pWeak) noexcept;
115 
116  /**
117  @brief link object constructor
118  @tparam U - T or type convertible to T
119  @param pStrong - strong object pointer
120  **/
121  template<class U>
122  link(const std::shared_ptr<U>& pStrong) noexcept;
123 
124  // I don't want to take the ownership. What if it's the last shared ptr alive?
125  // Use std::as_const(pStrong) if you are sure it's not.
126  template<class U>
127  link(std::shared_ptr<U>&& pStrong) noexcept = delete;
128 
129  /**
130  @brief link object constructor
131  @tparam U - T or type convertible to T
132  @param pLink - other link
133  **/
134  template<class U>
135  link(const link<U>& pLink) noexcept;
136 
137  /**
138  @brief operator=
139  @tparam U - T or type convertible to T
140  @param pLink - other link
141  @retval - this object reference
142  **/
143  template<class U>
144  link& operator=(const link<U>& pLink) noexcept;
145 
146  /**
147  @brief link object constructor
148  @tparam U - T or type convertible to T
149  @param pLink - other link rvalue ref
150  **/
151  template<class U>
152  link(link<U>&& pLink) noexcept;
153 
154  /**
155  @brief operator=
156  @tparam U - T or type convertible to T
157  @param pLink - other link rvalue ref
158  @retval - this object reference
159  **/
160  template<class U>
161  link& operator=(link<U>&& pLink) noexcept;
162 
163  /**
164  @brief link object constructor
165  @param - nullptr
166  **/
167  link(std::nullptr_t) noexcept;
168 
169  /**
170  @brief Lock and return strong pointer
171  @retval - strong pointer
172  **/
173  lock_ptr lock() const noexcept;
174 
175  /**
176  @brief Reset weak pointer
177  **/
178  void reset() noexcept;
179 
180  /**
181  @brief If pointer is expired
182  @retval - true if pointer is expired
183  **/
184  bool expired() const noexcept;
185 
186  /**
187  @brief If pointer is expired
188  @retval - true if pointer is expired
189  **/
190  explicit operator bool() const noexcept;
191 
192  template<class U>
193  bool operator==(const link<U>&) const noexcept = delete;
194  template<class U>
195  bool operator!=(const link<U>&) const noexcept = delete;
196 
197 private:
198  std::weak_ptr<T> m_pWeak;
199 };
200 
201 } // namespace qx
202 
203 #include <qx/smart_ptr/link.inl>
#define QX_NONCOPYMOVABLE(className)
Define class as non copyable and non movable.