]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/third_party/ms-gsl/include/gsl/pointers
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / third_party / ms-gsl / include / gsl / pointers
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4 //
5 // This code is licensed under the MIT License (MIT).
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13 // THE SOFTWARE.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #ifndef GSL_POINTERS_H
18 #define GSL_POINTERS_H
19
20 #include <gsl/gsl_assert> // for Ensures, Expects
21
22 #include <algorithm> // for forward
23 #include <iosfwd> // for ptrdiff_t, nullptr_t, ostream, size_t
24 #include <memory> // for shared_ptr, unique_ptr
25 #include <system_error> // for hash
26 #include <type_traits> // for enable_if_t, is_convertible, is_assignable
27
28 namespace gsl
29 {
30
31 //
32 // GSL.owner: ownership pointers
33 //
34 using std::unique_ptr;
35 using std::shared_ptr;
36
37 //
38 // owner
39 //
40 // owner<T> is designed as a bridge for code that must deal directly with owning pointers for some reason
41 //
42 // T must be a pointer type
43 // - disallow construction from any type other than pointer type
44 //
45 template <class T, class = std::enable_if_t<std::is_pointer<T>::value>>
46 using owner = T;
47
48 //
49 // not_null
50 //
51 // Restricts a pointer or smart pointer to only hold non-null values.
52 //
53 // Has zero size overhead over T.
54 //
55 // If T is a pointer (i.e. T == U*) then
56 // - allow construction from U*
57 // - disallow construction from nullptr_t
58 // - disallow default construction
59 // - ensure construction from null U* fails
60 // - allow implicit conversion to U*
61 //
62 template <class T>
63 class not_null
64 {
65 public:
66 static_assert(std::is_convertible<decltype(std::declval<T>() != nullptr), bool>::value, "T cannot be compared to nullptr.");
67
68 template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
69 constexpr not_null(U&& u) : ptr_(std::forward<U>(u))
70 {
71 Expects(ptr_ != nullptr);
72 }
73
74 template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
75 constexpr not_null(T u) : ptr_(std::move(u))
76 {
77 Expects(ptr_ != nullptr);
78 }
79
80 template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
81 constexpr not_null(const not_null<U>& other) : not_null(other.get())
82 {
83 }
84
85 not_null(const not_null& other) = default;
86 not_null& operator=(const not_null& other) = default;
87 constexpr std::conditional_t<std::is_copy_constructible<T>::value, T, const T&> get() const
88 {
89 Ensures(ptr_ != nullptr);
90 return ptr_;
91 }
92
93 constexpr operator T() const { return get(); }
94 constexpr decltype(auto) operator->() const { return get(); }
95 constexpr decltype(auto) operator*() const { return *get(); }
96
97 // prevents compilation when someone attempts to assign a null pointer constant
98 not_null(std::nullptr_t) = delete;
99 not_null& operator=(std::nullptr_t) = delete;
100
101 // unwanted operators...pointers only point to single objects!
102 not_null& operator++() = delete;
103 not_null& operator--() = delete;
104 not_null operator++(int) = delete;
105 not_null operator--(int) = delete;
106 not_null& operator+=(std::ptrdiff_t) = delete;
107 not_null& operator-=(std::ptrdiff_t) = delete;
108 void operator[](std::ptrdiff_t) const = delete;
109
110 private:
111 T ptr_;
112 };
113
114 template <class T>
115 auto make_not_null(T&& t) noexcept {
116 return not_null<std::remove_cv_t<std::remove_reference_t<T>>>{std::forward<T>(t)};
117 }
118
119 template <class T>
120 std::ostream& operator<<(std::ostream& os, const not_null<T>& val)
121 {
122 os << val.get();
123 return os;
124 }
125
126 template <class T, class U>
127 auto operator==(const not_null<T>& lhs, const not_null<U>& rhs) noexcept(noexcept(lhs.get() == rhs.get())) -> decltype(lhs.get() == rhs.get())
128 {
129 return lhs.get() == rhs.get();
130 }
131
132 template <class T, class U>
133 auto operator!=(const not_null<T>& lhs, const not_null<U>& rhs) noexcept(noexcept(lhs.get() != rhs.get())) -> decltype(lhs.get() != rhs.get())
134 {
135 return lhs.get() != rhs.get();
136 }
137
138 template <class T, class U>
139 auto operator<(const not_null<T>& lhs, const not_null<U>& rhs) noexcept(noexcept(lhs.get() < rhs.get())) -> decltype(lhs.get() < rhs.get())
140 {
141 return lhs.get() < rhs.get();
142 }
143
144 template <class T, class U>
145 auto operator<=(const not_null<T>& lhs, const not_null<U>& rhs) noexcept(noexcept(lhs.get() <= rhs.get())) -> decltype(lhs.get() <= rhs.get())
146 {
147 return lhs.get() <= rhs.get();
148 }
149
150 template <class T, class U>
151 auto operator>(const not_null<T>& lhs, const not_null<U>& rhs) noexcept(noexcept(lhs.get() > rhs.get())) -> decltype(lhs.get() > rhs.get())
152 {
153 return lhs.get() > rhs.get();
154 }
155
156 template <class T, class U>
157 auto operator>=(const not_null<T>& lhs, const not_null<U>& rhs) noexcept(noexcept(lhs.get() >= rhs.get())) -> decltype(lhs.get() >= rhs.get())
158 {
159 return lhs.get() >= rhs.get();
160 }
161
162 // more unwanted operators
163 template <class T, class U>
164 std::ptrdiff_t operator-(const not_null<T>&, const not_null<U>&) = delete;
165 template <class T>
166 not_null<T> operator-(const not_null<T>&, std::ptrdiff_t) = delete;
167 template <class T>
168 not_null<T> operator+(const not_null<T>&, std::ptrdiff_t) = delete;
169 template <class T>
170 not_null<T> operator+(std::ptrdiff_t, const not_null<T>&) = delete;
171
172 } // namespace gsl
173
174 namespace std
175 {
176 template <class T>
177 struct hash<gsl::not_null<T>>
178 {
179 std::size_t operator()(const gsl::not_null<T>& value) const { return hash<T>{}(value.get()); }
180 };
181
182 } // namespace std
183
184 namespace gsl
185 {
186
187 //
188 // strict_not_null
189 //
190 // Restricts a pointer or smart pointer to only hold non-null values,
191 //
192 // - provides a strict (i.e. explicit constructor from T) wrapper of not_null
193 // - to be used for new code that wishes the design to be cleaner and make not_null
194 // checks intentional, or in old code that would like to make the transition.
195 //
196 // To make the transition from not_null, incrementally replace not_null
197 // by strict_not_null and fix compilation errors
198 //
199 // Expect to
200 // - remove all unneeded conversions from raw pointer to not_null and back
201 // - make API clear by specifying not_null in parameters where needed
202 // - remove unnecessary asserts
203 //
204 template <class T>
205 class strict_not_null: public not_null<T>
206 {
207 public:
208
209 template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
210 constexpr explicit strict_not_null(U&& u) :
211 not_null<T>(std::forward<U>(u))
212 {}
213
214 template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
215 constexpr explicit strict_not_null(T u) :
216 not_null<T>(u)
217 {}
218
219 template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
220 constexpr strict_not_null(const not_null<U>& other) :
221 not_null<T>(other)
222 {}
223
224 template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
225 constexpr strict_not_null(const strict_not_null<U>& other) :
226 not_null<T>(other)
227 {}
228
229 strict_not_null(strict_not_null&& other) = default;
230 strict_not_null(const strict_not_null& other) = default;
231 strict_not_null& operator=(const strict_not_null& other) = default;
232 strict_not_null& operator=(const not_null<T>& other)
233 {
234 not_null<T>::operator=(other);
235 return *this;
236 }
237
238 // prevents compilation when someone attempts to assign a null pointer constant
239 strict_not_null(std::nullptr_t) = delete;
240 strict_not_null& operator=(std::nullptr_t) = delete;
241
242 // unwanted operators...pointers only point to single objects!
243 strict_not_null& operator++() = delete;
244 strict_not_null& operator--() = delete;
245 strict_not_null operator++(int) = delete;
246 strict_not_null operator--(int) = delete;
247 strict_not_null& operator+=(std::ptrdiff_t) = delete;
248 strict_not_null& operator-=(std::ptrdiff_t) = delete;
249 void operator[](std::ptrdiff_t) const = delete;
250 };
251
252 // more unwanted operators
253 template <class T, class U>
254 std::ptrdiff_t operator-(const strict_not_null<T>&, const strict_not_null<U>&) = delete;
255 template <class T>
256 strict_not_null<T> operator-(const strict_not_null<T>&, std::ptrdiff_t) = delete;
257 template <class T>
258 strict_not_null<T> operator+(const strict_not_null<T>&, std::ptrdiff_t) = delete;
259 template <class T>
260 strict_not_null<T> operator+(std::ptrdiff_t, const strict_not_null<T>&) = delete;
261
262 template <class T>
263 auto make_strict_not_null(T&& t) noexcept {
264 return strict_not_null<std::remove_cv_t<std::remove_reference_t<T>>>{std::forward<T>(t)};
265 }
266
267 #if ( defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L) )
268
269 // deduction guides to prevent the ctad-maybe-unsupported warning
270 template <class T> not_null(T) -> not_null<T>;
271 template <class T> strict_not_null(T) -> strict_not_null<T>;
272
273 #endif // ( defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L) )
274
275 } // namespace gsl
276
277 namespace std
278 {
279 template <class T>
280 struct hash<gsl::strict_not_null<T>>
281 {
282 std::size_t operator()(const gsl::strict_not_null<T>& value) const { return hash<T>{}(value.get()); }
283 };
284
285 } // namespace std
286
287 #endif // GSL_POINTERS_H