diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2022-12-10 19:36:32 +0330 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2022-12-11 20:44:54 +0330 |
commit | 96b36203a285baa942e2ca8e4e2d957879d00ba8 (patch) | |
tree | ca23b4e70d0355254a2b4ab95df80d11ed94472b /AK/Optional.h | |
parent | 0ed9fe3864c71a88c86d00e244bb70fcacdc035a (diff) | |
download | serenity-96b36203a285baa942e2ca8e4e2d957879d00ba8.zip |
AK: Add Optional::lazy_emplace(Callable)
This makes it possible to emplace using a given function instead of
passing constructor arguments.
Diffstat (limited to 'AK/Optional.h')
-rw-r--r-- | AK/Optional.h | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/AK/Optional.h b/AK/Optional.h index 08f659dce4..6acf64f384 100644 --- a/AK/Optional.h +++ b/AK/Optional.h @@ -200,6 +200,14 @@ public: new (&m_storage) T(forward<Parameters>(parameters)...); } + template<typename Callable> + ALWAYS_INLINE void lazy_emplace(Callable callable) + { + clear(); + m_has_value = true; + new (&m_storage) T { callable() }; + } + [[nodiscard]] ALWAYS_INLINE bool has_value() const { return m_has_value; } [[nodiscard]] ALWAYS_INLINE T& value() & |