1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:17:34 +00:00

AK: Add Optional::emplace method.

This commit is contained in:
asynts 2020-08-29 19:18:01 +02:00 committed by Andreas Kling
parent b2de1ba779
commit deb85c47b5

View file

@ -34,9 +34,10 @@
namespace AK { namespace AK {
template<typename T> template<typename T>
class alignas(T) [[nodiscard]] Optional { class alignas(T) [[nodiscard]] Optional
{
public: public:
Optional() {} Optional() { }
Optional(const T& value) Optional(const T& value)
: m_has_value(true) : m_has_value(true)
@ -51,13 +52,13 @@ public:
new (&m_storage) T(value); new (&m_storage) T(value);
} }
Optional(T&& value) Optional(T && value)
: m_has_value(true) : m_has_value(true)
{ {
new (&m_storage) T(move(value)); new (&m_storage) T(move(value));
} }
Optional(Optional&& other) Optional(Optional && other)
: m_has_value(other.m_has_value) : m_has_value(other.m_has_value)
{ {
if (other.has_value()) { if (other.has_value()) {
@ -116,6 +117,14 @@ public:
} }
} }
template<typename... Parameters>
ALWAYS_INLINE void emplace(Parameters && ... parameters)
{
clear();
m_has_value = true;
new (&m_storage) T(forward<Parameters>(parameters)...);
}
ALWAYS_INLINE bool has_value() const { return m_has_value; } ALWAYS_INLINE bool has_value() const { return m_has_value; }
ALWAYS_INLINE T& value() ALWAYS_INLINE T& value()