6#ifndef GIMO_ALGORITHM_VALUE_OR_ELSE_HPP
7#define GIMO_ALGORITHM_VALUE_OR_ELSE_HPP
20namespace gimo::detail::value_or_else
22 template <
typename Nullable>
23 using result_t = std::remove_cvref_t<value_result_t<Nullable>>;
25 template <
typename Nullable,
typename Action>
26 consteval Nullable* print_diagnostics()
28 if constexpr (!std::convertible_to<std::invoke_result_t<Action>, result_t<Nullable>>)
30 static_assert(always_false_v<Nullable>,
"The value_or algorithm requires an alternative that is convertible to the nullable's value-type.");
38 template <nullable Nullable,
typename Action>
39 static constexpr bool is_applicable_on =
requires {
40 requires std::convertible_to<
41 std::invoke_result_t<Action>,
45 template <
typename Action, nullable Nullable>
47 static constexpr auto on_value([[maybe_unused]] Action&& action, Nullable&& opt)
49 if constexpr (is_applicable_on<Nullable, Action>)
51 return detail::forward_value<Nullable>(opt);
55 return *value_or_else::print_diagnostics<Nullable, Action>();
59 template <
typename Action, nullable Nullable>
61 static constexpr auto on_null(Action&& action, [[maybe_unused]] Nullable&& opt)
63 if constexpr (is_applicable_on<Nullable, Action>)
65 return static_cast<result_t<Nullable>
>(
66 std::invoke(std::forward<Action>(action)));
70 return *value_or_else::print_diagnostics<Nullable, Action>();
80 template <
typename Action>
81 using value_or_else_t = BasicAlgorithm<
82 value_or_else::traits,
83 std::remove_cvref_t<Action>>;
85 template <unqualified T>
90 explicit constexpr ValueStorageFun(T value)
noexcept(std::is_nothrow_move_constructible_v<T>)
91 : m_value{std::move(value)}
96 constexpr T& operator()() &
noexcept
102 constexpr T
const& operator()() const& noexcept
108 constexpr T&& operator()() &&
noexcept
110 return std::move(m_value);
114 constexpr T
const&& operator()() const&& noexcept
116 return std::move(m_value);
123 template <
typename Alternative>
124 using value_or_t = value_or_else_t<
125 ValueStorageFun<std::decay_t<Alternative>>>;
141 template <std::invocable Action>
145 using Algorithm = detail::value_or_else_t<Action>;
148 std::tuple<Algorithm>{std::forward<Action>(action)}};
163 template <
typename Alternative>
167 using Algorithm = detail::value_or_t<Alternative>;
169 return Pipeline{std::tuple<Algorithm>{std::forward<Alternative>(alternative)}};
A composite object representing a sequence of monadic operations.
Definition Pipeline.hpp:30
constexpr auto value_or_else(Action &&action)
Creates a terminating pipeline step that returns the contained value or invokes a fallback action if ...
Definition ValueOrElse.hpp:143
constexpr auto value_or(Alternative &&alternative)
Creates a terminating pipeline step that returns the contained value or a specified alternative if th...
Definition ValueOrElse.hpp:165
Definition AndThen.hpp:21