mimic++ v9.2.1
Loading...
Searching...
No Matches
FinalizerPolicies.hpp
Go to the documentation of this file.
1// Copyright Dominic (DNKpp) Koepke 2024 - 2025.
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 MIMICPP_POLICIES_FINALIZER_POLICIES_HPP
7#define MIMICPP_POLICIES_FINALIZER_POLICIES_HPP
8
9#pragma once
10
11#include "mimic++/Fwd.hpp"
15
16#ifndef MIMICPP_DETAIL_IS_MODULE
17 #include <concepts>
18 #include <functional>
19 #include <type_traits>
20 #include <utility>
21#endif
22
24{
25 template <typename Action>
26 requires std::same_as<Action, std::remove_cvref_t<Action>>
27 && std::is_move_constructible_v<Action>
29 {
30 public:
31 [[nodiscard]]
32 explicit constexpr ReturnsResultOf(Action&& action)
33 noexcept(std::is_nothrow_move_constructible_v<Action>)
34 : m_Action{std::move(action)}
35 {
36 }
37
38 template <typename Return, typename... Args>
39 requires std::invocable<Action&, const call::Info<Return, Args...>&>
41 std::invoke_result_t<Action&, const call::Info<Return, Args...>&>,
42 Return>
43 [[nodiscard]]
44 constexpr Return finalize_call([[maybe_unused]] const call::Info<Return, Args...>& call)
45 noexcept(
46 std::is_nothrow_invocable_v<Action&, const call::Info<Return, Args...>&>
47 && util::nothrow_explicitly_convertible_to<std::invoke_result_t<Action&, const call::Info<Return, Args...>&>, Return>)
48 {
49 return static_cast<Return>(
50 std::invoke(m_Action, call));
51 }
52
53 private:
54 Action m_Action;
55 };
56
57 template <typename Exception>
58 requires(!std::is_reference_v<Exception>)
59 && std::copyable<Exception>
60 class Throws
61 {
62 public:
63 [[nodiscard]]
64 explicit constexpr Throws(Exception exception) noexcept(std::is_nothrow_move_constructible_v<Exception>)
65 : m_Exception{std::move(exception)}
66 {
67 }
68
69 template <typename Return, typename... Args>
70 constexpr Return finalize_call(
71 [[maybe_unused]] const call::Info<Return, Args...>& call)
72 {
73 throw m_Exception; // NOLINT(hicpp-exception-baseclass)
74 }
75
76 private:
77 Exception m_Exception;
78 };
79}
80
82{
101
112 template <typename Fun>
113 requires std::invocable<std::remove_cvref_t<Fun>&>
114 && (!std::is_void_v<std::invoke_result_t<std::remove_cvref_t<Fun>&>>)
115 [[nodiscard]]
116 constexpr auto returns_result_of(Fun&& fun)
117 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Fun>, Fun>)
118 {
120 [fn = std::forward<Fun>(fun)]([[maybe_unused]] const auto& call) mutable noexcept(
121 std::is_nothrow_invocable_v<decltype(fun)>) -> decltype(auto) {
122 return std::invoke(fn);
123 }};
124 }
125
143 template <typename T>
144 requires std::copyable<std::remove_cvref_t<T>>
145 [[nodiscard]]
146 constexpr auto returns(T&& value)
147 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<T>, T>)
148 {
150 [v = std::forward<T>(value)]([[maybe_unused]] const auto& call) mutable noexcept -> auto& {
151 return static_cast<std::unwrap_reference_t<decltype(v)>&>(v);
152 }};
153 }
154
167 template <std::size_t index, std::size_t... otherIndices, typename Action>
168 [[nodiscard]]
169 constexpr auto returns_apply_result_of(Action&& action)
170 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
171 {
172 using arg_selector_t = detail::args_selector_fn<
173 std::add_lvalue_reference_t,
174 std::index_sequence<index, otherIndices...>>;
175 using apply_strategy_t = detail::arg_list_forward_apply_fn;
176
178 std::bind_front(
179 detail::apply_args_fn{
180 arg_selector_t{},
181 apply_strategy_t{}},
182 std::forward<Action>(action))};
183 }
184
195 template <typename Action>
196 [[nodiscard]]
197 constexpr auto returns_apply_all_result_of(Action&& action)
198 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
199 {
200 using arg_selector_t = detail::all_args_selector_fn<std::add_lvalue_reference_t>;
201 using apply_strategy_t = detail::arg_list_forward_apply_fn;
202
204 std::bind_front(
205 detail::apply_args_fn{
206 arg_selector_t{},
207 apply_strategy_t{}},
208 std::forward<Action>(action))};
209 }
210
218 template <std::size_t index>
219 [[nodiscard]]
220 consteval auto returns_arg() noexcept
221 {
222 using arg_selector_t = detail::args_selector_fn<
223 std::add_rvalue_reference_t,
224 std::index_sequence<index>>;
225 using apply_strategy_t = detail::arg_list_forward_apply_fn;
226
228 std::bind_front(
229 detail::apply_args_fn{
230 arg_selector_t{},
231 apply_strategy_t{}},
232 std::identity{})};
233 }
234
244 template <typename T>
245 requires std::copyable<std::remove_cvref_t<T>>
246 [[nodiscard]]
248 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<T>, T>)
249 {
251 std::forward<T>(exception)};
252 }
253
257}
258
259#endif
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
Definition Call.hpp:27
Definition FinalizerPolicies.hpp:29
constexpr Return finalize_call(const call::Info< Return, Args... > &call) noexcept(std::is_nothrow_invocable_v< Action &, const call::Info< Return, Args... > & > &&util::nothrow_explicitly_convertible_to< std::invoke_result_t< Action &, const call::Info< Return, Args... > & >, Return >)
Definition FinalizerPolicies.hpp:44
constexpr ReturnsResultOf(Action &&action) noexcept(std::is_nothrow_move_constructible_v< Action >)
Definition FinalizerPolicies.hpp:32
Definition FinalizerPolicies.hpp:61
constexpr Return finalize_call(const call::Info< Return, Args... > &call)
Definition FinalizerPolicies.hpp:70
constexpr Throws(Exception exception) noexcept(std::is_nothrow_move_constructible_v< Exception >)
Definition FinalizerPolicies.hpp:64
Determines, whether From can be explicitly converted to To.
Definition Concepts.hpp:34
Determines, whether From can be explicitly converted to To, without throwing.
Definition Concepts.hpp:43
constexpr auto returns_apply_all_result_of(Action &&action) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Action >, Action >)
During the finalization step, all call arguments are applied on the given action.
Definition FinalizerPolicies.hpp:197
constexpr auto returns_apply_result_of(Action &&action) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Action >, Action >)
During the finalization step, the selected call arguments are applied on the given action.
Definition FinalizerPolicies.hpp:169
constexpr auto returns(T &&value) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< T >, T >)
During the finalization step, the stored value is returned.
Definition FinalizerPolicies.hpp:146
consteval auto returns_arg() noexcept
During the finalization step, the selected call argument is returned.
Definition FinalizerPolicies.hpp:220
constexpr auto returns_result_of(Fun &&fun) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Fun >, Fun >)
During the finalization step, the invocation result of the given function is returned.
Definition FinalizerPolicies.hpp:116
Definition Call.hpp:24
Definition ArgRequirementPolicies.hpp:28
Definition FinalizerPolicies.hpp:82