Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
not_fn.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FUNCTIONAL_NOT_FN_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FUNCTIONAL_NOT_FN_HPP_
7
8// IWYU pragma: private, include "arene/base/functional.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
12#include "arene/base/compiler_support/cpp14_inline.hpp"
13#include "arene/base/functional/invoke.hpp"
14#include "arene/base/functional/perfect_forward_call_wrapper.hpp"
15#include "arene/base/stdlib_choice/decay.hpp"
16#include "arene/base/stdlib_choice/forward.hpp"
17#include "arene/base/stdlib_choice/is_constructible.hpp"
18#include "arene/base/stdlib_choice/is_move_constructible.hpp"
19// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
20
21namespace arene {
22namespace base {
23
24namespace not_fn_detail {
25
26/// @brief Implementation helper for @c arene::base::not_fn intended to be used as a policy for
27/// @c arene::base::functional_detail::perfect_forwarding_call_wrapper .
28class not_fn_policy {
29 public:
30 // parasoft-begin-suppress CERT_C-EXP37-a "BoundArgsTupleT is unnamed as it is unused."
31 /// @brief Invokes the provided function as expression-equivalent to @c !func(call_args...) .
32 ///
33 /// @tparam FuncT The type of the callable to invoke.
34 /// @tparam BoundArgsTupleT The type of a tuple holding the bound arguments. Must be empty.
35 /// @tparam CallArgsT The types of the call arguments to invoke the callable with.
36 /// @param func The invocable to invoke.
37 /// @param call_args The set of call arguments to pass to the invocable.
38 /// @return bool Expression-equivalent to @c !func(call_args...) .
39 template <typename FuncT, typename BoundArgsTupleT, typename... CallArgsT>
40 constexpr auto operator()(FuncT&& func, BoundArgsTupleT&&, CallArgsT&&... call_args) const
41 noexcept((noexcept(!::arene::base::invoke(std::forward<FuncT>(func), std::forward<CallArgsT>(call_args)...))))
42 -> decltype(!::arene::base::invoke(std::forward<FuncT>(func), std::forward<CallArgsT>(call_args)...)) {
43 return !::arene::base::invoke(std::forward<FuncT>(func), std::forward<CallArgsT>(call_args)...);
44 }
45 // parasoft-end-suppress CERT_C-EXP37-a "BoundArgsTupleT is unnamed as it is unused."
46};
47
48/// @brief Simplifies correctly setting up the call wrapper and policy.
49///
50/// @tparam BoundFunc The type of the function to bind, post decaying.
51/// @note This needs to be a struct rather than just a type alias or else gcc crashes on all versions between 8 and 12.
52template <typename BoundFunc>
53class not_fn_t : public functional_detail::perfect_forward_call_wrapper<not_fn_policy, BoundFunc> {
54 public:
55 using functional_detail::perfect_forward_call_wrapper<not_fn_policy, BoundFunc>::perfect_forward_call_wrapper;
56};
57
58/// @brief Produces a call wrapper which inverts the result of an N-arity predicate .
59class not_fn_impl {
60 public:
61 /// @brief Produces a call wrapper which inverts the result of an N-arity predicate .
62 ///
63 /// @tparam BoundFunc The type of the callable to create a wrapper for. Must satisfy
64 // @c std::is_constructible<std::decay_t<BoundFunc>,BoundFunc> and
65 // @c std::is_move_constructible<std::decay_t<BoundFunc>> or else the program is ill-formed.
66 /// @param bound_func The callable to create a wrapper for. It will be perfect-forwarded into the call wrapper.
67 /// @return An implementation defined callable object whose call operator consumes a set of arguments @c call_args .
68 /// <c> not_fn(bound_func)(call_args...) </c> is expression-equivalent to
69 /// <c> !invoke(bound_func, call_args...) </c> . The @c noexcept -ness of @c bound_func is
70 /// preserved by the call operator of the returned object. The call operator does not participate in overload
71 /// resolution if @c !bound_func(call_args...) is not well formed.
72 // parasoft-begin-suppress CERT_C-EXP37-a "False positive: there is no function pointer decay/conversion."
73 // parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: static cannot be applied, this is not a member function."
74 template <typename BoundFunc>
75 constexpr auto operator()(BoundFunc&& bound_func) const
76 noexcept(std::is_nothrow_constructible<std::decay_t<BoundFunc>, BoundFunc>::value)
77 -> not_fn_detail::not_fn_t<std::decay_t<BoundFunc>> {
78 static_assert(
79 std::is_constructible<std::decay_t<BoundFunc>, BoundFunc&&>::value,
80 "Bound function must be constructible from input."
81 );
82 static_assert(
83 std::is_move_constructible<std::decay_t<BoundFunc>>::value,
84 "Bound function must be move-constructible."
85 );
86 return not_fn_detail::not_fn_t<std::decay_t<BoundFunc>>{std::forward<BoundFunc>(bound_func)};
87 }
88 // parasoft-end-suppress AUTOSAR-M3_3_2-a "False positive: static cannot be applied, this is not a member function."
89 // parasoft-begin-suppress CERT_C-EXP37-a "False positive: there is no function pointer decay/conversion."
90};
91
92} // namespace not_fn_detail
93
94/// @def arene::base::not_fn
95/// @copydoc arene::base::not_fn_detail::not_fn_impl::operator()
96// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
97// object used in multiple TUs."
98// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
99// object used in multiple TUs."
100ARENE_CPP14_INLINE_VARIABLE(not_fn_detail::not_fn_impl, not_fn);
101// parasoft-end-suppress AUTOSAR-M7_3_3-a
102// parasoft-end-suppress CERT_CPP-DCL59-a
103
104} // namespace base
105} // namespace arene
106#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FUNCTIONAL_NOT_FN_HPP_
Definition array_exceptions_disabled.cpp:11
ARENE_CPP14_INLINE_VARIABLE(not_fn_detail::not_fn_impl, not_fn)
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10