xref: /aosp_15_r20/external/abseil-cpp/absl/functional/internal/function_ref.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2019 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker 
15*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_FUNCTIONAL_INTERNAL_FUNCTION_REF_H_
16*9356374aSAndroid Build Coastguard Worker #define ABSL_FUNCTIONAL_INTERNAL_FUNCTION_REF_H_
17*9356374aSAndroid Build Coastguard Worker 
18*9356374aSAndroid Build Coastguard Worker #include <cassert>
19*9356374aSAndroid Build Coastguard Worker #include <functional>
20*9356374aSAndroid Build Coastguard Worker #include <type_traits>
21*9356374aSAndroid Build Coastguard Worker 
22*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/invoke.h"
23*9356374aSAndroid Build Coastguard Worker #include "absl/functional/any_invocable.h"
24*9356374aSAndroid Build Coastguard Worker #include "absl/meta/type_traits.h"
25*9356374aSAndroid Build Coastguard Worker 
26*9356374aSAndroid Build Coastguard Worker namespace absl {
27*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
28*9356374aSAndroid Build Coastguard Worker namespace functional_internal {
29*9356374aSAndroid Build Coastguard Worker 
30*9356374aSAndroid Build Coastguard Worker // Like a void* that can handle function pointers as well. The standard does not
31*9356374aSAndroid Build Coastguard Worker // allow function pointers to round-trip through void*, but void(*)() is fine.
32*9356374aSAndroid Build Coastguard Worker //
33*9356374aSAndroid Build Coastguard Worker // Note: It's important that this class remains trivial and is the same size as
34*9356374aSAndroid Build Coastguard Worker // a pointer, since this allows the compiler to perform tail-call optimizations
35*9356374aSAndroid Build Coastguard Worker // when the underlying function is a callable object with a matching signature.
36*9356374aSAndroid Build Coastguard Worker union VoidPtr {
37*9356374aSAndroid Build Coastguard Worker   const void* obj;
38*9356374aSAndroid Build Coastguard Worker   void (*fun)();
39*9356374aSAndroid Build Coastguard Worker };
40*9356374aSAndroid Build Coastguard Worker 
41*9356374aSAndroid Build Coastguard Worker // Chooses the best type for passing T as an argument.
42*9356374aSAndroid Build Coastguard Worker // Attempt to be close to SystemV AMD64 ABI. Objects with trivial copy ctor are
43*9356374aSAndroid Build Coastguard Worker // passed by value.
44*9356374aSAndroid Build Coastguard Worker template <typename T,
45*9356374aSAndroid Build Coastguard Worker           bool IsLValueReference = std::is_lvalue_reference<T>::value>
46*9356374aSAndroid Build Coastguard Worker struct PassByValue : std::false_type {};
47*9356374aSAndroid Build Coastguard Worker 
48*9356374aSAndroid Build Coastguard Worker template <typename T>
49*9356374aSAndroid Build Coastguard Worker struct PassByValue<T, /*IsLValueReference=*/false>
50*9356374aSAndroid Build Coastguard Worker     : std::integral_constant<bool,
51*9356374aSAndroid Build Coastguard Worker                              absl::is_trivially_copy_constructible<T>::value &&
52*9356374aSAndroid Build Coastguard Worker                                  absl::is_trivially_copy_assignable<
53*9356374aSAndroid Build Coastguard Worker                                      typename std::remove_cv<T>::type>::value &&
54*9356374aSAndroid Build Coastguard Worker                                  std::is_trivially_destructible<T>::value &&
55*9356374aSAndroid Build Coastguard Worker                                  sizeof(T) <= 2 * sizeof(void*)> {};
56*9356374aSAndroid Build Coastguard Worker 
57*9356374aSAndroid Build Coastguard Worker template <typename T>
58*9356374aSAndroid Build Coastguard Worker struct ForwardT : std::conditional<PassByValue<T>::value, T, T&&> {};
59*9356374aSAndroid Build Coastguard Worker 
60*9356374aSAndroid Build Coastguard Worker // An Invoker takes a pointer to the type-erased invokable object, followed by
61*9356374aSAndroid Build Coastguard Worker // the arguments that the invokable object expects.
62*9356374aSAndroid Build Coastguard Worker //
63*9356374aSAndroid Build Coastguard Worker // Note: The order of arguments here is an optimization, since member functions
64*9356374aSAndroid Build Coastguard Worker // have an implicit "this" pointer as their first argument, putting VoidPtr
65*9356374aSAndroid Build Coastguard Worker // first allows the compiler to perform tail-call optimization in many cases.
66*9356374aSAndroid Build Coastguard Worker template <typename R, typename... Args>
67*9356374aSAndroid Build Coastguard Worker using Invoker = R (*)(VoidPtr, typename ForwardT<Args>::type...);
68*9356374aSAndroid Build Coastguard Worker 
69*9356374aSAndroid Build Coastguard Worker //
70*9356374aSAndroid Build Coastguard Worker // InvokeObject and InvokeFunction provide static "Invoke" functions that can be
71*9356374aSAndroid Build Coastguard Worker // used as Invokers for objects or functions respectively.
72*9356374aSAndroid Build Coastguard Worker //
73*9356374aSAndroid Build Coastguard Worker // static_cast<R> handles the case the return type is void.
74*9356374aSAndroid Build Coastguard Worker template <typename Obj, typename R, typename... Args>
75*9356374aSAndroid Build Coastguard Worker R InvokeObject(VoidPtr ptr, typename ForwardT<Args>::type... args) {
76*9356374aSAndroid Build Coastguard Worker   auto o = static_cast<const Obj*>(ptr.obj);
77*9356374aSAndroid Build Coastguard Worker   return static_cast<R>(
78*9356374aSAndroid Build Coastguard Worker       absl::base_internal::invoke(*o, std::forward<Args>(args)...));
79*9356374aSAndroid Build Coastguard Worker }
80*9356374aSAndroid Build Coastguard Worker 
81*9356374aSAndroid Build Coastguard Worker template <typename Fun, typename R, typename... Args>
82*9356374aSAndroid Build Coastguard Worker R InvokeFunction(VoidPtr ptr, typename ForwardT<Args>::type... args) {
83*9356374aSAndroid Build Coastguard Worker   auto f = reinterpret_cast<Fun>(ptr.fun);
84*9356374aSAndroid Build Coastguard Worker   return static_cast<R>(
85*9356374aSAndroid Build Coastguard Worker       absl::base_internal::invoke(f, std::forward<Args>(args)...));
86*9356374aSAndroid Build Coastguard Worker }
87*9356374aSAndroid Build Coastguard Worker 
88*9356374aSAndroid Build Coastguard Worker template <typename Sig>
89*9356374aSAndroid Build Coastguard Worker void AssertNonNull(const std::function<Sig>& f) {
90*9356374aSAndroid Build Coastguard Worker   assert(f != nullptr);
91*9356374aSAndroid Build Coastguard Worker   (void)f;
92*9356374aSAndroid Build Coastguard Worker }
93*9356374aSAndroid Build Coastguard Worker 
94*9356374aSAndroid Build Coastguard Worker template <typename Sig>
95*9356374aSAndroid Build Coastguard Worker void AssertNonNull(const AnyInvocable<Sig>& f) {
96*9356374aSAndroid Build Coastguard Worker   assert(f != nullptr);
97*9356374aSAndroid Build Coastguard Worker   (void)f;
98*9356374aSAndroid Build Coastguard Worker }
99*9356374aSAndroid Build Coastguard Worker 
100*9356374aSAndroid Build Coastguard Worker template <typename F>
101*9356374aSAndroid Build Coastguard Worker void AssertNonNull(const F&) {}
102*9356374aSAndroid Build Coastguard Worker 
103*9356374aSAndroid Build Coastguard Worker template <typename F, typename C>
104*9356374aSAndroid Build Coastguard Worker void AssertNonNull(F C::*f) {
105*9356374aSAndroid Build Coastguard Worker   assert(f != nullptr);
106*9356374aSAndroid Build Coastguard Worker   (void)f;
107*9356374aSAndroid Build Coastguard Worker }
108*9356374aSAndroid Build Coastguard Worker 
109*9356374aSAndroid Build Coastguard Worker template <bool C>
110*9356374aSAndroid Build Coastguard Worker using EnableIf = typename ::std::enable_if<C, int>::type;
111*9356374aSAndroid Build Coastguard Worker 
112*9356374aSAndroid Build Coastguard Worker }  // namespace functional_internal
113*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
114*9356374aSAndroid Build Coastguard Worker }  // namespace absl
115*9356374aSAndroid Build Coastguard Worker 
116*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_FUNCTIONAL_INTERNAL_FUNCTION_REF_H_
117