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 CERT_C-EXP37-a "False positive: defines arene::base::apply, name not needed"
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"
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(
64 ))
65 )
66 -> decltype(apply_impl(
70 )) {
71 return apply_impl(
72 std::forward<F>(callable),
75 );
76 }
77};
78
79} // namespace apply_detail
80
81/// @brief Calls provided invocable with arguments from provided tuple.
82/// @tparam F Provided callable type (with arguments of @c Tuple)
83/// @tparam Tuple type implementing the tuple protocol.
84/// Pass a callable object as @c callable and a tuple-ish value as @c tuple.
85/// @return Result of applying the callable with arguments specified by @c tuple
86///
87/// @note A type implements the tuple protocol if it
88/// * provides a specialization of @c std::tuple_size
89/// * provides a specialization of @c std::tuple_element
90/// * provides a function to get an element by index @c I, which is specified
91/// as:
92/// ** <c> std::forward<Tuple>(tuple).get<I>() </c> if valid
93/// ** otherwise, <c> get<I>(std::forward<Tuple>(tuple)) </c> if valid,
94/// where @c get is looked up by ADL only
95/// Note that the tuple-like concept defines types that satisfy the tuple
96/// protocol but are restricted to a list of types defined in the @c std
97/// namespace (@c std::array, @c std::pair, @c std::tuple in C++14). The term
98/// tuple-ish is used to define types that satisfy the tuple protocol without
99/// the restriction that the type is defined in the @c std namespace -- which
100/// includes user-defined types.
101///
102// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
103// object used in multiple TUs."
104// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
105// object used in multiple TUs."
106ARENE_CPP14_INLINE_VARIABLE(apply_detail::do_apply, apply);
107// parasoft-end-suppress AUTOSAR-M7_3_3-a
108// parasoft-end-suppress CERT_CPP-DCL59-a
109
110} // namespace base
111} // namespace arene
112
113#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TUPLE_APPLY_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition accumulate.hpp:37
constexpr auto invoke(F &&func, Args &&... args) noexcept(is_nothrow_invocable_v< F, Args... >) ->
Implementation of std::invoke from C++23: invokes the specified callable with the specified arguments...
Definition invoke.hpp:281
Definition accumulate.hpp:36
make_integer_sequence< size_t, Size > make_index_sequence
A helper alias template for make_integer_sequence<size_t, Size>
Definition integer_sequence.hpp:224
integer_sequence< size_t, Ints... > index_sequence
A helper alias template for integer_sequence<size_t, Ints>
Definition integer_sequence.hpp:218
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
typename remove_reference< T >::type remove_reference_t
An alias for T, but with top-level reference qualifiers removed.
Definition remove_reference.hpp:47
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24