gimo v0.3.2
Loading...
Searching...
No Matches
AndForward.hpp
Go to the documentation of this file.
1// Copyright Dominic (DNKpp) Koepke 2026.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// https://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef GIMO_ALGORITHM_AND_FORWARD_HPP
7#define GIMO_ALGORITHM_AND_FORWARD_HPP
8
9#pragma once
10
11#include "gimo/Common.hpp"
12#include "gimo/Pipeline.hpp"
14
15#include <concepts>
16#include <functional>
17#include <tuple>
18#include <utility>
19
20namespace gimo::detail::and_forward
21{
22 template <typename Nullable, typename Action>
23 consteval void print_diagnostics()
24 {
25 if constexpr (!std::invocable<Action, value_result_t<Nullable>>)
26 {
27 static_assert(always_false_v<Nullable>, "The and_forward algorithm requires an action invocable with the nullable's value.");
28 }
29 }
30
31 struct traits
32 {
33 template <nullable Nullable, typename Action>
34 static constexpr bool is_applicable_on = requires {
35 requires std::invocable<Action, value_result_t<Nullable>>;
36 };
37
38 template <typename Action, nullable Nullable>
39 static constexpr void on_value(Action&& action, Nullable&& opt)
40 {
41 GIMO_ASSERT(detail::has_value(opt), "Nullable is empty while it's expected to contain a value.");
42
43 if constexpr (is_applicable_on<Nullable, Action>)
44 {
45 std::invoke(
46 std::forward<Action>(action),
47 detail::forward_value<Nullable>(opt));
48 }
49 else
50 {
51 and_forward::print_diagnostics<Nullable, Action>();
52 }
53 }
54
55 template <typename Action, nullable Nullable>
56 static constexpr void on_null(Action&& /*action*/, [[maybe_unused]] Nullable&& opt)
57 {
58 GIMO_ASSERT(!detail::has_value(opt), "Nullable contains a value while it's expected to be empty.");
59
60 if constexpr (!is_applicable_on<Nullable, Action>)
61 {
62 and_forward::print_diagnostics<Nullable, Action>();
63 }
64 }
65 };
66}
67
68namespace gimo
69{
70 namespace detail
71 {
72 template <typename Action>
73 using and_forward_t = BasicAlgorithm<
74 and_forward::traits,
75 std::remove_cvref_t<Action>>;
76 }
77
89 template <typename Action>
90 [[nodiscard]]
91 constexpr auto and_forward(Action&& action)
92 {
93 using Algorithm = detail::and_forward_t<Action>;
94
95 return Pipeline{std::tuple<Algorithm>{std::forward<Action>(action)}};
96 }
97}
98
99#endif
#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_forward(Action &&action)
Creates a terminating pipeline step that forwards the contained value to the specified action.
Definition AndForward.hpp:91
Definition AndForward.hpp:21
The central customization point for the library.
Definition Common.hpp:102