2020-01-18 03:38:21 -05:00
|
|
|
/*
|
2021-02-23 03:41:26 -05:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2021-07-02 11:42:50 -04:00
|
|
|
* Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
|
2020-01-18 03:38:21 -05:00
|
|
|
*
|
2021-04-22 04:24:48 -04:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 03:38:21 -05:00
|
|
|
*/
|
|
|
|
|
2019-07-08 05:29:38 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
2020-02-06 05:55:06 -05:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-03-08 07:34:33 -04:00
|
|
|
#include <AK/Types.h>
|
2020-11-21 20:02:56 -05:00
|
|
|
#include <AK/kmalloc.h>
|
2019-07-08 05:29:38 -04:00
|
|
|
|
2020-02-14 16:29:06 -05:00
|
|
|
namespace AK {
|
|
|
|
|
2021-09-07 16:46:23 -04:00
|
|
|
// NOTE: If you're here because of an internal compiler error in GCC 10.3.0+,
|
|
|
|
// it's because of the following bug:
|
|
|
|
//
|
|
|
|
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96745
|
|
|
|
//
|
|
|
|
// Make sure you didn't accidentally make your destructor private before
|
|
|
|
// you start bug hunting. :^)
|
|
|
|
|
2019-07-08 05:29:38 -04:00
|
|
|
template<typename T>
|
2021-07-01 05:29:28 -04:00
|
|
|
class [[nodiscard]] Optional {
|
2019-07-08 05:29:38 -04:00
|
|
|
public:
|
2021-04-15 10:32:48 -04:00
|
|
|
using ValueType = T;
|
|
|
|
|
2021-02-23 03:41:26 -05:00
|
|
|
ALWAYS_INLINE Optional() = default;
|
2019-07-08 05:29:38 -04:00
|
|
|
|
2021-07-02 11:42:50 -04:00
|
|
|
#ifdef AK_HAS_CONDITIONALLY_TRIVIAL
|
2021-09-03 17:57:37 -04:00
|
|
|
Optional(Optional const& other) requires(!IsCopyConstructible<T>) = delete;
|
|
|
|
Optional(Optional const& other) = default;
|
2019-07-08 05:29:38 -04:00
|
|
|
|
2021-07-02 11:42:50 -04:00
|
|
|
Optional(Optional&& other) requires(!IsMoveConstructible<T>) = delete;
|
2019-08-08 12:34:59 -04:00
|
|
|
|
2021-09-03 17:57:37 -04:00
|
|
|
Optional& operator=(Optional const&) requires(!IsCopyConstructible<T> || !IsDestructible<T>) = delete;
|
|
|
|
Optional& operator=(Optional const&) = default;
|
2019-07-08 05:29:38 -04:00
|
|
|
|
2021-07-02 11:42:50 -04:00
|
|
|
Optional& operator=(Optional&& other) requires(!IsMoveConstructible<T> || !IsDestructible<T>) = delete;
|
|
|
|
|
|
|
|
~Optional() requires(!IsDestructible<T>) = delete;
|
|
|
|
~Optional() = default;
|
|
|
|
#endif
|
2019-07-08 05:29:38 -04:00
|
|
|
|
2021-09-03 17:57:37 -04:00
|
|
|
ALWAYS_INLINE Optional(Optional const& other)
|
2021-07-02 11:42:50 -04:00
|
|
|
#ifdef AK_HAS_CONDITIONALLY_TRIVIAL
|
|
|
|
requires(!IsTriviallyCopyConstructible<T>)
|
|
|
|
#endif
|
2019-07-08 05:29:38 -04:00
|
|
|
: m_has_value(other.m_has_value)
|
|
|
|
{
|
2021-07-02 11:42:50 -04:00
|
|
|
if (other.has_value()) {
|
2021-02-23 03:41:26 -05:00
|
|
|
new (&m_storage) T(other.value());
|
2019-07-08 05:29:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 11:42:50 -04:00
|
|
|
ALWAYS_INLINE Optional(Optional&& other)
|
2021-05-08 05:22:57 -04:00
|
|
|
: m_has_value(other.m_has_value)
|
|
|
|
{
|
2021-07-02 11:42:50 -04:00
|
|
|
if (other.has_value()) {
|
|
|
|
new (&m_storage) T(other.release_value());
|
2021-05-08 05:22:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 11:42:50 -04:00
|
|
|
template<typename U = T>
|
|
|
|
ALWAYS_INLINE explicit(!IsConvertible<U&&, T>) Optional(U&& value) requires(!IsSame<RemoveCVReference<U>, Optional<T>> && IsConstructible<T, U&&>)
|
2021-05-08 05:22:57 -04:00
|
|
|
: m_has_value(true)
|
|
|
|
{
|
2021-07-02 11:42:50 -04:00
|
|
|
new (&m_storage) T(forward<U>(value));
|
2021-05-08 05:22:57 -04:00
|
|
|
}
|
|
|
|
|
2021-09-03 17:57:37 -04:00
|
|
|
ALWAYS_INLINE Optional& operator=(Optional const& other)
|
2021-07-02 11:42:50 -04:00
|
|
|
#ifdef AK_HAS_CONDITIONALLY_TRIVIAL
|
|
|
|
requires(!IsTriviallyCopyConstructible<T> || !IsTriviallyDestructible<T>)
|
|
|
|
#endif
|
2019-07-08 05:29:38 -04:00
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
m_has_value = other.m_has_value;
|
2021-07-02 11:42:50 -04:00
|
|
|
if (other.has_value()) {
|
2019-07-08 05:29:38 -04:00
|
|
|
new (&m_storage) T(other.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-02-23 03:41:26 -05:00
|
|
|
ALWAYS_INLINE Optional& operator=(Optional&& other)
|
2019-07-08 05:29:38 -04:00
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
m_has_value = other.m_has_value;
|
2021-07-02 11:42:50 -04:00
|
|
|
if (other.has_value()) {
|
2019-07-08 05:29:38 -04:00
|
|
|
new (&m_storage) T(other.release_value());
|
2021-07-02 11:42:50 -04:00
|
|
|
}
|
2019-07-08 05:29:38 -04:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-08-22 17:57:34 -04:00
|
|
|
template<typename O>
|
2021-09-03 17:57:37 -04:00
|
|
|
ALWAYS_INLINE bool operator==(Optional<O> const& other) const
|
2020-08-22 17:57:34 -04:00
|
|
|
{
|
|
|
|
return has_value() == other.has_value() && (!has_value() || value() == other.value());
|
|
|
|
}
|
|
|
|
|
2021-06-01 04:12:53 -04:00
|
|
|
template<typename O>
|
|
|
|
ALWAYS_INLINE bool operator==(O const& other) const
|
|
|
|
{
|
|
|
|
return has_value() && value() == other;
|
|
|
|
}
|
|
|
|
|
2020-04-30 05:43:25 -04:00
|
|
|
ALWAYS_INLINE ~Optional()
|
2021-07-02 11:42:50 -04:00
|
|
|
#ifdef AK_HAS_CONDITIONALLY_TRIVIAL
|
|
|
|
requires(!IsTriviallyDestructible<T>)
|
|
|
|
#endif
|
2019-07-08 05:29:38 -04:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2020-04-30 05:43:25 -04:00
|
|
|
ALWAYS_INLINE void clear()
|
2019-07-08 05:29:38 -04:00
|
|
|
{
|
|
|
|
if (m_has_value) {
|
|
|
|
value().~T();
|
|
|
|
m_has_value = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 13:18:01 -04:00
|
|
|
template<typename... Parameters>
|
2020-12-30 16:44:54 -05:00
|
|
|
ALWAYS_INLINE void emplace(Parameters&&... parameters)
|
2020-08-29 13:18:01 -04:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
m_has_value = true;
|
|
|
|
new (&m_storage) T(forward<Parameters>(parameters)...);
|
|
|
|
}
|
|
|
|
|
2021-02-14 18:21:04 -05:00
|
|
|
[[nodiscard]] ALWAYS_INLINE bool has_value() const { return m_has_value; }
|
2019-07-08 05:29:38 -04:00
|
|
|
|
2021-09-03 18:44:10 -04:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T& value() &
|
2019-07-08 05:29:38 -04:00
|
|
|
{
|
2021-02-23 14:42:32 -05:00
|
|
|
VERIFY(m_has_value);
|
2021-07-01 05:29:28 -04:00
|
|
|
return *__builtin_launder(reinterpret_cast<T*>(&m_storage));
|
2019-07-08 05:29:38 -04:00
|
|
|
}
|
|
|
|
|
2021-09-03 18:44:10 -04:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T const& value() const&
|
2019-07-08 05:29:38 -04:00
|
|
|
{
|
2021-02-23 14:42:32 -05:00
|
|
|
VERIFY(m_has_value);
|
2021-09-03 17:57:37 -04:00
|
|
|
return *__builtin_launder(reinterpret_cast<T const*>(&m_storage));
|
2019-07-08 05:29:38 -04:00
|
|
|
}
|
|
|
|
|
2021-09-03 18:44:10 -04:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T value() &&
|
|
|
|
{
|
|
|
|
return release_value();
|
|
|
|
}
|
|
|
|
|
2021-10-25 08:31:46 -04:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T release_value()
|
2019-07-08 05:29:38 -04:00
|
|
|
{
|
2021-02-23 14:42:32 -05:00
|
|
|
VERIFY(m_has_value);
|
2019-07-08 05:29:38 -04:00
|
|
|
T released_value = move(value());
|
|
|
|
value().~T();
|
2019-08-05 15:47:36 -04:00
|
|
|
m_has_value = false;
|
2019-07-08 05:29:38 -04:00
|
|
|
return released_value;
|
|
|
|
}
|
|
|
|
|
2021-09-03 18:44:10 -04:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T value_or(T const& fallback) const&
|
2019-07-24 01:26:02 -04:00
|
|
|
{
|
2019-07-30 13:45:40 -04:00
|
|
|
if (m_has_value)
|
2019-07-24 01:26:02 -04:00
|
|
|
return value();
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
2021-09-03 18:44:10 -04:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T value_or(T&& fallback) &&
|
|
|
|
{
|
|
|
|
if (m_has_value)
|
|
|
|
return move(value());
|
|
|
|
return move(fallback);
|
|
|
|
}
|
|
|
|
|
2021-09-03 17:57:37 -04:00
|
|
|
ALWAYS_INLINE T const& operator*() const { return value(); }
|
2021-02-23 03:41:26 -05:00
|
|
|
ALWAYS_INLINE T& operator*() { return value(); }
|
2020-12-30 15:16:37 -05:00
|
|
|
|
2021-09-03 17:57:37 -04:00
|
|
|
ALWAYS_INLINE T const* operator->() const { return &value(); }
|
2021-02-23 03:41:26 -05:00
|
|
|
ALWAYS_INLINE T* operator->() { return &value(); }
|
2020-12-30 15:16:37 -05:00
|
|
|
|
2019-07-08 05:29:38 -04:00
|
|
|
private:
|
2021-07-01 05:29:28 -04:00
|
|
|
alignas(T) u8 m_storage[sizeof(T)];
|
2019-07-08 05:29:38 -04:00
|
|
|
bool m_has_value { false };
|
|
|
|
};
|
2020-02-14 16:29:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::Optional;
|