forked from ares-emulator/ares
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maybe.hpp
93 lines (77 loc) · 3.11 KB
/
maybe.hpp
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
#pragma once
#include <nall/traits.hpp>
namespace nall {
struct nothing_t {};
static nothing_t nothing;
struct else_t {};
template<typename T>
struct maybe {
maybe() {}
maybe(nothing_t) {}
maybe(const T& source) { operator=(source); }
maybe(T&& source) { operator=(std::move(source)); }
maybe(const maybe& source) { operator=(source); }
maybe(maybe&& source) { operator=(std::move(source)); }
~maybe() { reset(); }
auto operator=(nothing_t) -> maybe& { reset(); return *this; }
auto operator=(const T& source) -> maybe& { reset(); _valid = true; new(&_value.t) T(source); return *this; }
auto operator=(T&& source) -> maybe& { reset(); _valid = true; new(&_value.t) T(std::move(source)); return *this; }
auto operator=(const maybe& source) -> maybe& {
if(this == &source) return *this;
reset();
if(_valid = source._valid) new(&_value.t) T(source.get());
return *this;
}
auto operator=(maybe&& source) -> maybe& {
if(this == &source) return *this;
reset();
if(_valid = source._valid) new(&_value.t) T(std::move(source.get()));
return *this;
}
explicit operator bool() const { return _valid; }
auto reset() -> void { if(_valid) { _value.t.~T(); _valid = false; } }
auto data() -> T* { return _valid ? &_value.t : nullptr; }
auto get() -> T& { assert(_valid); return _value.t; }
auto data() const -> const T* { return ((maybe*)this)->data(); }
auto get() const -> const T& { return ((maybe*)this)->get(); }
auto operator->() -> T* { return data(); }
auto operator->() const -> const T* { return data(); }
auto operator*() -> T& { return get(); }
auto operator*() const -> const T& { return get(); }
auto operator()() -> T& { return get(); }
auto operator()() const -> const T& { return get(); }
auto operator()(const T& invalid) const -> const T& { return _valid ? get() : invalid; }
private:
union U {
T t;
U() {}
~U() {}
} _value;
bool _valid = false;
};
template<typename T>
struct maybe<T&> {
maybe() : _value(nullptr) {}
maybe(nothing_t) : _value(nullptr) {}
maybe(const T& source) : _value((T*)&source) {}
maybe(const maybe& source) : _value(source._value) {}
auto operator=(nothing_t) -> maybe& { _value = nullptr; return *this; }
auto operator=(const T& source) -> maybe& { _value = (T*)&source; return *this; }
auto operator=(const maybe& source) -> maybe& { _value = source._value; return *this; }
explicit operator bool() const { return _value; }
auto reset() -> void { _value = nullptr; }
auto data() -> T* { return _value; }
auto get() -> T& { assert(_value); return *_value; }
auto data() const -> const T* { return ((maybe*)this)->data(); }
auto get() const -> const T& { return ((maybe*)this)->get(); }
auto operator->() -> T* { return data(); }
auto operator->() const -> const T* { return data(); }
auto operator*() -> T& { return get(); }
auto operator*() const -> const T& { return get(); }
auto operator()() -> T& { return get(); }
auto operator()() const -> const T& { return get(); }
auto operator()(const T& invalid) const -> const T& { return _value ? get() : invalid; }
private:
T* _value;
};
}