xref: /aosp_15_r20/external/abseil-cpp/absl/functional/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 // -----------------------------------------------------------------------------
16*9356374aSAndroid Build Coastguard Worker // File: function_ref.h
17*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
18*9356374aSAndroid Build Coastguard Worker //
19*9356374aSAndroid Build Coastguard Worker // This header file defines the `absl::FunctionRef` type for holding a
20*9356374aSAndroid Build Coastguard Worker // non-owning reference to an object of any invocable type. This function
21*9356374aSAndroid Build Coastguard Worker // reference is typically most useful as a type-erased argument type for
22*9356374aSAndroid Build Coastguard Worker // accepting function types that neither take ownership nor copy the type; using
23*9356374aSAndroid Build Coastguard Worker // the reference type in this case avoids a copy and an allocation. Best
24*9356374aSAndroid Build Coastguard Worker // practices of other non-owning reference-like objects (such as
25*9356374aSAndroid Build Coastguard Worker // `absl::string_view`) apply here.
26*9356374aSAndroid Build Coastguard Worker //
27*9356374aSAndroid Build Coastguard Worker //  An `absl::FunctionRef` is similar in usage to a `std::function` but has the
28*9356374aSAndroid Build Coastguard Worker //  following differences:
29*9356374aSAndroid Build Coastguard Worker //
30*9356374aSAndroid Build Coastguard Worker //  * It doesn't own the underlying object.
31*9356374aSAndroid Build Coastguard Worker //  * It doesn't have a null or empty state.
32*9356374aSAndroid Build Coastguard Worker //  * It never performs deep copies or allocations.
33*9356374aSAndroid Build Coastguard Worker //  * It's much faster and cheaper to construct.
34*9356374aSAndroid Build Coastguard Worker //  * It's trivially copyable and destructable.
35*9356374aSAndroid Build Coastguard Worker //
36*9356374aSAndroid Build Coastguard Worker // Generally, `absl::FunctionRef` should not be used as a return value, data
37*9356374aSAndroid Build Coastguard Worker // member, or to initialize a `std::function`. Such usages will often lead to
38*9356374aSAndroid Build Coastguard Worker // problematic lifetime issues. Once you convert something to an
39*9356374aSAndroid Build Coastguard Worker // `absl::FunctionRef` you cannot make a deep copy later.
40*9356374aSAndroid Build Coastguard Worker //
41*9356374aSAndroid Build Coastguard Worker // This class is suitable for use wherever a "const std::function<>&"
42*9356374aSAndroid Build Coastguard Worker // would be used without making a copy. ForEach functions and other versions of
43*9356374aSAndroid Build Coastguard Worker // the visitor pattern are a good example of when this class should be used.
44*9356374aSAndroid Build Coastguard Worker //
45*9356374aSAndroid Build Coastguard Worker // This class is trivial to copy and should be passed by value.
46*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_FUNCTIONAL_FUNCTION_REF_H_
47*9356374aSAndroid Build Coastguard Worker #define ABSL_FUNCTIONAL_FUNCTION_REF_H_
48*9356374aSAndroid Build Coastguard Worker 
49*9356374aSAndroid Build Coastguard Worker #include <cassert>
50*9356374aSAndroid Build Coastguard Worker #include <functional>
51*9356374aSAndroid Build Coastguard Worker #include <type_traits>
52*9356374aSAndroid Build Coastguard Worker 
53*9356374aSAndroid Build Coastguard Worker #include "absl/base/attributes.h"
54*9356374aSAndroid Build Coastguard Worker #include "absl/functional/internal/function_ref.h"
55*9356374aSAndroid Build Coastguard Worker #include "absl/meta/type_traits.h"
56*9356374aSAndroid Build Coastguard Worker 
57*9356374aSAndroid Build Coastguard Worker namespace absl {
58*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
59*9356374aSAndroid Build Coastguard Worker 
60*9356374aSAndroid Build Coastguard Worker // FunctionRef
61*9356374aSAndroid Build Coastguard Worker //
62*9356374aSAndroid Build Coastguard Worker // Dummy class declaration to allow the partial specialization based on function
63*9356374aSAndroid Build Coastguard Worker // types below.
64*9356374aSAndroid Build Coastguard Worker template <typename T>
65*9356374aSAndroid Build Coastguard Worker class FunctionRef;
66*9356374aSAndroid Build Coastguard Worker 
67*9356374aSAndroid Build Coastguard Worker // FunctionRef
68*9356374aSAndroid Build Coastguard Worker //
69*9356374aSAndroid Build Coastguard Worker // An `absl::FunctionRef` is a lightweight wrapper to any invocable object with
70*9356374aSAndroid Build Coastguard Worker // a compatible signature. Generally, an `absl::FunctionRef` should only be used
71*9356374aSAndroid Build Coastguard Worker // as an argument type and should be preferred as an argument over a const
72*9356374aSAndroid Build Coastguard Worker // reference to a `std::function`. `absl::FunctionRef` itself does not allocate,
73*9356374aSAndroid Build Coastguard Worker // although the wrapped invocable may.
74*9356374aSAndroid Build Coastguard Worker //
75*9356374aSAndroid Build Coastguard Worker // Example:
76*9356374aSAndroid Build Coastguard Worker //
77*9356374aSAndroid Build Coastguard Worker //   // The following function takes a function callback by const reference
78*9356374aSAndroid Build Coastguard Worker //   bool Visitor(const std::function<void(my_proto&,
79*9356374aSAndroid Build Coastguard Worker //                                         absl::string_view)>& callback);
80*9356374aSAndroid Build Coastguard Worker //
81*9356374aSAndroid Build Coastguard Worker //   // Assuming that the function is not stored or otherwise copied, it can be
82*9356374aSAndroid Build Coastguard Worker //   // replaced by an `absl::FunctionRef`:
83*9356374aSAndroid Build Coastguard Worker //   bool Visitor(absl::FunctionRef<void(my_proto&, absl::string_view)>
84*9356374aSAndroid Build Coastguard Worker //                  callback);
85*9356374aSAndroid Build Coastguard Worker //
86*9356374aSAndroid Build Coastguard Worker // Note: the assignment operator within an `absl::FunctionRef` is intentionally
87*9356374aSAndroid Build Coastguard Worker // deleted to prevent misuse; because the `absl::FunctionRef` does not own the
88*9356374aSAndroid Build Coastguard Worker // underlying type, assignment likely indicates misuse.
89*9356374aSAndroid Build Coastguard Worker template <typename R, typename... Args>
90*9356374aSAndroid Build Coastguard Worker class FunctionRef<R(Args...)> {
91*9356374aSAndroid Build Coastguard Worker  private:
92*9356374aSAndroid Build Coastguard Worker   // Used to disable constructors for objects that are not compatible with the
93*9356374aSAndroid Build Coastguard Worker   // signature of this FunctionRef.
94*9356374aSAndroid Build Coastguard Worker   template <typename F,
95*9356374aSAndroid Build Coastguard Worker             typename FR = absl::base_internal::invoke_result_t<F, Args&&...>>
96*9356374aSAndroid Build Coastguard Worker   using EnableIfCompatible =
97*9356374aSAndroid Build Coastguard Worker       typename std::enable_if<std::is_void<R>::value ||
98*9356374aSAndroid Build Coastguard Worker                               std::is_convertible<FR, R>::value>::type;
99*9356374aSAndroid Build Coastguard Worker 
100*9356374aSAndroid Build Coastguard Worker  public:
101*9356374aSAndroid Build Coastguard Worker   // Constructs a FunctionRef from any invocable type.
102*9356374aSAndroid Build Coastguard Worker   template <typename F, typename = EnableIfCompatible<const F&>>
103*9356374aSAndroid Build Coastguard Worker   // NOLINTNEXTLINE(runtime/explicit)
FunctionRef(const F & f ABSL_ATTRIBUTE_LIFETIME_BOUND)104*9356374aSAndroid Build Coastguard Worker   FunctionRef(const F& f ABSL_ATTRIBUTE_LIFETIME_BOUND)
105*9356374aSAndroid Build Coastguard Worker       : invoker_(&absl::functional_internal::InvokeObject<F, R, Args...>) {
106*9356374aSAndroid Build Coastguard Worker     absl::functional_internal::AssertNonNull(f);
107*9356374aSAndroid Build Coastguard Worker     ptr_.obj = &f;
108*9356374aSAndroid Build Coastguard Worker   }
109*9356374aSAndroid Build Coastguard Worker 
110*9356374aSAndroid Build Coastguard Worker   // Overload for function pointers. This eliminates a level of indirection that
111*9356374aSAndroid Build Coastguard Worker   // would happen if the above overload was used (it lets us store the pointer
112*9356374aSAndroid Build Coastguard Worker   // instead of a pointer to a pointer).
113*9356374aSAndroid Build Coastguard Worker   //
114*9356374aSAndroid Build Coastguard Worker   // This overload is also used for references to functions, since references to
115*9356374aSAndroid Build Coastguard Worker   // functions can decay to function pointers implicitly.
116*9356374aSAndroid Build Coastguard Worker   template <
117*9356374aSAndroid Build Coastguard Worker       typename F, typename = EnableIfCompatible<F*>,
118*9356374aSAndroid Build Coastguard Worker       absl::functional_internal::EnableIf<absl::is_function<F>::value> = 0>
FunctionRef(F * f)119*9356374aSAndroid Build Coastguard Worker   FunctionRef(F* f)  // NOLINT(runtime/explicit)
120*9356374aSAndroid Build Coastguard Worker       : invoker_(&absl::functional_internal::InvokeFunction<F*, R, Args...>) {
121*9356374aSAndroid Build Coastguard Worker     assert(f != nullptr);
122*9356374aSAndroid Build Coastguard Worker     ptr_.fun = reinterpret_cast<decltype(ptr_.fun)>(f);
123*9356374aSAndroid Build Coastguard Worker   }
124*9356374aSAndroid Build Coastguard Worker 
125*9356374aSAndroid Build Coastguard Worker   // To help prevent subtle lifetime bugs, FunctionRef is not assignable.
126*9356374aSAndroid Build Coastguard Worker   // Typically, it should only be used as an argument type.
127*9356374aSAndroid Build Coastguard Worker   FunctionRef& operator=(const FunctionRef& rhs) = delete;
128*9356374aSAndroid Build Coastguard Worker   FunctionRef(const FunctionRef& rhs) = default;
129*9356374aSAndroid Build Coastguard Worker 
130*9356374aSAndroid Build Coastguard Worker   // Call the underlying object.
operator()131*9356374aSAndroid Build Coastguard Worker   R operator()(Args... args) const {
132*9356374aSAndroid Build Coastguard Worker     return invoker_(ptr_, std::forward<Args>(args)...);
133*9356374aSAndroid Build Coastguard Worker   }
134*9356374aSAndroid Build Coastguard Worker 
135*9356374aSAndroid Build Coastguard Worker  private:
136*9356374aSAndroid Build Coastguard Worker   absl::functional_internal::VoidPtr ptr_;
137*9356374aSAndroid Build Coastguard Worker   absl::functional_internal::Invoker<R, Args...> invoker_;
138*9356374aSAndroid Build Coastguard Worker };
139*9356374aSAndroid Build Coastguard Worker 
140*9356374aSAndroid Build Coastguard Worker // Allow const qualified function signatures. Since FunctionRef requires
141*9356374aSAndroid Build Coastguard Worker // constness anyway we can just make this a no-op.
142*9356374aSAndroid Build Coastguard Worker template <typename R, typename... Args>
143*9356374aSAndroid Build Coastguard Worker class FunctionRef<R(Args...) const> : public FunctionRef<R(Args...)> {
144*9356374aSAndroid Build Coastguard Worker  public:
145*9356374aSAndroid Build Coastguard Worker   using FunctionRef<R(Args...)>::FunctionRef;
146*9356374aSAndroid Build Coastguard Worker };
147*9356374aSAndroid Build Coastguard Worker 
148*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
149*9356374aSAndroid Build Coastguard Worker }  // namespace absl
150*9356374aSAndroid Build Coastguard Worker 
151*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_FUNCTIONAL_FUNCTION_REF_H_
152