xref: /aosp_15_r20/external/libchrome/base/callback.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker //
5*635a8641SAndroid Build Coastguard Worker // NOTE: Header files that do not require the full definition of Callback or
6*635a8641SAndroid Build Coastguard Worker // Closure should #include "base/callback_forward.h" instead of this file.
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #ifndef BASE_CALLBACK_H_
9*635a8641SAndroid Build Coastguard Worker #define BASE_CALLBACK_H_
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
12*635a8641SAndroid Build Coastguard Worker 
13*635a8641SAndroid Build Coastguard Worker #include "base/callback_forward.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/callback_internal.h"
15*635a8641SAndroid Build Coastguard Worker 
16*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
17*635a8641SAndroid Build Coastguard Worker // Usage documentation
18*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
19*635a8641SAndroid Build Coastguard Worker //
20*635a8641SAndroid Build Coastguard Worker // Overview:
21*635a8641SAndroid Build Coastguard Worker // A callback is similar in concept to a function pointer: it wraps a runnable
22*635a8641SAndroid Build Coastguard Worker // object such as a function, method, lambda, or even another callback, allowing
23*635a8641SAndroid Build Coastguard Worker // the runnable object to be invoked later via the callback object.
24*635a8641SAndroid Build Coastguard Worker //
25*635a8641SAndroid Build Coastguard Worker // Unlike function pointers, callbacks are created with base::BindOnce() or
26*635a8641SAndroid Build Coastguard Worker // base::BindRepeating() and support partial function application.
27*635a8641SAndroid Build Coastguard Worker //
28*635a8641SAndroid Build Coastguard Worker // A base::OnceCallback may be Run() at most once; a base::RepeatingCallback may
29*635a8641SAndroid Build Coastguard Worker // be Run() any number of times. |is_null()| is guaranteed to return true for a
30*635a8641SAndroid Build Coastguard Worker // moved-from callback.
31*635a8641SAndroid Build Coastguard Worker //
32*635a8641SAndroid Build Coastguard Worker //   // The lambda takes two arguments, but the first argument |x| is bound at
33*635a8641SAndroid Build Coastguard Worker //   // callback creation.
34*635a8641SAndroid Build Coastguard Worker //   base::OnceCallback<int(int)> cb = base::BindOnce([] (int x, int y) {
35*635a8641SAndroid Build Coastguard Worker //     return x + y;
36*635a8641SAndroid Build Coastguard Worker //   }, 1);
37*635a8641SAndroid Build Coastguard Worker //   // Run() only needs the remaining unbound argument |y|.
38*635a8641SAndroid Build Coastguard Worker //   printf("1 + 2 = %d\n", std::move(cb).Run(2));  // Prints 3
39*635a8641SAndroid Build Coastguard Worker //   printf("cb is null? %s\n",
40*635a8641SAndroid Build Coastguard Worker //          cb.is_null() ? "true" : "false");  // Prints true
41*635a8641SAndroid Build Coastguard Worker //   std::move(cb).Run(2);  // Crashes since |cb| has already run.
42*635a8641SAndroid Build Coastguard Worker //
43*635a8641SAndroid Build Coastguard Worker // Callbacks also support cancellation. A common use is binding the receiver
44*635a8641SAndroid Build Coastguard Worker // object as a WeakPtr<T>. If that weak pointer is invalidated, calling Run()
45*635a8641SAndroid Build Coastguard Worker // will be a no-op. Note that |is_cancelled()| and |is_null()| are distinct:
46*635a8641SAndroid Build Coastguard Worker // simply cancelling a callback will not also make it null.
47*635a8641SAndroid Build Coastguard Worker //
48*635a8641SAndroid Build Coastguard Worker // base::Callback is currently a type alias for base::RepeatingCallback. In the
49*635a8641SAndroid Build Coastguard Worker // future, we expect to flip this to default to base::OnceCallback.
50*635a8641SAndroid Build Coastguard Worker //
51*635a8641SAndroid Build Coastguard Worker // See //docs/callback.md for the full documentation.
52*635a8641SAndroid Build Coastguard Worker 
53*635a8641SAndroid Build Coastguard Worker namespace base {
54*635a8641SAndroid Build Coastguard Worker 
55*635a8641SAndroid Build Coastguard Worker template <typename R, typename... Args>
56*635a8641SAndroid Build Coastguard Worker class OnceCallback<R(Args...)> : public internal::CallbackBase {
57*635a8641SAndroid Build Coastguard Worker  public:
58*635a8641SAndroid Build Coastguard Worker   using RunType = R(Args...);
59*635a8641SAndroid Build Coastguard Worker   using PolymorphicInvoke = R (*)(internal::BindStateBase*,
60*635a8641SAndroid Build Coastguard Worker                                   internal::PassingType<Args>...);
61*635a8641SAndroid Build Coastguard Worker 
62*635a8641SAndroid Build Coastguard Worker   constexpr OnceCallback() = default;
63*635a8641SAndroid Build Coastguard Worker   OnceCallback(std::nullptr_t) = delete;
64*635a8641SAndroid Build Coastguard Worker 
OnceCallback(internal::BindStateBase * bind_state)65*635a8641SAndroid Build Coastguard Worker   explicit OnceCallback(internal::BindStateBase* bind_state)
66*635a8641SAndroid Build Coastguard Worker       : internal::CallbackBase(bind_state) {}
67*635a8641SAndroid Build Coastguard Worker 
68*635a8641SAndroid Build Coastguard Worker   OnceCallback(const OnceCallback&) = delete;
69*635a8641SAndroid Build Coastguard Worker   OnceCallback& operator=(const OnceCallback&) = delete;
70*635a8641SAndroid Build Coastguard Worker 
71*635a8641SAndroid Build Coastguard Worker   OnceCallback(OnceCallback&&) noexcept = default;
72*635a8641SAndroid Build Coastguard Worker   OnceCallback& operator=(OnceCallback&&) noexcept = default;
73*635a8641SAndroid Build Coastguard Worker 
OnceCallback(RepeatingCallback<RunType> other)74*635a8641SAndroid Build Coastguard Worker   OnceCallback(RepeatingCallback<RunType> other)
75*635a8641SAndroid Build Coastguard Worker       : internal::CallbackBase(std::move(other)) {}
76*635a8641SAndroid Build Coastguard Worker 
77*635a8641SAndroid Build Coastguard Worker   OnceCallback& operator=(RepeatingCallback<RunType> other) {
78*635a8641SAndroid Build Coastguard Worker     static_cast<internal::CallbackBase&>(*this) = std::move(other);
79*635a8641SAndroid Build Coastguard Worker     return *this;
80*635a8641SAndroid Build Coastguard Worker   }
81*635a8641SAndroid Build Coastguard Worker 
Equals(const OnceCallback & other)82*635a8641SAndroid Build Coastguard Worker   bool Equals(const OnceCallback& other) const { return EqualsInternal(other); }
83*635a8641SAndroid Build Coastguard Worker 
Run(Args...args)84*635a8641SAndroid Build Coastguard Worker   R Run(Args... args) const & {
85*635a8641SAndroid Build Coastguard Worker     static_assert(!sizeof(*this),
86*635a8641SAndroid Build Coastguard Worker                   "OnceCallback::Run() may only be invoked on a non-const "
87*635a8641SAndroid Build Coastguard Worker                   "rvalue, i.e. std::move(callback).Run().");
88*635a8641SAndroid Build Coastguard Worker     NOTREACHED();
89*635a8641SAndroid Build Coastguard Worker   }
90*635a8641SAndroid Build Coastguard Worker 
Run(Args...args)91*635a8641SAndroid Build Coastguard Worker   R Run(Args... args) && {
92*635a8641SAndroid Build Coastguard Worker     // Move the callback instance into a local variable before the invocation,
93*635a8641SAndroid Build Coastguard Worker     // that ensures the internal state is cleared after the invocation.
94*635a8641SAndroid Build Coastguard Worker     // It's not safe to touch |this| after the invocation, since running the
95*635a8641SAndroid Build Coastguard Worker     // bound function may destroy |this|.
96*635a8641SAndroid Build Coastguard Worker     OnceCallback cb = std::move(*this);
97*635a8641SAndroid Build Coastguard Worker     PolymorphicInvoke f =
98*635a8641SAndroid Build Coastguard Worker         reinterpret_cast<PolymorphicInvoke>(cb.polymorphic_invoke());
99*635a8641SAndroid Build Coastguard Worker     return f(cb.bind_state_.get(), std::forward<Args>(args)...);
100*635a8641SAndroid Build Coastguard Worker   }
101*635a8641SAndroid Build Coastguard Worker };
102*635a8641SAndroid Build Coastguard Worker 
103*635a8641SAndroid Build Coastguard Worker template <typename R, typename... Args>
104*635a8641SAndroid Build Coastguard Worker class RepeatingCallback<R(Args...)> : public internal::CallbackBaseCopyable {
105*635a8641SAndroid Build Coastguard Worker  public:
106*635a8641SAndroid Build Coastguard Worker   using RunType = R(Args...);
107*635a8641SAndroid Build Coastguard Worker   using PolymorphicInvoke = R (*)(internal::BindStateBase*,
108*635a8641SAndroid Build Coastguard Worker                                   internal::PassingType<Args>...);
109*635a8641SAndroid Build Coastguard Worker 
110*635a8641SAndroid Build Coastguard Worker   constexpr RepeatingCallback() = default;
111*635a8641SAndroid Build Coastguard Worker   RepeatingCallback(std::nullptr_t) = delete;
112*635a8641SAndroid Build Coastguard Worker 
RepeatingCallback(internal::BindStateBase * bind_state)113*635a8641SAndroid Build Coastguard Worker   explicit RepeatingCallback(internal::BindStateBase* bind_state)
114*635a8641SAndroid Build Coastguard Worker       : internal::CallbackBaseCopyable(bind_state) {}
115*635a8641SAndroid Build Coastguard Worker 
116*635a8641SAndroid Build Coastguard Worker   // Copyable and movable.
117*635a8641SAndroid Build Coastguard Worker   RepeatingCallback(const RepeatingCallback&) = default;
118*635a8641SAndroid Build Coastguard Worker   RepeatingCallback& operator=(const RepeatingCallback&) = default;
119*635a8641SAndroid Build Coastguard Worker   RepeatingCallback(RepeatingCallback&&) noexcept = default;
120*635a8641SAndroid Build Coastguard Worker   RepeatingCallback& operator=(RepeatingCallback&&) noexcept = default;
121*635a8641SAndroid Build Coastguard Worker 
Equals(const RepeatingCallback & other)122*635a8641SAndroid Build Coastguard Worker   bool Equals(const RepeatingCallback& other) const {
123*635a8641SAndroid Build Coastguard Worker     return EqualsInternal(other);
124*635a8641SAndroid Build Coastguard Worker   }
125*635a8641SAndroid Build Coastguard Worker 
Run(Args...args)126*635a8641SAndroid Build Coastguard Worker   R Run(Args... args) const & {
127*635a8641SAndroid Build Coastguard Worker     PolymorphicInvoke f =
128*635a8641SAndroid Build Coastguard Worker         reinterpret_cast<PolymorphicInvoke>(this->polymorphic_invoke());
129*635a8641SAndroid Build Coastguard Worker     return f(this->bind_state_.get(), std::forward<Args>(args)...);
130*635a8641SAndroid Build Coastguard Worker   }
131*635a8641SAndroid Build Coastguard Worker 
Run(Args...args)132*635a8641SAndroid Build Coastguard Worker   R Run(Args... args) && {
133*635a8641SAndroid Build Coastguard Worker     // Move the callback instance into a local variable before the invocation,
134*635a8641SAndroid Build Coastguard Worker     // that ensures the internal state is cleared after the invocation.
135*635a8641SAndroid Build Coastguard Worker     // It's not safe to touch |this| after the invocation, since running the
136*635a8641SAndroid Build Coastguard Worker     // bound function may destroy |this|.
137*635a8641SAndroid Build Coastguard Worker     RepeatingCallback cb = std::move(*this);
138*635a8641SAndroid Build Coastguard Worker     PolymorphicInvoke f =
139*635a8641SAndroid Build Coastguard Worker         reinterpret_cast<PolymorphicInvoke>(cb.polymorphic_invoke());
140*635a8641SAndroid Build Coastguard Worker     return f(cb.bind_state_.get(), std::forward<Args>(args)...);
141*635a8641SAndroid Build Coastguard Worker   }
142*635a8641SAndroid Build Coastguard Worker };
143*635a8641SAndroid Build Coastguard Worker 
144*635a8641SAndroid Build Coastguard Worker }  // namespace base
145*635a8641SAndroid Build Coastguard Worker 
146*635a8641SAndroid Build Coastguard Worker #endif  // BASE_CALLBACK_H_
147