aboutsummaryrefslogtreecommitdiffstats
path: root/common/aloptional.h
blob: 6de16799e53c7f6a75b5de4e62be82bb4d1c8657 (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
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
#ifndef AL_OPTIONAL_H
#define AL_OPTIONAL_H

#include <initializer_list>
#include <type_traits>
#include <utility>

#include "almalloc.h"

namespace al {

struct nullopt_t { };
struct in_place_t { };

constexpr nullopt_t nullopt{};
constexpr in_place_t in_place{};

#define NOEXCEPT_AS(...)  noexcept(noexcept(__VA_ARGS__))

namespace detail_ {
/* Base storage struct for an optional. Defines a trivial destructor, for types
 * that can be trivially destructed.
 */
template<typename T, bool = std::is_trivially_destructible<T>::value>
struct optstore_base {
    bool mHasValue{false};
    union {
        char mDummy{};
        T mValue;
    };

    constexpr optstore_base() noexcept { }
    template<typename ...Args>
    constexpr explicit optstore_base(in_place_t, Args&& ...args)
        noexcept(std::is_nothrow_constructible<T, Args...>::value)
        : mHasValue{true}, mValue{std::forward<Args>(args)...}
    { }
    ~optstore_base() = default;
};

/* Specialization needing a non-trivial destructor. */
template<typename T>
struct optstore_base<T, false> {
    bool mHasValue{false};
    union {
        char mDummy{};
        T mValue;
    };

    constexpr optstore_base() noexcept { }
    template<typename ...Args>
    constexpr explicit optstore_base(in_place_t, Args&& ...args)
        noexcept(std::is_nothrow_constructible<T, Args...>::value)
        : mHasValue{true}, mValue{std::forward<Args>(args)...}
    { }
    ~optstore_base() { if(mHasValue) al::destroy_at(std::addressof(mValue)); }
};

/* Next level of storage, which defines helpers to construct and destruct the
 * stored object.
 */
template<typename T>
struct optstore_helper : public optstore_base<T> {
    using optstore_base<T>::optstore_base;

    template<typename... Args>
    constexpr void construct(Args&& ...args) noexcept(std::is_nothrow_constructible<T, Args...>::value)
    {
        al::construct_at(std::addressof(this->mValue), std::forward<Args>(args)...);
        this->mHasValue = true;
    }

    constexpr void reset() noexcept
    {
        if(this->mHasValue)
            al::destroy_at(std::addressof(this->mValue));
        this->mHasValue = false;
    }

    constexpr void assign(const optstore_helper &rhs)
        noexcept(std::is_nothrow_copy_constructible<T>::value
            && std::is_nothrow_copy_assignable<T>::value)
    {
        if(!rhs.mHasValue)
            this->reset();
        else if(this->mHasValue)
            this->mValue = rhs.mValue;
        else
            this->construct(rhs.mValue);
    }

    constexpr void assign(optstore_helper&& rhs)
        noexcept(std::is_nothrow_move_constructible<T>::value
            && std::is_nothrow_move_assignable<T>::value)
    {
        if(!rhs.mHasValue)
            this->reset();
        else if(this->mHasValue)
            this->mValue = std::move(rhs.mValue);
        else
            this->construct(std::move(rhs.mValue));
    }
};

/* Define copy and move constructors and assignment operators, which may or may
 * not be trivial.
 */
template<typename T, bool trivial_copy = std::is_trivially_copy_constructible<T>::value,
    bool trivial_move = std::is_trivially_move_constructible<T>::value,
    /* Trivial assignment is dependent on trivial construction+destruction. */
    bool = trivial_copy && std::is_trivially_copy_assignable<T>::value
        && std::is_trivially_destructible<T>::value,
    bool = trivial_move && std::is_trivially_move_assignable<T>::value
        && std::is_trivially_destructible<T>::value>
struct optional_storage;

/* Some versions of GCC have issues with 'this' in the following noexcept(...)
 * statements, so this macro is a workaround.
 */
#define _this std::declval<optional_storage*>()

/* Completely trivial. */
template<typename T>
struct optional_storage<T, true, true, true, true> : public optstore_helper<T> {
    using optstore_helper<T>::optstore_helper;
    constexpr optional_storage() noexcept = default;
    constexpr optional_storage(const optional_storage&) = default;
    constexpr optional_storage(optional_storage&&) = default;
    constexpr optional_storage& operator=(const optional_storage&) = default;
    constexpr optional_storage& operator=(optional_storage&&) = default;
};

/* Non-trivial move assignment. */
template<typename T>
struct optional_storage<T, true, true, true, false> : public optstore_helper<T> {
    using optstore_helper<T>::optstore_helper;
    constexpr optional_storage() noexcept = default;
    constexpr optional_storage(const optional_storage&) = default;
    constexpr optional_storage(optional_storage&&) = default;
    constexpr optional_storage& operator=(const optional_storage&) = default;
    constexpr optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(_this->assign(std::move(rhs)))
    { this->assign(std::move(rhs)); return *this; }
};

/* Non-trivial move construction. */
template<typename T>
struct optional_storage<T, true, false, true, false> : public optstore_helper<T> {
    using optstore_helper<T>::optstore_helper;
    constexpr optional_storage() noexcept = default;
    constexpr optional_storage(const optional_storage&) = default;
    constexpr optional_storage(optional_storage&& rhs) NOEXCEPT_AS(_this->construct(std::move(rhs.mValue)))
    { if(rhs.mHasValue) this->construct(std::move(rhs.mValue)); }
    constexpr optional_storage& operator=(const optional_storage&) = default;
    constexpr optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(_this->assign(std::move(rhs)))
    { this->assign(std::move(rhs)); return *this; }
};

/* Non-trivial copy assignment. */
template<typename T>
struct optional_storage<T, true, true, false, true> : public optstore_helper<T> {
    using optstore_helper<T>::optstore_helper;
    constexpr optional_storage() noexcept = default;
    constexpr optional_storage(const optional_storage&) = default;
    constexpr optional_storage(optional_storage&&) = default;
    constexpr optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(_this->assign(rhs))
    { this->assign(rhs); return *this; }
    constexpr optional_storage& operator=(optional_storage&&) = default;
};

/* Non-trivial copy construction. */
template<typename T>
struct optional_storage<T, false, true, false, true> : public optstore_helper<T> {
    using optstore_helper<T>::optstore_helper;
    constexpr optional_storage() noexcept = default;
    constexpr optional_storage(const optional_storage &rhs) NOEXCEPT_AS(_this->construct(rhs.mValue))
    { if(rhs.mHasValue) this->construct(rhs.mValue); }
    constexpr optional_storage(optional_storage&&) = default;
    constexpr optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(_this->assign(rhs))
    { this->assign(rhs); return *this; }
    constexpr optional_storage& operator=(optional_storage&&) = default;
};

/* Non-trivial assignment. */
template<typename T>
struct optional_storage<T, true, true, false, false> : public optstore_helper<T> {
    using optstore_helper<T>::optstore_helper;
    constexpr optional_storage() noexcept = default;
    constexpr optional_storage(const optional_storage&) = default;
    constexpr optional_storage(optional_storage&&) = default;
    constexpr optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(_this->assign(rhs))
    { this->assign(rhs); return *this; }
    constexpr optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(_this->assign(std::move(rhs)))
    { this->assign(std::move(rhs)); return *this; }
};

/* Non-trivial assignment, non-trivial move construction. */
template<typename T>
struct optional_storage<T, true, false, false, false> : public optstore_helper<T> {
    using optstore_helper<T>::optstore_helper;
    constexpr optional_storage() noexcept = default;
    constexpr optional_storage(const optional_storage&) = default;
    constexpr optional_storage(optional_storage&& rhs) NOEXCEPT_AS(_this->construct(std::move(rhs.mValue)))
    { if(rhs.mHasValue) this->construct(std::move(rhs.mValue)); }
    constexpr optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(_this->assign(rhs))
    { this->assign(rhs); return *this; }
    constexpr optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(_this->assign(std::move(rhs)))
    { this->assign(std::move(rhs)); return *this; }
};

/* Non-trivial assignment, non-trivial copy construction. */
template<typename T>
struct optional_storage<T, false, true, false, false> : public optstore_helper<T> {
    using optstore_helper<T>::optstore_helper;
    constexpr optional_storage() noexcept = default;
    constexpr optional_storage(const optional_storage &rhs) NOEXCEPT_AS(_this->construct(rhs.mValue))
    { if(rhs.mHasValue) this->construct(rhs.mValue); }
    constexpr optional_storage(optional_storage&&) = default;
    constexpr optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(_this->assign(rhs))
    { this->assign(rhs); return *this; }
    constexpr optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(_this->assign(std::move(rhs)))
    { this->assign(std::move(rhs)); return *this; }
};

/* Completely non-trivial. */
template<typename T>
struct optional_storage<T, false, false, false, false> : public optstore_helper<T> {
    using optstore_helper<T>::optstore_helper;
    constexpr optional_storage() noexcept = default;
    constexpr optional_storage(const optional_storage &rhs) NOEXCEPT_AS(_this->construct(rhs.mValue))
    { if(rhs.mHasValue) this->construct(rhs.mValue); }
    constexpr optional_storage(optional_storage&& rhs) NOEXCEPT_AS(_this->construct(std::move(rhs.mValue)))
    { if(rhs.mHasValue) this->construct(std::move(rhs.mValue)); }
    constexpr optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(_this->assign(rhs))
    { this->assign(rhs); return *this; }
    constexpr optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(_this->assign(std::move(rhs)))
    { this->assign(std::move(rhs)); return *this; }
};

#undef _this

} // namespace detail_

#define REQUIRES(...) std::enable_if_t<(__VA_ARGS__),bool> = true

template<typename T>
class optional {
    using storage_t = detail_::optional_storage<T>;

    storage_t mStore{};

public:
    using value_type = T;

    constexpr optional() = default;
    constexpr optional(const optional&) = default;
    constexpr optional(optional&&) = default;
    constexpr optional(nullopt_t) noexcept { }
    template<typename ...Args>
    constexpr explicit optional(in_place_t, Args&& ...args)
        NOEXCEPT_AS(storage_t{al::in_place, std::forward<Args>(args)...})
        : mStore{al::in_place, std::forward<Args>(args)...}
    { }
    template<typename U, REQUIRES(std::is_constructible<T, U&&>::value
        && !std::is_same<std::decay_t<U>, al::in_place_t>::value
        && !std::is_same<std::decay_t<U>, optional<T>>::value
        && std::is_convertible<U&&, T>::value)>
    constexpr optional(U&& rhs) NOEXCEPT_AS(storage_t{al::in_place, std::forward<U>(rhs)})
        : mStore{al::in_place, std::forward<U>(rhs)}
    { }
    template<typename U, REQUIRES(std::is_constructible<T, U&&>::value
        && !std::is_same<std::decay_t<U>, al::in_place_t>::value
        && !std::is_same<std::decay_t<U>, optional<T>>::value
        && !std::is_convertible<U&&, T>::value)>
    constexpr explicit optional(U&& rhs) NOEXCEPT_AS(storage_t{al::in_place, std::forward<U>(rhs)})
        : mStore{al::in_place, std::forward<U>(rhs)}
    { }
    ~optional() = default;

    constexpr optional& operator=(const optional&) = default;
    constexpr optional& operator=(optional&&) = default;
    constexpr optional& operator=(nullopt_t) noexcept { mStore.reset(); return *this; }
    template<typename U=T>
    constexpr std::enable_if_t<std::is_constructible<T, U>::value
        && std::is_assignable<T&, U>::value
        && !std::is_same<std::decay_t<U>, optional<T>>::value
        && (!std::is_same<std::decay_t<U>, T>::value || !std::is_scalar<U>::value),
    optional&> operator=(U&& rhs)
    {
        if(mStore.mHasValue)
            mStore.mValue = std::forward<U>(rhs);
        else
            mStore.construct(std::forward<U>(rhs));
        return *this;
    }

    constexpr const T* operator->() const { return std::addressof(mStore.mValue); }
    constexpr T* operator->() { return std::addressof(mStore.mValue); }
    constexpr const T& operator*() const& { return mStore.mValue; }
    constexpr T& operator*() & { return mStore.mValue; }
    constexpr const T&& operator*() const&& { return std::move(mStore.mValue); }
    constexpr T&& operator*() && { return std::move(mStore.mValue); }

    constexpr explicit operator bool() const noexcept { return mStore.mHasValue; }
    constexpr bool has_value() const noexcept { return mStore.mHasValue; }

    constexpr T& value() & { return mStore.mValue; }
    constexpr const T& value() const& { return mStore.mValue; }
    constexpr T&& value() && { return std::move(mStore.mValue); }
    constexpr const T&& value() const&& { return std::move(mStore.mValue); }

    template<typename U>
    constexpr T value_or(U&& defval) const&
    { return bool(*this) ? **this : static_cast<T>(std::forward<U>(defval)); }
    template<typename U>
    constexpr T value_or(U&& defval) &&
    { return bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(defval)); }

    template<typename ...Args>
    constexpr T& emplace(Args&& ...args)
    {
        mStore.reset();
        mStore.construct(std::forward<Args>(args)...);
        return mStore.mValue;
    }
    template<typename U, typename ...Args>
    constexpr std::enable_if_t<std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value,
    T&> emplace(std::initializer_list<U> il, Args&& ...args)
    {
        mStore.reset();
        mStore.construct(il, std::forward<Args>(args)...);
        return mStore.mValue;
    }

    constexpr void reset() noexcept { mStore.reset(); }
};

template<typename T>
constexpr optional<std::decay_t<T>> make_optional(T&& arg)
{ return optional<std::decay_t<T>>{in_place, std::forward<T>(arg)}; }

template<typename T, typename... Args>
constexpr optional<T> make_optional(Args&& ...args)
{ return optional<T>{in_place, std::forward<Args>(args)...}; }

template<typename T, typename U, typename... Args>
constexpr optional<T> make_optional(std::initializer_list<U> il, Args&& ...args)
{ return optional<T>{in_place, il, std::forward<Args>(args)...}; }

#undef REQUIRES
#undef NOEXCEPT_AS
} // namespace al

#endif /* AL_OPTIONAL_H */