-
Notifications
You must be signed in to change notification settings - Fork 49
/
optional.h
394 lines (343 loc) · 12.5 KB
/
optional.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// think-cell public library
//
// Copyright (C) 2016-2023 think-cell Software GmbH
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
#pragma once
#include "base/assert_defs.h"
#include "base/conditional.h"
#include "base/utility.h"
#include "base/as_lvalue.h"
#include "base/explicit_cast_fwd.h"
#include "base/scope.h"
#include "range/meta.h"
#include "storage_for.h"
#include <optional>
namespace tc {
// TODO implement fully functional tc::optional
// In most std::optional implementations, the "holding a value" flag is false during con- and destruction.
// For them it actually doesn't matter, because querying the status from inside is undefined according to 16.4.6.9 [reentrancy]
// > Except where explicitly specified in this document, it is implementation-defined which functions in the C++ standard library may be recursively reentered.
// We explicitly want optionals to be accessible during con- and destruction so we implement this custom type
namespace no_adl {
template<typename T>
struct optional {
using value_type = T;
constexpr optional() noexcept : m_bValue(false) {
}
constexpr optional(std::nullopt_t) noexcept : m_bValue(false) {
}
constexpr optional(optional const& rhs) noexcept(noexcept(construct(*rhs.m_oValue)))
: m_bValue(rhs.m_bValue) {
if (m_bValue) {
construct(*rhs.m_oValue);
}
}
template<typename... Args>
optional(std::in_place_t, Args&& ... args) noexcept(noexcept(construct(tc_move_if_owned(args)...)))
: m_bValue(true) {
construct(tc_move_if_owned(args)...);
}
~optional() {
reset();
}
void reset() & noexcept {
if (tc::change(m_bValue, false)) { // allow querying the flag to prevent double-destruction
#ifdef _CHECKS
_ASSERT(!m_bInsideDtor);
tc_scoped_assign(m_bInsideDtor, true);
#endif
m_oValue.dtor();
}
}
template<typename... Args>
T& emplace(Args&& ... args) & noexcept(noexcept(construct(tc_move_if_owned(args)...))) {
reset();
VERIFY(tc::change(m_bValue, true));
construct(tc_move_if_owned(args)...);
return *m_oValue;
}
optional& operator=(std::nullopt_t) & noexcept {
reset();
return *this;
}
constexpr bool has_value() const& noexcept {
return m_bValue;
}
constexpr explicit operator bool() const& noexcept {
return has_value();
}
T const* operator->() const& noexcept {
_ASSERT(*this || m_bInsideDtor);
return m_oValue.operator->();
}
T* operator->() & noexcept {
_ASSERT(*this || m_bInsideDtor);
return m_oValue.operator->();
}
T const& operator*() const& noexcept {
_ASSERT(*this || m_bInsideDtor);
return *m_oValue;
}
T const&& operator*() const&& noexcept {
_ASSERT(*this || m_bInsideDtor);
return *tc_move(m_oValue);
}
T& operator*() & noexcept {
_ASSERT(*this || m_bInsideDtor);
return *m_oValue;
}
T&& operator*() && noexcept {
_ASSERT(*this || m_bInsideDtor);
return *tc_move(m_oValue);
}
private:
bool m_bValue;
#ifdef _CHECKS
// The constructor is allowed to be re-entrant, the destructor is not
bool m_bInsideDtor = false;
#endif
tc::storage_for<T> m_oValue;
template<typename... Args>
void construct(Args&& ... args) & noexcept(noexcept(std::declval<storage_for<T>&>().ctor(tc_move_if_owned(args)...))) {
_ASSERT(m_bValue); // allow querying the flag to prevent double-construction
_ASSERT(!m_bInsideDtor);
if constexpr (noexcept(m_oValue.ctor(tc_move_if_owned(args)...))) {
m_oValue.ctor(tc_move_if_owned(args)...);
} else {
try {
m_oValue.ctor(tc_move_if_owned(args)...); // MAYTHROW
} catch( ... ) {
m_bValue = false;
throw; // MSVC and gcc: throw in catch block of a noexcept function triggers warning even if try block never throws
}
}
}
};
template<tc::empty_type TEmpty>
struct optional<TEmpty> {
constexpr optional() noexcept = default;
constexpr optional(std::nullopt_t) noexcept {}
template<typename... Args>
constexpr optional(std::in_place_t, Args&& ... args) noexcept(std::is_nothrow_constructible_v<TEmpty, Args&&...>) : m_bValue(true) {
tc::discard(TEmpty(tc_move_if_owned(args)...));
}
constexpr void reset() & noexcept {
m_bValue = false;
}
template<typename... Args>
constexpr TEmpty emplace(Args&& ... args) & noexcept(std::is_nothrow_constructible_v<TEmpty, Args&&...>) {
TEmpty result(tc_move_if_owned(args)...);
m_bValue = true;
return result;
}
constexpr optional& operator=(std::nullopt_t) & noexcept {
reset();
return *this;
}
constexpr bool has_value() const& noexcept {
return m_bValue;
}
constexpr explicit operator bool() const& noexcept {
return has_value();
}
private:
struct pointer final {
TEmpty m_t;
constexpr TEmpty* operator->() && noexcept { return std::addressof(m_t); }
};
public:
constexpr auto operator->() const& noexcept {
_ASSERT(*this);
return pointer();
}
constexpr TEmpty operator*() const& noexcept {
_ASSERT(*this);
return TEmpty();
}
private:
bool m_bValue = false;
};
template<typename TRef> requires std::is_reference<TRef>::value
struct optional<TRef> {
template<typename U>
friend struct optional;
constexpr optional() noexcept = default;
constexpr optional(std::nullopt_t) noexcept {}
template<typename U> requires tc::safely_constructible_from<TRef, U&&>
constexpr optional(U&& u) noexcept(noexcept(static_cast<TRef>(tc_move_if_owned(u))))
: m_pt(std::addressof(tc::as_lvalue(static_cast<TRef>(tc_move_if_owned(u))))) // tc::safely_constructible_from makes sure no reference to temporary is created
{}
template<typename TOptional> requires
tc::safely_constructible_from<TRef, decltype(*std::declval<TOptional>()) >
constexpr optional(TOptional&& rhs) noexcept
: m_pt([&]() noexcept {
return rhs ? std::addressof(tc::as_lvalue(*tc_move_if_owned(rhs))) : nullptr;
}())
{}
constexpr optional& operator=(std::nullopt_t) /*no &*/ noexcept {
this->reset();
return *this;
}
template<typename U> requires tc::safely_constructible_from<TRef, U&&>
constexpr TRef emplace(U&& u) /*no &*/ noexcept(noexcept(static_cast<TRef>(tc_move_if_owned(u)))) {
m_pt = std::addressof(tc::as_lvalue(static_cast<TRef>(tc_move_if_owned(u)))); // tc::safely_constructible_from makes sure no reference to temporary is created
return *m_pt;
}
constexpr void reset() /*no &*/ noexcept {
m_pt=nullptr;
}
constexpr bool has_value() const& noexcept {
return m_pt;
}
constexpr explicit operator bool() const& noexcept {
return has_value();
}
constexpr std::remove_reference_t<TRef>& operator*() const& noexcept {
_ASSERT(*this);
return *m_pt;
}
constexpr TRef operator*() const&& noexcept {
_ASSERT(*this);
return static_cast<TRef>(*m_pt);
}
constexpr auto operator->() const& noexcept {
_ASSERT(*this);
return m_pt;
}
private:
std::remove_reference_t<TRef>* m_pt = nullptr;
};
}
using no_adl::optional;
template <typename T>
using optional_reference_or_value = std::conditional_t<
tc::empty_type<std::remove_cvref_t<T>> || std::is_rvalue_reference<T&&>::value,
tc::optional<std::remove_cvref_t<T>>,
tc::optional<T>
>;
template <typename T> requires (!tc::empty_type<T>)
[[nodiscard]] constexpr optional_reference_or_value<T&> make_optional_reference_or_value(T& ref) noexcept {
return ref;
}
template <typename T> requires tc::empty_type<std::remove_cvref_t<T>> || std::is_rvalue_reference<T&&>::value
[[nodiscard]] constexpr optional_reference_or_value<T> make_optional_reference_or_value(T&& ref) noexcept {
return {std::in_place, tc_move_if_owned(ref)};
}
}
namespace tc {
template<typename Optional, typename T>
[[nodiscard]] constexpr decltype(auto) value_or( Optional&& optional, T&& t ) MAYTHROW {
if constexpr( tc::instance_or_derived<std::remove_reference_t<T>, tc::make_lazy> ) {
static_assert( !tc::has_actual_common_reference<decltype(*std::declval<Optional>()), T&&> ); // Should value_or(std::optional<make_lazy<T>>(), make_lazy<T>()) return make_lazy<T>&& or T?
return tc_conditional_prvalue_as_val(optional, *tc_move_if_owned(optional), tc_move_if_owned(t)());
} else {
return tc_conditional_rvalue_as_ref(optional, *tc_move_if_owned(optional), tc_move_if_owned(t));
}
}
template<typename Func>
auto not_singleton_or(auto&& t, Func func) noexcept->decltype(func()) {
if( tc::explicit_cast<bool>(t) ) {
return tc_move_if_owned(t);
} else {
return func();
}
}
template<typename Exception>
decltype(auto) throw_if_null(auto&& t) THROW(Exception) {
if( tc::explicit_cast<bool>(t) ) {
return tc_move_if_owned(t);
} else {
throw Exception();
}
}
//-------------------------------------------------------------------------------------------------------------------------
// and_then
namespace and_then_detail {
template<typename T, typename TypeList, typename=void>
struct and_then_result_impl /*not final*/ {};
template<typename T>
struct and_then_result_impl<T, tc::type::list<>> /*not final*/: tc::type::identity<tc::decay_t<T>> {};
template<>
struct and_then_result_impl<void, tc::type::list<>> /*not final*/: tc::type::identity<bool> {};
TC_HAS_EXPR(dereference_operator, (T), *std::declval<T>())
template<typename Func>
constexpr auto invoke(Func&& func, tc::unused) return_decltype_xvalue_by_ref_MAYTHROW(
tc::invoke(tc_move_if_owned(func)) // MAYTHROW
)
template<typename Func, has_dereference_operator T>
constexpr auto invoke(Func&& func, T&& t) return_decltype_xvalue_by_ref_MAYTHROW(
tc::invoke(tc_move_if_owned(func), *tc_move_if_owned(t)) // MAYTHROW
)
template<typename T, typename Func, typename ...FuncTail>
struct and_then_result_impl<
T,
tc::type::list<Func, FuncTail...>,
decltype(tc::and_then_detail::invoke(std::declval<std::remove_reference_t<Func>>(), std::declval<T>()), void())
> /*not final*/: and_then_result_impl<
decltype(tc::and_then_detail::invoke(std::declval<std::remove_reference_t<Func>>(), std::declval<T>())),
tc::type::list<FuncTail...>
> {};
template<typename T, typename Func, typename... FuncTail>
struct and_then_result final: and_then_result_impl<T, tc::type::list<Func, FuncTail...>> {};
}
namespace and_then_adl {
DEFINE_ADL_TAG_TYPE(adl_tag);
template<typename T, typename Func>
auto and_then_impl(adl_tag_t, T&& t, Func func) noexcept(noexcept(tc::and_then_detail::invoke(tc_move(func), tc_move_if_owned(t))))
-> typename tc::and_then_detail::and_then_result<T, Func>::type
{
if(t) {
if constexpr(std::is_void<decltype(tc::and_then_detail::invoke(tc_move(func), tc_move_if_owned(t)))>::value) {
tc::and_then_detail::invoke(tc_move(func), tc_move_if_owned(t)); // MAYTHROW
return true;
} else {
return tc::and_then_detail::invoke(tc_move(func), tc_move_if_owned(t)); // MAYTHROW
}
} else {
return {};
}
}
template<typename T, typename Func, typename ...FuncTail>
auto and_then_impl(adl_tag_t, T&& t, Func func, FuncTail&& ...funcTail) noexcept(noexcept(
and_then_impl(adl_tag, tc::and_then_detail::invoke(tc_move(func), tc_move_if_owned(t)), tc_move_if_owned(funcTail)...)
)) // noexcept operator cannot see and_then_impl itself without ADL
-> typename tc::and_then_detail::and_then_result<T, Func, FuncTail...>::type
{
if(t) {
return and_then_impl( // MAYTHROW
adl_tag,
tc::and_then_detail::invoke(tc_move(func), tc_move_if_owned(t)), // MAYTHROW
tc_move_if_owned(funcTail)...
);
} else {
return {};
}
}
}
template<typename T, typename... Func>
auto and_then(T&& t, Func&&... func) return_decltype_MAYTHROW(
tc::and_then_adl::and_then_impl(tc::and_then_adl::adl_tag, tc_move_if_owned(t), tc_move_if_owned(func)...)
)
// For `tc::and_then(opt, tc::chained(fn_make_optional, f))`.
DEFINE_FN2(std::make_optional, fn_make_optional);
}
namespace tc::begin_end_adl {
template<typename T>
constexpr T* begin(begin_end_tag_t, std::optional<T>& ot) noexcept {
return ot ? std::addressof(*ot) : nullptr;
}
template<typename T>
constexpr T const* begin(begin_end_tag_t, std::optional<T> const& ot) noexcept {
return ot ? std::addressof(*ot) : nullptr;
}
template<typename T>
constexpr T* end(begin_end_tag_t, std::optional<T>& ot) noexcept {
return ot ? std::addressof(*ot)+1 : nullptr;
}
template<typename T>
constexpr T const* end(begin_end_tag_t, std::optional<T> const& ot) noexcept {
return ot ? std::addressof(*ot)+1 : nullptr;
}
}