mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 19:45:12 +00:00
AK: Add Optional::emplace method.
This commit is contained in:
parent
b2de1ba779
commit
deb85c47b5
Notes:
sideshowbarker
2024-07-19 03:01:45 +09:00
Author: https://github.com/asynts Commit: https://github.com/SerenityOS/serenity/commit/deb85c47b54 Pull-request: https://github.com/SerenityOS/serenity/pull/3341 Reviewed-by: https://github.com/stelar7
1 changed files with 13 additions and 4 deletions
|
@ -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()
|
||||
|
|
Loading…
Add table
Reference in a new issue