6#ifndef GIMO_ALGORITHM_AND_THEN_HPP
7#define GIMO_ALGORITHM_AND_THEN_HPP
20namespace gimo::detail::and_then
22 template <
typename Nullable,
typename Action>
23 consteval Nullable* print_diagnostics()
25 if constexpr (!std::is_invocable_v<Action, value_result_t<Nullable>>)
27 static_assert(always_false_v<Nullable>,
"The and_then algorithm requires an action invocable with the nullable's value.");
29 else if constexpr (!nullable<std::invoke_result_t<Action, value_result_t<Nullable>>>)
31 static_assert(always_false_v<Nullable>,
"The and_then algorithm requires an action with a nullable return-type.");
37 template <
typename Nullable,
typename Action>
38 using result_t = std::invoke_result_t<
40 value_result_t<Nullable>>;
42 template <
typename Action, nullable Nullable>
44 constexpr result_t<Nullable, Action> on_value(Action&& action, Nullable&& opt)
47 std::forward<Action>(action),
48 detail::forward_value<Nullable>(opt));
51 template <
typename Action, nullable Nullable,
typename Next,
typename... Steps>
53 constexpr auto on_value(
60 std::forward<Next>(next),
61 and_then::on_value(std::forward<Action>(action), std::forward<Nullable>(opt)),
62 std::forward<Steps>(steps)...);
65 template <
typename Action, nullable Nullable>
67 constexpr result_t<Nullable, Action> on_null(Action&& , Nullable&& )
69 return detail::construct_empty<result_t<Nullable, Action>>();
72 template <
typename Action, expected_like Expected>
74 constexpr result_t<Expected, Action> on_null(Action&& , Expected&& expected)
76 return detail::rebind_error<result_t<Expected, Action>, Expected>(expected);
79 template <
typename Action, nullable Nullable,
typename Next,
typename... Steps>
81 constexpr auto on_null(Action&& action, Nullable&& opt, Next&& next, Steps&&... steps)
83 return std::forward<Next>(next).on_null(
84 and_then::on_null(std::forward<Action>(action), std::forward<Nullable>(opt)),
85 std::forward<Steps>(steps)...);
90 template <nullable Nullable,
typename Action>
91 static constexpr bool is_applicable_on =
requires {
92 requires nullable<result_t<Nullable, Action>>;
95 template <
typename Action, nullable Nullable,
typename... Steps>
97 static constexpr auto on_value(Action&& action, Nullable&& opt, Steps&&... steps)
99 GIMO_ASSERT(detail::has_value(opt),
"Nullable is empty while it's expected to contain a value.");
101 if constexpr (is_applicable_on<Nullable, Action>)
103 return and_then::on_value(
104 std::forward<Action>(action),
105 std::forward<Nullable>(opt),
106 std::forward<Steps>(steps)...);
110 return *and_then::print_diagnostics<Nullable, Action>();
114 template <
typename Action, nullable Nullable,
typename... Steps>
116 static constexpr auto on_null(Action&& action, Nullable&& opt, Steps&&... steps)
118 GIMO_ASSERT(!detail::has_value(opt),
"Nullable contains a value while it's expected to be empty.");
120 if constexpr (is_applicable_on<Nullable, Action>)
122 return and_then::on_null(
123 std::forward<Action>(action),
124 std::forward<Nullable>(opt),
125 std::forward<Steps>(steps)...);
129 return *and_then::print_diagnostics<Nullable, Action>();
139 template <
typename Action>
140 using and_then_t = BasicAlgorithm<and_then::traits, std::remove_cvref_t<Action>>;
159 template <
typename Action>
163 using Algorithm = detail::and_then_t<Action>;
165 return Pipeline{std::tuple<Algorithm>{std::forward<Action>(action)}};
#define GIMO_ASSERT(condition, msg,...)
Definition Config.hpp:11
A composite object representing a sequence of monadic operations.
Definition Pipeline.hpp:30
constexpr auto and_then(Action &&action)
Creates a pipeline step that applies a function returning a nullable type.
Definition AndThen.hpp:161
Definition AndForward.hpp:21