-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathNeverAlwaysNull.h
168 lines (141 loc) · 6.44 KB
/
NeverAlwaysNull.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#pragma once
#include <type_traits> // for false_type, is_convertible etc
#include <utility> // for forward, move
#include <system_error> // for hash
#include <cstddef> // for ptrdiff_t, etc
#include <cassert>
// Copied from Microsoft GSL: https://github.com/microsoft/GSL/blob/main/include/gsl/pointers
namespace Ichor {
namespace Detail {
template <typename T, typename = void>
struct is_comparable_to_nullptr : std::false_type {
};
template <typename T>
struct is_comparable_to_nullptr<T, std::enable_if_t<std::is_convertible<decltype(std::declval<T>() != nullptr), bool>::value>> : std::true_type {
};
// Resolves to the more efficient of `const T` or `const T&`, in the context of returning a const-qualified value
// of type T.
//
// Copied from cppfront's implementation of the CppCoreGuidelines F.16 (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-in)
template<typename T>
using value_or_reference_return_t = std::conditional_t<sizeof(T) < 2*sizeof(void*) && std::is_trivially_copy_constructible<T>::value, const T, const T&>;
}
template <typename T>
class NeverNull {
public:
static_assert(Detail::is_comparable_to_nullptr<T>::value, "T cannot be compared to nullptr.");
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr NeverNull(U&& u) : ptr_(std::forward<U>(u))
{
assert(ptr_ != nullptr);
}
template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
constexpr NeverNull(T u) : ptr_(std::move(u))
{
assert(ptr_ != nullptr);
}
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr NeverNull(const NeverNull<U>& other) : NeverNull(other.get())
{}
NeverNull(const NeverNull& other) = default;
NeverNull& operator=(const NeverNull& other) = default;
constexpr Detail::value_or_reference_return_t<T> get() const
{
return ptr_;
}
constexpr operator T() const { return get(); }
constexpr decltype(auto) operator->() const { return get(); }
constexpr decltype(auto) operator*() const { return *get(); }
// prevents compilation when someone attempts to assign a null pointer constant
NeverNull(std::nullptr_t) = delete;
NeverNull& operator=(std::nullptr_t) = delete;
// unwanted operators...pointers only point to single objects!
NeverNull& operator++() = delete;
NeverNull& operator--() = delete;
NeverNull operator++(int) = delete;
NeverNull operator--(int) = delete;
NeverNull& operator+=(std::ptrdiff_t) = delete;
NeverNull& operator-=(std::ptrdiff_t) = delete;
void operator[](std::ptrdiff_t) const = delete;
private:
T ptr_;
};
template <class T, class U>
auto operator==(const NeverNull<T>& lhs, const NeverNull<U>& rhs) noexcept(noexcept(lhs.get() == rhs.get()))
-> decltype(lhs.get() == rhs.get()) {
return lhs.get() == rhs.get();
}
template <class T, class U>
auto operator!=(const NeverNull<T>& lhs, const NeverNull<U>& rhs) noexcept(noexcept(lhs.get() != rhs.get()))
-> decltype(lhs.get() != rhs.get()) {
return lhs.get() != rhs.get();
}
template <class T, class U>
auto operator<(const NeverNull<T>& lhs, const NeverNull<U>& rhs) noexcept(noexcept(lhs.get() < rhs.get()))
-> decltype(lhs.get() < rhs.get()) {
return lhs.get() < rhs.get();
}
template <class T, class U>
auto operator<=(const NeverNull<T>& lhs, const NeverNull<U>& rhs) noexcept(noexcept(lhs.get() <= rhs.get()))
-> decltype(lhs.get() <= rhs.get()) {
return lhs.get() <= rhs.get();
}
template <class T, class U>
auto operator>(const NeverNull<T>& lhs, const NeverNull<U>& rhs) noexcept(noexcept(lhs.get() > rhs.get()))
-> decltype(lhs.get() > rhs.get()) {
return lhs.get() > rhs.get();
}
template <class T, class U>
auto operator>=(const NeverNull<T>& lhs, const NeverNull<U>& rhs) noexcept(noexcept(lhs.get() >= rhs.get()))
-> decltype(lhs.get() >= rhs.get()) {
return lhs.get() >= rhs.get();
}
// more unwanted operators
template <class T, class U>
std::ptrdiff_t operator-(const NeverNull<T>&, const NeverNull<U>&) = delete;
template <class T>
NeverNull<T> operator-(const NeverNull<T>&, std::ptrdiff_t) = delete;
template <class T>
NeverNull<T> operator+(const NeverNull<T>&, std::ptrdiff_t) = delete;
template <class T>
NeverNull<T> operator+(std::ptrdiff_t, const NeverNull<T>&) = delete;
template <typename T>
class AlwaysNull {
public:
static_assert(Detail::is_comparable_to_nullptr<T>::value, "T cannot be compared to nullptr.");
constexpr AlwaysNull() = default;
AlwaysNull(const AlwaysNull& other) = default;
AlwaysNull& operator=(const AlwaysNull& other) = default;
constexpr operator T() const = delete;
constexpr decltype(auto) operator->() const = delete;
constexpr decltype(auto) operator*() const = delete;
// prevents compilation when someone attempts to assign a null pointer constant
AlwaysNull(std::nullptr_t) = delete;
AlwaysNull& operator=(std::nullptr_t) = delete;
// unwanted operators...pointers only point to single objects!
AlwaysNull& operator++() = delete;
AlwaysNull& operator--() = delete;
AlwaysNull operator++(int) = delete;
AlwaysNull operator--(int) = delete;
AlwaysNull& operator+=(std::ptrdiff_t) = delete;
AlwaysNull& operator-=(std::ptrdiff_t) = delete;
void operator[](std::ptrdiff_t) const = delete;
};
// more unwanted operators
template <class T, class U>
std::ptrdiff_t operator-(const AlwaysNull<T>&, const AlwaysNull<U>&) = delete;
template <class T>
AlwaysNull<T> operator-(const AlwaysNull<T>&, std::ptrdiff_t) = delete;
template <class T>
AlwaysNull<T> operator+(const AlwaysNull<T>&, std::ptrdiff_t) = delete;
template <class T>
AlwaysNull<T> operator+(std::ptrdiff_t, const AlwaysNull<T>&) = delete;
}
namespace std
{
template <class T>
struct hash<Ichor::NeverNull<T>>
{
std::size_t operator()(const Ichor::NeverNull<T>& value) const { return hash<T>{}(value.get()); }
};
} // namespace std