2019-07-08 09:29:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
2019-07-30 17:45:40 +00:00
|
|
|
#include <AK/Platform.h>
|
2019-07-08 09:29:38 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2019-07-30 17:45:40 +00:00
|
|
|
class CONSUMABLE(unknown) alignas(T) Optional {
|
2019-07-08 09:29:38 +00:00
|
|
|
public:
|
2019-07-30 17:45:40 +00:00
|
|
|
RETURN_TYPESTATE(unknown)
|
2019-07-08 09:29:38 +00:00
|
|
|
Optional() {}
|
|
|
|
|
2019-07-30 17:45:40 +00:00
|
|
|
RETURN_TYPESTATE(unknown)
|
2019-07-08 09:29:38 +00:00
|
|
|
Optional(T&& value)
|
|
|
|
: m_has_value(true)
|
|
|
|
{
|
|
|
|
new (&m_storage) T(move(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2019-07-30 17:45:40 +00:00
|
|
|
RETURN_TYPESTATE(unknown)
|
2019-07-08 09:29:38 +00:00
|
|
|
Optional(U&& value)
|
|
|
|
: m_has_value(true)
|
|
|
|
{
|
|
|
|
new (&m_storage) T(move(value));
|
|
|
|
}
|
|
|
|
|
2019-07-30 17:45:40 +00:00
|
|
|
RETURN_TYPESTATE(unknown)
|
2019-07-08 09:29:38 +00:00
|
|
|
Optional(Optional&& other)
|
|
|
|
: m_has_value(other.m_has_value)
|
|
|
|
{
|
2019-08-07 05:17:52 +00:00
|
|
|
if (other.has_value()) {
|
2019-08-05 20:27:47 +00:00
|
|
|
new (&m_storage) T(other.release_value());
|
2019-07-08 09:29:38 +00:00
|
|
|
other.m_has_value = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 17:45:40 +00:00
|
|
|
RETURN_TYPESTATE(unknown)
|
2019-07-08 09:29:38 +00:00
|
|
|
Optional(const Optional& other)
|
|
|
|
: m_has_value(other.m_has_value)
|
|
|
|
{
|
|
|
|
if (m_has_value) {
|
2019-07-30 17:45:40 +00:00
|
|
|
new (&m_storage) T(other.value_without_consume_state());
|
2019-07-08 09:29:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 17:45:40 +00:00
|
|
|
RETURN_TYPESTATE(unknown)
|
2019-07-08 09:29:38 +00:00
|
|
|
Optional& operator=(const Optional& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
m_has_value = other.m_has_value;
|
|
|
|
if (m_has_value) {
|
|
|
|
new (&m_storage) T(other.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-07-30 17:45:40 +00:00
|
|
|
RETURN_TYPESTATE(unknown)
|
2019-07-08 09:29:38 +00:00
|
|
|
Optional& operator=(Optional&& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
m_has_value = other.m_has_value;
|
2019-08-07 05:17:52 +00:00
|
|
|
if (other.has_value())
|
2019-07-08 09:29:38 +00:00
|
|
|
new (&m_storage) T(other.release_value());
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Optional()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
if (m_has_value) {
|
|
|
|
value().~T();
|
|
|
|
m_has_value = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 17:45:40 +00:00
|
|
|
SET_TYPESTATE(consumed)
|
2019-07-08 09:29:38 +00:00
|
|
|
bool has_value() const { return m_has_value; }
|
|
|
|
|
2019-07-30 17:45:40 +00:00
|
|
|
CALLABLE_WHEN(consumed)
|
2019-07-08 09:29:38 +00:00
|
|
|
T& value()
|
|
|
|
{
|
|
|
|
ASSERT(m_has_value);
|
|
|
|
return *reinterpret_cast<T*>(&m_storage);
|
|
|
|
}
|
|
|
|
|
2019-07-30 17:45:40 +00:00
|
|
|
CALLABLE_WHEN(consumed)
|
2019-07-08 09:29:38 +00:00
|
|
|
const T& value() const
|
|
|
|
{
|
2019-07-30 17:45:40 +00:00
|
|
|
return value_without_consume_state();
|
2019-07-08 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 17:45:40 +00:00
|
|
|
CALLABLE_WHEN(consumed)
|
2019-07-08 09:29:38 +00:00
|
|
|
T release_value()
|
|
|
|
{
|
|
|
|
ASSERT(m_has_value);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-24 05:26:02 +00:00
|
|
|
T value_or(const T& fallback) const
|
|
|
|
{
|
2019-07-30 17:45:40 +00:00
|
|
|
if (m_has_value)
|
2019-07-24 05:26:02 +00:00
|
|
|
return value();
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
2019-07-30 17:45:40 +00:00
|
|
|
operator bool() const { return m_has_value; }
|
2019-07-24 08:25:43 +00:00
|
|
|
|
2019-07-08 09:29:38 +00:00
|
|
|
private:
|
2019-07-30 17:45:40 +00:00
|
|
|
// Call when we don't want to alter the consume state
|
|
|
|
const T& value_without_consume_state() const
|
|
|
|
{
|
|
|
|
ASSERT(m_has_value);
|
|
|
|
return *reinterpret_cast<const T*>(&m_storage);
|
|
|
|
}
|
2019-07-24 08:25:43 +00:00
|
|
|
char m_storage[sizeof(T)];
|
2019-07-08 09:29:38 +00:00
|
|
|
bool m_has_value { false };
|
|
|
|
};
|