blob: 69499d6fe57d6d2c7995779c27e4dc76b8406c3d [file] [log] [blame]
Brian Silvermana6f7ce02018-07-07 15:04:00 -07001///////////////////////////////////////////////////////////////////////////////
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#if defined(_MSC_VER) && _MSC_VER < 1910
29#pragma push_macro("constexpr")
30#define constexpr /*constexpr*/
31
32#endif // defined(_MSC_VER) && _MSC_VER < 1910
33
34namespace gsl
35{
36
37//
38// GSL.owner: ownership pointers
39//
40using std::unique_ptr;
41using std::shared_ptr;
42
43//
44// owner
45//
46// owner<T> is designed as a bridge for code that must deal directly with owning pointers for some reason
47//
48// T must be a pointer type
49// - disallow construction from any type other than pointer type
50//
51template <class T, class = std::enable_if_t<std::is_pointer<T>::value>>
52using owner = T;
53
54//
55// not_null
56//
57// Restricts a pointer or smart pointer to only hold non-null values.
58//
59// Has zero size overhead over T.
60//
61// If T is a pointer (i.e. T == U*) then
62// - allow construction from U*
63// - disallow construction from nullptr_t
64// - disallow default construction
65// - ensure construction from null U* fails
66// - allow implicit conversion to U*
67//
68template <class T>
69class not_null
70{
71public:
72 static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
73
74 template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
75 constexpr explicit not_null(U&& u) : ptr_(std::forward<U>(u))
76 {
77 Expects(ptr_ != nullptr);
78 }
79
80 template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
81 constexpr explicit not_null(T u) : ptr_(u)
82 {
83 Expects(ptr_ != nullptr);
84 }
85
86 template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
87 constexpr not_null(const not_null<U>& other) : not_null(other.get())
88 {
89 }
90
91 not_null(not_null&& other) = default;
92 not_null(const not_null& other) = default;
93 not_null& operator=(const not_null& other) = default;
94
95 constexpr T get() const
96 {
97 Ensures(ptr_ != nullptr);
98 return ptr_;
99 }
100
101 constexpr operator T() const { return get(); }
102 constexpr T operator->() const { return get(); }
103 constexpr decltype(auto) operator*() const { return *get(); }
104
105 // prevents compilation when someone attempts to assign a null pointer constant
106 not_null(std::nullptr_t) = delete;
107 not_null& operator=(std::nullptr_t) = delete;
108
109 // unwanted operators...pointers only point to single objects!
110 not_null& operator++() = delete;
111 not_null& operator--() = delete;
112 not_null operator++(int) = delete;
113 not_null operator--(int) = delete;
114 not_null& operator+=(std::ptrdiff_t) = delete;
115 not_null& operator-=(std::ptrdiff_t) = delete;
116 void operator[](std::ptrdiff_t) const = delete;
117
118private:
119 T ptr_;
120};
121
122template <class T>
123std::ostream& operator<<(std::ostream& os, const not_null<T>& val)
124{
125 os << val.get();
126 return os;
127}
128
129template <class T, class U>
130auto operator==(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() == rhs.get())
131{
132 return lhs.get() == rhs.get();
133}
134
135template <class T, class U>
136auto operator!=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() != rhs.get())
137{
138 return lhs.get() != rhs.get();
139}
140
141template <class T, class U>
142auto operator<(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() < rhs.get())
143{
144 return lhs.get() < rhs.get();
145}
146
147template <class T, class U>
148auto operator<=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() <= rhs.get())
149{
150 return lhs.get() <= rhs.get();
151}
152
153template <class T, class U>
154auto operator>(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() > rhs.get())
155{
156 return lhs.get() > rhs.get();
157}
158
159template <class T, class U>
160auto operator>=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() >= rhs.get())
161{
162 return lhs.get() >= rhs.get();
163}
164
165// more unwanted operators
166template <class T, class U>
167std::ptrdiff_t operator-(const not_null<T>&, const not_null<U>&) = delete;
168template <class T>
169not_null<T> operator-(const not_null<T>&, std::ptrdiff_t) = delete;
170template <class T>
171not_null<T> operator+(const not_null<T>&, std::ptrdiff_t) = delete;
172template <class T>
173not_null<T> operator+(std::ptrdiff_t, const not_null<T>&) = delete;
174
175} // namespace gsl
176
177namespace std
178{
179template <class T>
180struct hash<gsl::not_null<T>>
181{
182 std::size_t operator()(const gsl::not_null<T>& value) const { return hash<T>{}(value); }
183};
184
185} // namespace std
186
187#if defined(_MSC_VER) && _MSC_VER < 1910
188#undef constexpr
189#pragma pop_macro("constexpr")
190
191#endif // defined(_MSC_VER) && _MSC_VER < 1910
192
193#endif // GSL_POINTERS_H