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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
* This file is part of the source code of the software program
* Vampire. It is protected by applicable
* copyright laws.
*
* This source code is distributed under the licence found here
* https://vprover.github.io/license.html
* and in the source directory
*/
/**
* This file mainly defined the class Option, which can be thought of as a NULLable pointer, that is
* stack-allocated, with RAII semantics.
*
* \see UnitTests/tOption.cpp for examples of the usage
*/
#ifndef __OPTIONAL_H__
#define __OPTIONAL_H__
#include <iosfwd>
#include <type_traits>
#include "Debug/Assertion.hpp"
#include "Lib/Reflection.hpp"
namespace Lib {
template<
class T,
typename std::enable_if<std::is_reference<T>::value, bool>::type = true
>
T move_if_value(std::remove_reference_t<T> && t)
{ return t; }
template<
class T,
typename std::enable_if<std::is_reference<T>::value, bool>::type = true
>
T move_if_value(std::remove_reference_t<T>& t)
{ return t; }
template<
class T,
typename std::enable_if< !std::is_reference<T>::value , bool>::type = true
>
T move_if_value(T& t)
{ return std::move(t); }
template<
class T,
typename std::enable_if< !std::is_reference<T>::value , bool>::type = true
>
T move_if_value(T const& t)
{ return std::move(t); }
template<
class T,
typename std::enable_if< !std::is_reference<T>::value , bool>::type = true
>
T move_if_value(T && t)
{ return std::move(t); }
#define FOR_REF_QUALIFIER(macro) \
macro(const &, ) macro(&, ) macro(&&, std::move)
template<class T>
struct MaybeUninit {
union Value {
T init; int uninint[0];
Value() : uninint{} {};
~Value() {};
} _elem;
MaybeUninit() : _elem() {}
~MaybeUninit() {}
// This macro will b expanded for (REF,MV) in { (`&&`, `std::move`), (`&`, ``), (`const &`, ``) }
#define methods(REF, MV) \
operator T REF() REF \
{ return MV(_elem.init); } \
\
void init(T REF content) \
{ ::new(&_elem.init)T(MV(content)); } \
\
MaybeUninit& operator=(T REF content) \
{ \
_elem.init = MV(content); \
return *this; \
} \
FOR_REF_QUALIFIER(methods)
#undef methods
};
template<class A>
class OptionBase
{
bool _isSome;
MaybeUninit<A> _elem;
public:
constexpr OptionBase() : _isSome(false) {}
~OptionBase()
{
if (isSome()) {
unwrap().~A();
}
}
#define methods(REF, MV) \
constexpr explicit OptionBase(A REF content) \
: _isSome(true) \
, _elem() \
{ \
_elem.init(move_if_value<A>(content)); \
} \
\
A REF unwrap() REF \
{ \
ASS(_isSome); \
return MV(_elem); \
} \
\
OptionBase(OptionBase REF a) : _isSome(a._isSome) \
{ \
if (isSome()) { \
_elem.init(MV(a).unwrap()); \
} \
} \
FOR_REF_QUALIFIER(methods)
#undef methods
OptionBase& operator=(OptionBase&& other)
{
if (_isSome) {
unwrap().~A();
}
if (other._isSome) {
_elem.init(move_if_value<A>(other.unwrap()));
}
_isSome = other._isSome;
return *this;
}
OptionBase& operator=(OptionBase const& other)
{
if (_isSome) {
unwrap().~A();
}
if (other._isSome) {
_elem.init(other.unwrap());
}
_isSome = other._isSome;
return *this;
}
bool isSome() const { return _isSome; }
bool isNone() const { return !isSome(); }
static OptionBase fromPtr(A* ptr)
{ return ptr == nullptr ? OptionBase() : OptionBase(*ptr); }
friend bool operator==(OptionBase const& lhs, OptionBase const& rhs)
{
if (lhs._isSome != rhs._isSome) return false;
if (lhs._isSome) {
return lhs.unwrap() == rhs.unwrap();
} else {
return true;
}
}
};
template<class Ptr>
class OptionBaseRef
{
Ptr _elem;
public:
constexpr OptionBaseRef( ) : _elem(nullptr) { }
constexpr OptionBaseRef(Ptr content) : _elem(content) { }
bool isSome() const { return _elem != nullptr; }
auto unwrap() const& -> decltype(auto) { ASS(isSome()); return *_elem ; }
auto unwrap() && -> decltype(auto) { ASS(isSome()); return std::move(*_elem); }
auto unwrap() & -> decltype(auto) { ASS(isSome()); return *_elem ; }
constexpr OptionBaseRef(OptionBaseRef & a) = default;
constexpr OptionBaseRef(OptionBaseRef && a) = default;
constexpr OptionBaseRef(OptionBaseRef const& a) = default;
OptionBaseRef& operator=(OptionBaseRef & a) = default;
OptionBaseRef& operator=(OptionBaseRef && a) = default;
OptionBaseRef& operator=(OptionBaseRef const& a) = default;
// static OptionBaseRef fromPtr(A* ptr)
// { return ptr == nullptr ? OptionBaseRef() : *ptr; }
friend bool operator==(OptionBaseRef const& lhs, OptionBaseRef const& rhs)
{ return (lhs._elem == nullptr && rhs._elem == nullptr)
|| (lhs._elem != nullptr && rhs._elem != nullptr && (lhs._elem == rhs._elem || *lhs._elem == *rhs._elem )); }
};
template<class A>
class OptionBase<A const&> : public OptionBaseRef<A const*>
{
public:
constexpr OptionBase() : OptionBaseRef<A const*>() {}
constexpr OptionBase(A const& item) : OptionBaseRef<A const*>(&item) {}
constexpr OptionBase(OptionBase const& b) : OptionBaseRef<A const*>(b) {}
};
template<class A>
class OptionBase<A&> : public OptionBaseRef<A*>
{
public:
constexpr OptionBase() : OptionBaseRef<A*>() {}
constexpr OptionBase(A& item) : OptionBaseRef<A*>(&item) {}
constexpr OptionBase(OptionBase const& b) : OptionBaseRef<A*>(b) {}
};
/** The actual Option class
* An Option<A> is a class that holds either a value of type A, or is none/empty.
* It can be thought of a nullable pointer, that has the advantage that does not need to be allocated
* in a separate structure, and does not expose any uninitialized memory to the user. Further it
* automatically calls the destructor when it goes out of scope.
*
* \see UnitTests/tOption.cpp for usage examples
*/
template<class A>
class Option : OptionBase<A> {
explicit constexpr Option(OptionBase<A>&& base) : OptionBase<A>(std::move(base)) { }
public:
using Content = A;
/** constructs an option from a value of type A&, A const&, or A&&. */
using OptionBase<A>::OptionBase;
/** checks whether the Option holds a value */
using OptionBase<A>::isSome;
/** returns the Options value if it holds one */
using OptionBase<A>::unwrap;
friend bool operator==(Option const& lhs, Option const& rhs)
{ return static_cast<OptionBase<A>const&>(lhs) == static_cast<OptionBase<A>const&>(rhs); }
friend bool operator!=(Option const& lhs, Option const& rhs)
{ return !(lhs == rhs); }
/** creates an Option<A&>, or Option<A const&> from a pointer A*. if the pointer is NULL the option will be empty */
template<class C> static Option<A> fromPtr(C self)
{ return Option(OptionBase<A>::fromPtr(self)); }
/** checks whether the option is empty */
bool isNone() const { return !this->isSome(); }
operator bool() const { return isSome(); }
template<class T, std::enable_if_t<!std::is_same_v<T, bool>, bool> = true>
operator T() const = delete;
A const& operator*() const { return unwrap(); }
A & operator*() { return unwrap(); }
std::remove_reference_t<A> const* operator->() const { return &unwrap(); }
std::remove_reference_t<A> * operator->() { return &unwrap(); }
std::remove_reference_t<A> * asPtr() { return isSome() ? &unwrap() : nullptr; }
std::remove_reference_t<A> const* asPtr() const { return isSome() ? &unwrap() : nullptr; }
Option take()
{
Option out;
std::swap(*this,out);
return out;
}
/**
* returns the value held by this option if there is one, or calls the given function f without arguments,
* initializes the closuer with the returned value, and returns a reference to the value afterwards.
*/
template<class Clsr>
A& unwrapOrInit(Clsr f) {
if (isNone()) {
::new(this) Option(f());
}
return this->unwrap();
}
#define ref_polymorphic(REF, MOVE, MOVE_IF_VALUE) \
\
/** \
* applies the given function to the value of this option and returns an option of the return type. \
* if the Option was None an empty option of the function's return type is returned. \
*/ \
template<class Clsr> \
Option<typename std::invoke_result<Clsr, A REF>::type> map(Clsr clsr) REF { \
using OptOut = Option<typename std::invoke_result<Clsr, A REF>::type>; \
return this->isSome() ? OptOut(clsr(MOVE_IF_VALUE(unwrap(), A))) \
: OptOut(); \
} \
\
/** \
* if the Option holds a value the first function is applied to the value. \
* if the Option is none the second function is called without arguments and the result is returned.\
* \pre both CaseSome and CaseNone must have the same return type \
*/ \
template<class CaseSome, class CaseNone> \
typename std::invoke_result<CaseSome, A REF>::type match(CaseSome present, CaseNone none) REF { \
if (this->isSome()) { \
return present(MOVE((*this)).unwrap()); \
} else { \
return none(); \
} \
} \
\
/** \
* returns the value held by this option if there is one, or returns the value alt otherwise \
*/ \
A REF unwrapOr(A REF alt) REF { \
if (this->isSome()) { \
return MOVE(*this).unwrap(); \
} else { \
return MOVE(alt); \
} \
} \
\
/** \
* returns the value held by this option if there is one, or calls the given function f without arguments \
* and returns the value otherwise. \
*/ \
template<class Clsr> \
A unwrapOrElse(Clsr f) REF { \
if (this->isSome()) { \
return MOVE(*this).unwrap(); \
} else { \
return f(); \
} \
} \
\
/** \
* Returns this, if this is Some, or uses the closure to create an alternative option if this is None. \
*/ \
template<class Clsr, \
typename std::enable_if<std::is_same< typename std::invoke_result<Clsr>::type \
, Option \
>::value \
, bool \
>::type = true \
> \
auto orElse(Clsr clsr) REF -> Option \
{ return this->isSome() ? MOVE(*this) : clsr(); } \
\
/** Returns the value of this, if this is Some, or uses the closure to create a value otherwise. */\
template<class Clsr, \
typename std::enable_if<std::is_same< typename std::invoke_result<Clsr>::type \
, A \
>::value \
, bool \
>::type = true \
> \
auto orElse(Clsr clsr) REF -> A \
{ return this->isSome() ? MOVE(*this).unwrap() : clsr(); } \
\
/** \
* applies a function to the value of this closure if there is one. the function is expected to return\
* another option. the resulting Option<Option<Result>> will then be flattened to an Option<Result>.\
* \
* This function is the same as flatMap/andThen/(>>=) in other programming languages with monads.\
*/ \
template<class Clsr> \
typename std::invoke_result<Clsr, A REF>::type andThen(Clsr clsr) REF { \
using OptOut = typename std::invoke_result<Clsr, A REF>::type; \
return this->isSome() ? clsr(MOVE(*this).unwrap()) \
: OptOut(); \
} \
\
template<class Clsr> auto flatMap(Clsr clsr) REF { return andThen(clsr); } \
\
template<class Pred> \
Option filter(Pred p) REF { \
return isSome() && p(unwrap()) \
? MOVE(*this) \
: Option(); \
} \
\
#define MV_IF_VAL(e, ...) move_if_value<__VA_ARGS__>(e)
#define NO_MV_IF_VAL(e, ...) e
ref_polymorphic( &, , NO_MV_IF_VAL)
ref_polymorphic(const&, , NO_MV_IF_VAL)
ref_polymorphic( &&, std::move, MV_IF_VAL)
#undef ref_polymorphic
/**
* turns an Option<A&>, Option<A const&>, or Option<A&&> into an Option<A> by calling the
* appropriate move or copy constructor.
*/
Option<typename std::remove_const<typename std::remove_reference<A>::type>::type> toOwned() const&
{
using Out = typename std::remove_const<typename std::remove_reference<A>::type>::type;
return map([](A elem) -> Out { return Out(std::move(elem)); });
}
/**
* turns an Option<A&>, Option<A const&>, or Option<A&&> into an Option<A> by calling the
* appropriate move or copy constructor.
*/
Option<typename std::remove_const<typename std::remove_reference<A>::type>::type> toOwned() &&
{
using Out = typename std::remove_const<typename std::remove_reference<A>::type>::type;
return map([](A elem) -> Out { return Out(move_if_value<A>(elem)); });
}
class OptionIter {
Option _self;
OptionIter(Option self) : _self(std::move(self)) {}
public:
friend class Option;
DECL_ELEMENT_TYPE(A);
inline bool hasNext() const { return _self.isSome(); }
inline bool hasNext() { return _self.isSome(); }
inline A next() { return _self.take().unwrap(); }
};
Option<A const&> asRef() const { return someIf(isSome(), [&]() -> decltype(auto) { return unwrap(); }); }
Option<A &> asRef() { return someIf(isSome(), [&]() -> decltype(auto) { return unwrap(); }); }
OptionIter intoIter() &&
{ return OptionIter(std::move(*this)); }
auto iter() const& { return asRef().intoIter(); }
auto iter() & { return asRef().intoIter(); }
friend std::ostream& operator<<(std::ostream& out, Option const& self)
{ return self.isSome() ? out << self.unwrap() : out << "None"; }
auto flatten() & { return andThen([](auto x) { return std::move(x); }); }
auto flatten() && { return std::move(*this).andThen([](auto x) { return std::move(x); }); }
auto flatten() const& { return andThen([](auto x) { return std::move(x); }); }
friend bool operator<(Option const& lhs, Option const& rhs)
{
if (lhs.isSome() < rhs.isSome()) {
return true;
} else if (lhs.isSome() > rhs.isSome()) {
return false;
} else if (lhs.isNone()) {
ASS(rhs.isNone())
return false;
} else {
ASS(rhs.isSome())
ASS(lhs.isSome())
return lhs.unwrap() < rhs.unwrap();
}
}
};
template<class F>
auto someIf(bool condition, F create) -> Option<decltype(create())>
{ return condition ? Option<decltype(create())>(create())
: Option<decltype(create())>(); }
template<class T> constexpr Option<T> some(T const& t) { return Option<T>(t); }
template<class T> constexpr Option<T> some(T & t) { return Option<T>(t); }
template<class T> constexpr Option<T> some(T && t) { return Option<T>(std::move(t)); }
template<class T> constexpr Option<T> none() { return Option<T>(); }
template<class T> constexpr Option<T> optionalFromPtr(T* t) { return Option<T>::fromPtr(t); }
template<class T> constexpr Option<T> optionFromPtr(T* t) { return Option<T>::fromPtr(t); }
template<class T>
T operator||(Option<T> t, T c)
{ return std::move(t).unwrapOr(std::move(c)); }
template<class T, class Clsr>
auto operator||(Option<T> t, Clsr f) -> decltype(f())
{ return std::move(t).orElse(f); }
template<class T>
Option<T> operator||(Option<T> t, Option<T> c)
{ return std::move(t).orElse([&](){ return std::move(c); }); }
template<class T, class Clsr>
Option<T> operator&&(Option<T> t, Clsr c)
{ return std::move(t).andThen(c); }
template<class T>
Option<T> operator&&(Option<T> t, Option<T> c)
{ return std::move(t).andThen([&](){ return std::move(c); }); }
} // namespace Lib
#endif // __OPTIONAL_H__