2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-02-23 09:41:26 +01:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-07-08 11:29:38 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
2020-02-06 11:55:06 +01:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-03-08 12:34:33 +01:00
|
|
|
#include <AK/Types.h>
|
2020-11-21 18:02:56 -07:00
|
|
|
#include <AK/kmalloc.h>
|
2019-07-08 11:29:38 +02:00
|
|
|
|
2020-02-14 22:29:06 +01:00
|
|
|
namespace AK {
|
|
|
|
|
2019-07-08 11:29:38 +02:00
|
|
|
template<typename T>
|
2020-12-30 22:44:54 +01:00
|
|
|
class alignas(T) [[nodiscard]] Optional {
|
2019-07-08 11:29:38 +02:00
|
|
|
public:
|
2021-04-15 10:32:48 -04:00
|
|
|
using ValueType = T;
|
|
|
|
|
2021-02-23 09:41:26 +01:00
|
|
|
ALWAYS_INLINE Optional() = default;
|
2019-07-08 11:29:38 +02:00
|
|
|
|
2021-02-23 09:41:26 +01:00
|
|
|
ALWAYS_INLINE Optional(const T& value)
|
2019-07-08 11:29:38 +02:00
|
|
|
: m_has_value(true)
|
|
|
|
{
|
2019-08-08 18:34:59 +02:00
|
|
|
new (&m_storage) T(value);
|
2019-07-08 11:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2021-02-23 09:41:26 +01:00
|
|
|
ALWAYS_INLINE Optional(const U& value)
|
2019-08-08 18:34:59 +02:00
|
|
|
: m_has_value(true)
|
|
|
|
{
|
|
|
|
new (&m_storage) T(value);
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:41:26 +01:00
|
|
|
ALWAYS_INLINE Optional(T&& value)
|
2019-07-08 11:29:38 +02:00
|
|
|
: m_has_value(true)
|
|
|
|
{
|
|
|
|
new (&m_storage) T(move(value));
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:41:26 +01:00
|
|
|
ALWAYS_INLINE Optional(Optional&& other)
|
2019-07-08 11:29:38 +02:00
|
|
|
: m_has_value(other.m_has_value)
|
|
|
|
{
|
2019-08-07 07:17:52 +02:00
|
|
|
if (other.has_value()) {
|
2019-08-05 22:27:47 +02:00
|
|
|
new (&m_storage) T(other.release_value());
|
2019-07-08 11:29:38 +02:00
|
|
|
other.m_has_value = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:41:26 +01:00
|
|
|
ALWAYS_INLINE Optional(const Optional& other)
|
2019-07-08 11:29:38 +02:00
|
|
|
: m_has_value(other.m_has_value)
|
|
|
|
{
|
|
|
|
if (m_has_value) {
|
2021-02-23 09:41:26 +01:00
|
|
|
new (&m_storage) T(other.value());
|
2019-07-08 11:29:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-08 12:22:57 +03:00
|
|
|
ALWAYS_INLINE Optional(Optional& other)
|
|
|
|
: m_has_value(other.m_has_value)
|
|
|
|
{
|
|
|
|
if (m_has_value) {
|
|
|
|
new (&m_storage) T(other.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
ALWAYS_INLINE Optional(U& value)
|
|
|
|
: m_has_value(true)
|
|
|
|
{
|
|
|
|
new (&m_storage) T(value);
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:41:26 +01:00
|
|
|
ALWAYS_INLINE Optional& operator=(const Optional& other)
|
2019-07-08 11:29:38 +02:00
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
m_has_value = other.m_has_value;
|
|
|
|
if (m_has_value) {
|
|
|
|
new (&m_storage) T(other.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:41:26 +01:00
|
|
|
ALWAYS_INLINE Optional& operator=(Optional&& other)
|
2019-07-08 11:29:38 +02:00
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
m_has_value = other.m_has_value;
|
2019-08-07 07:17:52 +02:00
|
|
|
if (other.has_value())
|
2019-07-08 11:29:38 +02:00
|
|
|
new (&m_storage) T(other.release_value());
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-08-22 15:57:34 -06:00
|
|
|
template<typename O>
|
2021-02-23 09:41:26 +01:00
|
|
|
ALWAYS_INLINE bool operator==(const Optional<O>& other) const
|
2020-08-22 15:57:34 -06:00
|
|
|
{
|
|
|
|
return has_value() == other.has_value() && (!has_value() || value() == other.value());
|
|
|
|
}
|
|
|
|
|
2020-04-30 11:43:25 +02:00
|
|
|
ALWAYS_INLINE ~Optional()
|
2019-07-08 11:29:38 +02:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2020-04-30 11:43:25 +02:00
|
|
|
ALWAYS_INLINE void clear()
|
2019-07-08 11:29:38 +02:00
|
|
|
{
|
|
|
|
if (m_has_value) {
|
|
|
|
value().~T();
|
|
|
|
m_has_value = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 19:18:01 +02:00
|
|
|
template<typename... Parameters>
|
2020-12-30 22:44:54 +01:00
|
|
|
ALWAYS_INLINE void emplace(Parameters&&... parameters)
|
2020-08-29 19:18:01 +02:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
m_has_value = true;
|
|
|
|
new (&m_storage) T(forward<Parameters>(parameters)...);
|
|
|
|
}
|
|
|
|
|
2021-02-14 15:21:04 -08:00
|
|
|
[[nodiscard]] ALWAYS_INLINE bool has_value() const { return m_has_value; }
|
2019-07-08 11:29:38 +02:00
|
|
|
|
2021-02-14 15:21:04 -08:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T& value()
|
2019-07-08 11:29:38 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_has_value);
|
2019-07-08 11:29:38 +02:00
|
|
|
return *reinterpret_cast<T*>(&m_storage);
|
|
|
|
}
|
|
|
|
|
2021-02-14 15:21:04 -08:00
|
|
|
[[nodiscard]] ALWAYS_INLINE const T& value() const
|
2019-07-08 11:29:38 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_has_value);
|
2021-02-23 09:41:26 +01:00
|
|
|
return *reinterpret_cast<const T*>(&m_storage);
|
2019-07-08 11:29:38 +02:00
|
|
|
}
|
|
|
|
|
2021-02-14 15:21:04 -08:00
|
|
|
[[nodiscard]] T release_value()
|
2019-07-08 11:29:38 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_has_value);
|
2019-07-08 11:29:38 +02:00
|
|
|
T released_value = move(value());
|
|
|
|
value().~T();
|
2019-08-05 21:47:36 +02:00
|
|
|
m_has_value = false;
|
2019-07-08 11:29:38 +02:00
|
|
|
return released_value;
|
|
|
|
}
|
|
|
|
|
2021-02-14 15:21:04 -08:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T value_or(const T& fallback) const
|
2019-07-24 07:26:02 +02:00
|
|
|
{
|
2019-07-30 19:45:40 +02:00
|
|
|
if (m_has_value)
|
2019-07-24 07:26:02 +02:00
|
|
|
return value();
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:41:26 +01:00
|
|
|
ALWAYS_INLINE const T& operator*() const { return value(); }
|
|
|
|
ALWAYS_INLINE T& operator*() { return value(); }
|
2020-12-30 21:16:37 +01:00
|
|
|
|
2021-02-23 09:41:26 +01:00
|
|
|
ALWAYS_INLINE const T* operator->() const { return &value(); }
|
|
|
|
ALWAYS_INLINE T* operator->() { return &value(); }
|
2020-12-30 21:16:37 +01:00
|
|
|
|
2019-07-08 11:29:38 +02:00
|
|
|
private:
|
2020-03-08 12:34:33 +01:00
|
|
|
u8 m_storage[sizeof(T)] { 0 };
|
2019-07-08 11:29:38 +02:00
|
|
|
bool m_has_value { false };
|
|
|
|
};
|
2020-02-14 22:29:06 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::Optional;
|