Made Optional::then() use deducing this so it supports const and non-const optionals.
This commit is contained in:
parent
7658e8fbda
commit
686dff506d
@ -157,17 +157,16 @@ public:
|
|||||||
[[nodiscard]] constexpr bool empty() const noexcept { return storage_.empty(); }
|
[[nodiscard]] constexpr bool empty() const noexcept { return storage_.empty(); }
|
||||||
inline void reset() noexcept;
|
inline void reset() noexcept;
|
||||||
|
|
||||||
|
template<typename TSelf, typename TCallable, typename TErrorCallable = int>
|
||||||
template<typename TCallable, typename TErrorCallable = int>
|
auto then(this TSelf&& self, TCallable&& onSuccess, TErrorCallable&& onError = 0)
|
||||||
auto then(TCallable&& onSuccess, TErrorCallable&& onError = 0)
|
|
||||||
{
|
{
|
||||||
using result_t = std::invoke_result_t<TCallable, std::add_rvalue_reference_t<TValue>>;
|
using result_t = std::invoke_result_t<TCallable, std::add_rvalue_reference_t<TValue>>;
|
||||||
|
|
||||||
if constexpr (std::is_same_v<result_t, void>)
|
if constexpr (std::is_same_v<result_t, void>)
|
||||||
{
|
{
|
||||||
if (!empty())
|
if (!self.empty())
|
||||||
{
|
{
|
||||||
std::invoke(std::forward<TCallable>(onSuccess), get());
|
std::invoke(std::forward<TCallable>(onSuccess), self.get());
|
||||||
}
|
}
|
||||||
else if constexpr (!std::is_same_v<TErrorCallable, int>)
|
else if constexpr (!std::is_same_v<TErrorCallable, int>)
|
||||||
{
|
{
|
||||||
@ -176,9 +175,9 @@ public:
|
|||||||
}
|
}
|
||||||
else if constexpr (requires(result_t obj) { {obj == NULL_OPTIONAL} -> std::convertible_to<bool>; })
|
else if constexpr (requires(result_t obj) { {obj == NULL_OPTIONAL} -> std::convertible_to<bool>; })
|
||||||
{
|
{
|
||||||
if (!empty())
|
if (!self.empty())
|
||||||
{
|
{
|
||||||
return std::invoke(std::forward<TCallable>(onSuccess), get());
|
return std::invoke(std::forward<TCallable>(onSuccess), self.get());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user