From b9603d9d23d7eb5e4b592fbc40755b31e956acb8 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 12 Jun 2023 22:33:04 +0300 Subject: [PATCH] BAN: Optional can be constructed inplace --- BAN/include/BAN/Optional.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/BAN/include/BAN/Optional.h b/BAN/include/BAN/Optional.h index 7f31c23749..10dc2d7c8f 100644 --- a/BAN/include/BAN/Optional.h +++ b/BAN/include/BAN/Optional.h @@ -15,11 +15,17 @@ namespace BAN Optional(); Optional(const T&); Optional(T&&); + template + Optional(Args&&...); + ~Optional(); Optional& operator=(const Optional&); Optional& operator=(Optional&&); + template + Optional& emplace(Args&&...); + T* operator->(); const T* operator->() const; @@ -58,6 +64,14 @@ namespace BAN new (m_storage) T(BAN::move(value)); } + template + template + Optional::Optional(Args&&... args) + : m_has_value(true) + { + new (m_storage) T(BAN::forward(args)...); + } + template Optional::~Optional() { @@ -73,6 +87,7 @@ namespace BAN m_has_value = true; new (m_storage) T(other.value()); } + return *this; } template @@ -84,6 +99,17 @@ namespace BAN m_has_value = true; new (m_storage) T(BAN::move(other.release_value())); } + return *this; + } + + template + template + Optional& Optional::emplace(Args&&... args) + { + clear(); + m_has_value = true; + new (m_storage) T(BAN::forward(args)...); + return *this; } template