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