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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cstdlib>
#include <type_traits>
#include <utility>
namespace android::ftl {
// Enforces and documents non-null pre/post-condition for (raw or smart) pointers.
//
// void get_length(const ftl::NonNull<std::shared_ptr<std::string>>& string_ptr,
// ftl::NonNull<std::size_t*> length_ptr) {
// // No need for `nullptr` checks.
// *length_ptr = string_ptr->length();
// }
//
// const auto string_ptr = ftl::as_non_null(std::make_shared<std::string>("android"));
// std::size_t size;
// get_length(string_ptr, ftl::as_non_null(&size));
// assert(size == 7u);
//
// For compatibility with std::unique_ptr<T> and performance with std::shared_ptr<T>, move
// operations are allowed despite breaking the invariant:
//
// using Pair = std::pair<ftl::NonNull<std::shared_ptr<int>>, std::shared_ptr<int>>;
//
// Pair dupe_if(ftl::NonNull<std::unique_ptr<int>> non_null_ptr, bool condition) {
// // Move the underlying pointer out, so `non_null_ptr` must not be accessed after this point.
// auto unique_ptr = std::move(non_null_ptr).take();
//
// auto non_null_shared_ptr = ftl::as_non_null(std::shared_ptr<int>(std::move(unique_ptr)));
// auto nullable_shared_ptr = condition ? non_null_shared_ptr.get() : nullptr;
//
// return {std::move(non_null_shared_ptr), std::move(nullable_shared_ptr)};
// }
//
// auto ptr = ftl::as_non_null(std::make_unique<int>(42));
// const auto [ptr1, ptr2] = dupe_if(std::move(ptr), true);
// assert(ptr1.get() == ptr2);
//
template <typename Pointer>
class NonNull final {
struct Passkey {};
public:
// Disallow `nullptr` explicitly for clear compilation errors.
NonNull() = delete;
NonNull(std::nullptr_t) = delete;
// Copy operations.
constexpr NonNull(const NonNull&) = default;
constexpr NonNull& operator=(const NonNull&) = default;
template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, Pointer>>>
constexpr NonNull(const NonNull<U>& other) : pointer_(other.get()) {}
template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, Pointer>>>
constexpr NonNull& operator=(const NonNull<U>& other) {
pointer_ = other.get();
return *this;
}
[[nodiscard]] constexpr const Pointer& get() const { return pointer_; }
[[nodiscard]] constexpr explicit operator const Pointer&() const { return get(); }
// Move operations. These break the invariant, so care must be taken to avoid subsequent access.
constexpr NonNull(NonNull&&) = default;
constexpr NonNull& operator=(NonNull&&) = default;
[[nodiscard]] constexpr Pointer take() && { return std::move(pointer_); }
[[nodiscard]] constexpr explicit operator Pointer() && { return take(); }
// Dereferencing.
[[nodiscard]] constexpr decltype(auto) operator*() const { return *get(); }
[[nodiscard]] constexpr decltype(auto) operator->() const { return get(); }
[[nodiscard]] constexpr explicit operator bool() const { return !(pointer_ == nullptr); }
// Private constructor for ftl::as_non_null. Excluded from candidate constructors for conversions
// through the passkey idiom, for clear compilation errors.
template <typename P>
constexpr NonNull(Passkey, P&& pointer) : pointer_(std::forward<P>(pointer)) {
if (pointer_ == nullptr) std::abort();
}
private:
template <typename P>
friend constexpr auto as_non_null(P&&) -> NonNull<std::decay_t<P>>;
Pointer pointer_;
};
template <typename P>
[[nodiscard]] constexpr auto as_non_null(P&& pointer) -> NonNull<std::decay_t<P>> {
using Passkey = typename NonNull<std::decay_t<P>>::Passkey;
return {Passkey{}, std::forward<P>(pointer)};
}
// NonNull<P> <=> NonNull<Q>
template <typename P, typename Q>
constexpr bool operator==(const NonNull<P>& lhs, const NonNull<Q>& rhs) {
return lhs.get() == rhs.get();
}
template <typename P, typename Q>
constexpr bool operator!=(const NonNull<P>& lhs, const NonNull<Q>& rhs) {
return !operator==(lhs, rhs);
}
template <typename P, typename Q>
constexpr bool operator<(const NonNull<P>& lhs, const NonNull<Q>& rhs) {
return lhs.get() < rhs.get();
}
template <typename P, typename Q>
constexpr bool operator<=(const NonNull<P>& lhs, const NonNull<Q>& rhs) {
return lhs.get() <= rhs.get();
}
template <typename P, typename Q>
constexpr bool operator>=(const NonNull<P>& lhs, const NonNull<Q>& rhs) {
return lhs.get() >= rhs.get();
}
template <typename P, typename Q>
constexpr bool operator>(const NonNull<P>& lhs, const NonNull<Q>& rhs) {
return lhs.get() > rhs.get();
}
// NonNull<P> <=> Q
template <typename P, typename Q>
constexpr bool operator==(const NonNull<P>& lhs, const Q& rhs) {
return lhs.get() == rhs;
}
template <typename P, typename Q>
constexpr bool operator!=(const NonNull<P>& lhs, const Q& rhs) {
return lhs.get() != rhs;
}
template <typename P, typename Q>
constexpr bool operator<(const NonNull<P>& lhs, const Q& rhs) {
return lhs.get() < rhs;
}
template <typename P, typename Q>
constexpr bool operator<=(const NonNull<P>& lhs, const Q& rhs) {
return lhs.get() <= rhs;
}
template <typename P, typename Q>
constexpr bool operator>=(const NonNull<P>& lhs, const Q& rhs) {
return lhs.get() >= rhs;
}
template <typename P, typename Q>
constexpr bool operator>(const NonNull<P>& lhs, const Q& rhs) {
return lhs.get() > rhs;
}
// P <=> NonNull<Q>
template <typename P, typename Q>
constexpr bool operator==(const P& lhs, const NonNull<Q>& rhs) {
return lhs == rhs.get();
}
template <typename P, typename Q>
constexpr bool operator!=(const P& lhs, const NonNull<Q>& rhs) {
return lhs != rhs.get();
}
template <typename P, typename Q>
constexpr bool operator<(const P& lhs, const NonNull<Q>& rhs) {
return lhs < rhs.get();
}
template <typename P, typename Q>
constexpr bool operator<=(const P& lhs, const NonNull<Q>& rhs) {
return lhs <= rhs.get();
}
template <typename P, typename Q>
constexpr bool operator>=(const P& lhs, const NonNull<Q>& rhs) {
return lhs >= rhs.get();
}
template <typename P, typename Q>
constexpr bool operator>(const P& lhs, const NonNull<Q>& rhs) {
return lhs > rhs.get();
}
} // namespace android::ftl
// Specialize std::hash for ftl::NonNull<T>
template <typename P>
struct std::hash<android::ftl::NonNull<P>> {
std::size_t operator()(const android::ftl::NonNull<P>& ptr) const {
return std::hash<P>()(ptr.get());
}
};
|