1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:17:35 +00:00

Optional: Add consumable checks

These will, when building with clang, prevent using Optional::value
without first checking Optional::has_value() - at compile time.
This commit is contained in:
Robin Burchell 2019-07-30 19:45:40 +02:00 committed by Andreas Kling
parent 993ab84a0d
commit 28362fcc57

View file

@ -1,12 +1,15 @@
#pragma once #pragma once
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/Platform.h>
template<typename T> template<typename T>
class alignas(T) Optional { class CONSUMABLE(unknown) alignas(T) Optional {
public: public:
RETURN_TYPESTATE(unknown)
Optional() {} Optional() {}
RETURN_TYPESTATE(unknown)
Optional(T&& value) Optional(T&& value)
: m_has_value(true) : m_has_value(true)
{ {
@ -14,29 +17,33 @@ public:
} }
template<typename U> template<typename U>
RETURN_TYPESTATE(unknown)
Optional(U&& value) Optional(U&& value)
: m_has_value(true) : m_has_value(true)
{ {
new (&m_storage) T(move(value)); new (&m_storage) T(move(value));
} }
RETURN_TYPESTATE(unknown)
Optional(Optional&& other) Optional(Optional&& other)
: m_has_value(other.m_has_value) : m_has_value(other.m_has_value)
{ {
if (m_has_value) { if (m_has_value) {
new (&m_storage) T(move(other.value())); new (&m_storage) T(move(other.value_without_consume_state()));
other.m_has_value = false; other.m_has_value = false;
} }
} }
RETURN_TYPESTATE(unknown)
Optional(const Optional& other) Optional(const Optional& other)
: m_has_value(other.m_has_value) : m_has_value(other.m_has_value)
{ {
if (m_has_value) { if (m_has_value) {
new (&m_storage) T(other.value()); new (&m_storage) T(other.value_without_consume_state());
} }
} }
RETURN_TYPESTATE(unknown)
Optional& operator=(const Optional& other) Optional& operator=(const Optional& other)
{ {
if (this != &other) { if (this != &other) {
@ -49,6 +56,7 @@ public:
return *this; return *this;
} }
RETURN_TYPESTATE(unknown)
Optional& operator=(Optional&& other) Optional& operator=(Optional&& other)
{ {
if (this != &other) { if (this != &other) {
@ -74,20 +82,23 @@ public:
} }
} }
SET_TYPESTATE(consumed)
bool has_value() const { return m_has_value; } bool has_value() const { return m_has_value; }
CALLABLE_WHEN(consumed)
T& value() T& value()
{ {
ASSERT(m_has_value); ASSERT(m_has_value);
return *reinterpret_cast<T*>(&m_storage); return *reinterpret_cast<T*>(&m_storage);
} }
CALLABLE_WHEN(consumed)
const T& value() const const T& value() const
{ {
ASSERT(m_has_value); return value_without_consume_state();
return *reinterpret_cast<const T*>(&m_storage);
} }
CALLABLE_WHEN(consumed)
T release_value() T release_value()
{ {
ASSERT(m_has_value); ASSERT(m_has_value);
@ -98,14 +109,20 @@ public:
T value_or(const T& fallback) const T value_or(const T& fallback) const
{ {
if (has_value()) if (m_has_value)
return value(); return value();
return fallback; return fallback;
} }
operator bool() const { return has_value(); } operator bool() const { return m_has_value; }
private: private:
// 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);
}
char m_storage[sizeof(T)]; char m_storage[sizeof(T)];
bool m_has_value { false }; bool m_has_value { false };
}; };