blob: d50c24f0248199eb1be4e1aae01790e868898f9f (
plain)
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
|
#ifndef optional_hh_INCLUDED
#define optional_hh_INCLUDED
#include "assert.hh"
#include <utility>
namespace Kakoune
{
template<typename T>
struct Optional
{
public:
constexpr Optional() : m_valid{false} {}
Optional(const T& other) : m_valid{true} { new (&m_value) T(other); }
Optional(T&& other) : m_valid{true} { new (&m_value) T(std::move(other)); }
Optional(const Optional& other)
: m_valid{other.m_valid}
{
if (m_valid)
new (&m_value) T(other.m_value);
}
Optional(Optional&& other)
noexcept(noexcept(new (nullptr) T(std::move(other.m_value))))
: m_valid{other.m_valid}
{
if (m_valid)
new (&m_value) T(std::move(other.m_value));
}
Optional& operator=(const Optional& other)
{
destruct_ifn();
if ((m_valid = other.m_valid))
new (&m_value) T(other.m_value);
return *this;
}
Optional& operator=(Optional&& other)
{
destruct_ifn();
if ((m_valid = other.m_valid))
new (&m_value) T(std::move(other.m_value));
return *this;
}
~Optional() { destruct_ifn(); }
constexpr explicit operator bool() const noexcept { return m_valid; }
bool operator==(const Optional& other) const
{
return m_valid == other.m_valid and
(not m_valid or m_value == other.m_value);
}
template<typename... Args>
T& emplace(Args&&... args)
{
destruct_ifn();
new (&m_value) T{std::forward<Args>(args)...};
m_valid = true;
return m_value;
}
T& operator*() &
{
kak_assert(m_valid);
return m_value;
}
T&& operator*() &&
{
kak_assert(m_valid);
return std::move(m_value);
}
const T& operator*() const & { return *const_cast<Optional&>(*this); }
const T& operator*() const && { return *const_cast<Optional&>(*this); }
T* operator->()
{
kak_assert(m_valid);
return &m_value;
}
const T* operator->() const { return const_cast<Optional&>(*this).operator->(); }
template<typename U> struct DecayOptionalImpl { using Type = U; };
template<typename U> struct DecayOptionalImpl<Optional<U>> { using Type = typename DecayOptionalImpl<U>::Type; };
template<typename U> using DecayOptional = typename DecayOptionalImpl<U>::Type;
template<typename F>
auto map(F f) -> Optional<DecayOptional<decltype(f(std::declval<T>()))>>
{
if (not m_valid)
return {};
return {f(m_value)};
}
template<typename U>
auto cast() const -> Optional<U>
{
if (not m_valid)
return {};
return {(U)m_value};
}
template<typename U>
T value_or(U&& fallback) const { return m_valid ? m_value : T{std::forward<U>(fallback)}; }
template<typename U>
T value_or_compute(U&& compute_func) const { return m_valid ? m_value : compute_func(); }
void reset() { destruct_ifn(); m_valid = false; }
private:
void destruct_ifn() { if (m_valid) m_value.~T(); }
struct Empty {};
union
{
Empty m_empty; // disable default construction of value
T m_value;
};
bool m_valid;
};
}
#endif // optional_hh_INCLUDED
|