2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-02-23 08:41:26 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2021-07-02 15:42:50 +00:00
|
|
|
* Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-07-08 09:29:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
2020-02-06 10:55:06 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2022-12-09 16:42:24 +00:00
|
|
|
#include <AK/Try.h>
|
2020-03-08 11:34:33 +00:00
|
|
|
#include <AK/Types.h>
|
2020-11-22 01:02:56 +00:00
|
|
|
#include <AK/kmalloc.h>
|
2019-07-08 09:29:38 +00:00
|
|
|
|
2020-02-14 21:29:06 +00:00
|
|
|
namespace AK {
|
|
|
|
|
2022-12-09 16:42:24 +00:00
|
|
|
namespace Detail {
|
|
|
|
template<auto condition, typename T>
|
|
|
|
struct ConditionallyResultType;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct ConditionallyResultType<true, T> {
|
|
|
|
using Type = typename T::ResultType;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct ConditionallyResultType<false, T> {
|
|
|
|
using Type = T;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<auto condition, typename T>
|
|
|
|
using ConditionallyResultType = typename Detail::ConditionallyResultType<condition, T>::Type;
|
|
|
|
|
2021-09-07 20:46:23 +00: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. :^)
|
|
|
|
|
2022-04-03 11:06:34 +00:00
|
|
|
template<typename>
|
|
|
|
class Optional;
|
|
|
|
|
2019-07-08 09:29:38 +00:00
|
|
|
template<typename T>
|
2022-04-03 11:06:34 +00:00
|
|
|
requires(!IsLvalueReference<T>) class [[nodiscard]] Optional<T> {
|
2022-01-23 11:03:29 +00:00
|
|
|
template<typename U>
|
|
|
|
friend class Optional;
|
|
|
|
|
2022-04-03 11:06:34 +00:00
|
|
|
static_assert(!IsLvalueReference<T> && !IsRvalueReference<T>);
|
|
|
|
|
2019-07-08 09:29:38 +00:00
|
|
|
public:
|
2021-04-15 14:32:48 +00:00
|
|
|
using ValueType = T;
|
|
|
|
|
2021-02-23 08:41:26 +00:00
|
|
|
ALWAYS_INLINE Optional() = default;
|
2019-07-08 09:29:38 +00:00
|
|
|
|
2021-07-02 15:42:50 +00:00
|
|
|
#ifdef AK_HAS_CONDITIONALLY_TRIVIAL
|
2022-10-16 22:06:11 +00:00
|
|
|
Optional(Optional const& other)
|
|
|
|
requires(!IsCopyConstructible<T>)
|
|
|
|
= delete;
|
2021-09-03 21:57:37 +00:00
|
|
|
Optional(Optional const& other) = default;
|
2019-07-08 09:29:38 +00:00
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
Optional(Optional&& other)
|
|
|
|
requires(!IsMoveConstructible<T>)
|
|
|
|
= delete;
|
2019-08-08 16:34:59 +00:00
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
Optional& operator=(Optional const&)
|
|
|
|
requires(!IsCopyConstructible<T> || !IsDestructible<T>)
|
|
|
|
= delete;
|
2021-09-03 21:57:37 +00:00
|
|
|
Optional& operator=(Optional const&) = default;
|
2019-07-08 09:29:38 +00:00
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
Optional& operator=(Optional&& other)
|
|
|
|
requires(!IsMoveConstructible<T> || !IsDestructible<T>)
|
|
|
|
= delete;
|
2021-07-02 15:42:50 +00:00
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
~Optional()
|
|
|
|
requires(!IsDestructible<T>)
|
|
|
|
= delete;
|
2021-07-02 15:42:50 +00:00
|
|
|
~Optional() = default;
|
|
|
|
#endif
|
2019-07-08 09:29:38 +00:00
|
|
|
|
2021-09-03 21:57:37 +00:00
|
|
|
ALWAYS_INLINE Optional(Optional const& other)
|
2021-07-02 15:42:50 +00:00
|
|
|
#ifdef AK_HAS_CONDITIONALLY_TRIVIAL
|
2022-10-16 22:06:11 +00:00
|
|
|
requires(!IsTriviallyCopyConstructible<T>)
|
2021-07-02 15:42:50 +00:00
|
|
|
#endif
|
2019-07-08 09:29:38 +00:00
|
|
|
: m_has_value(other.m_has_value)
|
|
|
|
{
|
2022-01-23 11:03:29 +00:00
|
|
|
if (other.has_value())
|
2021-02-23 08:41:26 +00:00
|
|
|
new (&m_storage) T(other.value());
|
2019-07-08 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 15:42:50 +00:00
|
|
|
ALWAYS_INLINE Optional(Optional&& other)
|
2021-05-08 09:22:57 +00:00
|
|
|
: m_has_value(other.m_has_value)
|
|
|
|
{
|
2022-01-23 11:03:29 +00:00
|
|
|
if (other.has_value())
|
|
|
|
new (&m_storage) T(other.release_value());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
requires(IsConstructible<T, U const&> && !IsSpecializationOf<T, Optional> && !IsSpecializationOf<U, Optional>) ALWAYS_INLINE explicit Optional(Optional<U> const& other)
|
|
|
|
: m_has_value(other.m_has_value)
|
|
|
|
{
|
|
|
|
if (other.has_value())
|
|
|
|
new (&m_storage) T(other.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2022-10-16 22:06:11 +00:00
|
|
|
requires(IsConstructible<T, U &&> && !IsSpecializationOf<T, Optional> && !IsSpecializationOf<U, Optional>) ALWAYS_INLINE explicit Optional(Optional<U>&& other)
|
2022-01-23 11:03:29 +00:00
|
|
|
: m_has_value(other.m_has_value)
|
|
|
|
{
|
|
|
|
if (other.has_value())
|
2021-07-02 15:42:50 +00:00
|
|
|
new (&m_storage) T(other.release_value());
|
2021-05-08 09:22:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 15:42:50 +00:00
|
|
|
template<typename U = T>
|
2022-10-16 22:06:11 +00:00
|
|
|
ALWAYS_INLINE explicit(!IsConvertible<U&&, T>) Optional(U&& value)
|
|
|
|
requires(!IsSame<RemoveCVReference<U>, Optional<T>> && IsConstructible<T, U &&>)
|
2021-05-08 09:22:57 +00:00
|
|
|
: m_has_value(true)
|
|
|
|
{
|
2021-07-02 15:42:50 +00:00
|
|
|
new (&m_storage) T(forward<U>(value));
|
2021-05-08 09:22:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 21:57:37 +00:00
|
|
|
ALWAYS_INLINE Optional& operator=(Optional const& other)
|
2021-07-02 15:42:50 +00:00
|
|
|
#ifdef AK_HAS_CONDITIONALLY_TRIVIAL
|
2022-10-16 22:06:11 +00:00
|
|
|
requires(!IsTriviallyCopyConstructible<T> || !IsTriviallyDestructible<T>)
|
2021-07-02 15:42:50 +00:00
|
|
|
#endif
|
2019-07-08 09:29:38 +00:00
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
m_has_value = other.m_has_value;
|
2021-07-02 15:42:50 +00:00
|
|
|
if (other.has_value()) {
|
2019-07-08 09:29:38 +00:00
|
|
|
new (&m_storage) T(other.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-02-23 08:41:26 +00:00
|
|
|
ALWAYS_INLINE Optional& operator=(Optional&& other)
|
2019-07-08 09:29:38 +00:00
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
m_has_value = other.m_has_value;
|
2021-07-02 15:42:50 +00:00
|
|
|
if (other.has_value()) {
|
2019-07-08 09:29:38 +00:00
|
|
|
new (&m_storage) T(other.release_value());
|
2021-07-02 15:42:50 +00:00
|
|
|
}
|
2019-07-08 09:29:38 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-08-22 21:57:34 +00:00
|
|
|
template<typename O>
|
2021-09-03 21:57:37 +00:00
|
|
|
ALWAYS_INLINE bool operator==(Optional<O> const& other) const
|
2020-08-22 21:57:34 +00:00
|
|
|
{
|
|
|
|
return has_value() == other.has_value() && (!has_value() || value() == other.value());
|
|
|
|
}
|
|
|
|
|
2021-06-01 08:12:53 +00:00
|
|
|
template<typename O>
|
|
|
|
ALWAYS_INLINE bool operator==(O const& other) const
|
|
|
|
{
|
|
|
|
return has_value() && value() == other;
|
|
|
|
}
|
|
|
|
|
2020-04-30 09:43:25 +00:00
|
|
|
ALWAYS_INLINE ~Optional()
|
2021-07-02 15:42:50 +00:00
|
|
|
#ifdef AK_HAS_CONDITIONALLY_TRIVIAL
|
2022-10-16 22:06:11 +00:00
|
|
|
requires(!IsTriviallyDestructible<T>)
|
2021-07-02 15:42:50 +00:00
|
|
|
#endif
|
2019-07-08 09:29:38 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2020-04-30 09:43:25 +00:00
|
|
|
ALWAYS_INLINE void clear()
|
2019-07-08 09:29:38 +00:00
|
|
|
{
|
|
|
|
if (m_has_value) {
|
|
|
|
value().~T();
|
|
|
|
m_has_value = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 17:18:01 +00:00
|
|
|
template<typename... Parameters>
|
2020-12-30 21:44:54 +00:00
|
|
|
ALWAYS_INLINE void emplace(Parameters&&... parameters)
|
2020-08-29 17:18:01 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
m_has_value = true;
|
|
|
|
new (&m_storage) T(forward<Parameters>(parameters)...);
|
|
|
|
}
|
|
|
|
|
2021-02-14 23:21:04 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE bool has_value() const { return m_has_value; }
|
2019-07-08 09:29:38 +00:00
|
|
|
|
2021-09-03 22:44:10 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T& value() &
|
2019-07-08 09:29:38 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_has_value);
|
2021-07-01 09:29:28 +00:00
|
|
|
return *__builtin_launder(reinterpret_cast<T*>(&m_storage));
|
2019-07-08 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 22:44:10 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T const& value() const&
|
2019-07-08 09:29:38 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_has_value);
|
2021-09-03 21:57:37 +00:00
|
|
|
return *__builtin_launder(reinterpret_cast<T const*>(&m_storage));
|
2019-07-08 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 22:44:10 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T value() &&
|
|
|
|
{
|
|
|
|
return release_value();
|
|
|
|
}
|
|
|
|
|
2021-10-25 12:31:46 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T release_value()
|
2019-07-08 09:29:38 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_has_value);
|
2019-07-08 09:29:38 +00:00
|
|
|
T released_value = move(value());
|
|
|
|
value().~T();
|
2019-08-05 19:47:36 +00:00
|
|
|
m_has_value = false;
|
2019-07-08 09:29:38 +00:00
|
|
|
return released_value;
|
|
|
|
}
|
|
|
|
|
2021-09-03 22:44:10 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T value_or(T const& fallback) const&
|
2019-07-24 05:26:02 +00:00
|
|
|
{
|
2019-07-30 17:45:40 +00:00
|
|
|
if (m_has_value)
|
2019-07-24 05:26:02 +00:00
|
|
|
return value();
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
2021-09-03 22:44:10 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T value_or(T&& fallback) &&
|
|
|
|
{
|
|
|
|
if (m_has_value)
|
|
|
|
return move(value());
|
|
|
|
return move(fallback);
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:44:06 +00:00
|
|
|
template<typename Callback>
|
|
|
|
[[nodiscard]] ALWAYS_INLINE T value_or_lazy_evaluated(Callback callback) const
|
|
|
|
{
|
|
|
|
if (m_has_value)
|
|
|
|
return value();
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
[[nodiscard]] ALWAYS_INLINE Optional<T> value_or_lazy_evaluated_optional(Callback callback) const
|
|
|
|
{
|
|
|
|
if (m_has_value)
|
|
|
|
return value();
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
[[nodiscard]] ALWAYS_INLINE ErrorOr<T> try_value_or_lazy_evaluated(Callback callback) const
|
|
|
|
{
|
|
|
|
if (m_has_value)
|
|
|
|
return value();
|
|
|
|
return TRY(callback());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
[[nodiscard]] ALWAYS_INLINE ErrorOr<Optional<T>> try_value_or_lazy_evaluated_optional(Callback callback) const
|
|
|
|
{
|
|
|
|
if (m_has_value)
|
|
|
|
return value();
|
|
|
|
return TRY(callback());
|
|
|
|
}
|
|
|
|
|
2021-09-03 21:57:37 +00:00
|
|
|
ALWAYS_INLINE T const& operator*() const { return value(); }
|
2021-02-23 08:41:26 +00:00
|
|
|
ALWAYS_INLINE T& operator*() { return value(); }
|
2020-12-30 20:16:37 +00:00
|
|
|
|
2021-09-03 21:57:37 +00:00
|
|
|
ALWAYS_INLINE T const* operator->() const { return &value(); }
|
2021-02-23 08:41:26 +00:00
|
|
|
ALWAYS_INLINE T* operator->() { return &value(); }
|
2020-12-30 20:16:37 +00:00
|
|
|
|
2022-12-09 16:42:24 +00:00
|
|
|
template<typename F, typename MappedType = decltype(declval<F>()(declval<T&>())), auto IsErrorOr = IsSpecializationOf<MappedType, ErrorOr>, typename OptionalType = Optional<ConditionallyResultType<IsErrorOr, MappedType>>>
|
|
|
|
ALWAYS_INLINE Conditional<IsErrorOr, ErrorOr<OptionalType>, OptionalType> map(F&& mapper)
|
|
|
|
{
|
|
|
|
if constexpr (IsErrorOr) {
|
|
|
|
if (m_has_value)
|
|
|
|
return OptionalType { TRY(mapper(value())) };
|
|
|
|
return OptionalType {};
|
|
|
|
} else {
|
|
|
|
if (m_has_value)
|
|
|
|
return OptionalType { mapper(value()) };
|
|
|
|
|
|
|
|
return OptionalType {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename F, typename MappedType = decltype(declval<F>()(declval<T&>())), auto IsErrorOr = IsSpecializationOf<MappedType, ErrorOr>, typename OptionalType = Optional<ConditionallyResultType<IsErrorOr, MappedType>>>
|
|
|
|
ALWAYS_INLINE Conditional<IsErrorOr, ErrorOr<OptionalType>, OptionalType> map(F&& mapper) const
|
|
|
|
{
|
|
|
|
if constexpr (IsErrorOr) {
|
|
|
|
if (m_has_value)
|
|
|
|
return OptionalType { TRY(mapper(value())) };
|
|
|
|
return OptionalType {};
|
|
|
|
} else {
|
|
|
|
if (m_has_value)
|
|
|
|
return OptionalType { mapper(value()) };
|
|
|
|
|
|
|
|
return OptionalType {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 09:29:38 +00:00
|
|
|
private:
|
2021-07-01 09:29:28 +00:00
|
|
|
alignas(T) u8 m_storage[sizeof(T)];
|
2019-07-08 09:29:38 +00:00
|
|
|
bool m_has_value { false };
|
|
|
|
};
|
2022-04-03 11:06:34 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
requires(IsLvalueReference<T>) class [[nodiscard]] Optional<T> {
|
|
|
|
template<typename>
|
|
|
|
friend class Optional;
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
constexpr static bool CanBePlacedInOptional = IsSame<RemoveReference<T>, RemoveReference<AddConstToReferencedType<U>>> && (IsBaseOf<RemoveCVReference<T>, RemoveCVReference<U>> || IsSame<RemoveCVReference<T>, RemoveCVReference<U>>);
|
|
|
|
|
|
|
|
public:
|
|
|
|
using ValueType = T;
|
|
|
|
|
|
|
|
ALWAYS_INLINE Optional() = default;
|
|
|
|
|
|
|
|
template<typename U = T>
|
2022-10-16 22:06:11 +00:00
|
|
|
ALWAYS_INLINE Optional(U& value)
|
|
|
|
requires(CanBePlacedInOptional<U&>)
|
2022-04-03 11:06:34 +00:00
|
|
|
: m_pointer(&value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE Optional(RemoveReference<T>& value)
|
|
|
|
: m_pointer(&value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE Optional(Optional const& other)
|
|
|
|
: m_pointer(other.m_pointer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE Optional(Optional&& other)
|
|
|
|
: m_pointer(other.m_pointer)
|
|
|
|
{
|
|
|
|
other.m_pointer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2022-10-16 22:06:11 +00:00
|
|
|
ALWAYS_INLINE Optional(Optional<U> const& other)
|
|
|
|
requires(CanBePlacedInOptional<U>)
|
2022-04-03 11:06:34 +00:00
|
|
|
: m_pointer(other.m_pointer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2022-10-16 22:06:11 +00:00
|
|
|
ALWAYS_INLINE Optional(Optional<U>&& other)
|
|
|
|
requires(CanBePlacedInOptional<U>)
|
2022-04-03 11:06:34 +00:00
|
|
|
: m_pointer(other.m_pointer)
|
|
|
|
{
|
|
|
|
other.m_pointer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE Optional& operator=(Optional const& other)
|
|
|
|
{
|
|
|
|
m_pointer = other.m_pointer;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE Optional& operator=(Optional&& other)
|
|
|
|
{
|
|
|
|
m_pointer = other.m_pointer;
|
|
|
|
other.m_pointer = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2022-10-16 22:06:11 +00:00
|
|
|
ALWAYS_INLINE Optional& operator=(Optional<U> const& other)
|
|
|
|
requires(CanBePlacedInOptional<U>)
|
2022-04-03 11:06:34 +00:00
|
|
|
{
|
|
|
|
m_pointer = other.m_pointer;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2022-10-16 22:06:11 +00:00
|
|
|
ALWAYS_INLINE Optional& operator=(Optional<U>&& other)
|
|
|
|
requires(CanBePlacedInOptional<U>)
|
2022-04-03 11:06:34 +00:00
|
|
|
{
|
|
|
|
m_pointer = other.m_pointer;
|
|
|
|
other.m_pointer = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: Disallows assignment from a temporary as this does not do any lifetime extension.
|
|
|
|
template<typename U>
|
2022-10-16 22:06:11 +00:00
|
|
|
ALWAYS_INLINE Optional& operator=(U&& value)
|
|
|
|
requires(CanBePlacedInOptional<U> && IsLvalueReference<U>)
|
2022-04-03 11:06:34 +00:00
|
|
|
{
|
|
|
|
m_pointer = &value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE void clear()
|
|
|
|
{
|
|
|
|
m_pointer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ALWAYS_INLINE bool has_value() const { return m_pointer != nullptr; }
|
|
|
|
|
|
|
|
[[nodiscard]] ALWAYS_INLINE T value()
|
|
|
|
{
|
|
|
|
VERIFY(m_pointer);
|
|
|
|
return *m_pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ALWAYS_INLINE AddConstToReferencedType<T> value() const
|
|
|
|
{
|
|
|
|
VERIFY(m_pointer);
|
|
|
|
return *m_pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
requires(IsBaseOf<RemoveCVReference<T>, U>) [[nodiscard]] ALWAYS_INLINE AddConstToReferencedType<T> value_or(U& fallback) const
|
|
|
|
{
|
|
|
|
if (m_pointer)
|
|
|
|
return value();
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that this ends up copying the value.
|
|
|
|
[[nodiscard]] ALWAYS_INLINE RemoveCVReference<T> value_or(RemoveCVReference<T> fallback) const
|
|
|
|
{
|
|
|
|
if (m_pointer)
|
|
|
|
return value();
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ALWAYS_INLINE T release_value()
|
|
|
|
{
|
|
|
|
return *exchange(m_pointer, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
ALWAYS_INLINE bool operator==(Optional<U> const& other) const
|
|
|
|
{
|
|
|
|
return has_value() == other.has_value() && (!has_value() || value() == other.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
ALWAYS_INLINE bool operator==(U const& other) const
|
|
|
|
{
|
|
|
|
return has_value() && value() == other;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE AddConstToReferencedType<T> operator*() const { return value(); }
|
|
|
|
ALWAYS_INLINE T operator*() { return value(); }
|
|
|
|
|
|
|
|
ALWAYS_INLINE RawPtr<AddConst<RemoveReference<T>>> operator->() const { return &value(); }
|
|
|
|
ALWAYS_INLINE RawPtr<RemoveReference<T>> operator->() { return &value(); }
|
|
|
|
|
|
|
|
// Conversion operators from Optional<T&> -> Optional<T>
|
|
|
|
ALWAYS_INLINE operator Optional<RemoveCVReference<T>>() const
|
|
|
|
{
|
|
|
|
if (has_value())
|
|
|
|
return Optional<RemoveCVReference<T>>(value());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:44:06 +00:00
|
|
|
template<typename Callback>
|
|
|
|
[[nodiscard]] ALWAYS_INLINE T value_or_lazy_evaluated(Callback callback) const
|
|
|
|
{
|
|
|
|
if (m_pointer != nullptr)
|
|
|
|
return value();
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
[[nodiscard]] ALWAYS_INLINE Optional<T> value_or_lazy_evaluated_optional(Callback callback) const
|
|
|
|
{
|
|
|
|
if (m_pointer != nullptr)
|
|
|
|
return value();
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
[[nodiscard]] ALWAYS_INLINE ErrorOr<T> try_value_or_lazy_evaluated(Callback callback) const
|
|
|
|
{
|
|
|
|
if (m_pointer != nullptr)
|
|
|
|
return value();
|
|
|
|
return TRY(callback());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
[[nodiscard]] ALWAYS_INLINE ErrorOr<Optional<T>> try_value_or_lazy_evaluated_optional(Callback callback) const
|
|
|
|
{
|
|
|
|
if (m_pointer != nullptr)
|
|
|
|
return value();
|
|
|
|
return TRY(callback());
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:42:24 +00:00
|
|
|
template<typename F, typename MappedType = decltype(declval<F>()(declval<T&>())), auto IsErrorOr = IsSpecializationOf<MappedType, ErrorOr>, typename OptionalType = Optional<ConditionallyResultType<IsErrorOr, MappedType>>>
|
|
|
|
ALWAYS_INLINE Conditional<IsErrorOr, ErrorOr<OptionalType>, OptionalType> map(F&& mapper)
|
|
|
|
{
|
|
|
|
if constexpr (IsErrorOr) {
|
|
|
|
if (m_pointer != nullptr)
|
|
|
|
return OptionalType { TRY(mapper(value())) };
|
|
|
|
return OptionalType {};
|
|
|
|
} else {
|
|
|
|
if (m_pointer != nullptr)
|
|
|
|
return OptionalType { mapper(value()) };
|
|
|
|
|
|
|
|
return OptionalType {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename F, typename MappedType = decltype(declval<F>()(declval<T&>())), auto IsErrorOr = IsSpecializationOf<MappedType, ErrorOr>, typename OptionalType = Optional<ConditionallyResultType<IsErrorOr, MappedType>>>
|
|
|
|
ALWAYS_INLINE Conditional<IsErrorOr, ErrorOr<OptionalType>, OptionalType> map(F&& mapper) const
|
|
|
|
{
|
|
|
|
if constexpr (IsErrorOr) {
|
|
|
|
if (m_pointer != nullptr)
|
|
|
|
return OptionalType { TRY(mapper(value())) };
|
|
|
|
return OptionalType {};
|
|
|
|
} else {
|
|
|
|
if (m_pointer != nullptr)
|
|
|
|
return OptionalType { mapper(value()) };
|
|
|
|
|
|
|
|
return OptionalType {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 11:06:34 +00:00
|
|
|
private:
|
|
|
|
RemoveReference<T>* m_pointer { nullptr };
|
|
|
|
};
|
|
|
|
|
2020-02-14 21:29:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-02-14 21:29:06 +00:00
|
|
|
using AK::Optional;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|