From deb85c47b54b3ea98f74ba297fe0cc0b8d6d1a28 Mon Sep 17 00:00:00 2001 From: asynts Date: Sat, 29 Aug 2020 19:18:01 +0200 Subject: AK: Add Optional::emplace method. --- AK/Optional.h | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/AK/Optional.h b/AK/Optional.h index ac7d04d6e5..9fcfb1a884 100644 --- a/AK/Optional.h +++ b/AK/Optional.h @@ -34,9 +34,10 @@ namespace AK { template -class alignas(T) [[nodiscard]] Optional { +class alignas(T) [[nodiscard]] Optional +{ public: - Optional() {} + Optional() { } Optional(const T& value) : m_has_value(true) @@ -51,13 +52,13 @@ public: new (&m_storage) T(value); } - Optional(T&& value) + Optional(T && value) : m_has_value(true) { new (&m_storage) T(move(value)); } - Optional(Optional&& other) + Optional(Optional && other) : m_has_value(other.m_has_value) { if (other.has_value()) { @@ -116,6 +117,14 @@ public: } } + template + ALWAYS_INLINE void emplace(Parameters && ... parameters) + { + clear(); + m_has_value = true; + new (&m_storage) T(forward(parameters)...); + } + ALWAYS_INLINE bool has_value() const { return m_has_value; } ALWAYS_INLINE T& value() -- cgit v1.2.3