mimic++ v6
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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"
12#include "mimic++/Utility.hpp"
14
15#include <concepts>
16#include <functional>
17#include <type_traits>
18#include <utility>
19
21{
22 template <typename Action>
23 requires std::same_as<Action, std::remove_cvref_t<Action>>
24 && std::is_move_constructible_v<Action>
26 {
27 public:
28 [[nodiscard]]
29 explicit constexpr ReturnsResultOf(Action&& action)
30 noexcept(std::is_nothrow_move_constructible_v<Action>)
31 : m_Action{std::move(action)}
32 {
33 }
34
35 template <typename Return, typename... Args>
36 requires std::invocable<Action&, const call::Info<Return, Args...>&>
38 std::invoke_result_t<Action&, const call::Info<Return, Args...>&>,
39 Return>
40 [[nodiscard]]
41 constexpr Return finalize_call([[maybe_unused]] const call::Info<Return, Args...>& call)
42 noexcept(
43 std::is_nothrow_invocable_v<Action&, const call::Info<Return, Args...>&>
44 && nothrow_explicitly_convertible_to<std::invoke_result_t<Action&, const call::Info<Return, Args...>&>, Return>)
45 {
46 return static_cast<Return>(
47 std::invoke(m_Action, call));
48 }
49
50 private:
51 Action m_Action;
52 };
53
54 template <typename Exception>
55 requires(!std::is_reference_v<Exception>)
56 && std::copyable<Exception>
57 class Throws
58 {
59 public:
60 [[nodiscard]]
61 explicit constexpr Throws(Exception exception) noexcept(std::is_nothrow_move_constructible_v<Exception>)
62 : m_Exception{std::move(exception)}
63 {
64 }
65
66 template <typename Return, typename... Args>
67 constexpr Return finalize_call(
68 [[maybe_unused]] const call::Info<Return, Args...>& call)
69 {
70 throw m_Exception; // NOLINT(hicpp-exception-baseclass)
71 }
72
73 private:
74 Exception m_Exception;
75 };
76}
77
79{
98
109 template <typename Fun>
110 requires std::invocable<std::remove_cvref_t<Fun>&>
111 && (!std::is_void_v<std::invoke_result_t<std::remove_cvref_t<Fun>&>>)
112 [[nodiscard]]
113 constexpr auto returns_result_of(Fun&& fun)
114 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Fun>, Fun>)
115 {
117 [fn = std::forward<Fun>(fun)]([[maybe_unused]] const auto& call) mutable noexcept(
118 std::is_nothrow_invocable_v<decltype(fun)>) -> decltype(auto) {
119 return std::invoke(fn);
120 }};
121 }
122
140 template <typename T>
141 requires std::copyable<std::remove_cvref_t<T>>
142 [[nodiscard]]
143 constexpr auto returns(T&& value)
144 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<T>, T>)
145 {
147 [v = std::forward<T>(value)]([[maybe_unused]] const auto& call) mutable noexcept -> auto& {
148 return static_cast<std::unwrap_reference_t<decltype(v)>&>(v);
149 }};
150 }
151
164 template <std::size_t index, std::size_t... otherIndices, typename Action>
165 [[nodiscard]]
166 constexpr auto returns_apply_result_of(Action&& action)
167 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
168 {
169 using arg_selector_t = detail::args_selector_fn<
170 std::add_lvalue_reference_t,
171 std::index_sequence<index, otherIndices...>>;
172 using apply_strategy_t = detail::arg_list_forward_apply_fn;
173
175 std::bind_front(
176 detail::apply_args_fn{
177 arg_selector_t{},
178 apply_strategy_t{}},
179 std::forward<Action>(action))};
180 }
181
192 template <typename Action>
193 [[nodiscard]]
194 constexpr auto returns_apply_all_result_of(Action&& action)
195 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
196 {
197 using arg_selector_t = detail::all_args_selector_fn<std::add_lvalue_reference_t>;
198 using apply_strategy_t = detail::arg_list_forward_apply_fn;
199
201 std::bind_front(
202 detail::apply_args_fn{
203 arg_selector_t{},
204 apply_strategy_t{}},
205 std::forward<Action>(action))};
206 }
207
215 template <std::size_t index>
216 [[nodiscard]]
217 consteval auto returns_arg() noexcept
218 {
219 using arg_selector_t = detail::args_selector_fn<
220 std::add_rvalue_reference_t,
221 std::index_sequence<index>>;
222 using apply_strategy_t = detail::arg_list_forward_apply_fn;
223
225 std::bind_front(
226 detail::apply_args_fn{
227 arg_selector_t{},
228 apply_strategy_t{}},
229 std::identity{})};
230 }
231
241 template <typename T>
242 requires std::copyable<std::remove_cvref_t<T>>
243 [[nodiscard]]
245 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<T>, T>)
246 {
248 std::forward<T>(exception)};
249 }
250
254}
255
256#endif
Definition Fwd.hpp:19
Definition FinalizerPolicies.hpp:26
constexpr Return finalize_call(const call::Info< Return, Args... > &call) noexcept(std::is_nothrow_invocable_v< Action &, const call::Info< Return, Args... > & > &&nothrow_explicitly_convertible_to< std::invoke_result_t< Action &, const call::Info< Return, Args... > & >, Return >)
Definition FinalizerPolicies.hpp:41
constexpr ReturnsResultOf(Action &&action) noexcept(std::is_nothrow_move_constructible_v< Action >)
Definition FinalizerPolicies.hpp:29
Definition FinalizerPolicies.hpp:58
constexpr Return finalize_call(const call::Info< Return, Args... > &call)
Definition FinalizerPolicies.hpp:67
constexpr Throws(Exception exception) noexcept(std::is_nothrow_move_constructible_v< Exception >)
Definition FinalizerPolicies.hpp:61
Definition Utility.hpp:53
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:194
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:166
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:143
consteval auto returns_arg() noexcept
During the finalization step, the selected call argument is returned.
Definition FinalizerPolicies.hpp:217
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:113
Definition Call.hpp:20
Definition ArgRequirementPolicies.hpp:22
Definition FinalizerPolicies.hpp:79