xref: /aosp_15_r20/external/abseil-cpp/absl/functional/any_invocable.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2022 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: any_invocable.h
17*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
18*9356374aSAndroid Build Coastguard Worker //
19*9356374aSAndroid Build Coastguard Worker // This header file defines an `absl::AnyInvocable` type that assumes ownership
20*9356374aSAndroid Build Coastguard Worker // and wraps an object of an invocable type. (Invocable types adhere to the
21*9356374aSAndroid Build Coastguard Worker // concept specified in https://en.cppreference.com/w/cpp/concepts/invocable.)
22*9356374aSAndroid Build Coastguard Worker //
23*9356374aSAndroid Build Coastguard Worker // In general, prefer `absl::AnyInvocable` when you need a type-erased
24*9356374aSAndroid Build Coastguard Worker // function parameter that needs to take ownership of the type.
25*9356374aSAndroid Build Coastguard Worker //
26*9356374aSAndroid Build Coastguard Worker // NOTE: `absl::AnyInvocable` is similar to the C++23 `std::move_only_function`
27*9356374aSAndroid Build Coastguard Worker // abstraction, but has a slightly different API and is not designed to be a
28*9356374aSAndroid Build Coastguard Worker // drop-in replacement or C++11-compatible backfill of that type.
29*9356374aSAndroid Build Coastguard Worker //
30*9356374aSAndroid Build Coastguard Worker // Credits to Matt Calabrese (https://github.com/mattcalabrese) for the original
31*9356374aSAndroid Build Coastguard Worker // implementation.
32*9356374aSAndroid Build Coastguard Worker 
33*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_FUNCTIONAL_ANY_INVOCABLE_H_
34*9356374aSAndroid Build Coastguard Worker #define ABSL_FUNCTIONAL_ANY_INVOCABLE_H_
35*9356374aSAndroid Build Coastguard Worker 
36*9356374aSAndroid Build Coastguard Worker #include <cstddef>
37*9356374aSAndroid Build Coastguard Worker #include <functional>
38*9356374aSAndroid Build Coastguard Worker #include <initializer_list>
39*9356374aSAndroid Build Coastguard Worker #include <type_traits>
40*9356374aSAndroid Build Coastguard Worker #include <utility>
41*9356374aSAndroid Build Coastguard Worker 
42*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
43*9356374aSAndroid Build Coastguard Worker #include "absl/functional/internal/any_invocable.h"
44*9356374aSAndroid Build Coastguard Worker #include "absl/meta/type_traits.h"
45*9356374aSAndroid Build Coastguard Worker #include "absl/utility/utility.h"
46*9356374aSAndroid Build Coastguard Worker 
47*9356374aSAndroid Build Coastguard Worker namespace absl {
48*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
49*9356374aSAndroid Build Coastguard Worker 
50*9356374aSAndroid Build Coastguard Worker // absl::AnyInvocable
51*9356374aSAndroid Build Coastguard Worker //
52*9356374aSAndroid Build Coastguard Worker // `absl::AnyInvocable` is a functional wrapper type, like `std::function`, that
53*9356374aSAndroid Build Coastguard Worker // assumes ownership of an invocable object. Unlike `std::function`, an
54*9356374aSAndroid Build Coastguard Worker // `absl::AnyInvocable` is more type-safe and provides the following additional
55*9356374aSAndroid Build Coastguard Worker // benefits:
56*9356374aSAndroid Build Coastguard Worker //
57*9356374aSAndroid Build Coastguard Worker // * Properly adheres to const correctness of the underlying type
58*9356374aSAndroid Build Coastguard Worker // * Is move-only so avoids concurrency problems with copied invocables and
59*9356374aSAndroid Build Coastguard Worker //   unnecessary copies in general.
60*9356374aSAndroid Build Coastguard Worker // * Supports reference qualifiers allowing it to perform unique actions (noted
61*9356374aSAndroid Build Coastguard Worker //   below).
62*9356374aSAndroid Build Coastguard Worker //
63*9356374aSAndroid Build Coastguard Worker // `absl::AnyInvocable` is a template, and an `absl::AnyInvocable` instantiation
64*9356374aSAndroid Build Coastguard Worker // may wrap any invocable object with a compatible function signature, e.g.
65*9356374aSAndroid Build Coastguard Worker // having arguments and return types convertible to types matching the
66*9356374aSAndroid Build Coastguard Worker // `absl::AnyInvocable` signature, and also matching any stated reference
67*9356374aSAndroid Build Coastguard Worker // qualifiers, as long as that type is moveable. It therefore provides broad
68*9356374aSAndroid Build Coastguard Worker // type erasure for functional objects.
69*9356374aSAndroid Build Coastguard Worker //
70*9356374aSAndroid Build Coastguard Worker // An `absl::AnyInvocable` is typically used as a type-erased function parameter
71*9356374aSAndroid Build Coastguard Worker // for accepting various functional objects:
72*9356374aSAndroid Build Coastguard Worker //
73*9356374aSAndroid Build Coastguard Worker // // Define a function taking an AnyInvocable parameter.
74*9356374aSAndroid Build Coastguard Worker // void my_func(absl::AnyInvocable<int()> f) {
75*9356374aSAndroid Build Coastguard Worker //   ...
76*9356374aSAndroid Build Coastguard Worker // };
77*9356374aSAndroid Build Coastguard Worker //
78*9356374aSAndroid Build Coastguard Worker // // That function can accept any invocable type:
79*9356374aSAndroid Build Coastguard Worker //
80*9356374aSAndroid Build Coastguard Worker // // Accept a function reference. We don't need to move a reference.
81*9356374aSAndroid Build Coastguard Worker // int func1() { return 0; };
82*9356374aSAndroid Build Coastguard Worker // my_func(func1);
83*9356374aSAndroid Build Coastguard Worker //
84*9356374aSAndroid Build Coastguard Worker // // Accept a lambda. We use std::move here because otherwise my_func would
85*9356374aSAndroid Build Coastguard Worker // // copy the lambda.
86*9356374aSAndroid Build Coastguard Worker // auto lambda = []() { return 0; };
87*9356374aSAndroid Build Coastguard Worker // my_func(std::move(lambda));
88*9356374aSAndroid Build Coastguard Worker //
89*9356374aSAndroid Build Coastguard Worker // // Accept a function pointer. We don't need to move a function pointer.
90*9356374aSAndroid Build Coastguard Worker // func2 = &func1;
91*9356374aSAndroid Build Coastguard Worker // my_func(func2);
92*9356374aSAndroid Build Coastguard Worker //
93*9356374aSAndroid Build Coastguard Worker // // Accept an std::function by moving it. Note that the lambda is copyable
94*9356374aSAndroid Build Coastguard Worker // // (satisfying std::function requirements) and moveable (satisfying
95*9356374aSAndroid Build Coastguard Worker // // absl::AnyInvocable requirements).
96*9356374aSAndroid Build Coastguard Worker // std::function<int()> func6 = []() { return 0; };
97*9356374aSAndroid Build Coastguard Worker // my_func(std::move(func6));
98*9356374aSAndroid Build Coastguard Worker //
99*9356374aSAndroid Build Coastguard Worker // `AnyInvocable` also properly respects `const` qualifiers, reference
100*9356374aSAndroid Build Coastguard Worker // qualifiers, and the `noexcept` specification (only in C++ 17 and beyond) as
101*9356374aSAndroid Build Coastguard Worker // part of the user-specified function type (e.g.
102*9356374aSAndroid Build Coastguard Worker // `AnyInvocable<void() const && noexcept>`). These qualifiers will be applied
103*9356374aSAndroid Build Coastguard Worker // to the `AnyInvocable` object's `operator()`, and the underlying invocable
104*9356374aSAndroid Build Coastguard Worker // must be compatible with those qualifiers.
105*9356374aSAndroid Build Coastguard Worker //
106*9356374aSAndroid Build Coastguard Worker // Comparison of const and non-const function types:
107*9356374aSAndroid Build Coastguard Worker //
108*9356374aSAndroid Build Coastguard Worker //   // Store a closure inside of `func` with the function type `int()`.
109*9356374aSAndroid Build Coastguard Worker //   // Note that we have made `func` itself `const`.
110*9356374aSAndroid Build Coastguard Worker //   const AnyInvocable<int()> func = [](){ return 0; };
111*9356374aSAndroid Build Coastguard Worker //
112*9356374aSAndroid Build Coastguard Worker //   func();  // Compile-error: the passed type `int()` isn't `const`.
113*9356374aSAndroid Build Coastguard Worker //
114*9356374aSAndroid Build Coastguard Worker //   // Store a closure inside of `const_func` with the function type
115*9356374aSAndroid Build Coastguard Worker //   // `int() const`.
116*9356374aSAndroid Build Coastguard Worker //   // Note that we have also made `const_func` itself `const`.
117*9356374aSAndroid Build Coastguard Worker //   const AnyInvocable<int() const> const_func = [](){ return 0; };
118*9356374aSAndroid Build Coastguard Worker //
119*9356374aSAndroid Build Coastguard Worker //   const_func();  // Fine: `int() const` is `const`.
120*9356374aSAndroid Build Coastguard Worker //
121*9356374aSAndroid Build Coastguard Worker // In the above example, the call `func()` would have compiled if
122*9356374aSAndroid Build Coastguard Worker // `std::function` were used even though the types are not const compatible.
123*9356374aSAndroid Build Coastguard Worker // This is a bug, and using `absl::AnyInvocable` properly detects that bug.
124*9356374aSAndroid Build Coastguard Worker //
125*9356374aSAndroid Build Coastguard Worker // In addition to affecting the signature of `operator()`, the `const` and
126*9356374aSAndroid Build Coastguard Worker // reference qualifiers of the function type also appropriately constrain which
127*9356374aSAndroid Build Coastguard Worker // kinds of invocable objects you are allowed to place into the `AnyInvocable`
128*9356374aSAndroid Build Coastguard Worker // instance. If you specify a function type that is const-qualified, then
129*9356374aSAndroid Build Coastguard Worker // anything that you attempt to put into the `AnyInvocable` must be callable on
130*9356374aSAndroid Build Coastguard Worker // a `const` instance of that type.
131*9356374aSAndroid Build Coastguard Worker //
132*9356374aSAndroid Build Coastguard Worker // Constraint example:
133*9356374aSAndroid Build Coastguard Worker //
134*9356374aSAndroid Build Coastguard Worker //   // Fine because the lambda is callable when `const`.
135*9356374aSAndroid Build Coastguard Worker //   AnyInvocable<int() const> func = [=](){ return 0; };
136*9356374aSAndroid Build Coastguard Worker //
137*9356374aSAndroid Build Coastguard Worker //   // This is a compile-error because the lambda isn't callable when `const`.
138*9356374aSAndroid Build Coastguard Worker //   AnyInvocable<int() const> error = [=]() mutable { return 0; };
139*9356374aSAndroid Build Coastguard Worker //
140*9356374aSAndroid Build Coastguard Worker // An `&&` qualifier can be used to express that an `absl::AnyInvocable`
141*9356374aSAndroid Build Coastguard Worker // instance should be invoked at most once:
142*9356374aSAndroid Build Coastguard Worker //
143*9356374aSAndroid Build Coastguard Worker //   // Invokes `continuation` with the logical result of an operation when
144*9356374aSAndroid Build Coastguard Worker //   // that operation completes (common in asynchronous code).
145*9356374aSAndroid Build Coastguard Worker //   void CallOnCompletion(AnyInvocable<void(int)&&> continuation) {
146*9356374aSAndroid Build Coastguard Worker //     int result_of_foo = foo();
147*9356374aSAndroid Build Coastguard Worker //
148*9356374aSAndroid Build Coastguard Worker //     // `std::move` is required because the `operator()` of `continuation` is
149*9356374aSAndroid Build Coastguard Worker //     // rvalue-reference qualified.
150*9356374aSAndroid Build Coastguard Worker //     std::move(continuation)(result_of_foo);
151*9356374aSAndroid Build Coastguard Worker //   }
152*9356374aSAndroid Build Coastguard Worker //
153*9356374aSAndroid Build Coastguard Worker // Attempting to call `absl::AnyInvocable` multiple times in such a case
154*9356374aSAndroid Build Coastguard Worker // results in undefined behavior.
155*9356374aSAndroid Build Coastguard Worker //
156*9356374aSAndroid Build Coastguard Worker // Invoking an empty `absl::AnyInvocable` results in undefined behavior:
157*9356374aSAndroid Build Coastguard Worker //
158*9356374aSAndroid Build Coastguard Worker //   // Create an empty instance using the default constructor.
159*9356374aSAndroid Build Coastguard Worker //   AnyInvocable<void()> empty;
160*9356374aSAndroid Build Coastguard Worker //   empty();  // WARNING: Undefined behavior!
161*9356374aSAndroid Build Coastguard Worker template <class Sig>
162*9356374aSAndroid Build Coastguard Worker class AnyInvocable : private internal_any_invocable::Impl<Sig> {
163*9356374aSAndroid Build Coastguard Worker  private:
164*9356374aSAndroid Build Coastguard Worker   static_assert(
165*9356374aSAndroid Build Coastguard Worker       std::is_function<Sig>::value,
166*9356374aSAndroid Build Coastguard Worker       "The template argument of AnyInvocable must be a function type.");
167*9356374aSAndroid Build Coastguard Worker 
168*9356374aSAndroid Build Coastguard Worker   using Impl = internal_any_invocable::Impl<Sig>;
169*9356374aSAndroid Build Coastguard Worker 
170*9356374aSAndroid Build Coastguard Worker  public:
171*9356374aSAndroid Build Coastguard Worker   // The return type of Sig
172*9356374aSAndroid Build Coastguard Worker   using result_type = typename Impl::result_type;
173*9356374aSAndroid Build Coastguard Worker 
174*9356374aSAndroid Build Coastguard Worker   // Constructors
175*9356374aSAndroid Build Coastguard Worker 
176*9356374aSAndroid Build Coastguard Worker   // Constructs the `AnyInvocable` in an empty state.
177*9356374aSAndroid Build Coastguard Worker   // Invoking it results in undefined behavior.
178*9356374aSAndroid Build Coastguard Worker   AnyInvocable() noexcept = default;
AnyInvocable(std::nullptr_t)179*9356374aSAndroid Build Coastguard Worker   AnyInvocable(std::nullptr_t) noexcept {}  // NOLINT
180*9356374aSAndroid Build Coastguard Worker 
181*9356374aSAndroid Build Coastguard Worker   // Constructs the `AnyInvocable` from an existing `AnyInvocable` by a move.
182*9356374aSAndroid Build Coastguard Worker   // Note that `f` is not guaranteed to be empty after move-construction,
183*9356374aSAndroid Build Coastguard Worker   // although it may be.
184*9356374aSAndroid Build Coastguard Worker   AnyInvocable(AnyInvocable&& /*f*/) noexcept = default;
185*9356374aSAndroid Build Coastguard Worker 
186*9356374aSAndroid Build Coastguard Worker   // Constructs an `AnyInvocable` from an invocable object.
187*9356374aSAndroid Build Coastguard Worker   //
188*9356374aSAndroid Build Coastguard Worker   // Upon construction, `*this` is only empty if `f` is a function pointer or
189*9356374aSAndroid Build Coastguard Worker   // member pointer type and is null, or if `f` is an `AnyInvocable` that is
190*9356374aSAndroid Build Coastguard Worker   // empty.
191*9356374aSAndroid Build Coastguard Worker   template <class F, typename = absl::enable_if_t<
192*9356374aSAndroid Build Coastguard Worker                          internal_any_invocable::CanConvert<Sig, F>::value>>
AnyInvocable(F && f)193*9356374aSAndroid Build Coastguard Worker   AnyInvocable(F&& f)  // NOLINT
194*9356374aSAndroid Build Coastguard Worker       : Impl(internal_any_invocable::ConversionConstruct(),
195*9356374aSAndroid Build Coastguard Worker              std::forward<F>(f)) {}
196*9356374aSAndroid Build Coastguard Worker 
197*9356374aSAndroid Build Coastguard Worker   // Constructs an `AnyInvocable` that holds an invocable object of type `T`,
198*9356374aSAndroid Build Coastguard Worker   // which is constructed in-place from the given arguments.
199*9356374aSAndroid Build Coastguard Worker   //
200*9356374aSAndroid Build Coastguard Worker   // Example:
201*9356374aSAndroid Build Coastguard Worker   //
202*9356374aSAndroid Build Coastguard Worker   //   AnyInvocable<int(int)> func(
203*9356374aSAndroid Build Coastguard Worker   //       absl::in_place_type<PossiblyImmovableType>, arg1, arg2);
204*9356374aSAndroid Build Coastguard Worker   //
205*9356374aSAndroid Build Coastguard Worker   template <class T, class... Args,
206*9356374aSAndroid Build Coastguard Worker             typename = absl::enable_if_t<
207*9356374aSAndroid Build Coastguard Worker                 internal_any_invocable::CanEmplace<Sig, T, Args...>::value>>
AnyInvocable(absl::in_place_type_t<T>,Args &&...args)208*9356374aSAndroid Build Coastguard Worker   explicit AnyInvocable(absl::in_place_type_t<T>, Args&&... args)
209*9356374aSAndroid Build Coastguard Worker       : Impl(absl::in_place_type<absl::decay_t<T>>,
210*9356374aSAndroid Build Coastguard Worker              std::forward<Args>(args)...) {
211*9356374aSAndroid Build Coastguard Worker     static_assert(std::is_same<T, absl::decay_t<T>>::value,
212*9356374aSAndroid Build Coastguard Worker                   "The explicit template argument of in_place_type is required "
213*9356374aSAndroid Build Coastguard Worker                   "to be an unqualified object type.");
214*9356374aSAndroid Build Coastguard Worker   }
215*9356374aSAndroid Build Coastguard Worker 
216*9356374aSAndroid Build Coastguard Worker   // Overload of the above constructor to support list-initialization.
217*9356374aSAndroid Build Coastguard Worker   template <class T, class U, class... Args,
218*9356374aSAndroid Build Coastguard Worker             typename = absl::enable_if_t<internal_any_invocable::CanEmplace<
219*9356374aSAndroid Build Coastguard Worker                 Sig, T, std::initializer_list<U>&, Args...>::value>>
AnyInvocable(absl::in_place_type_t<T>,std::initializer_list<U> ilist,Args &&...args)220*9356374aSAndroid Build Coastguard Worker   explicit AnyInvocable(absl::in_place_type_t<T>,
221*9356374aSAndroid Build Coastguard Worker                         std::initializer_list<U> ilist, Args&&... args)
222*9356374aSAndroid Build Coastguard Worker       : Impl(absl::in_place_type<absl::decay_t<T>>, ilist,
223*9356374aSAndroid Build Coastguard Worker              std::forward<Args>(args)...) {
224*9356374aSAndroid Build Coastguard Worker     static_assert(std::is_same<T, absl::decay_t<T>>::value,
225*9356374aSAndroid Build Coastguard Worker                   "The explicit template argument of in_place_type is required "
226*9356374aSAndroid Build Coastguard Worker                   "to be an unqualified object type.");
227*9356374aSAndroid Build Coastguard Worker   }
228*9356374aSAndroid Build Coastguard Worker 
229*9356374aSAndroid Build Coastguard Worker   // Assignment Operators
230*9356374aSAndroid Build Coastguard Worker 
231*9356374aSAndroid Build Coastguard Worker   // Assigns an `AnyInvocable` through move-assignment.
232*9356374aSAndroid Build Coastguard Worker   // Note that `f` is not guaranteed to be empty after move-assignment
233*9356374aSAndroid Build Coastguard Worker   // although it may be.
234*9356374aSAndroid Build Coastguard Worker   AnyInvocable& operator=(AnyInvocable&& /*f*/) noexcept = default;
235*9356374aSAndroid Build Coastguard Worker 
236*9356374aSAndroid Build Coastguard Worker   // Assigns an `AnyInvocable` from a nullptr, clearing the `AnyInvocable`. If
237*9356374aSAndroid Build Coastguard Worker   // not empty, destroys the target, putting `*this` into an empty state.
238*9356374aSAndroid Build Coastguard Worker   AnyInvocable& operator=(std::nullptr_t) noexcept {
239*9356374aSAndroid Build Coastguard Worker     this->Clear();
240*9356374aSAndroid Build Coastguard Worker     return *this;
241*9356374aSAndroid Build Coastguard Worker   }
242*9356374aSAndroid Build Coastguard Worker 
243*9356374aSAndroid Build Coastguard Worker   // Assigns an `AnyInvocable` from an existing `AnyInvocable` instance.
244*9356374aSAndroid Build Coastguard Worker   //
245*9356374aSAndroid Build Coastguard Worker   // Upon assignment, `*this` is only empty if `f` is a function pointer or
246*9356374aSAndroid Build Coastguard Worker   // member pointer type and is null, or if `f` is an `AnyInvocable` that is
247*9356374aSAndroid Build Coastguard Worker   // empty.
248*9356374aSAndroid Build Coastguard Worker   template <class F, typename = absl::enable_if_t<
249*9356374aSAndroid Build Coastguard Worker                          internal_any_invocable::CanAssign<Sig, F>::value>>
250*9356374aSAndroid Build Coastguard Worker   AnyInvocable& operator=(F&& f) {
251*9356374aSAndroid Build Coastguard Worker     *this = AnyInvocable(std::forward<F>(f));
252*9356374aSAndroid Build Coastguard Worker     return *this;
253*9356374aSAndroid Build Coastguard Worker   }
254*9356374aSAndroid Build Coastguard Worker 
255*9356374aSAndroid Build Coastguard Worker   // Assigns an `AnyInvocable` from a reference to an invocable object.
256*9356374aSAndroid Build Coastguard Worker   // Upon assignment, stores a reference to the invocable object in the
257*9356374aSAndroid Build Coastguard Worker   // `AnyInvocable` instance.
258*9356374aSAndroid Build Coastguard Worker   template <
259*9356374aSAndroid Build Coastguard Worker       class F,
260*9356374aSAndroid Build Coastguard Worker       typename = absl::enable_if_t<
261*9356374aSAndroid Build Coastguard Worker           internal_any_invocable::CanAssignReferenceWrapper<Sig, F>::value>>
262*9356374aSAndroid Build Coastguard Worker   AnyInvocable& operator=(std::reference_wrapper<F> f) noexcept {
263*9356374aSAndroid Build Coastguard Worker     *this = AnyInvocable(f);
264*9356374aSAndroid Build Coastguard Worker     return *this;
265*9356374aSAndroid Build Coastguard Worker   }
266*9356374aSAndroid Build Coastguard Worker 
267*9356374aSAndroid Build Coastguard Worker   // Destructor
268*9356374aSAndroid Build Coastguard Worker 
269*9356374aSAndroid Build Coastguard Worker   // If not empty, destroys the target.
270*9356374aSAndroid Build Coastguard Worker   ~AnyInvocable() = default;
271*9356374aSAndroid Build Coastguard Worker 
272*9356374aSAndroid Build Coastguard Worker   // absl::AnyInvocable::swap()
273*9356374aSAndroid Build Coastguard Worker   //
274*9356374aSAndroid Build Coastguard Worker   // Exchanges the targets of `*this` and `other`.
swap(AnyInvocable & other)275*9356374aSAndroid Build Coastguard Worker   void swap(AnyInvocable& other) noexcept { std::swap(*this, other); }
276*9356374aSAndroid Build Coastguard Worker 
277*9356374aSAndroid Build Coastguard Worker   // absl::AnyInvocable::operator bool()
278*9356374aSAndroid Build Coastguard Worker   //
279*9356374aSAndroid Build Coastguard Worker   // Returns `true` if `*this` is not empty.
280*9356374aSAndroid Build Coastguard Worker   //
281*9356374aSAndroid Build Coastguard Worker   // WARNING: An `AnyInvocable` that wraps an empty `std::function` is not
282*9356374aSAndroid Build Coastguard Worker   // itself empty. This behavior is consistent with the standard equivalent
283*9356374aSAndroid Build Coastguard Worker   // `std::move_only_function`.
284*9356374aSAndroid Build Coastguard Worker   //
285*9356374aSAndroid Build Coastguard Worker   // In other words:
286*9356374aSAndroid Build Coastguard Worker   //   std::function<void()> f;  // empty
287*9356374aSAndroid Build Coastguard Worker   //   absl::AnyInvocable<void()> a = std::move(f);  // not empty
288*9356374aSAndroid Build Coastguard Worker   //
289*9356374aSAndroid Build Coastguard Worker   // Invoking an empty `AnyInvocable` results in undefined behavior.
290*9356374aSAndroid Build Coastguard Worker   explicit operator bool() const noexcept { return this->HasValue(); }
291*9356374aSAndroid Build Coastguard Worker 
292*9356374aSAndroid Build Coastguard Worker   // Invokes the target object of `*this`. `*this` must not be empty.
293*9356374aSAndroid Build Coastguard Worker   //
294*9356374aSAndroid Build Coastguard Worker   // Note: The signature of this function call operator is the same as the
295*9356374aSAndroid Build Coastguard Worker   //       template parameter `Sig`.
296*9356374aSAndroid Build Coastguard Worker   using Impl::operator();
297*9356374aSAndroid Build Coastguard Worker 
298*9356374aSAndroid Build Coastguard Worker   // Equality operators
299*9356374aSAndroid Build Coastguard Worker 
300*9356374aSAndroid Build Coastguard Worker   // Returns `true` if `*this` is empty.
301*9356374aSAndroid Build Coastguard Worker   friend bool operator==(const AnyInvocable& f, std::nullptr_t) noexcept {
302*9356374aSAndroid Build Coastguard Worker     return !f.HasValue();
303*9356374aSAndroid Build Coastguard Worker   }
304*9356374aSAndroid Build Coastguard Worker 
305*9356374aSAndroid Build Coastguard Worker   // Returns `true` if `*this` is empty.
306*9356374aSAndroid Build Coastguard Worker   friend bool operator==(std::nullptr_t, const AnyInvocable& f) noexcept {
307*9356374aSAndroid Build Coastguard Worker     return !f.HasValue();
308*9356374aSAndroid Build Coastguard Worker   }
309*9356374aSAndroid Build Coastguard Worker 
310*9356374aSAndroid Build Coastguard Worker   // Returns `false` if `*this` is empty.
311*9356374aSAndroid Build Coastguard Worker   friend bool operator!=(const AnyInvocable& f, std::nullptr_t) noexcept {
312*9356374aSAndroid Build Coastguard Worker     return f.HasValue();
313*9356374aSAndroid Build Coastguard Worker   }
314*9356374aSAndroid Build Coastguard Worker 
315*9356374aSAndroid Build Coastguard Worker   // Returns `false` if `*this` is empty.
316*9356374aSAndroid Build Coastguard Worker   friend bool operator!=(std::nullptr_t, const AnyInvocable& f) noexcept {
317*9356374aSAndroid Build Coastguard Worker     return f.HasValue();
318*9356374aSAndroid Build Coastguard Worker   }
319*9356374aSAndroid Build Coastguard Worker 
320*9356374aSAndroid Build Coastguard Worker   // swap()
321*9356374aSAndroid Build Coastguard Worker   //
322*9356374aSAndroid Build Coastguard Worker   // Exchanges the targets of `f1` and `f2`.
swap(AnyInvocable & f1,AnyInvocable & f2)323*9356374aSAndroid Build Coastguard Worker   friend void swap(AnyInvocable& f1, AnyInvocable& f2) noexcept { f1.swap(f2); }
324*9356374aSAndroid Build Coastguard Worker 
325*9356374aSAndroid Build Coastguard Worker  private:
326*9356374aSAndroid Build Coastguard Worker   // Friending other instantiations is necessary for conversions.
327*9356374aSAndroid Build Coastguard Worker   template <bool /*SigIsNoexcept*/, class /*ReturnType*/, class... /*P*/>
328*9356374aSAndroid Build Coastguard Worker   friend class internal_any_invocable::CoreImpl;
329*9356374aSAndroid Build Coastguard Worker };
330*9356374aSAndroid Build Coastguard Worker 
331*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
332*9356374aSAndroid Build Coastguard Worker }  // namespace absl
333*9356374aSAndroid Build Coastguard Worker 
334*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_FUNCTIONAL_ANY_INVOCABLE_H_
335