mimic++ v10.0.1
Loading...
Searching...
No Matches
FinalizerPolicies.hpp
Go to the documentation of this file.
1// Copyright Dominic (DNKpp) Koepke 2024-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 MIMICPP_EXPECTATION_POLICIES_FINALIZER_POLICIES_HPP
7#define MIMICPP_EXPECTATION_POLICIES_FINALIZER_POLICIES_HPP
8
9#pragma once
10
11#include "mimic++/Fwd.hpp"
16
17#ifndef MIMICPP_DETAIL_IS_MODULE
18 #include <concepts>
19 #include <functional>
20 #include <type_traits>
21 #include <utility>
22#endif
23
25{
26 template <typename Action>
27 requires std::same_as<Action, std::remove_cvref_t<Action>>
28 && std::is_move_constructible_v<Action>
30 {
31 public:
32 [[nodiscard]]
33 explicit constexpr ReturnsResultOf(Action&& action)
34 noexcept(std::is_nothrow_move_constructible_v<Action>)
35 : m_Action{std::move(action)}
36 {
37 }
38
39 template <typename Return, typename... Args>
40 requires std::invocable<Action&, call::Info<Return, Args...> const&>
42 std::invoke_result_t<Action&, call::Info<Return, Args...> const&>,
43 Return>
44 [[nodiscard]]
45 constexpr Return finalize_call([[maybe_unused]] call::Info<Return, Args...> const& call)
46 noexcept(
47 std::is_nothrow_invocable_v<Action&, call::Info<Return, Args...> const&>
48 && util::nothrow_explicitly_convertible_to<std::invoke_result_t<Action&, call::Info<Return, Args...> const&>, Return>)
49 {
50 return static_cast<Return>(
51 std::invoke(m_Action, call));
52 }
53
54 private:
55 Action m_Action;
56 };
57
58 template <typename Exception>
59 requires(!std::is_reference_v<Exception>)
60 && std::copyable<Exception>
61 class Throws
62 {
63 public:
64 [[nodiscard]]
65 explicit constexpr Throws(Exception exception) noexcept(std::is_nothrow_move_constructible_v<Exception>)
66 : m_Exception{std::move(exception)}
67 {
68 }
69
70 template <typename Return, typename... Args>
71 constexpr Return finalize_call(call::Info<Return, Args...> const& /*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]] auto const& 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]] auto const& call) mutable noexcept -> auto& {
151 return util::unwrap_ref(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 = expectation::policies::detail::args_selector_fn<
173 std::add_lvalue_reference_t,
174 std::index_sequence<index, otherIndices...>>;
175 using apply_strategy_t = expectation::policies::detail::arg_list_forward_apply_fn;
176
178 std::bind_front(
179 expectation::policies::detail::apply_args_fn{arg_selector_t{}, apply_strategy_t{}},
180 std::forward<Action>(action))};
181 }
182
193 template <typename Action>
194 [[nodiscard]]
195 constexpr auto returns_apply_all_result_of(Action&& action)
196 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
197 {
198 using arg_selector_t = expectation::policies::detail::all_args_selector_fn<std::add_lvalue_reference_t>;
199 using apply_strategy_t = expectation::policies::detail::arg_list_forward_apply_fn;
200
202 std::bind_front(
203 expectation::policies::detail::apply_args_fn{arg_selector_t{}, apply_strategy_t{}},
204 std::forward<Action>(action))};
205 }
206
214 template <std::size_t index>
215 [[nodiscard]]
216 consteval auto returns_arg() noexcept
217 {
218 using arg_selector_t = expectation::policies::detail::args_selector_fn<
219 std::add_rvalue_reference_t,
220 std::index_sequence<index>>;
221 using apply_strategy_t = expectation::policies::detail::arg_list_forward_apply_fn;
222
224 std::bind_front(
225 expectation::policies::detail::apply_args_fn{arg_selector_t{}, apply_strategy_t{}},
226 std::identity{})};
227 }
228
238 template <typename T>
239 requires std::copyable<std::remove_cvref_t<T>>
240 [[nodiscard]]
242 T && exception) noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<T>, T>)
243 {
245 std::forward<T>(exception)};
246 }
247
251}
252
253#endif
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
Definition Call.hpp:27
Definition FinalizerPolicies.hpp:30
constexpr ReturnsResultOf(Action &&action) noexcept(std::is_nothrow_move_constructible_v< Action >)
Definition FinalizerPolicies.hpp:33
constexpr Return finalize_call(call::Info< Return, Args... > const &call) noexcept(std::is_nothrow_invocable_v< Action &, call::Info< Return, Args... > const & > &&util::nothrow_explicitly_convertible_to< std::invoke_result_t< Action &, call::Info< Return, Args... > const & >, Return >)
Definition FinalizerPolicies.hpp:45
Definition FinalizerPolicies.hpp:62
constexpr Return finalize_call(call::Info< Return, Args... > const &)
Definition FinalizerPolicies.hpp:71
constexpr Throws(Exception exception) noexcept(std::is_nothrow_move_constructible_v< Exception >)
Definition FinalizerPolicies.hpp:65
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:195
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:216
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 ArgumentList.hpp:24
Definition FinalizerPolicies.hpp:82
constexpr T & unwrap_ref(std::reference_wrapper< T > const ref) noexcept
Unwraps the given std::reference_wrapper and returns the referenced object.
Definition UnwrapRef.hpp:29