• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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#if defined(_MSC_VER) && _MSC_VER < 1910 && !defined(__clang__)
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 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 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>
123auto make_not_null(T&& t) {
124    return not_null<std::remove_cv_t<std::remove_reference_t<T>>>{std::forward<T>(t)};
125}
126
127template <class T>
128std::ostream& operator<<(std::ostream& os, const not_null<T>& val)
129{
130    os << val.get();
131    return os;
132}
133
134template <class T, class U>
135auto operator==(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() == rhs.get())
136{
137    return lhs.get() == rhs.get();
138}
139
140template <class T, class U>
141auto operator!=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() != rhs.get())
142{
143    return lhs.get() != rhs.get();
144}
145
146template <class T, class U>
147auto operator<(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() < rhs.get())
148{
149    return lhs.get() < rhs.get();
150}
151
152template <class T, class U>
153auto operator<=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() <= rhs.get())
154{
155    return lhs.get() <= rhs.get();
156}
157
158template <class T, class U>
159auto operator>(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() > rhs.get())
160{
161    return lhs.get() > rhs.get();
162}
163
164template <class T, class U>
165auto operator>=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() >= rhs.get())
166{
167    return lhs.get() >= rhs.get();
168}
169
170// more unwanted operators
171template <class T, class U>
172std::ptrdiff_t operator-(const not_null<T>&, const not_null<U>&) = delete;
173template <class T>
174not_null<T> operator-(const not_null<T>&, std::ptrdiff_t) = delete;
175template <class T>
176not_null<T> operator+(const not_null<T>&, std::ptrdiff_t) = delete;
177template <class T>
178not_null<T> operator+(std::ptrdiff_t, const not_null<T>&) = delete;
179
180} // namespace gsl
181
182namespace std
183{
184template <class T>
185struct hash<gsl::not_null<T>>
186{
187    std::size_t operator()(const gsl::not_null<T>& value) const { return hash<T>{}(value); }
188};
189
190} // namespace std
191
192namespace gsl
193{
194
195//
196// strict_not_null
197//
198// Restricts a pointer or smart pointer to only hold non-null values,
199//
200// - provides a strict (i.e. explicit contructor from T) wrapper of not_null
201// - to be used for new code that wishes the design to be cleaner and make not_null
202//   checks intentional, or in old code that would like to make the transition.
203//
204//   To make the transition from not_null, incrementally replace not_null
205//   by strict_not_null and fix compilation errors
206//
207//   Expect to
208//   - remove all unneded conversions from raw pointer to not_null and back
209//   - make API clear by specifyning not_null in parameters where needed
210//   - remove unnesessary asserts
211//
212template <class T>
213class strict_not_null: public not_null<T>
214{
215public:
216
217    template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
218    constexpr explicit strict_not_null(U&& u) :
219        not_null<T>(std::forward<U>(u))
220    {}
221
222    template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
223    constexpr explicit strict_not_null(T u) :
224        not_null<T>(u)
225    {}
226
227    template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
228    constexpr strict_not_null(const not_null<U>& other) :
229        not_null<T>(other)
230    {}
231
232    template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
233    constexpr strict_not_null(const strict_not_null<U>& other) :
234        not_null<T>(other)
235    {}
236
237    strict_not_null(strict_not_null&& other) = default;
238    strict_not_null(const strict_not_null& other) = default;
239    strict_not_null& operator=(const strict_not_null& other) = default;
240    strict_not_null& operator=(const not_null<T>& other)
241    {
242        not_null<T>::operator=(other);
243        return *this;
244    }
245
246    // prevents compilation when someone attempts to assign a null pointer constant
247    strict_not_null(std::nullptr_t) = delete;
248    strict_not_null& operator=(std::nullptr_t) = delete;
249
250    // unwanted operators...pointers only point to single objects!
251    strict_not_null& operator++() = delete;
252    strict_not_null& operator--() = delete;
253    strict_not_null operator++(int) = delete;
254    strict_not_null operator--(int) = delete;
255    strict_not_null& operator+=(std::ptrdiff_t) = delete;
256    strict_not_null& operator-=(std::ptrdiff_t) = delete;
257    void operator[](std::ptrdiff_t) const = delete;
258};
259
260// more unwanted operators
261template <class T, class U>
262std::ptrdiff_t operator-(const strict_not_null<T>&, const strict_not_null<U>&) = delete;
263template <class T>
264strict_not_null<T> operator-(const strict_not_null<T>&, std::ptrdiff_t) = delete;
265template <class T>
266strict_not_null<T> operator+(const strict_not_null<T>&, std::ptrdiff_t) = delete;
267template <class T>
268strict_not_null<T> operator+(std::ptrdiff_t, const strict_not_null<T>&) = delete;
269
270template <class T>
271auto make_strict_not_null(T&& t) {
272    return strict_not_null<std::remove_cv_t<std::remove_reference_t<T>>>{std::forward<T>(t)};
273}
274
275} // namespace gsl
276
277namespace std
278{
279template <class T>
280struct hash<gsl::strict_not_null<T>>
281{
282    std::size_t operator()(const gsl::strict_not_null<T>& value) const { return hash<T>{}(value); }
283};
284
285} // namespace std
286
287#if defined(_MSC_VER) && _MSC_VER < 1910 && !defined(__clang__)
288
289#undef constexpr
290#pragma pop_macro("constexpr")
291
292#endif // defined(_MSC_VER) && _MSC_VER < 1910 && !defined(__clang__)
293
294#endif // GSL_POINTERS_H
295