1 ///////////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
5 // This code is licensed under the MIT License (MIT).
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
15 ///////////////////////////////////////////////////////////////////////////////
17 #ifndef GSL_POINTERS_H
18 #define GSL_POINTERS_H
20 #include <gsl/gsl_assert> // for Ensures, Expects
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
32 // GSL.owner: ownership pointers
34 using std::unique_ptr;
35 using std::shared_ptr;
40 // owner<T> is designed as a bridge for code that must deal directly with owning pointers for some reason
42 // T must be a pointer type
43 // - disallow construction from any type other than pointer type
45 template <class T, class = std::enable_if_t<std::is_pointer<T>::value>>
51 // Restricts a pointer or smart pointer to only hold non-null values.
53 // Has zero size overhead over T.
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*
66 static_assert(std::is_convertible<decltype(std::declval<T>() != nullptr), bool>::value, "T cannot be compared to nullptr.");
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))
71 Expects(ptr_ != nullptr);
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))
77 Expects(ptr_ != nullptr);
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())
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
89 Ensures(ptr_ != nullptr);
93 constexpr operator T() const { return get(); }
94 constexpr decltype(auto) operator->() const { return get(); }
95 constexpr decltype(auto) operator*() const { return *get(); }
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;
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;
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)};
120 std::ostream& operator<<(std::ostream& os, const not_null<T>& val)
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())
129 return lhs.get() == rhs.get();
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())
135 return lhs.get() != rhs.get();
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())
141 return lhs.get() < rhs.get();
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())
147 return lhs.get() <= rhs.get();
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())
153 return lhs.get() > rhs.get();
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())
159 return lhs.get() >= rhs.get();
162 // more unwanted operators
163 template <class T, class U>
164 std::ptrdiff_t operator-(const not_null<T>&, const not_null<U>&) = delete;
166 not_null<T> operator-(const not_null<T>&, std::ptrdiff_t) = delete;
168 not_null<T> operator+(const not_null<T>&, std::ptrdiff_t) = delete;
170 not_null<T> operator+(std::ptrdiff_t, const not_null<T>&) = delete;
177 struct hash<gsl::not_null<T>>
179 std::size_t operator()(const gsl::not_null<T>& value) const { return hash<T>{}(value.get()); }
190 // Restricts a pointer or smart pointer to only hold non-null values,
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.
196 // To make the transition from not_null, incrementally replace not_null
197 // by strict_not_null and fix compilation errors
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
205 class strict_not_null: public not_null<T>
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))
214 template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
215 constexpr explicit strict_not_null(T u) :
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) :
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) :
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)
234 not_null<T>::operator=(other);
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;
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;
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;
256 strict_not_null<T> operator-(const strict_not_null<T>&, std::ptrdiff_t) = delete;
258 strict_not_null<T> operator+(const strict_not_null<T>&, std::ptrdiff_t) = delete;
260 strict_not_null<T> operator+(std::ptrdiff_t, const strict_not_null<T>&) = delete;
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)};
267 #if ( defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L) )
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>;
273 #endif // ( defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L) )
280 struct hash<gsl::strict_not_null<T>>
282 std::size_t operator()(const gsl::strict_not_null<T>& value) const { return hash<T>{}(value.get()); }
287 #endif // GSL_POINTERS_H