diff options
author | asynts <asynts@gmail.com> | 2020-08-29 19:18:01 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-30 09:56:10 +0200 |
commit | deb85c47b54b3ea98f74ba297fe0cc0b8d6d1a28 (patch) | |
tree | 933327ef4d37d71c750f2e7256d39ffb5c317901 | |
parent | b2de1ba7796a456858e063638ed0c55a13e3c6c7 (diff) | |
download | serenity-deb85c47b54b3ea98f74ba297fe0cc0b8d6d1a28.zip |
AK: Add Optional::emplace method.
-rw-r--r-- | AK/Optional.h | 17 |
1 files 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<typename T> -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<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 T& value() |