6#ifndef GIMO_ALGORITHM_TRANSFORM_HPP
7#define GIMO_ALGORITHM_TRANSFORM_HPP
20namespace gimo::detail::transform
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 transform algorithm requires an action invocable with the nullable's value.");
29 else if constexpr (!rebindable_value_to<Nullable, std::invoke_result_t<Action, value_result_t<Nullable>>>)
31 static_assert(always_false_v<Nullable>,
"The transform algorithm requires a nullable whose value-type can be rebound.");
37 template <
typename Nullable,
typename Action>
40 std::invoke_result_t<Action, value_result_t<Nullable>>>;
42 template <
typename Action, nullable Nullable>
44 constexpr result_t<Nullable, Action> on_value(Action&& action, Nullable&& opt)
48 std::forward<Action>(action),
49 detail::forward_value<Nullable>(opt)));
52 template <
typename Action, nullable Nullable,
typename Next,
typename... Steps>
54 constexpr auto on_value(
60 return std::forward<Next>(next).on_value(
61 transform::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 <nullable Nullable,
typename Action,
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 transform::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 rebindable_value_to<
94 std::invoke_result_t<Action, value_result_t<Nullable>>>;
97 template <
typename Action, nullable Nullable,
typename... Steps>
99 static constexpr auto on_value(Action&& action, Nullable&& opt, Steps&&... steps)
101 GIMO_ASSERT(detail::has_value(opt),
"Nullable is empty while it's expected to contain a value.");
103 if constexpr (is_applicable_on<Nullable, Action>)
105 return transform::on_value(
106 std::forward<Action>(action),
107 std::forward<Nullable>(opt),
108 std::forward<Steps>(steps)...);
112 return *transform::print_diagnostics<Nullable, Action>();
116 template <
typename Action, nullable Nullable,
typename... Steps>
118 static constexpr auto on_null(Action&& action, Nullable&& opt, Steps&&... steps)
120 GIMO_ASSERT(!detail::has_value(opt),
"Nullable contains a value while it's expected to be empty.");
122 if constexpr (is_applicable_on<Nullable, Action>)
124 return transform::on_null(
125 std::forward<Action>(action),
126 std::forward<Nullable>(opt),
127 std::forward<Steps>(steps)...);
131 return *transform::print_diagnostics<Nullable, Action>();
141 template <
typename Action>
142 using transform_t = BasicAlgorithm<transform::traits, std::remove_cvref_t<Action>>;
164 template <
typename Action>
168 using Algorithm = detail::transform_t<Action>;
170 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 transform(Action &&action)
Creates a pipeline step that transforms the underlying value.
Definition Transform.hpp:166
Definition AndForward.hpp:21
constexpr Nullable construct_from_value(Arg &&arg) noexcept(detail::nothrow_constructible_from_value< Nullable, Arg && >)
Constructs the specified Nullable with the provided value.
Definition Common.hpp:269
typename traits< std::remove_cvref_t< Nullable > >::template rebind_value< Value > rebind_value_t
Helper alias to obtain the Nullable type with the rebound Value type.
Definition Common.hpp:280