Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
invoke.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A5_0_3-a "False positive: There is no pointer indirection in return types"
2
3// Copyright 2024, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FUNCTIONAL_INVOKE_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FUNCTIONAL_INVOKE_HPP_
9
10// IWYU pragma: private, include "arene/base/functional.hpp"
11// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
12
13// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
14#include "arene/base/constraints/constraints.hpp"
15#include "arene/base/stdlib_choice/enable_if.hpp"
16#include "arene/base/stdlib_choice/forward.hpp"
17#include "arene/base/stdlib_choice/is_base_of.hpp"
18#include "arene/base/stdlib_choice/is_member_function_pointer.hpp"
19#include "arene/base/stdlib_choice/is_member_object_pointer.hpp"
20#include "arene/base/stdlib_choice/remove_cv.hpp"
21#include "arene/base/stdlib_choice/remove_reference.hpp"
22#include "arene/base/type_traits/is_invocable.hpp"
23#include "arene/base/type_traits/is_reference_wrapper.hpp"
24#include "arene/base/type_traits/member_pointer_class_type.hpp"
25#include "arene/base/type_traits/remove_cvref.hpp"
26// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
27
28// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
29// parasoft-begin-suppress AUTOSAR-A15_4_3-a-2 "False positive: This file is the only declaration of these functions"
30// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
31
32namespace arene {
33namespace base {
34
35namespace invoke_detail {
36// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
37// parasoft-begin-suppress CERT_CPP-ERR55-a-2 "False positive: Exception specification is conditional"
38// parasoft-begin-suppress CERT_CPP-ERR50-h-3 "False positive: Exception specification is conditional"
39// parasoft-begin-suppress AUTOSAR-A15_5_3-h-2 "False positive: Exception specification is conditional"
40/// @brief Invoke a function or object with a function call operator, passing a specified list of arguments
41/// @tparam F The type of the function or callable object
42/// @tparam Args The types of the arguments to pass to the function or callable object
43/// @param func The function or callable object
44/// @param args The arguments to pass to the invoked function or callable object
45/// @return The return value of the invocation of the specified function or callable object with the specified
46/// arguments
47template <
48 typename F,
49 typename... Args,
50 constraints<
51 std::enable_if_t<!std::is_member_function_pointer<std::remove_reference_t<F>>::value>,
52 std::enable_if_t<!std::is_member_object_pointer<std::remove_reference_t<F>>::value>> = nullptr>
53constexpr auto invoke_impl(F&& func, Args&&... args) noexcept(noexcept(std::forward<F>(func)(std::forward<Args>(args)...
54))) -> decltype(std::forward<F>(func)(std::forward<Args>(args)...)) {
55 // parasoft-begin-suppress CERT_CPP-ERR51-b-3 "False positive: Exception specification is conditional"
56 // parasoft-begin-suppress AUTOSAR-A15_3_2-a-2 "False positive: Exception specification is conditional"
57 // parasoft-begin-suppress AUTOSAR-A15_5_3-g-2 "False positive: Exception specification is conditional"
58 // parasoft-begin-suppress CERT_CPP-ERR50-g-3 "False positive: Exception specification is conditional"
59 // parasoft-begin-suppress AUTOSAR-M15_3_4-b-2 "False positive: Exception is caught"
60 // parasoft-begin-suppress AUTOSAR-A15_2_1-b-2 "False positive: Throwing constructor not invoked"
61 return std::forward<F>(func)(std::forward<Args>(args)...);
62 // parasoft-end-suppress AUTOSAR-A15_2_1-b-2
63 // parasoft-end-suppress AUTOSAR-M15_3_4-b-2
64 // parasoft-end-suppress CERT_CPP-ERR50-g-3
65 // parasoft-end-suppress AUTOSAR-A15_5_3-g-2
66 // parasoft-end-suppress AUTOSAR-A15_3_2-a-2
67 // parasoft-end-suppress CERT_CPP-ERR51-b-3
68}
69// parasoft-end-suppress AUTOSAR-A15_5_3-h-2
70// parasoft-end-suppress CERT_CPP-ERR55-a-2
71// parasoft-end-suppress CERT_CPP-ERR50-h-3
72// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
73
74// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
75/// @brief Invoke a pointer to a member function with a @c std::reference_wrapper to an object, and a specified list of
76/// arguments
77/// @tparam F The type of the pointer to member function
78/// @tparam Object The type of the refeence object to apply the pointer to member function to
79/// @tparam Args The types of the arguments to pass to the member function
80/// @param func The pointer to member function to invoke
81/// @param object The instance from which to obtain the object to apply the pointer to member function to
82/// @param args The arguments to pass to the invoked member function
83/// @return The return value of the invocation of the pointed-to member function for the specified object with the
84/// specified arguments
85template <
86 typename F,
87 typename Object,
88 typename... Args,
89 constraints<std::enable_if_t<is_reference_wrapper_v<remove_cvref_t<Object>>>> = nullptr>
90auto invoke_member_function_impl(F func, Object object, Args&&... args) noexcept(
91 noexcept((object.get().*func)(std::forward<Args>(args)...))
92) -> decltype((object.get().*func)(std::forward<Args>(args)...)) {
93 return (object.get().*func)(std::forward<Args>(args)...);
94}
95// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
96
97// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
98/// @brief Invoke a pointer to a member function against an object, with a specified list of arguments
99/// @tparam F The type of the pointer to member function
100/// @tparam Object The type of the object to apply the pointer to member function to.
101/// @tparam Args The types of the arguments to pass to the member function
102/// @param func The pointer to member function to invoke
103/// @param object The object to apply the pointer to member function to. If it is a pointer-like object, it will be
104/// dereferenced before invoking the member pointer.
105/// @param args The arguments to pass to the invoked member function
106/// @return The return value of the invocation of the pointed-to member function for the specified object with the
107/// specified arguments
108/// @{
109template <
110 typename F,
111 typename Object,
112 typename... Args,
113 constraints<std::enable_if_t<std::is_base_of<member_pointer_class_type<F>, remove_cvref_t<Object>>::value>> =
114 nullptr>
115constexpr auto invoke_member_function_impl(F func, Object&& object, Args&&... args) noexcept(
116 noexcept((std::forward<Object>(object).*func)(std::forward<Args>(args)...))
117) -> decltype((std::forward<Object>(object).*func)(std::forward<Args>(args)...)) {
118 return (std::forward<Object>(object).*func)(std::forward<Args>(args)...);
119}
120// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
121
122// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
123// parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: Documented by group"
124// parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: Documented by group"
125template <
126 typename F,
127 typename Object,
128 typename... Args,
129 constraints<
130 std::enable_if_t<!std::is_base_of<member_pointer_class_type<F>, remove_cvref_t<Object>>::value>,
131 std::enable_if_t<!is_reference_wrapper_v<remove_cvref_t<Object>>>> = nullptr>
132constexpr auto invoke_member_function_impl(F func, Object&& object, Args&&... args) noexcept(
133 noexcept(((*std::forward<Object>(object)).*func)(std::forward<Args>(args)...))
134) -> decltype(((*std::forward<Object>(object)).*func)(std::forward<Args>(args)...)) {
135 return ((*std::forward<Object>(object)).*func)(std::forward<Args>(args)...);
136}
137// parasoft-end-suppress AUTOSAR-A2_7_3-a-2
138// parasoft-end-suppress AUTOSAR-A2_7_3-b-2
139// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
140
141/// @}
142
143// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
144/// @brief Invoke a pointer to a member function with an object, a reference to an object, a @c std::reference_wrapper
145/// to an object, or a pointer-like entity that refers to an object, and a specified list of arguments
146/// @tparam F The type of the pointer to member function
147/// @tparam Object The type from which to obtain the object to apply the pointer to member function to
148/// @tparam Args The types of the arguments to pass to the member function
149/// @param func The pointer to member function to invoke
150/// @param object The instance from which to obtain the object to apply the pointer to member function to
151/// @param args The arguments to pass to the invoked member function
152/// @return The return value of the invocation of the pointed-to member function for the specified object with the
153/// specified arguments
154template <
155 typename F,
156 typename Object,
157 typename... Args,
158 constraints<std::enable_if_t<std::is_member_function_pointer<std::remove_reference_t<F>>::value>> = nullptr>
159constexpr auto invoke_impl(F&& func, Object&& object, Args&&... args) noexcept(noexcept(
160 invoke_member_function_impl(std::forward<F>(func), std::forward<Object>(object), std::forward<Args>(args)...)
161))
162 -> decltype(invoke_member_function_impl(
163 std::forward<F>(func),
164 std::forward<Object>(object),
165 std::forward<Args>(args)...
166 )) {
167 return invoke_member_function_impl(std::forward<F>(func), std::forward<Object>(object), std::forward<Args>(args)...);
168}
169// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
170
171// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
172/// @brief Invoke a pointer to a data member against an object object
173/// @tparam F The type of the pointer to data member
174/// @tparam Object The type of the object. May be the object itself, or a pointer-like type pointing to the object.
175/// @param func The pointer to data member to invoke
176/// @param object The object to apply the pointer to data member to. If it is a pointer-like object, it will be
177/// dereferenced before invoking the member pointer.
178/// @return The result of applying the the pointed-to data member to the specified object.
179/// @{
180template <
181 typename F,
182 typename Object,
183 constraints<std::enable_if_t<std::is_base_of<member_pointer_class_type<F>, remove_cvref_t<Object>>::value>> =
184 nullptr>
185constexpr auto invoke_data_member_impl(F func, Object&& object) noexcept
186 -> decltype(std::forward<Object>(object).*func) {
187 return std::forward<Object>(object).*func;
188}
189// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
190
191// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
192// parasoft-begin-suppress CERT_CPP-ERR55-a-2 "False positive: Exception specification is conditional"
193// parasoft-begin-suppress CERT_CPP-ERR50-h-3 "False positive: Exception specification is conditional"
194// parasoft-begin-suppress AUTOSAR-A15_5_3-h-2 "False positive: Exception specification is conditional"
195// parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: Documented by group"
196// parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: Documented by group"
197template <
198 typename F,
199 typename Object,
200 constraints<
201 std::enable_if_t<!std::is_base_of<member_pointer_class_type<F>, remove_cvref_t<Object>>::value>,
202 std::enable_if_t<!is_reference_wrapper_v<remove_cvref_t<Object>>>> = nullptr>
203constexpr auto invoke_data_member_impl(F func, Object&& object) noexcept(noexcept((*std::forward<Object>(object)).*func)
204) -> decltype((*std::forward<Object>(object)).*func) {
205 // parasoft-begin-suppress CERT_CPP-ERR51-b-3 "False positive: Exception specification is conditional"
206 // parasoft-begin-suppress AUTOSAR-A15_3_2-a-2 "False positive: Exception specification is conditional"
207 // parasoft-begin-suppress AUTOSAR-A15_5_3-g-2 "False positive: Exception specification is conditional"
208 // parasoft-begin-suppress CERT_CPP-ERR50-g-3 "False positive: Exception specification is conditional"
209 // parasoft-begin-suppress AUTOSAR-M15_3_4-b-2 "False positive: Exception is caught"
210 // parasoft-begin-suppress AUTOSAR-A15_2_1-b-2 "False positive: Throwing constructor not invoked"
211 return (*std::forward<Object>(object)).*func;
212 // parasoft-end-suppress AUTOSAR-A15_2_1-b-2
213 // parasoft-end-suppress AUTOSAR-M15_3_4-b-2
214 // parasoft-end-suppress CERT_CPP-ERR50-g-3
215 // parasoft-end-suppress AUTOSAR-A15_5_3-g-2
216 // parasoft-end-suppress AUTOSAR-A15_3_2-a-2
217 // parasoft-end-suppress CERT_CPP-ERR51-b-3
218}
219// parasoft-end-suppress AUTOSAR-A2_7_3-a-2
220// parasoft-end-suppress AUTOSAR-A2_7_3-b-2
221// parasoft-end-suppress AUTOSAR-A15_5_3-h-2
222// parasoft-end-suppress CERT_CPP-ERR55-a-2
223// parasoft-end-suppress CERT_CPP-ERR50-h-3
224// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
225/// @}
226
227// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
228/// @brief Invoke a pointer to a data member with a @c std::reference_wrapper to an object
229/// @tparam F The type of the pointer to data member
230/// @tparam Object The type of the referenced object
231/// @param func The pointer to data member to invoke
232/// @param object The instance from which to obtain the object to apply the pointer to data member to
233/// @return The value of the pointed-to data member for the specified object
234template <
235 typename F,
236 typename Object,
237 constraints<std::enable_if_t<is_reference_wrapper_v<remove_cvref_t<Object>>>> = nullptr>
238auto invoke_data_member_impl(F func, Object object) noexcept -> decltype(object.get().*func) {
239 return object.get().*func;
240}
241// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
242
243// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
244// parasoft-begin-suppress CERT_CPP-ERR55-a-2 "False positive: Exception specification is conditional"
245// parasoft-begin-suppress CERT_CPP-ERR50-h-3 "False positive: Exception specification is conditional"
246// parasoft-begin-suppress AUTOSAR-A15_5_3-h-2 "False positive: Exception specification is conditional"
247/// @brief Invoke a pointer to a data member with an object, a reference to an object, a @c std::reference_wrapper to an
248/// object, or a pointer-like entity that refers to an object.
249/// @tparam F The type of the pointer to data member
250/// @tparam Object The type from which to obtain the object to apply the pointer to data member to
251/// @param func The pointer to data member to invoke
252/// @param object The instance from which to obtain the object to apply the pointer to data member to
253/// @return The value of the pointed-to data member for the specified object
254template <
255 typename F,
256 typename Object,
257 constraints<std::enable_if_t<std::is_member_object_pointer<std::remove_reference_t<F>>::value>> = nullptr>
258constexpr auto invoke_impl(F&& func, Object&& object) noexcept(
259 noexcept(invoke_data_member_impl(std::forward<F>(func), std::forward<Object>(object)))
260) -> decltype(invoke_data_member_impl(std::forward<F>(func), std::forward<Object>(object))) {
261 return invoke_data_member_impl(std::forward<F>(func), std::forward<Object>(object));
262}
263// parasoft-end-suppress AUTOSAR-A15_5_3-h-2
264// parasoft-end-suppress CERT_CPP-ERR55-a-2
265// parasoft-end-suppress CERT_CPP-ERR50-h-3
266// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
267} // namespace invoke_detail
268
269// parasoft-begin-suppress CERT_CPP-ERR55-a-2 "False positive: Exception specification is conditional"
270// parasoft-begin-suppress CERT_CPP-ERR50-h-3 "False positive: Exception specification is conditional"
271// parasoft-begin-suppress AUTOSAR-A15_5_3-h-2 "False positive: Exception specification is conditional"
272// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
273/// @brief Implementation of std::invoke from C++23: invokes the specified callable with the specified arguments.
274/// @tparam F The type of the callable to invoke
275/// @tparam Args The types of the arguments
276/// @param func The callable to invoke
277/// @param args The arguments with which to invoke the callable
278/// @return The result of invoking the callable with the specified arguments
279/// @note Does not participate in overload resolution if the specified @c F cannot be invoked with the specified @c Args
280template <typename F, typename... Args, constraints<std::enable_if_t<is_invocable_v<F, Args...>>> = nullptr>
281constexpr auto invoke(F&& func, Args&&... args) noexcept(is_nothrow_invocable_v<F, Args...>)
282 -> decltype(invoke_detail::invoke_impl(std::forward<F>(func), std::forward<Args>(args)...)) {
284}
285// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
286// parasoft-end-suppress AUTOSAR-A15_5_3-h-2
287// parasoft-end-suppress CERT_CPP-ERR55-a-2
288// parasoft-end-suppress CERT_CPP-ERR50-h-3
289
290} // namespace base
291} // namespace arene
292
293// parasoft-end-suppress AUTOSAR-M2_10_1-a-2
294
295#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FUNCTIONAL_INVOKE_HPP_
296// parasoft-end-suppress AUTOSAR-A5_0_3-a "False positive: There is no pointer indirection in return types"
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10