mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +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:
parent
993ab84a0d
commit
28362fcc57
Notes:
sideshowbarker
2024-07-19 12:59:05 +09:00
Author: https://github.com/rburchell Commit: https://github.com/SerenityOS/serenity/commit/28362fcc578 Pull-request: https://github.com/SerenityOS/serenity/pull/385
1 changed files with 24 additions and 7 deletions
|
@ -1,12 +1,15 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/Platform.h>
|
||||
|
||||
template<typename T>
|
||||
class alignas(T) Optional {
|
||||
class CONSUMABLE(unknown) alignas(T) Optional {
|
||||
public:
|
||||
RETURN_TYPESTATE(unknown)
|
||||
Optional() {}
|
||||
|
||||
RETURN_TYPESTATE(unknown)
|
||||
Optional(T&& value)
|
||||
: m_has_value(true)
|
||||
{
|
||||
|
@ -14,29 +17,33 @@ public:
|
|||
}
|
||||
|
||||
template<typename U>
|
||||
RETURN_TYPESTATE(unknown)
|
||||
Optional(U&& value)
|
||||
: m_has_value(true)
|
||||
{
|
||||
new (&m_storage) T(move(value));
|
||||
}
|
||||
|
||||
RETURN_TYPESTATE(unknown)
|
||||
Optional(Optional&& other)
|
||||
: m_has_value(other.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;
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPESTATE(unknown)
|
||||
Optional(const Optional& other)
|
||||
: m_has_value(other.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)
|
||||
{
|
||||
if (this != &other) {
|
||||
|
@ -49,6 +56,7 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
RETURN_TYPESTATE(unknown)
|
||||
Optional& operator=(Optional&& other)
|
||||
{
|
||||
if (this != &other) {
|
||||
|
@ -74,20 +82,23 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
SET_TYPESTATE(consumed)
|
||||
bool has_value() const { return m_has_value; }
|
||||
|
||||
CALLABLE_WHEN(consumed)
|
||||
T& value()
|
||||
{
|
||||
ASSERT(m_has_value);
|
||||
return *reinterpret_cast<T*>(&m_storage);
|
||||
}
|
||||
|
||||
CALLABLE_WHEN(consumed)
|
||||
const T& value() const
|
||||
{
|
||||
ASSERT(m_has_value);
|
||||
return *reinterpret_cast<const T*>(&m_storage);
|
||||
return value_without_consume_state();
|
||||
}
|
||||
|
||||
CALLABLE_WHEN(consumed)
|
||||
T release_value()
|
||||
{
|
||||
ASSERT(m_has_value);
|
||||
|
@ -98,14 +109,20 @@ public:
|
|||
|
||||
T value_or(const T& fallback) const
|
||||
{
|
||||
if (has_value())
|
||||
if (m_has_value)
|
||||
return value();
|
||||
return fallback;
|
||||
}
|
||||
|
||||
operator bool() const { return has_value(); }
|
||||
operator bool() const { return m_has_value; }
|
||||
|
||||
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)];
|
||||
bool m_has_value { false };
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue