mimic++ v6
Loading...
Searching...
No Matches
SideEffectPolicies.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_SIDE_EFFECT_POLICIES_HPP
7#define MIMICPP_POLICIES_SIDE_EFFECT_POLICIES_HPP
8
9#pragma once
10
11#include "mimic++/Fwd.hpp"
13
14#include <concepts>
15#include <functional>
16#include <type_traits>
17#include <utility>
18
20{
21 template <typename Action>
23 {
24 public:
25 ~SideEffectAction() = default;
26
27 [[nodiscard]]
28 explicit constexpr SideEffectAction(
29 Action&& action) noexcept(std::is_nothrow_move_constructible_v<Action>)
30 : m_Action{std::move(action)}
31 {
32 }
33
36
37 [[nodiscard]]
40
41 static constexpr bool is_satisfied() noexcept
42 {
43 return true;
44 }
45
46 template <typename Return, typename... Args>
47 [[nodiscard]]
48 static constexpr bool matches(const call::Info<Return, Args...>&) noexcept
49 {
50 return true;
51 }
52
53 [[nodiscard]]
54 static std::nullopt_t describe() noexcept
55 {
56 return std::nullopt;
57 }
58
59 template <typename Return, typename... Args>
60 requires std::invocable<Action&, const call::Info<Return, Args...>&>
61 constexpr void consume(const call::Info<Return, Args...>& info)
62 noexcept(std::is_nothrow_invocable_v<Action&, const call::Info<Return, Args...>&>)
63 {
64 std::invoke(m_Action, info);
65 }
66
67 private:
68 Action m_Action;
69 };
70}
71
73{
94
102 template <std::size_t index, typename Action>
103 [[nodiscard]]
104 constexpr auto apply_arg(Action&& action)
105 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
106 {
107 using arg_selector_t = detail::args_selector_fn<
108 std::add_lvalue_reference_t,
109 std::index_sequence<index>>;
110 using apply_strategy_t = detail::arg_list_forward_apply_fn;
111
113 std::bind_front(
114 detail::apply_args_fn{
115 arg_selector_t{},
116 apply_strategy_t{}},
117 std::forward<Action>(action))};
118 }
119
130 template <std::size_t index, std::size_t... additionalIndices, typename Action>
131 [[nodiscard]]
132 constexpr auto apply_args(Action&& action)
133 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
134 {
135 using arg_selector_t = detail::args_selector_fn<
136 std::add_lvalue_reference_t,
137 std::index_sequence<index, additionalIndices...>>;
138 using apply_strategy_t = detail::arg_list_forward_apply_fn;
139
141 std::bind_front(
142 detail::apply_args_fn{
143 arg_selector_t{},
144 apply_strategy_t{}},
145 std::forward<Action>(action))};
146 }
147
154 template <typename Action>
155 [[nodiscard]]
156 constexpr auto apply_all(Action&& action)
157 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
158 {
159 using arg_selector_t = detail::all_args_selector_fn<std::add_lvalue_reference_t>;
160 using apply_strategy_t = detail::arg_list_forward_apply_fn;
161
163 std::bind_front(
164 detail::apply_args_fn{
165 arg_selector_t{},
166 apply_strategy_t{}},
167 std::forward<Action>(action))};
168 }
169
176 template <std::invocable Action>
177 [[nodiscard]]
178 constexpr auto invoke(Action&& action)
179 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
180 {
182 [fn = std::forward<Action>(action)]([[maybe_unused]] const auto& call) mutable noexcept(
183 std::is_nothrow_invocable_v<Action&>) {
184 std::invoke(fn);
185 }};
186 }
187
191}
192
193#endif
Definition Fwd.hpp:19
Definition SideEffectPolicies.hpp:23
static constexpr bool matches(const call::Info< Return, Args... > &) noexcept
Definition SideEffectPolicies.hpp:48
static std::nullopt_t describe() noexcept
Definition SideEffectPolicies.hpp:54
constexpr void consume(const call::Info< Return, Args... > &info) noexcept(std::is_nothrow_invocable_v< Action &, const call::Info< Return, Args... > & >)
Definition SideEffectPolicies.hpp:61
SideEffectAction & operator=(const SideEffectAction &)=delete
static constexpr bool is_satisfied() noexcept
Definition SideEffectPolicies.hpp:41
SideEffectAction(SideEffectAction &&)=default
constexpr SideEffectAction(Action &&action) noexcept(std::is_nothrow_move_constructible_v< Action >)
Definition SideEffectPolicies.hpp:28
SideEffectAction & operator=(SideEffectAction &&)=default
SideEffectAction(const SideEffectAction &)=delete
constexpr auto apply_all(Action &&action) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Action >, Action >)
Applies all arguments on the given action.
Definition SideEffectPolicies.hpp:156
constexpr auto apply_arg(Action &&action) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Action >, Action >)
Applies the argument at the specified index on the given action.
Definition SideEffectPolicies.hpp:104
constexpr auto invoke(Action &&action) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Action >, Action >)
Invokes the given function.
Definition SideEffectPolicies.hpp:178
constexpr auto apply_args(Action &&action) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Action >, Action >)
Applies the arguments at the specified index and in that order on the given action.
Definition SideEffectPolicies.hpp:132
Definition Call.hpp:20
Definition ArgRequirementPolicies.hpp:22
Definition SideEffectPolicies.hpp:73