From 7e8e2ef0be2eebc7f872ea37620dc1b60d62197f Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Sat, 2 Nov 2024 20:08:07 +0100 Subject: Add option::and_then --- asl/option.hpp | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'asl/option.hpp') diff --git a/asl/option.hpp b/asl/option.hpp index ab88992..ebb9eef 100644 --- a/asl/option.hpp +++ b/asl/option.hpp @@ -3,6 +3,7 @@ #include "asl/assert.hpp" #include "asl/meta.hpp" #include "asl/maybe_uninit.hpp" +#include "asl/functional.hpp" namespace asl { @@ -334,7 +335,6 @@ public: constexpr bool has_value() const { return m_has_value; } - // @Todo Do we want this on rvalues? Or maybe some kind of unwrap? constexpr T&& value() && { ASL_ASSERT(m_has_value); // @Todo Release assert @@ -389,13 +389,46 @@ public: } template - T& emplace(Args&&... args) & + constexpr T& emplace(Args&&... args) & requires constructible { if (m_has_value) { reset(); } construct(ASL_FWD(args)...); return value(); } + + template + constexpr auto and_then(F&& f) & + requires is_option> + { + if (has_value()) + { + return invoke(ASL_FWD(f), value()); + } + return un_cvref_t>{}; + } + + template + constexpr auto and_then(F&& f) const& + requires is_option> + { + if (has_value()) + { + return invoke(ASL_FWD(f), value()); + } + return un_cvref_t>{}; + } + + template + constexpr auto and_then(F&& f) && + requires is_option> + { + if (has_value()) + { + return invoke(ASL_FWD(f), ASL_MOVE(value())); + } + return un_cvref_t>{}; + } }; template -- cgit