Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
apply.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::apply"
2
3// Copyright 2024, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TUPLE_APPLY_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TUPLE_APPLY_HPP_
8
9// IWYU pragma: private, include "arene/base/tuple.hpp"
10// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
11
12// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
13#include "arene/base/compiler_support/cpp14_inline.hpp"
14#include "arene/base/functional/invoke.hpp"
15#include "arene/base/stdlib_choice/cstddef.hpp"
16#include "arene/base/stdlib_choice/declval.hpp"
17#include "arene/base/stdlib_choice/forward.hpp"
18#include "arene/base/stdlib_choice/integer_sequence.hpp"
19#include "arene/base/stdlib_choice/remove_reference.hpp"
20#include "arene/base/stdlib_choice/tuple_size.hpp"
21#include "arene/base/tuple/detail/get.hpp"
22// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
23
24// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
25
26namespace arene {
27namespace base {
28
29namespace apply_detail {
30
31// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
32// parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: parameter 'tuple' is used"
33/// @brief Helper function to call provided invocable with arguments from provided tuple.
34/// @tparam F Provided callable type (with arguments of @c Tuple)
35/// @tparam Tuple type implementing the tuple protocol
36/// @tparam Is The parameter pack of indices.
37/// @param callable A callable object.
38/// @param tuple reference to a tuple-ish
39/// @return Result of applying the callable to argument from tuple like a function call f(args...)
40template <typename F, typename Tuple, std::size_t... Is>
41constexpr auto apply_impl(F&& callable, Tuple&& tuple, std::index_sequence<Is...>) noexcept( //
42 noexcept(arene::base::invoke(std::declval<F&&>(), tuple_detail::get<Is>(std::declval<Tuple&&>())...))
43) -> decltype(arene::base::invoke(std::declval<F&&>(), tuple_detail::get<Is>(std::declval<Tuple&&>())...)) {
44 return arene::base::invoke(std::forward<F>(callable), tuple_detail::get<Is>(std::forward<Tuple>(tuple))...);
45}
46// parasoft-end-suppress AUTOSAR-A0_1_4-a
47// parasoft-end-suppress AUTOSAR-M3_3_2-a
48
49/// @brief Function object class providing the implementation of @c apply
50class do_apply {
51 public:
52 /// @brief Calls provided invocable with arguments from provided tuple.
53 /// @tparam F Provided callable type (with arguments of @c Tuple)
54 /// @tparam Tuple type implementing the tuple protocol
55 /// @param callable A callable object.
56 /// @param tuple A tuple-ish object.
57 /// @return Result of applying the callable to argument from tuple like a function call f(args...)
58 template <typename F, typename Tuple>
59 constexpr auto operator()(F&& callable, Tuple&& tuple) const noexcept( //
60 noexcept(apply_impl(
61 std::declval<F&&>(),
62 std::declval<Tuple&&>(),
63 std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value>{}
64 ))
65 )
66 -> decltype(apply_impl(
67 std::declval<F&&>(),
68 std::declval<Tuple&&>(),
69 std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value>{}
70 )) {
71 return apply_impl(
72 std::forward<F>(callable),
73 std::forward<Tuple>(tuple),
74 std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value>{}
75 );
76 }
77};
78
79} // namespace apply_detail
80
81/// @def arene::base::apply
82/// @brief Calls provided invocable with arguments from provided tuple.
83/// @tparam F Provided callable type (with arguments of @c Tuple)
84/// @tparam Tuple type implementing the tuple protocol.
85/// @param callable A callable object.
86/// @param tuple reference to a tuple-ish
87/// @return Result of applying the callable with arguments specified by @c tuple
88///
89/// @note A type implements the tuple protocol if it
90/// * provides a specialization of @c std::tuple_size
91/// * provides a specialization of @c std::tuple_element
92/// * provides a function to get an element by index @c I, which is specified
93/// as:
94/// ** <c> std::forward<Tuple>(tuple).get<I>() </c> if valid
95/// ** otherwise, <c> get<I>(std::forward<Tuple>(tuple)) </c> if valid,
96/// where @c get is looked up by ADL only
97/// Note that the tuple-like concept defines types that satisfy the tuple
98/// protocol but are restricted to a list of types defined in the @c std
99/// namespace (@c std::array, @c std::pair, @c std::tuple in C++14). The term
100/// tuple-ish is used to define types that satisfy the tuple protocol without
101/// the restriction that the type is defined in the @c std namespace -- which
102/// includes user-defined types.
103///
104// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
105// object used in multiple TUs."
106// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
107// object used in multiple TUs."
108ARENE_CPP14_INLINE_VARIABLE(apply_detail::do_apply, apply);
109// parasoft-end-suppress AUTOSAR-M7_3_3-a
110// parasoft-end-suppress CERT_CPP-DCL59-a
111
112} // namespace base
113} // namespace arene
114
115#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TUPLE_APPLY_HPP_
Definition array_exceptions_disabled.cpp:11
ARENE_CPP14_INLINE_VARIABLE(apply_detail::do_apply, apply)
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10