blob: 8aa759e3243e54cec4df176feab243def898dc04 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2011 The Chromium Authors
[email protected]b38d3572011-02-15 01:27:382// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Daniel Cheng91f6fbaf2022-09-16 12:07:485#ifndef BASE_FUNCTIONAL_BIND_INTERNAL_H_
6#define BASE_FUNCTIONAL_BIND_INTERNAL_H_
[email protected]b38d3572011-02-15 01:27:387
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
9
Peter Kastingd077bb22023-12-16 08:40:0010#include <concepts>
jdoerriec79c2fa22019-02-26 12:35:0311#include <functional>
jdoerrie68f2d51b2019-02-28 17:32:4412#include <memory>
jdoerrie5c4dc4e2019-02-01 18:02:3313#include <tuple>
vmpstrc52317f2015-11-18 08:43:2614#include <type_traits>
Jeremy Roman84956fa2017-08-16 15:55:2015#include <utility>
vmpstrc52317f2015-11-18 08:43:2616
Hans Wennborg7b533712020-06-22 20:52:2717#include "base/check.h"
Sylvain Defresneec3270c2018-05-31 17:19:1518#include "base/compiler_specific.h"
Daniel Cheng91f6fbaf2022-09-16 12:07:4819#include "base/functional/callback_internal.h"
Daniel Chengef3dad32022-11-25 03:26:2820#include "base/functional/unretained_traits.h"
Lukasz Anforowiczfe7bcbae2021-11-03 01:49:5621#include "base/memory/raw_ptr.h"
danakj92061c12022-07-28 23:53:5022#include "base/memory/raw_ref.h"
[email protected]93540582011-05-16 22:35:1423#include "base/memory/weak_ptr.h"
Peter Kasting336972e2024-02-09 22:08:0024#include "base/types/is_complete.h"
Peter Kastingf871b5e2023-11-17 00:29:2725#include "base/types/is_instantiation.h"
Nick Diego Yamanecc318b732024-02-27 03:51:4526#include "base/types/to_address.h"
[email protected]054ac7542011-02-27 01:25:5927#include "build/build_config.h"
Daniel Cheng2248b332022-07-27 06:16:5928#include "third_party/abseil-cpp/absl/functional/function_ref.h"
[email protected]054ac7542011-02-27 01:25:5929
David Sanders9f1bb402025-03-03 19:13:5630#if PA_BUILDFLAG(USE_ASAN_BACKUP_REF_PTR)
31#include "base/memory/raw_ptr_asan_bound_arg_tracker.h"
32#endif
33
Peter Kasting7419b0be2023-12-19 05:31:1934// See docs/callback.md for user documentation.
[email protected]24292642012-07-12 20:06:4035//
Peter Kasting7419b0be2023-12-19 05:31:1936// Concepts:
tzik99de02b2016-07-01 05:54:1237// Functor -- A movable type representing something that should be called.
Peter Kasting7419b0be2023-12-19 05:31:1938// All function pointers and `Callback<>` are functors even if the
tzik99de02b2016-07-01 05:54:1239// invocation syntax differs.
[email protected]7296f2762011-11-21 19:23:4440// RunType -- A function type (as opposed to function _pointer_ type) for
Peter Kasting7419b0be2023-12-19 05:31:1941// a `Callback<>::Run()`. Usually just a convenience typedef.
tzikce3ecf82015-12-15 06:41:4942// (Bound)Args -- A set of types that stores the arguments.
[email protected]b38d3572011-02-15 01:27:3843//
[email protected]7296f2762011-11-21 19:23:4444// Types:
Peter Kasting7419b0be2023-12-19 05:31:1945// `ForceVoidReturn<>` -- Helper class for translating function signatures to
46// equivalent forms with a `void` return type.
47// `FunctorTraits<>` -- Type traits used to determine the correct RunType and
48// invocation manner for a Functor. This is where
49// function signature adapters are applied.
50// `StorageTraits<>` -- Type traits that determine how a bound argument is
51// stored in `BindState<>`.
52// `InvokeHelper<>` -- Takes a Functor + arguments and actually invokes it.
53// Handles the differing syntaxes needed for `WeakPtr<>`
54// support. This is separate from `Invoker<>` to avoid
55// creating multiple versions of `Invoker<>`.
56// `Invoker<>` -- Unwraps the curried parameters and executes the Functor.
57// `BindState<>` -- Stores the curried parameters, and is the main entry point
58// into the `Bind()` system.
[email protected]4346ef912011-02-19 00:52:1559
Xiaohan Wang38e4ebb2022-01-19 06:57:4360#if BUILDFLAG(IS_WIN)
tzikc44f8102018-07-24 09:49:1961namespace Microsoft {
62namespace WRL {
63template <typename>
64class ComPtr;
65} // namespace WRL
66} // namespace Microsoft
67#endif
68
Peter Kastinga85265e32018-02-15 08:30:2369namespace base {
70
71template <typename T>
72struct IsWeakReceiver;
73
74template <typename>
75struct BindUnwrapTraits;
76
Daniel Chengef1375f22023-11-15 00:06:1277template <typename Functor, typename BoundArgsTuple>
Peter Kastinga85265e32018-02-15 08:30:2378struct CallbackCancellationTraits;
79
Daniel Cheng2248b332022-07-27 06:16:5980template <typename Signature>
81class FunctionRef;
82
Peter Kastinga7e941e2023-12-20 01:13:4983// A tag type to return when `Bind()` calls fail. In this case we intentionally
84// don't return `void`, since that would produce spurious errors like "variable
85// has incomplete type 'void'" when assigning the result of
86// `Bind{Once,Repeating}()` to an `auto`.
87struct BindFailedCheckPreviousErrors {};
88
Bartek Nowierski58ce9be82023-01-06 03:55:4789namespace unretained_traits {
90
Peter Kasting7419b0be2023-12-19 05:31:1991// `UnretainedWrapper` will check and report if pointer is dangling upon
Bartek Nowierski58ce9be82023-01-06 03:55:4792// invocation.
93struct MayNotDangle {};
Peter Kasting7419b0be2023-12-19 05:31:1994// `UnretainedWrapper` won't check if pointer is dangling upon invocation. For
95// extra safety, the receiver must be of type `MayBeDangling<>`.
Bartek Nowierski58ce9be82023-01-06 03:55:4796struct MayDangle {};
Peter Kasting7419b0be2023-12-19 05:31:1997// `UnretainedWrapper` won't check if pointer is dangling upon invocation. The
98// receiver doesn't have to be a `raw_ptr<>`. This is just a temporary state, to
99// allow dangling pointers that would otherwise crash if `MayNotDangle` was
100// used. It should be replaced ASAP with `MayNotDangle` (after fixing the
101// dangling pointers) or with `MayDangle` if there is really no other way (after
102// making receivers `MayBeDangling<>`).
Bartek Nowierski58ce9be82023-01-06 03:55:47103struct MayDangleUntriaged {};
104
105} // namespace unretained_traits
106
Peter Kastinga85265e32018-02-15 08:30:23107namespace internal {
108
Bartek Nowierskie132f6482023-02-15 20:19:52109template <typename T,
110 typename UnretainedTrait,
111 RawPtrTraits PtrTraits = RawPtrTraits::kEmpty>
Peter Kastinga85265e32018-02-15 08:30:23112class UnretainedWrapper {
Peter Kasting7419b0be2023-12-19 05:31:19113 // Note that if `PtrTraits` already includes `MayDangle`, `DanglingRawPtrType`
Bartek Nowierskie132f6482023-02-15 20:19:52114 // will be identical to `raw_ptr<T, PtrTraits>`.
115 using DanglingRawPtrType = MayBeDangling<T, PtrTraits>;
116
Paul Semel6cef46d2023-02-28 10:02:21117 public:
Bartek Nowierski0ccd76b2023-03-22 17:02:58118 // We want the getter type to match the receiver parameter that it is passed
119 // into, to minimize `raw_ptr<T>` <-> `T*` conversions. We also would like to
120 // match `StorageType`, but sometimes we can't have both, as shown in
121 // https://docs.google.com/document/d/1dLM34aKqbNBfRdOYxxV_T-zQU4J5wjmXwIBJZr7JvZM/edit
122 // When we can't have both, prefer the former, mostly because
Peter Kasting3b01151f2023-11-27 20:04:21123 // `GetPtrType`=`raw_ptr<T>` would break if e.g. `UnretainedWrapper()` is
Bartek Nowierski0ccd76b2023-03-22 17:02:58124 // constructed using `char*`, but the receiver is of type `std::string&`.
Peter Kastinga7e941e2023-12-20 01:13:49125 // This is enforced by `static_assert()`s in `ParamCanBeBound`.
Bartek Nowierskie132f6482023-02-15 20:19:52126 using GetPtrType = std::conditional_t<
Bartek Nowierski0ccd76b2023-03-22 17:02:58127 raw_ptr_traits::IsSupportedType<T>::value &&
Peter Kastingd077bb22023-12-16 08:40:00128 std::same_as<UnretainedTrait, unretained_traits::MayDangle>,
Bartek Nowierskie132f6482023-02-15 20:19:52129 DanglingRawPtrType,
130 T*>;
Paul Semelff2ca67b2022-12-21 15:46:02131
Peter Kasting7419b0be2023-12-19 05:31:19132 // Raw pointer makes sense only if there are no `PtrTrait`s. If there are,
Bartek Nowierskie132f6482023-02-15 20:19:52133 // it means that a `raw_ptr` is being passed, so use the ctors below instead.
Daniel Chengef1375f22023-11-15 00:06:12134 explicit UnretainedWrapper(T* o)
135 requires(PtrTraits == RawPtrTraits::kEmpty)
Peter Kasting6d4812b72023-12-19 22:10:21136 : ptr_(o) {
137 VerifyPreconditions();
138 }
Daniel Chengef1375f22023-11-15 00:06:12139
140 explicit UnretainedWrapper(const raw_ptr<T, PtrTraits>& o)
141 requires(raw_ptr_traits::IsSupportedType<T>::value)
Peter Kasting6d4812b72023-12-19 22:10:21142 : ptr_(o) {
143 VerifyPreconditions();
144 }
145
Daniel Chengef1375f22023-11-15 00:06:12146 explicit UnretainedWrapper(raw_ptr<T, PtrTraits>&& o)
147 requires(raw_ptr_traits::IsSupportedType<T>::value)
Peter Kasting6d4812b72023-12-19 22:10:21148 : ptr_(std::move(o)) {
149 VerifyPreconditions();
150 }
Bartek Nowierskied8072b2022-07-29 14:25:53151
Bartek Nowierski3b565eab2023-03-13 00:48:11152 GetPtrType get() const { return GetInternal(ptr_); }
Peter Kastinga85265e32018-02-15 08:30:23153
Peter Kasting6d4812b72023-12-19 22:10:21154 // True if this type is valid. When this is false, a `static_assert` will have
155 // been fired explaining why.
156 static constexpr bool value = SupportsUnretained<T>;
157
Peter Kastinga85265e32018-02-15 08:30:23158 private:
Bartek Nowierski3b565eab2023-03-13 00:48:11159 // `ptr_` is either a `raw_ptr` or a regular C++ pointer.
160 template <typename U>
Peter Kastingd077bb22023-12-16 08:40:00161 requires std::same_as<T, U>
Bartek Nowierski3b565eab2023-03-13 00:48:11162 static GetPtrType GetInternal(U* ptr) {
Bartek Nowierski3b565eab2023-03-13 00:48:11163 return ptr;
164 }
165 template <typename U, RawPtrTraits Traits>
Peter Kastingd077bb22023-12-16 08:40:00166 requires std::same_as<T, U>
Bartek Nowierski3b565eab2023-03-13 00:48:11167 static GetPtrType GetInternal(const raw_ptr<U, Traits>& ptr) {
Peter Kastingd077bb22023-12-16 08:40:00168 if constexpr (std::same_as<UnretainedTrait,
169 unretained_traits::MayNotDangle>) {
Bartek Nowierski3b565eab2023-03-13 00:48:11170 ptr.ReportIfDangling();
171 }
172 return ptr;
173 }
174
Bartek Nowierski58ce9be82023-01-06 03:55:47175 // `Unretained()` arguments often dangle by design (a common design pattern
176 // is to manage an object's lifetime inside the callback itself, using
177 // stateful information), so disable direct dangling pointer detection
178 // of `ptr_`.
Paul Semelb0ce4842022-09-28 08:09:53179 //
180 // If the callback is invoked, dangling pointer detection will be triggered
Bartek Nowierski58ce9be82023-01-06 03:55:47181 // before invoking the bound functor (unless stated otherwise, see
182 // `UnsafeDangling()` and `UnsafeDanglingUntriaged()`), when retrieving the
183 // pointer value via `get()` above.
Bartek Nowierskic7d72332023-01-12 14:06:40184 using StorageType =
185 std::conditional_t<raw_ptr_traits::IsSupportedType<T>::value,
Bartek Nowierskie132f6482023-02-15 20:19:52186 DanglingRawPtrType,
Bartek Nowierskic7d72332023-01-12 14:06:40187 T*>;
Bartek Nowierski58ce9be82023-01-06 03:55:47188 // Avoid converting between different `raw_ptr` types when calling `get()`.
Bartek Nowierski0ccd76b2023-03-22 17:02:58189 // It is allowable to convert `raw_ptr<T>` -> `T*`, but not in the other
190 // direction. See the comment by `GetPtrType` describing for more details.
Bartek Nowierski58ce9be82023-01-06 03:55:47191 static_assert(std::is_pointer_v<GetPtrType> ||
Peter Kastingd077bb22023-12-16 08:40:00192 std::same_as<GetPtrType, StorageType>);
Peter Kasting6d4812b72023-12-19 22:10:21193
194 // Forces `value` to be materialized, performing a compile-time check of the
195 // preconditions if it hasn't already occurred. This is called from every
196 // constructor so the wrappers in bind.h don't have to each check it, and so
197 // no one can go around them and construct this underlying type directly.
198 static constexpr void VerifyPreconditions() {
199 // Using `static_assert(value);` here would work but fire an extra error.
200 std::ignore = value;
201 }
202
Bartek Nowierski58ce9be82023-01-06 03:55:47203 StorageType ptr_;
Peter Kastinga85265e32018-02-15 08:30:23204};
205
Peter Kasting7419b0be2023-12-19 05:31:19206// Storage type for `std::reference_wrapper` so `BindState` can internally store
207// unprotected references using `raw_ref`.
Daniel Chengadd3e4e2021-10-14 05:04:12208//
Peter Kasting7419b0be2023-12-19 05:31:19209// `std::reference_wrapper<T>` and `T&` do not work, since the reference
210// lifetime is not safely protected by MiraclePtr.
Daniel Chengadd3e4e2021-10-14 05:04:12211//
Peter Kasting7419b0be2023-12-19 05:31:19212// `UnretainedWrapper<T>` and `raw_ptr<T>` do not work, since `BindUnwrapTraits`
213// would try to pass by `T*` rather than `T&`.
Bartek Nowierskie132f6482023-02-15 20:19:52214template <typename T,
215 typename UnretainedTrait,
216 RawPtrTraits PtrTraits = RawPtrTraits::kEmpty>
Daniel Chengadd3e4e2021-10-14 05:04:12217class UnretainedRefWrapper {
218 public:
Peter Kasting7419b0be2023-12-19 05:31:19219 // Raw reference makes sense only if there are no `PtrTrait`s. If there are,
Bartek Nowierskie132f6482023-02-15 20:19:52220 // it means that a `raw_ref` is being passed, so use the ctors below instead.
Daniel Chengef1375f22023-11-15 00:06:12221 explicit UnretainedRefWrapper(T& o)
222 requires(PtrTraits == RawPtrTraits::kEmpty)
Peter Kasting6d4812b72023-12-19 22:10:21223 : ref_(o) {
224 VerifyPreconditions();
225 }
Daniel Chengef1375f22023-11-15 00:06:12226
227 explicit UnretainedRefWrapper(const raw_ref<T, PtrTraits>& o)
228 requires(raw_ptr_traits::IsSupportedType<T>::value)
Peter Kasting6d4812b72023-12-19 22:10:21229 : ref_(o) {
230 VerifyPreconditions();
231 }
Daniel Chengef1375f22023-11-15 00:06:12232
Peter Kasting6d4812b72023-12-19 22:10:21233 explicit UnretainedRefWrapper(raw_ref<T, PtrTraits>&& o)
Daniel Chengef1375f22023-11-15 00:06:12234 requires(raw_ptr_traits::IsSupportedType<T>::value)
Peter Kasting6d4812b72023-12-19 22:10:21235 : ref_(std::move(o)) {
236 VerifyPreconditions();
237 }
Bartek Nowierskie899ad582023-01-20 06:21:29238
Bartek Nowierski3b565eab2023-03-13 00:48:11239 T& get() const { return GetInternal(ref_); }
danakj92061c12022-07-28 23:53:50240
Peter Kasting6d4812b72023-12-19 22:10:21241 // See comments in `UnretainedWrapper` regarding this and
242 // `VerifyPreconditions()`.
243 static constexpr bool value = SupportsUnretained<T>;
244
danakj92061c12022-07-28 23:53:50245 private:
Bartek Nowierski3b565eab2023-03-13 00:48:11246 // `ref_` is either a `raw_ref` or a regular C++ reference.
247 template <typename U>
Peter Kastingd077bb22023-12-16 08:40:00248 requires std::same_as<T, U>
Bartek Nowierski3b565eab2023-03-13 00:48:11249 static T& GetInternal(U& ref) {
Bartek Nowierski3b565eab2023-03-13 00:48:11250 return ref;
251 }
252 template <typename U, RawPtrTraits Traits>
Peter Kastingd077bb22023-12-16 08:40:00253 requires std::same_as<T, U>
Bartek Nowierski3b565eab2023-03-13 00:48:11254 static T& GetInternal(const raw_ref<U, Traits>& ref) {
Bartek Nowierski3b565eab2023-03-13 00:48:11255 // The ultimate goal is to crash when a callback is invoked with a
256 // dangling pointer. This is checked here. For now, it is configured to
257 // either crash, DumpWithoutCrashing or be ignored. This depends on the
Peter Kasting7419b0be2023-12-19 05:31:19258 // `PartitionAllocUnretainedDanglingPtr` feature.
Bartek Nowierski3b565eab2023-03-13 00:48:11259 if constexpr (std::is_same_v<UnretainedTrait,
260 unretained_traits::MayNotDangle>) {
261 ref.ReportIfDangling();
262 }
Peter Kasting7419b0be2023-12-19 05:31:19263 // We can't use `operator*` here, we need to use `raw_ptr`'s
264 // `GetForExtraction` instead of `GetForDereference`. If we did use
265 // `GetForDereference` then we'd crash in ASAN builds on calling a bound
266 // callback with a dangling reference parameter even if that parameter is
267 // not used. This could hide a later unprotected issue that would be reached
268 // in release builds.
Bartek Nowierski3b565eab2023-03-13 00:48:11269 return ref.get();
270 }
271
Bartek Nowierskif31d2c5e2023-02-14 21:41:43272 // `Unretained()` arguments often dangle by design (a common design pattern
273 // is to manage an object's lifetime inside the callback itself, using
274 // stateful information), so disable direct dangling pointer detection
275 // of `ref_`.
276 //
277 // If the callback is invoked, dangling pointer detection will be triggered
278 // before invoking the bound functor (unless stated otherwise, see
279 // `UnsafeDangling()` and `UnsafeDanglingUntriaged()`), when retrieving the
280 // pointer value via `get()` above.
281 using StorageType =
282 std::conditional_t<raw_ptr_traits::IsSupportedType<T>::value,
283 raw_ref<T, DisableDanglingPtrDetection>,
284 T&>;
Bartek Nowierskif31d2c5e2023-02-14 21:41:43285
Peter Kasting6d4812b72023-12-19 22:10:21286 static constexpr void VerifyPreconditions() { std::ignore = value; }
287
Bartek Nowierskif31d2c5e2023-02-14 21:41:43288 StorageType ref_;
danakj92061c12022-07-28 23:53:50289};
danakj92061c12022-07-28 23:53:50290
Peter Kastingd077bb22023-12-16 08:40:00291// Can't use `is_instantiation` to detect the unretained wrappers, since they
292// have non-type template params.
293template <template <typename, typename, RawPtrTraits> typename WrapperT,
294 typename T>
295inline constexpr bool kIsUnretainedWrapper = false;
296
297template <template <typename, typename, RawPtrTraits> typename WrapperT,
298 typename T,
299 typename UnretainedTrait,
300 RawPtrTraits PtrTraits>
301inline constexpr bool
302 kIsUnretainedWrapper<WrapperT, WrapperT<T, UnretainedTrait, PtrTraits>> =
303 true;
304
Paul Semel9d2f06972022-12-12 16:50:44305// The class is used to wrap `UnretainedRefWrapper` when the latter is used as
306// a method receiver (a reference on `this` argument). This is needed because
307// the internal callback mechanism expects the receiver to have the type
308// `MyClass*` and to have `operator*`.
309// This is used as storage.
Bartek Nowierskie132f6482023-02-15 20:19:52310template <typename T, typename UnretainedTrait, RawPtrTraits PtrTraits>
Paul Semel9d2f06972022-12-12 16:50:44311class UnretainedRefWrapperReceiver {
312 public:
313 // NOLINTNEXTLINE(google-explicit-constructor)
Bartek Nowierskie132f6482023-02-15 20:19:52314 UnretainedRefWrapperReceiver(
Peter Kastinga16713f2023-09-02 23:03:10315 UnretainedRefWrapper<T, UnretainedTrait, PtrTraits>&& obj)
316 : obj_(std::move(obj)) {}
Peter Kasting3b01151f2023-11-27 20:04:21317
Paul Semel9d2f06972022-12-12 16:50:44318 T& operator*() const { return obj_.get(); }
Peter Kasting3b01151f2023-11-27 20:04:21319 T* operator->() const { return &obj_.get(); }
Paul Semel9d2f06972022-12-12 16:50:44320
321 private:
Bartek Nowierskie132f6482023-02-15 20:19:52322 UnretainedRefWrapper<T, UnretainedTrait, PtrTraits> obj_;
Paul Semel9d2f06972022-12-12 16:50:44323};
324
Peter Kasting7419b0be2023-12-19 05:31:19325// `MethodReceiverStorage` converts the current receiver type to its stored
Paul Semel9d2f06972022-12-12 16:50:44326// type. For instance, it converts pointers to `scoped_refptr`, and wraps
327// `UnretainedRefWrapper` to make it compliant with the internal callback
328// invocation mechanism.
329template <typename T>
Peter Kasting12f92692023-12-19 04:18:47330struct MethodReceiverStorage {
danakje2dd9c02024-04-06 19:18:32331 using Type = std::
Peter Kasting5e29968d2024-12-18 04:42:03332 conditional_t<IsPointerOrRawPtr<T>, scoped_refptr<RemovePointerT<T>>, T>;
Paul Semel9d2f06972022-12-12 16:50:44333};
334
Bartek Nowierskie132f6482023-02-15 20:19:52335template <typename T, typename UnretainedTrait, RawPtrTraits PtrTraits>
Peter Kasting12f92692023-12-19 04:18:47336struct MethodReceiverStorage<
Bartek Nowierskie132f6482023-02-15 20:19:52337 UnretainedRefWrapper<T, UnretainedTrait, PtrTraits>> {
Peter Kasting7419b0be2023-12-19 05:31:19338 // We can't use `UnretainedRefWrapper` as a receiver directly (see
339 // `UnretainedRefWrapperReceiver` for why).
Bartek Nowierskie132f6482023-02-15 20:19:52340 using Type = UnretainedRefWrapperReceiver<T, UnretainedTrait, PtrTraits>;
Paul Semel9d2f06972022-12-12 16:50:44341};
342
Peter Kastinga85265e32018-02-15 08:30:23343template <typename T>
Peter Kastinga85265e32018-02-15 08:30:23344class RetainedRefWrapper {
345 public:
346 explicit RetainedRefWrapper(T* o) : ptr_(o) {}
347 explicit RetainedRefWrapper(scoped_refptr<T> o) : ptr_(std::move(o)) {}
348 T* get() const { return ptr_.get(); }
349
350 private:
351 scoped_refptr<T> ptr_;
352};
353
354template <typename T>
355struct IgnoreResultHelper {
356 explicit IgnoreResultHelper(T functor) : functor_(std::move(functor)) {}
357 explicit operator bool() const { return !!functor_; }
358
359 T functor_;
360};
361
Marijn Kruisselbrinkc1e775f2019-12-19 01:24:22362template <typename T, typename Deleter = std::default_delete<T>>
Peter Kastinga85265e32018-02-15 08:30:23363class OwnedWrapper {
364 public:
365 explicit OwnedWrapper(T* o) : ptr_(o) {}
Marijn Kruisselbrinkc1e775f2019-12-19 01:24:22366 explicit OwnedWrapper(std::unique_ptr<T, Deleter>&& ptr)
367 : ptr_(std::move(ptr)) {}
jdoerrie68f2d51b2019-02-28 17:32:44368 T* get() const { return ptr_.get(); }
Peter Kastinga85265e32018-02-15 08:30:23369
370 private:
Marijn Kruisselbrinkc1e775f2019-12-19 01:24:22371 std::unique_ptr<T, Deleter> ptr_;
Peter Kastinga85265e32018-02-15 08:30:23372};
373
Jan Wilken Dörrie1d011fb2021-03-11 19:37:37374template <typename T>
375class OwnedRefWrapper {
376 public:
377 explicit OwnedRefWrapper(const T& t) : t_(t) {}
378 explicit OwnedRefWrapper(T&& t) : t_(std::move(t)) {}
379 T& get() const { return t_; }
380
381 private:
382 mutable T t_;
383};
384
Peter Kasting7419b0be2023-12-19 05:31:19385// `PassedWrapper` is a copyable adapter for a scoper that ignores `const`.
Peter Kastinga85265e32018-02-15 08:30:23386//
Peter Kasting7419b0be2023-12-19 05:31:19387// It is needed to get around the fact that `Bind()` takes a const reference to
388// all its arguments. Because `Bind()` takes a const reference to avoid
Peter Kastinga85265e32018-02-15 08:30:23389// unnecessary copies, it is incompatible with movable-but-not-copyable
Peter Kasting7419b0be2023-12-19 05:31:19390// types; doing a destructive "move" of the type into `Bind()` would violate
Peter Kastinga85265e32018-02-15 08:30:23391// the const correctness.
392//
Peter Kasting7419b0be2023-12-19 05:31:19393// This conundrum cannot be solved without either rvalue references or an O(2^n)
394// blowup of `Bind()` templates to handle each combination of regular types and
395// movable-but-not-copyable types. Thus we introduce a wrapper type that is
396// copyable to transmit the correct type information down into `BindState<>`.
397// Ignoring `const` in this type makes sense because it is only created when we
398// are explicitly trying to do a destructive move.
Peter Kastinga85265e32018-02-15 08:30:23399//
400// Two notes:
Peter Kasting7419b0be2023-12-19 05:31:19401// 1) `PassedWrapper` supports any type that has a move constructor, however
Nate Fischer04068aa2021-03-10 22:20:54402// the type will need to be specifically allowed in order for it to be
Peter Kasting7419b0be2023-12-19 05:31:19403// bound to a `Callback`. We guard this explicitly at the call of `Passed()`
404// to make for clear errors. Things not given to `Passed()` will be
405// forwarded and stored by value which will not work for general move-only
406// types.
407// 2) `is_valid_` is distinct from `nullptr` because it is valid to bind a null
408// scoper to a `Callback` and allow the `Callback` to execute once.
409//
Alison Gale47d1537d2024-04-19 21:31:46410// TODO(crbug.com/40840557): We have rvalue references and such now. Remove.
Peter Kastinga85265e32018-02-15 08:30:23411template <typename T>
412class PassedWrapper {
413 public:
danakj92061c12022-07-28 23:53:50414 explicit PassedWrapper(T&& scoper) : scoper_(std::move(scoper)) {}
Peter Kastinga85265e32018-02-15 08:30:23415 PassedWrapper(PassedWrapper&& other)
416 : is_valid_(other.is_valid_), scoper_(std::move(other.scoper_)) {}
417 T Take() const {
418 CHECK(is_valid_);
419 is_valid_ = false;
420 return std::move(scoper_);
421 }
422
423 private:
danakj92061c12022-07-28 23:53:50424 mutable bool is_valid_ = true;
Peter Kastinga85265e32018-02-15 08:30:23425 mutable T scoper_;
426};
427
428template <typename T>
429using Unwrapper = BindUnwrapTraits<std::decay_t<T>>;
430
431template <typename T>
Peter Kastingc2f8749bf2018-03-31 03:32:37432decltype(auto) Unwrap(T&& o) {
Peter Kastinga85265e32018-02-15 08:30:23433 return Unwrapper<T>::Unwrap(std::forward<T>(o));
434}
435
Peter Kasting7419b0be2023-12-19 05:31:19436// `kIsWeakMethod` is a helper that determines if we are binding a `WeakPtr<>`
437// to a method. It is used internally by `Bind()` to select the correct
438// `InvokeHelper` that will no-op itself in the event the `WeakPtr<>` for the
439// target object is invalidated.
Peter Kastinga85265e32018-02-15 08:30:23440//
441// The first argument should be the type of the object that will be received by
442// the method.
443template <bool is_method, typename... Args>
Peter Kastingd077bb22023-12-16 08:40:00444inline constexpr bool kIsWeakMethod = false;
Peter Kastinga85265e32018-02-15 08:30:23445
446template <typename T, typename... Args>
Peter Kastingd077bb22023-12-16 08:40:00447inline constexpr bool kIsWeakMethod<true, T, Args...> =
448 IsWeakReceiver<T>::value;
Peter Kastinga85265e32018-02-15 08:30:23449
450// Packs a list of types to hold them in a single type.
451template <typename... Types>
452struct TypeList {};
453
Peter Kasting7419b0be2023-12-19 05:31:19454// Implements `DropTypeListItem`.
Peter Kastinga85265e32018-02-15 08:30:23455template <size_t n, typename List>
Daniel Chengfb999fd32025-06-13 02:30:16456 requires is_instantiation<List, TypeList>
Peter Kastingd077bb22023-12-16 08:40:00457struct DropTypeListItemImpl {
458 using Type = List;
459};
Peter Kastinga85265e32018-02-15 08:30:23460
Peter Kastinga85265e32018-02-15 08:30:23461template <size_t n, typename T, typename... List>
Peter Kastingd077bb22023-12-16 08:40:00462 requires(n > 0)
Peter Kastinga85265e32018-02-15 08:30:23463struct DropTypeListItemImpl<n, TypeList<T, List...>>
464 : DropTypeListItemImpl<n - 1, TypeList<List...>> {};
465
Peter Kasting7419b0be2023-12-19 05:31:19466// A type-level function that drops `n` list items from a given `TypeList`.
Peter Kastinga85265e32018-02-15 08:30:23467template <size_t n, typename List>
468using DropTypeListItem = typename DropTypeListItemImpl<n, List>::Type;
469
Peter Kasting7419b0be2023-12-19 05:31:19470// Implements `TakeTypeListItem`.
Peter Kastinga85265e32018-02-15 08:30:23471template <size_t n, typename List, typename... Accum>
Daniel Chengfb999fd32025-06-13 02:30:16472 requires is_instantiation<List, TypeList>
Peter Kastingd077bb22023-12-16 08:40:00473struct TakeTypeListItemImpl {
474 using Type = TypeList<Accum...>;
475};
Peter Kastinga85265e32018-02-15 08:30:23476
Peter Kastinga85265e32018-02-15 08:30:23477template <size_t n, typename T, typename... List, typename... Accum>
Peter Kastingd077bb22023-12-16 08:40:00478 requires(n > 0)
Peter Kastinga85265e32018-02-15 08:30:23479struct TakeTypeListItemImpl<n, TypeList<T, List...>, Accum...>
480 : TakeTypeListItemImpl<n - 1, TypeList<List...>, Accum..., T> {};
481
Peter Kasting7419b0be2023-12-19 05:31:19482// A type-level function that takes the first `n` items from a given `TypeList`;
483// e.g. `TakeTypeListItem<3, TypeList<A, B, C, D>>` -> `TypeList<A, B, C>`.
Peter Kastinga85265e32018-02-15 08:30:23484template <size_t n, typename List>
485using TakeTypeListItem = typename TakeTypeListItemImpl<n, List>::Type;
486
Peter Kasting7419b0be2023-12-19 05:31:19487// Implements `MakeFunctionType`.
Peter Kastinga85265e32018-02-15 08:30:23488template <typename R, typename ArgList>
489struct MakeFunctionTypeImpl;
490
491template <typename R, typename... Args>
492struct MakeFunctionTypeImpl<R, TypeList<Args...>> {
Peter Kastingd077bb22023-12-16 08:40:00493 using Type = R(Args...);
Peter Kastinga85265e32018-02-15 08:30:23494};
495
Peter Kasting7419b0be2023-12-19 05:31:19496// A type-level function that constructs a function type that has `R` as its
497// return type and has a `TypeList`'s items as its arguments.
Peter Kastinga85265e32018-02-15 08:30:23498template <typename R, typename ArgList>
499using MakeFunctionType = typename MakeFunctionTypeImpl<R, ArgList>::Type;
500
Peter Kasting7419b0be2023-12-19 05:31:19501// Implements `ExtractArgs` and `ExtractReturnType`.
Peter Kastinga85265e32018-02-15 08:30:23502template <typename Signature>
503struct ExtractArgsImpl;
504
505template <typename R, typename... Args>
506struct ExtractArgsImpl<R(Args...)> {
507 using ReturnType = R;
508 using ArgsList = TypeList<Args...>;
509};
510
Peter Kasting7419b0be2023-12-19 05:31:19511// A type-level function that extracts function arguments into a `TypeList`;
512// e.g. `ExtractArgs<R(A, B, C)>` -> `TypeList<A, B, C>`.
Peter Kastinga85265e32018-02-15 08:30:23513template <typename Signature>
514using ExtractArgs = typename ExtractArgsImpl<Signature>::ArgsList;
515
516// A type-level function that extracts the return type of a function.
Peter Kasting7419b0be2023-12-19 05:31:19517// e.g. `ExtractReturnType<R(A, B, C)>` -> `R`.
Peter Kastinga85265e32018-02-15 08:30:23518template <typename Signature>
519using ExtractReturnType = typename ExtractArgsImpl<Signature>::ReturnType;
520
tzikc1db72652016-07-08 09:42:38521template <typename Callable,
522 typename Signature = decltype(&Callable::operator())>
523struct ExtractCallableRunTypeImpl;
524
Peter Kasting8e4901d2024-02-16 16:34:05525#define BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS(quals) \
526 template <typename Callable, typename R, typename... Args> \
527 struct ExtractCallableRunTypeImpl<Callable, \
528 R (Callable::*)(Args...) quals> { \
529 using Type = R(Args...); \
530 }
tzikf98654b2017-12-02 03:28:58531
Peter Kasting8e4901d2024-02-16 16:34:05532BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS();
533BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS(const);
534BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS(noexcept);
535BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS(const noexcept);
tzikc1db72652016-07-08 09:42:38536
Peter Kasting8e4901d2024-02-16 16:34:05537#undef BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS
Ian McKellar29f262d2022-07-20 20:23:41538
Peter Kasting7419b0be2023-12-19 05:31:19539// Evaluated to the RunType of the given callable type; e.g.
540// `ExtractCallableRunType<decltype([](int, char*) { return 0.1; })>` ->
541// `double(int, char*)`.
tzikc1db72652016-07-08 09:42:38542template <typename Callable>
543using ExtractCallableRunType =
544 typename ExtractCallableRunTypeImpl<Callable>::Type;
545
Peter Kasting336972e2024-02-09 22:08:00546// True when `Functor` has a non-overloaded `operator()()`, e.g.:
547// struct S1 {
548// int operator()(int);
549// };
550// static_assert(HasNonOverloadedCallOp<S1>);
tzikc1db72652016-07-08 09:42:38551//
552// int i = 0;
Peter Kastinga16713f2023-09-02 23:03:10553// auto f = [i] {};
Peter Kasting336972e2024-02-09 22:08:00554// static_assert(HasNonOverloadedCallOp<decltype(f)>);
Peter Kasting7419b0be2023-12-19 05:31:19555//
Peter Kasting336972e2024-02-09 22:08:00556// struct S2 {
Peter Kasting7419b0be2023-12-19 05:31:19557// int operator()(int);
Peter Kasting336972e2024-02-09 22:08:00558// std::string operator()(std::string);
Peter Kasting7419b0be2023-12-19 05:31:19559// };
Peter Kasting336972e2024-02-09 22:08:00560// static_assert(!HasNonOverloadedCallOp<S2>);
561//
562// static_assert(!HasNonOverloadedCallOp<void(*)()>);
563//
564// struct S3 {};
565// static_assert(!HasNonOverloadedCallOp<S3>);
Peter Kasting7419b0be2023-12-19 05:31:19566// ```
Daniel Chengef1375f22023-11-15 00:06:12567template <typename Functor>
Peter Kasting336972e2024-02-09 22:08:00568concept HasNonOverloadedCallOp = requires { &Functor::operator(); };
tzikc1db72652016-07-08 09:42:38569
Peter Kastingbb8853292024-02-15 19:46:18570template <typename T>
571inline constexpr bool IsObjCArcBlockPointer = false;
572
573#if __OBJC__ && HAS_FEATURE(objc_arc)
574template <typename R, typename... Args>
575inline constexpr bool IsObjCArcBlockPointer<R (^)(Args...)> = true;
576#endif
577
578// True when `Functor` has an overloaded `operator()()` that can be invoked with
579// the provided `BoundArgs`.
Peter Kasting2eef24f42024-02-21 18:42:22580//
581// Do not decay `Functor` before testing this, lest it give an incorrect result
582// for overloads with different ref-qualifiers.
Peter Kastingbb8853292024-02-15 19:46:18583template <typename Functor, typename... BoundArgs>
584concept HasOverloadedCallOp = requires {
585 // The functor must be invocable with the bound args.
Peter Kasting2eef24f42024-02-21 18:42:22586 requires requires(Functor&& f, BoundArgs&&... args) {
587 std::forward<Functor>(f)(std::forward<BoundArgs>(args)...);
588 };
Peter Kastingbb8853292024-02-15 19:46:18589 // Now exclude invocables that are not cases of overloaded `operator()()`s:
590 // * `operator()()` exists, but isn't overloaded
Peter Kasting2eef24f42024-02-21 18:42:22591 requires !HasNonOverloadedCallOp<std::decay_t<Functor>>;
Peter Kastingbb8853292024-02-15 19:46:18592 // * Function pointer (doesn't have `operator()()`)
Peter Kasting2eef24f42024-02-21 18:42:22593 requires !std::is_pointer_v<std::decay_t<Functor>>;
Peter Kastingbb8853292024-02-15 19:46:18594 // * Block pointer (doesn't have `operator()()`)
Peter Kasting2eef24f42024-02-21 18:42:22595 requires !IsObjCArcBlockPointer<std::decay_t<Functor>>;
Peter Kastingbb8853292024-02-15 19:46:18596};
597
Peter Kasting7419b0be2023-12-19 05:31:19598// `ForceVoidReturn<>` converts a signature to have a `void` return type.
[email protected]7296f2762011-11-21 19:23:44599template <typename Sig>
600struct ForceVoidReturn;
601
tzikc82149922014-11-20 10:09:45602template <typename R, typename... Args>
603struct ForceVoidReturn<R(Args...)> {
tzik99de02b2016-07-01 05:54:12604 using RunType = void(Args...);
[email protected]fccef1552011-11-28 22:13:54605};
606
Peter Kasting7419b0be2023-12-19 05:31:19607// `FunctorTraits<>`
[email protected]7296f2762011-11-21 19:23:44608//
Peter Kasting8e4901d2024-02-16 16:34:05609// See description at top of file. This must be declared here so it can be
610// referenced in `DecayedFunctorTraits`.
Peter Kastingbb8853292024-02-15 19:46:18611template <typename Functor, typename... BoundArgs>
tzik99de02b2016-07-01 05:54:12612struct FunctorTraits;
613
Peter Kasting8e4901d2024-02-16 16:34:05614// Provides functor traits for pre-decayed functor types.
615template <typename Functor, typename... BoundArgs>
616struct DecayedFunctorTraits;
617
Peter Kasting7419b0be2023-12-19 05:31:19618// Callable types.
Daniel Chengda25e8ac2022-07-19 23:02:52619// This specialization handles lambdas (captureless and capturing) and functors
620// with a call operator. Capturing lambdas and stateful functors are explicitly
Peter Kasting7419b0be2023-12-19 05:31:19621// disallowed by `BindHelper<>::Bind()`; e.g.:
622// ```
623// // Captureless lambda: Allowed
Peter Kastinga16713f2023-09-02 23:03:10624// [] { return 42; };
tzikf98654b2017-12-02 03:28:58625//
Peter Kasting7419b0be2023-12-19 05:31:19626// // Capturing lambda: Disallowed
tzikf98654b2017-12-02 03:28:58627// int x;
Peter Kastinga16713f2023-09-02 23:03:10628// [x] { return x; };
tzikf98654b2017-12-02 03:28:58629//
Peter Kasting7419b0be2023-12-19 05:31:19630// // Empty class with `operator()()`: Allowed
tzikf98654b2017-12-02 03:28:58631// struct Foo {
632// void operator()() const {}
Peter Kasting7419b0be2023-12-19 05:31:19633// // No non-`static` member variables and no virtual functions.
tzikf98654b2017-12-02 03:28:58634// };
Peter Kasting7419b0be2023-12-19 05:31:19635// ```
Peter Kastingbb8853292024-02-15 19:46:18636template <typename Functor, typename... BoundArgs>
Peter Kasting336972e2024-02-09 22:08:00637 requires HasNonOverloadedCallOp<Functor>
Peter Kasting8e4901d2024-02-16 16:34:05638struct DecayedFunctorTraits<Functor, BoundArgs...> {
tzikc1db72652016-07-08 09:42:38639 using RunType = ExtractCallableRunType<Functor>;
640 static constexpr bool is_method = false;
641 static constexpr bool is_nullable = false;
Sami Kyostila712b4e32020-04-27 16:51:08642 static constexpr bool is_callback = false;
Daniel Chengda25e8ac2022-07-19 23:02:52643 static constexpr bool is_stateless = std::is_empty_v<Functor>;
tzikc1db72652016-07-08 09:42:38644
tzikf98654b2017-12-02 03:28:58645 template <typename RunFunctor, typename... RunArgs>
646 static ExtractReturnType<RunType> Invoke(RunFunctor&& functor,
647 RunArgs&&... args) {
648 return std::forward<RunFunctor>(functor)(std::forward<RunArgs>(args)...);
tzikc1db72652016-07-08 09:42:38649 }
650};
651
Peter Kasting7419b0be2023-12-19 05:31:19652// Functions.
Peter Kastingbb8853292024-02-15 19:46:18653template <typename R, typename... Args, typename... BoundArgs>
Peter Kasting8e4901d2024-02-16 16:34:05654struct DecayedFunctorTraits<R (*)(Args...), BoundArgs...> {
tzik99de02b2016-07-01 05:54:12655 using RunType = R(Args...);
656 static constexpr bool is_method = false;
tzikc1db72652016-07-08 09:42:38657 static constexpr bool is_nullable = true;
Sami Kyostila712b4e32020-04-27 16:51:08658 static constexpr bool is_callback = false;
Daniel Chengda25e8ac2022-07-19 23:02:52659 static constexpr bool is_stateless = true;
tzik99de02b2016-07-01 05:54:12660
tzikd58a89232018-04-26 04:29:53661 template <typename Function, typename... RunArgs>
662 static R Invoke(Function&& function, RunArgs&&... args) {
663 return std::forward<Function>(function)(std::forward<RunArgs>(args)...);
tzik99de02b2016-07-01 05:54:12664 }
[email protected]7296f2762011-11-21 19:23:44665};
666
Peter Kastingbb8853292024-02-15 19:46:18667template <typename R, typename... Args, typename... BoundArgs>
Peter Kasting8e4901d2024-02-16 16:34:05668struct DecayedFunctorTraits<R (*)(Args...) noexcept, BoundArgs...>
669 : DecayedFunctorTraits<R (*)(Args...), BoundArgs...> {};
Peter Kastingd077bb22023-12-16 08:40:00670
Xiaohan Wang38e4ebb2022-01-19 06:57:43671#if BUILDFLAG(IS_WIN) && !defined(ARCH_CPU_64_BITS)
tzik99de02b2016-07-01 05:54:12672
Peter Kasting8e4901d2024-02-16 16:34:05673// `__stdcall` and `__fastcall` functions.
674#define BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS(conv, quals) \
675 template <typename R, typename... Args, typename... BoundArgs> \
676 struct DecayedFunctorTraits<R(conv*)(Args...) quals, BoundArgs...> \
677 : DecayedFunctorTraits<R (*)(Args...) quals, BoundArgs...> {}
tzik99de02b2016-07-01 05:54:12678
Peter Kasting8e4901d2024-02-16 16:34:05679BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS(__stdcall, );
680BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS(__stdcall, noexcept);
681BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS(__fastcall, );
682BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS(__fastcall, noexcept);
Peter Kasting648bb942023-12-16 15:10:23683
Peter Kasting8e4901d2024-02-16 16:34:05684#undef BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS
Xiaohan Wang38e4ebb2022-01-19 06:57:43685#endif // BUILDFLAG(IS_WIN) && !defined(ARCH_CPU_64_BITS)
tzik99de02b2016-07-01 05:54:12686
Peter Kasting41472f32023-12-22 03:19:04687#if __OBJC__ && HAS_FEATURE(objc_arc)
Sylvain Defresneec3270c2018-05-31 17:19:15688
Peter Kasting41472f32023-12-22 03:19:04689// Objective-C blocks. Blocks can be bound as the compiler will ensure their
690// lifetimes will be correctly managed.
Peter Kastingbb8853292024-02-15 19:46:18691template <typename R, typename... Args, typename... BoundArgs>
Peter Kasting8e4901d2024-02-16 16:34:05692struct DecayedFunctorTraits<R (^)(Args...), BoundArgs...> {
Sylvain Defresneec3270c2018-05-31 17:19:15693 using RunType = R(Args...);
694 static constexpr bool is_method = false;
695 static constexpr bool is_nullable = true;
Sami Kyostila712b4e32020-04-27 16:51:08696 static constexpr bool is_callback = false;
Daniel Chengda25e8ac2022-07-19 23:02:52697 static constexpr bool is_stateless = true;
Sylvain Defresneec3270c2018-05-31 17:19:15698
699 template <typename BlockType, typename... RunArgs>
700 static R Invoke(BlockType&& block, RunArgs&&... args) {
701 // According to LLVM documentation (§ 6.3), "local variables of automatic
Peter Kasting7419b0be2023-12-19 05:31:19702 // storage duration do not have precise lifetime." Use
703 // `objc_precise_lifetime` to ensure that the Objective-C block is not
704 // deallocated until it has finished executing even if the `Callback<>` is
705 // destroyed during the block execution.
Sylvain Defresneec3270c2018-05-31 17:19:15706 // https://clang.llvm.org/docs/AutomaticReferenceCounting.html#precise-lifetime-semantics
707 __attribute__((objc_precise_lifetime)) R (^scoped_block)(Args...) = block;
708 return scoped_block(std::forward<RunArgs>(args)...);
709 }
710};
711
Peter Kasting41472f32023-12-22 03:19:04712#endif // __OBJC__ && HAS_FEATURE(objc_arc)
Sylvain Defresneec3270c2018-05-31 17:19:15713
Peter Kasting7419b0be2023-12-19 05:31:19714// Methods.
Peter Kastingbb8853292024-02-15 19:46:18715template <typename R,
716 typename Receiver,
717 typename... Args,
718 typename... BoundArgs>
Peter Kasting8e4901d2024-02-16 16:34:05719struct DecayedFunctorTraits<R (Receiver::*)(Args...), BoundArgs...> {
tzik99de02b2016-07-01 05:54:12720 using RunType = R(Receiver*, Args...);
721 static constexpr bool is_method = true;
tzikc1db72652016-07-08 09:42:38722 static constexpr bool is_nullable = true;
Sami Kyostila712b4e32020-04-27 16:51:08723 static constexpr bool is_callback = false;
Daniel Chengda25e8ac2022-07-19 23:02:52724 static constexpr bool is_stateless = true;
tzik99de02b2016-07-01 05:54:12725
tzikd58a89232018-04-26 04:29:53726 template <typename Method, typename ReceiverPtr, typename... RunArgs>
727 static R Invoke(Method method,
tzik99de02b2016-07-01 05:54:12728 ReceiverPtr&& receiver_ptr,
729 RunArgs&&... args) {
tzik75851f42017-06-14 06:57:01730 return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...);
tzik99de02b2016-07-01 05:54:12731 }
732};
733
Peter Kastingbb8853292024-02-15 19:46:18734template <typename R,
735 typename Receiver,
736 typename... Args,
737 typename... BoundArgs>
Peter Kasting8e4901d2024-02-16 16:34:05738struct DecayedFunctorTraits<R (Receiver::*)(Args...) const, BoundArgs...>
739 : DecayedFunctorTraits<R (Receiver::*)(Args...), BoundArgs...> {
tzik99de02b2016-07-01 05:54:12740 using RunType = R(const Receiver*, Args...);
tzik99de02b2016-07-01 05:54:12741};
742
Peter Kasting8e4901d2024-02-16 16:34:05743#define BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONST_AND_QUALS(constqual, \
744 quals) \
745 template <typename R, typename Receiver, typename... Args, \
746 typename... BoundArgs> \
747 struct DecayedFunctorTraits<R (Receiver::*)(Args...) constqual quals, \
748 BoundArgs...> \
749 : DecayedFunctorTraits<R (Receiver::*)(Args...) constqual, \
750 BoundArgs...> {}
751
752BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONST_AND_QUALS(, noexcept);
753BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONST_AND_QUALS(const, noexcept);
754
755#undef BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONST_AND_QUALS
Peter Kastingd077bb22023-12-16 08:40:00756
Xiaohan Wang38e4ebb2022-01-19 06:57:43757#if BUILDFLAG(IS_WIN) && !defined(ARCH_CPU_64_BITS)
Sorin Jianu6bc2e352020-04-01 18:01:41758
Peter Kasting7419b0be2023-12-19 05:31:19759// `__stdcall` methods.
Peter Kasting8e4901d2024-02-16 16:34:05760#define BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS(quals) \
761 template <typename R, typename Receiver, typename... Args, \
762 typename... BoundArgs> \
763 struct DecayedFunctorTraits<R (__stdcall Receiver::*)(Args...) quals, \
764 BoundArgs...> \
765 : public DecayedFunctorTraits<R (Receiver::*)(Args...) quals, \
766 BoundArgs...> {}
Sorin Jianu6bc2e352020-04-01 18:01:41767
Peter Kasting8e4901d2024-02-16 16:34:05768BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS();
769BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS(const);
770BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS(noexcept);
771BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS(const noexcept);
Peter Kasting648bb942023-12-16 15:10:23772
Peter Kasting8e4901d2024-02-16 16:34:05773#undef BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS
Peter Kasting648bb942023-12-16 15:10:23774
Xiaohan Wang38e4ebb2022-01-19 06:57:43775#endif // BUILDFLAG(IS_WIN) && !defined(ARCH_CPU_64_BITS)
Sorin Jianu6bc2e352020-04-01 18:01:41776
Peter Kasting7419b0be2023-12-19 05:31:19777// `IgnoreResult`s.
Peter Kastingbb8853292024-02-15 19:46:18778template <typename T, typename... BoundArgs>
Peter Kasting8e4901d2024-02-16 16:34:05779struct DecayedFunctorTraits<IgnoreResultHelper<T>, BoundArgs...>
Peter Kastingbb8853292024-02-15 19:46:18780 : FunctorTraits<T, BoundArgs...> {
781 using RunType = typename ForceVoidReturn<
782 typename FunctorTraits<T, BoundArgs...>::RunType>::RunType;
tzik99de02b2016-07-01 05:54:12783
784 template <typename IgnoreResultType, typename... RunArgs>
785 static void Invoke(IgnoreResultType&& ignore_result_helper,
786 RunArgs&&... args) {
Peter Kastingbb8853292024-02-15 19:46:18787 FunctorTraits<T, BoundArgs...>::Invoke(
tzikff54a5b152016-08-31 11:50:41788 std::forward<IgnoreResultType>(ignore_result_helper).functor_,
789 std::forward<RunArgs>(args)...);
tzik99de02b2016-07-01 05:54:12790 }
[email protected]7296f2762011-11-21 19:23:44791};
792
Peter Kasting7419b0be2023-12-19 05:31:19793// `OnceCallback`s.
Peter Kastingbb8853292024-02-15 19:46:18794template <typename R, typename... Args, typename... BoundArgs>
Peter Kasting8e4901d2024-02-16 16:34:05795struct DecayedFunctorTraits<OnceCallback<R(Args...)>, BoundArgs...> {
tzikd4bb5b7d2017-08-28 19:08:52796 using RunType = R(Args...);
797 static constexpr bool is_method = false;
798 static constexpr bool is_nullable = true;
Sami Kyostila712b4e32020-04-27 16:51:08799 static constexpr bool is_callback = true;
Daniel Chengda25e8ac2022-07-19 23:02:52800 static constexpr bool is_stateless = true;
tzikd4bb5b7d2017-08-28 19:08:52801
802 template <typename CallbackType, typename... RunArgs>
803 static R Invoke(CallbackType&& callback, RunArgs&&... args) {
804 DCHECK(!callback.is_null());
805 return std::forward<CallbackType>(callback).Run(
806 std::forward<RunArgs>(args)...);
807 }
808};
809
Peter Kasting7419b0be2023-12-19 05:31:19810// `RepeatingCallback`s.
Peter Kastingbb8853292024-02-15 19:46:18811template <typename R, typename... Args, typename... BoundArgs>
Peter Kasting8e4901d2024-02-16 16:34:05812struct DecayedFunctorTraits<RepeatingCallback<R(Args...)>, BoundArgs...> {
tzik99de02b2016-07-01 05:54:12813 using RunType = R(Args...);
814 static constexpr bool is_method = false;
tzikc1db72652016-07-08 09:42:38815 static constexpr bool is_nullable = true;
Sami Kyostila712b4e32020-04-27 16:51:08816 static constexpr bool is_callback = true;
Daniel Chengda25e8ac2022-07-19 23:02:52817 static constexpr bool is_stateless = true;
tzik99de02b2016-07-01 05:54:12818
819 template <typename CallbackType, typename... RunArgs>
820 static R Invoke(CallbackType&& callback, RunArgs&&... args) {
821 DCHECK(!callback.is_null());
822 return std::forward<CallbackType>(callback).Run(
823 std::forward<RunArgs>(args)...);
824 }
[email protected]7296f2762011-11-21 19:23:44825};
826
Peter Kasting8e4901d2024-02-16 16:34:05827// For most functors, the traits should not depend on how the functor is passed,
828// so decay the functor.
Peter Kastingbb8853292024-02-15 19:46:18829template <typename Functor, typename... BoundArgs>
Peter Kasting8e4901d2024-02-16 16:34:05830// This requirement avoids "implicit instantiation of undefined template" errors
831// when the underlying `DecayedFunctorTraits<>` cannot be instantiated. Instead,
832// this template will also not be instantiated, and the caller can detect and
833// handle that.
834 requires IsComplete<DecayedFunctorTraits<std::decay_t<Functor>, BoundArgs...>>
835struct FunctorTraits<Functor, BoundArgs...>
836 : DecayedFunctorTraits<std::decay_t<Functor>, BoundArgs...> {};
tzikae4202e2017-07-31 10:41:54837
Peter Kasting2eef24f42024-02-21 18:42:22838// For `overloaded operator()()`s, it's possible the ref qualifiers of the
839// functor matter, so be careful to use the undecayed type.
840template <typename Functor, typename... BoundArgs>
841 requires HasOverloadedCallOp<Functor, BoundArgs...>
842struct FunctorTraits<Functor, BoundArgs...> {
843 // For an overloaded operator()(), it is not possible to resolve the
844 // actual declared type. Since it is invocable with the bound args, make up a
845 // signature based on their types.
846 using RunType = decltype(std::declval<Functor>()(
847 std::declval<BoundArgs>()...))(std::decay_t<BoundArgs>...);
848 static constexpr bool is_method = false;
849 static constexpr bool is_nullable = false;
850 static constexpr bool is_callback = false;
851 static constexpr bool is_stateless = std::is_empty_v<std::decay_t<Functor>>;
852
853 template <typename RunFunctor, typename... RunArgs>
854 static ExtractReturnType<RunType> Invoke(RunFunctor&& functor,
855 RunArgs&&... args) {
856 return std::forward<RunFunctor>(functor)(std::forward<RunArgs>(args)...);
857 }
858};
859
Peter Kasting7419b0be2023-12-19 05:31:19860// `StorageTraits<>`
Daniel Chengadd3e4e2021-10-14 05:04:12861//
862// See description at top of file.
Bartek Nowierski6be2e1f02021-11-26 00:36:33863template <typename T>
Daniel Chengadd3e4e2021-10-14 05:04:12864struct StorageTraits {
Peter Kastinga7e941e2023-12-20 01:13:49865 // The type to use for storing the bound arg inside `BindState`.
Daniel Chengadd3e4e2021-10-14 05:04:12866 using Type = T;
Peter Kastinga7e941e2023-12-20 01:13:49867
868 // True iff all compile-time preconditions for using this specialization are
869 // satisfied. Specializations that set this to `false` should ensure a
870 // `static_assert()` explains why.
871 static constexpr bool value = true;
Daniel Chengadd3e4e2021-10-14 05:04:12872};
873
Peter Kasting7419b0be2023-12-19 05:31:19874// For `T*`, store as `UnretainedWrapper<T>` for safety, as it internally uses
875// `raw_ptr<T>` (when possible).
Daniel Chengadd3e4e2021-10-14 05:04:12876template <typename T>
Bartek Nowierski6be2e1f02021-11-26 00:36:33877struct StorageTraits<T*> {
Bartek Nowierski58ce9be82023-01-06 03:55:47878 using Type = UnretainedWrapper<T, unretained_traits::MayNotDangle>;
Peter Kastinga7e941e2023-12-20 01:13:49879 static constexpr bool value = Type::value;
Daniel Chengadd3e4e2021-10-14 05:04:12880};
881
Peter Kasting7419b0be2023-12-19 05:31:19882// For `raw_ptr<T>`, store as `UnretainedWrapper<T>` for safety. This may seem
Mark Brandf48e9172022-09-13 10:58:12883// contradictory, but this ensures guaranteed protection for the pointer even
Peter Kasting7419b0be2023-12-19 05:31:19884// during execution of callbacks with parameters of type `raw_ptr<T>`.
Bartek Nowierskie132f6482023-02-15 20:19:52885template <typename T, RawPtrTraits PtrTraits>
886struct StorageTraits<raw_ptr<T, PtrTraits>> {
887 using Type = UnretainedWrapper<T, unretained_traits::MayNotDangle, PtrTraits>;
Peter Kastinga7e941e2023-12-20 01:13:49888 static constexpr bool value = Type::value;
Mark Brandf48e9172022-09-13 10:58:12889};
890
Peter Kasting7419b0be2023-12-19 05:31:19891// Unwrap `std::reference_wrapper` and store it in a custom wrapper so that
892// references are also protected with `raw_ptr<T>`.
Daniel Chengadd3e4e2021-10-14 05:04:12893template <typename T>
894struct StorageTraits<std::reference_wrapper<T>> {
Bartek Nowierski58ce9be82023-01-06 03:55:47895 using Type = UnretainedRefWrapper<T, unretained_traits::MayNotDangle>;
Peter Kastinga7e941e2023-12-20 01:13:49896 static constexpr bool value = Type::value;
Daniel Chengadd3e4e2021-10-14 05:04:12897};
898
899template <typename T>
Peter Kastinga7e941e2023-12-20 01:13:49900using ValidateStorageTraits = StorageTraits<std::decay_t<T>>;
Daniel Chengadd3e4e2021-10-14 05:04:12901
Peter Kasting7419b0be2023-12-19 05:31:19902// `InvokeHelper<>`
[email protected]7296f2762011-11-21 19:23:44903//
Peter Kasting7419b0be2023-12-19 05:31:19904// There are 2 logical `InvokeHelper<>` specializations: normal, weak.
[email protected]7296f2762011-11-21 19:23:44905//
906// The normal type just calls the underlying runnable.
907//
Peter Kasting7419b0be2023-12-19 05:31:19908// Weak calls need special syntax that is applied to the first argument to check
tzik99de02b2016-07-01 05:54:12909// if they should no-op themselves.
Peter Kasting336972e2024-02-09 22:08:00910template <bool is_weak_call,
911 typename Traits,
912 typename ReturnType,
913 size_t... indices>
[email protected]7296f2762011-11-21 19:23:44914struct InvokeHelper;
915
Peter Kasting336972e2024-02-09 22:08:00916template <typename Traits, typename ReturnType, size_t... indices>
917struct InvokeHelper<false, Traits, ReturnType, indices...> {
Paul Semelb0ce4842022-09-28 08:09:53918 template <typename Functor, typename BoundArgsTuple, typename... RunArgs>
919 static inline ReturnType MakeItSo(Functor&& functor,
920 BoundArgsTuple&& bound,
921 RunArgs&&... args) {
Peter Kasting336972e2024-02-09 22:08:00922 return Traits::Invoke(
Peter Kastingf818a6f2024-02-21 18:21:28923 Unwrap(std::forward<Functor>(functor)),
Paul Semelb0ce4842022-09-28 08:09:53924 Unwrap(std::get<indices>(std::forward<BoundArgsTuple>(bound)))...,
925 std::forward<RunArgs>(args)...);
[email protected]7296f2762011-11-21 19:23:44926 }
927};
928
Peter Kasting336972e2024-02-09 22:08:00929template <typename Traits,
930 typename ReturnType,
931 size_t index_target,
932 size_t... index_tail>
933struct InvokeHelper<true, Traits, ReturnType, index_target, index_tail...> {
Paul Semelb0ce4842022-09-28 08:09:53934 template <typename Functor, typename BoundArgsTuple, typename... RunArgs>
tzik99de02b2016-07-01 05:54:12935 static inline void MakeItSo(Functor&& functor,
Paul Semelb0ce4842022-09-28 08:09:53936 BoundArgsTuple&& bound,
tzik99de02b2016-07-01 05:54:12937 RunArgs&&... args) {
Andrey Kosyakov64b025b2023-03-31 21:40:15938 static_assert(index_target == 0);
939 // Note the validity of the weak pointer should be tested _after_ it is
940 // unwrapped, otherwise it creates a race for weak pointer implementations
Peter Kasting7419b0be2023-12-19 05:31:19941 // that allow cross-thread usage and perform `Lock()` in `Unwrap()` traits.
Andrey Kosyakov64b025b2023-03-31 21:40:15942 const auto& target = Unwrap(std::get<0>(bound));
943 if (!target) {
tzik99de02b2016-07-01 05:54:12944 return;
Bartek Nowierskiaf8d5d22023-01-11 11:01:02945 }
Peter Kasting336972e2024-02-09 22:08:00946 Traits::Invoke(
Peter Kastingf818a6f2024-02-21 18:21:28947 Unwrap(std::forward<Functor>(functor)), target,
Andrey Kosyakov64b025b2023-03-31 21:40:15948 Unwrap(std::get<index_tail>(std::forward<BoundArgsTuple>(bound)))...,
Paul Semelb0ce4842022-09-28 08:09:53949 std::forward<RunArgs>(args)...);
tzik99de02b2016-07-01 05:54:12950 }
951};
[email protected]b38d3572011-02-15 01:27:38952
Peter Kasting7419b0be2023-12-19 05:31:19953// `Invoker<>`
[email protected]7296f2762011-11-21 19:23:44954//
955// See description at the top of the file.
Peter Kasting336972e2024-02-09 22:08:00956template <typename Traits, typename StorageType, typename UnboundRunType>
[email protected]7296f2762011-11-21 19:23:44957struct Invoker;
958
Peter Kasting336972e2024-02-09 22:08:00959template <typename Traits,
960 typename StorageType,
961 typename R,
962 typename... UnboundArgs>
963struct Invoker<Traits, StorageType, R(UnboundArgs...)> {
Peter Kastinga7e941e2023-12-20 01:13:49964 private:
965 using Indices = std::make_index_sequence<
966 std::tuple_size_v<decltype(StorageType::bound_args_)>>;
967
968 public:
Vladislav Kuzkokov6d208e12017-11-08 21:31:08969 static R RunOnce(BindStateBase* base,
tzik9bc6837b2018-06-28 20:20:47970 PassingType<UnboundArgs>... unbound_args) {
Peter Kasting41472f32023-12-22 03:19:04971 auto* const storage = static_cast<StorageType*>(base);
tzik27d1e312016-09-13 05:28:59972 return RunImpl(std::move(storage->functor_),
Peter Kastinga7e941e2023-12-20 01:13:49973 std::move(storage->bound_args_), Indices(),
tzik27d1e312016-09-13 05:28:59974 std::forward<UnboundArgs>(unbound_args)...);
975 }
976
tzik9bc6837b2018-06-28 20:20:47977 static R Run(BindStateBase* base, PassingType<UnboundArgs>... unbound_args) {
Peter Kasting41472f32023-12-22 03:19:04978 auto* const storage = static_cast<const StorageType*>(base);
Peter Kastinga7e941e2023-12-20 01:13:49979 return RunImpl(storage->functor_, storage->bound_args_, Indices(),
tzikcaf1d84b2016-06-28 12:22:21980 std::forward<UnboundArgs>(unbound_args)...);
981 }
982
tzik99de02b2016-07-01 05:54:12983 private:
Peter Kastinga7e941e2023-12-20 01:13:49984 // The "templated struct with a lambda that asserts" pattern below is used
985 // repeatedly in Bind/Callback code to verify compile-time preconditions. The
986 // goal is to print only the root cause failure when users violate a
987 // precondition, and not also a host of resulting compile errors.
988 //
989 // There are three key aspects:
990 // 1. By placing the assertion inside a lambda that initializes a variable,
991 // the assertion will not be verified until the compiler tries to read
992 // the value of that variable. This allows the containing types to be
993 // complete. As a result, code that needs to know if the assertion failed
994 // can read the variable's value and get the right answer. (If we instead
995 // placed the assertion at struct scope, the resulting type would be
996 // incomplete when the assertion failed; in practice, reading a
997 // `constexpr` member of an incomplete type seems to return the default
998 // value regardless of what the code tried to set the value to, which
999 // makes it impossible for other code to check whether the assertion
1000 // failed.)
1001 // 2. Code that will not successfully compile unless the assertion holds is
1002 // guarded by a constexpr if that checks the variable.
1003 // 3. By placing the variable inside an independent, templated struct and
1004 // naming it `value`, we allow checking multiple conditions via
1005 // `std::conjunction_v<>`. This short-circuits type instantiation, so
1006 // that when one condition fails, the others are never examined and thus
1007 // never assert. As a result, we can verify dependent conditions without
1008 // worrying that "if one fails, we'll get errors from several others".
1009 // (This would not be true if we simply checked all the values with `&&`,
1010 // which would instantiate all the types before evaluating the
1011 // expression.)
1012 //
1013 // For caller convenience and to avoid potential repetition, the actual
1014 // condition to be checked is always used as the default value of a template
1015 // argument, so callers can simply instantiate the struct with no template
1016 // params to verify the condition.
1017
1018 // Weak calls are only supported for functions with a `void` return type.
1019 // Otherwise, the desired function result would be unclear if the `WeakPtr<>`
1020 // is invalidated. In theory, we could support default-constructible return
1021 // types (and return the default value) or allow callers to specify a default
1022 // return value via a template arg. It's not clear these are necessary.
1023 template <bool is_weak_call, bool v = !is_weak_call || std::is_void_v<R>>
1024 struct WeakCallReturnsVoid {
1025 static constexpr bool value = [] {
1026 static_assert(v,
1027 "WeakPtrs can only bind to methods without return values.");
1028 return v;
1029 }();
1030 };
1031
tzik99de02b2016-07-01 05:54:121032 template <typename Functor, typename BoundArgsTuple, size_t... indices>
1033 static inline R RunImpl(Functor&& functor,
tzikcaf1d84b2016-06-28 12:22:211034 BoundArgsTuple&& bound,
Peter Kasting41472f32023-12-22 03:19:041035 std::index_sequence<indices...>,
tzikcaf1d84b2016-06-28 12:22:211036 UnboundArgs&&... unbound_args) {
Arthur Sonzogni62e877a2024-04-30 16:09:431037#if PA_BUILDFLAG(USE_ASAN_BACKUP_REF_PTR)
Mark Brandf48e9172022-09-13 10:58:121038 RawPtrAsanBoundArgTracker raw_ptr_asan_bound_arg_tracker;
1039 raw_ptr_asan_bound_arg_tracker.AddArgs(
1040 std::get<indices>(std::forward<BoundArgsTuple>(bound))...,
1041 std::forward<UnboundArgs>(unbound_args)...);
Arthur Sonzogni62e877a2024-04-30 16:09:431042#endif // PA_BUILDFLAG(USE_ASAN_BACKUP_REF_PTR)
Mark Brandf48e9172022-09-13 10:58:121043
Peter Kastingd077bb22023-12-16 08:40:001044 using DecayedArgsTuple = std::decay_t<BoundArgsTuple>;
1045 static constexpr bool kIsWeakCall =
Peter Kasting336972e2024-02-09 22:08:001046 kIsWeakMethod<Traits::is_method,
Peter Kastingd077bb22023-12-16 08:40:001047 std::tuple_element_t<indices, DecayedArgsTuple>...>;
Peter Kastinga7e941e2023-12-20 01:13:491048 if constexpr (WeakCallReturnsVoid<kIsWeakCall>::value) {
1049 // Do not `Unwrap()` here, as that immediately triggers dangling pointer
1050 // detection. Dangling pointer detection should only be triggered if the
1051 // callback is not cancelled, but cancellation status is not determined
1052 // until later inside the `InvokeHelper::MakeItSo()` specialization for
1053 // weak calls.
1054 //
1055 // Dangling pointers when invoking a cancelled callback are not considered
1056 // a memory safety error because protecting raw pointers usage with weak
1057 // receivers (where the weak receiver usually own the pointed objects) is
1058 // a common and broadly used pattern in the codebase.
Peter Kasting336972e2024-02-09 22:08:001059 return InvokeHelper<kIsWeakCall, Traits, R, indices...>::MakeItSo(
Peter Kastinga7e941e2023-12-20 01:13:491060 std::forward<Functor>(functor), std::forward<BoundArgsTuple>(bound),
1061 std::forward<UnboundArgs>(unbound_args)...);
1062 }
[email protected]fccef1552011-11-28 22:13:541063 }
1064};
1065
Peter Kastingd077bb22023-12-16 08:40:001066// Allow binding a method call with no receiver.
Alison Gale923a33e2024-04-22 23:34:281067// TODO(crbug.com/41484339): Remove or make safe.
Peter Kastingd077bb22023-12-16 08:40:001068template <typename... Unused>
1069void VerifyMethodReceiver(Unused&&...) {}
tzikc1db72652016-07-08 09:42:381070
Peter Kastingd077bb22023-12-16 08:40:001071template <typename Receiver, typename... Unused>
1072void VerifyMethodReceiver(Receiver&& receiver, Unused&&...) {
Peter Kasting7419b0be2023-12-19 05:31:191073 // Asserts that a callback is not the first owner of a ref-counted receiver.
Peter Kasting5e29968d2024-12-18 04:42:031074 if constexpr (IsPointerOrRawPtr<std::decay_t<Receiver>> &&
1075 IsRefCountedType<RemovePointerT<std::decay_t<Receiver>>>) {
Peter Kastingd077bb22023-12-16 08:40:001076 DCHECK(receiver);
tzikcaf1d84b2016-06-28 12:22:211077
Peter Kastingd077bb22023-12-16 08:40:001078 // It's error prone to make the implicit first reference to ref-counted
Peter Kasting7419b0be2023-12-19 05:31:191079 // types. In the example below, `BindOnce()` would make the implicit first
1080 // reference to the ref-counted `Foo`. If `PostTask()` failed or the posted
1081 // task ran fast enough, the newly created instance could be destroyed
1082 // before `oo` makes another reference.
1083 // ```
Peter Kastingd077bb22023-12-16 08:40:001084 // Foo::Foo() {
Peter Kasting7419b0be2023-12-19 05:31:191085 // ThreadPool::PostTask(FROM_HERE, BindOnce(&Foo::Bar, this));
Peter Kastingd077bb22023-12-16 08:40:001086 // }
1087 //
1088 // scoped_refptr<Foo> oo = new Foo();
Peter Kasting7419b0be2023-12-19 05:31:191089 // ```
Peter Kastingd077bb22023-12-16 08:40:001090 //
Peter Kasting7419b0be2023-12-19 05:31:191091 // Hence, `Bind()` refuses to create the first reference to ref-counted
1092 // objects, and `DCHECK()`s otherwise. As above, that typically happens
1093 // around `PostTask()` in their constructors, and such objects can be
1094 // destroyed before `new` returns if the tasks resolve fast enough.
Peter Kastingd077bb22023-12-16 08:40:001095 //
Peter Kasting7419b0be2023-12-19 05:31:191096 // Instead, consider adding a static factory, and keeping the first
1097 // reference alive explicitly.
1098 // ```
Peter Kastingd077bb22023-12-16 08:40:001099 // // static
1100 // scoped_refptr<Foo> Foo::Create() {
1101 // auto foo = base::WrapRefCounted(new Foo());
Peter Kasting7419b0be2023-12-19 05:31:191102 // ThreadPool::PostTask(FROM_HERE, BindOnce(&Foo::Bar, foo));
Peter Kastingd077bb22023-12-16 08:40:001103 // return foo;
1104 // }
1105 //
Peter Kastingd077bb22023-12-16 08:40:001106 // scoped_refptr<Foo> oo = Foo::Create();
Peter Kasting7419b0be2023-12-19 05:31:191107 // ```
Peter Kastingd077bb22023-12-16 08:40:001108 DCHECK(receiver->HasAtLeastOneRef());
1109 }
tzikefea4f52018-08-02 15:20:461110}
1111
Peter Kasting7419b0be2023-12-19 05:31:191112// `BindState<>`
[email protected]7296f2762011-11-21 19:23:441113//
Peter Kasting7419b0be2023-12-19 05:31:191114// This stores all the state passed into `Bind()`.
Peter Kasting336972e2024-02-09 22:08:001115template <bool is_method,
1116 bool is_nullable,
1117 bool is_callback,
1118 typename Functor,
1119 typename... BoundArgs>
tzik99de02b2016-07-01 05:54:121120struct BindState final : BindStateBase {
Peter Kasting20169252023-12-15 23:52:201121 private:
1122 using BoundArgsTuple = std::tuple<BoundArgs...>;
Peter Kasting20169252023-12-15 23:52:201123
1124 public:
tzikbfe66122016-07-08 14:14:011125 template <typename ForwardFunctor, typename... ForwardBoundArgs>
tzikefea4f52018-08-02 15:20:461126 static BindState* Create(BindStateBase::InvokeFuncStorage invoke_func,
1127 ForwardFunctor&& functor,
1128 ForwardBoundArgs&&... bound_args) {
Peter Kasting336972e2024-02-09 22:08:001129 if constexpr (is_method) {
Peter Kastingd077bb22023-12-16 08:40:001130 VerifyMethodReceiver(bound_args...);
1131 }
1132 return new BindState(invoke_func, std::forward<ForwardFunctor>(functor),
tzikefea4f52018-08-02 15:20:461133 std::forward<ForwardBoundArgs>(bound_args)...);
1134 }
tzik1fdcca32016-09-14 07:15:001135
1136 Functor functor_;
Peter Kasting20169252023-12-15 23:52:201137 BoundArgsTuple bound_args_;
tzik1fdcca32016-09-14 07:15:001138
1139 private:
Peter Kastingd077bb22023-12-16 08:40:001140 using CancellationTraits =
1141 CallbackCancellationTraits<Functor, BoundArgsTuple>;
Sami Kyostila712b4e32020-04-27 16:51:081142
tzik1fdcca32016-09-14 07:15:001143 template <typename ForwardFunctor, typename... ForwardBoundArgs>
Peter Kastingd077bb22023-12-16 08:40:001144 requires CancellationTraits::is_cancellable
Lei Zhangb39481fd2025-06-26 01:03:401145 BindState(BindStateBase::InvokeFuncStorage invoke_func,
1146 ForwardFunctor&& functor,
1147 ForwardBoundArgs&&... bound_args)
Peter Kasting20169252023-12-15 23:52:201148 : BindStateBase(invoke_func, &Destroy, &QueryCancellationTraits),
tzikff54a5b152016-08-31 11:50:411149 functor_(std::forward<ForwardFunctor>(functor)),
tzik99de02b2016-07-01 05:54:121150 bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) {
Peter Kasting41472f32023-12-22 03:19:041151 CheckFunctorIsNonNull();
tzik99de02b2016-07-01 05:54:121152 }
[email protected]7296f2762011-11-21 19:23:441153
tzik1fdcca32016-09-14 07:15:001154 template <typename ForwardFunctor, typename... ForwardBoundArgs>
Peter Kastingd077bb22023-12-16 08:40:001155 requires(!CancellationTraits::is_cancellable)
Lei Zhangb39481fd2025-06-26 01:03:401156 BindState(BindStateBase::InvokeFuncStorage invoke_func,
1157 ForwardFunctor&& functor,
1158 ForwardBoundArgs&&... bound_args)
tzik1fdcca32016-09-14 07:15:001159 : BindStateBase(invoke_func, &Destroy),
1160 functor_(std::forward<ForwardFunctor>(functor)),
1161 bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) {
Peter Kasting41472f32023-12-22 03:19:041162 CheckFunctorIsNonNull();
tzik1fdcca32016-09-14 07:15:001163 }
dmichael7d09007e2014-12-18 22:30:111164
Chris Watkins091d6292017-12-13 04:25:581165 ~BindState() = default;
taptede7e804c2015-05-14 08:03:321166
Peter Kasting20169252023-12-15 23:52:201167 static bool QueryCancellationTraits(
1168 const BindStateBase* base,
1169 BindStateBase::CancellationQueryMode mode) {
1170 auto* const storage = static_cast<const BindState*>(base);
1171 static constexpr std::make_index_sequence<sizeof...(BoundArgs)> kIndices;
1172 return (mode == BindStateBase::CancellationQueryMode::kIsCancelled)
1173 ? storage->IsCancelled(kIndices)
1174 : storage->MaybeValid(kIndices);
1175 }
1176
tzik30e0c312016-09-21 08:06:541177 static void Destroy(const BindStateBase* self) {
1178 delete static_cast<const BindState*>(self);
taptede7e804c2015-05-14 08:03:321179 }
Peter Kasting20169252023-12-15 23:52:201180
1181 // Helpers to do arg tuple expansion.
1182 template <size_t... indices>
1183 bool IsCancelled(std::index_sequence<indices...>) const {
Peter Kastingd077bb22023-12-16 08:40:001184 return CancellationTraits::IsCancelled(functor_,
1185 std::get<indices>(bound_args_)...);
Peter Kasting20169252023-12-15 23:52:201186 }
1187
1188 template <size_t... indices>
1189 bool MaybeValid(std::index_sequence<indices...>) const {
Peter Kastingd077bb22023-12-16 08:40:001190 return CancellationTraits::MaybeValid(functor_,
1191 std::get<indices>(bound_args_)...);
Peter Kasting20169252023-12-15 23:52:201192 }
Peter Kasting41472f32023-12-22 03:19:041193
1194 void CheckFunctorIsNonNull() const {
Peter Kasting336972e2024-02-09 22:08:001195 if constexpr (is_nullable) {
Peter Kasting41472f32023-12-22 03:19:041196 // Check the validity of `functor_` to avoid hard-to-diagnose crashes.
1197 // Ideally we'd do this unconditionally, but release builds limit this to
1198 // the case of nested callbacks (e.g. `Bind(callback, ...)`) to limit
1199 // binary size impact.
Peter Kasting336972e2024-02-09 22:08:001200 if constexpr (is_callback) {
Peter Kasting41472f32023-12-22 03:19:041201 CHECK(!!functor_);
1202 } else {
1203 DCHECK(!!functor_);
1204 }
1205 }
1206 }
[email protected]fccef1552011-11-28 22:13:541207};
1208
Peter Kasting5e29968d2024-12-18 04:42:031209template <typename... BoundArgs>
1210struct ValidateBindStateTypeCommonChecks {
1211 private:
1212 // Refcounted parameters must be passed as `scoped_refptr` instead of raw
1213 // pointers, to ensure they are not deleted before use.
1214 // TODO(danakj): Ban native references and `std::reference_wrapper` too.
1215 template <typename T,
1216 bool v =
1217 (IsRawRef<T> && IsRefCountedType<base::RemoveRawRefT<T>>) ||
1218 (IsPointerOrRawPtr<T> &&
1219 IsRefCountedType<base::RemovePointerT<T>>)>
1220 struct RefCountedTypeNotPassedByRawPointer {
1221 static constexpr bool value = [] {
1222 static_assert(
1223 !v, "A parameter is a refcounted type and needs scoped_refptr.");
1224 return !v;
1225 }();
1226 };
1227
1228 public:
1229 using CommonCheckResult = std::conjunction<
1230 RefCountedTypeNotPassedByRawPointer<std::decay_t<BoundArgs>>...,
1231 ValidateStorageTraits<BoundArgs>...>;
1232};
1233
Peter Kasting12f92692023-12-19 04:18:471234// Used to determine and validate the appropriate `BindState`. The
Peter Kastinga7e941e2023-12-20 01:13:491235// specializations below cover all cases. The members are similar in intent to
1236// those in `StorageTraits`; see comments there.
Peter Kasting336972e2024-02-09 22:08:001237template <bool is_method,
1238 bool is_nullable,
1239 bool is_callback,
1240 typename Functor,
1241 typename... BoundArgs>
Peter Kasting12f92692023-12-19 04:18:471242struct ValidateBindStateType;
tzik99de02b2016-07-01 05:54:121243
Peter Kasting336972e2024-02-09 22:08:001244template <bool is_nullable,
1245 bool is_callback,
1246 typename Functor,
1247 typename... BoundArgs>
1248struct ValidateBindStateType<false,
1249 is_nullable,
1250 is_callback,
1251 Functor,
1252 BoundArgs...> {
Peter Kasting336972e2024-02-09 22:08:001253 using Type = BindState<false,
1254 is_nullable,
1255 is_callback,
1256 std::decay_t<Functor>,
Peter Kastinga7e941e2023-12-20 01:13:491257 typename ValidateStorageTraits<BoundArgs>::Type...>;
1258 static constexpr bool value =
Peter Kasting5e29968d2024-12-18 04:42:031259 ValidateBindStateTypeCommonChecks<BoundArgs...>::CommonCheckResult::value;
tzik99de02b2016-07-01 05:54:121260};
1261
Peter Kasting336972e2024-02-09 22:08:001262template <bool is_nullable, bool is_callback, typename Functor>
1263struct ValidateBindStateType<true, is_nullable, is_callback, Functor> {
1264 using Type = BindState<true, is_nullable, is_callback, std::decay_t<Functor>>;
Peter Kasting2ab0f6a2023-11-30 23:27:371265 static constexpr bool value = true;
tzik99de02b2016-07-01 05:54:121266};
1267
Peter Kasting336972e2024-02-09 22:08:001268template <bool is_nullable,
1269 bool is_callback,
1270 typename Functor,
1271 typename Receiver,
1272 typename... BoundArgs>
1273struct ValidateBindStateType<true,
1274 is_nullable,
1275 is_callback,
1276 Functor,
1277 Receiver,
1278 BoundArgs...> {
tzik99de02b2016-07-01 05:54:121279 private:
Jeremy Roman35a317432017-08-16 22:20:531280 using DecayedReceiver = std::decay_t<Receiver>;
Paul Semel9d2f06972022-12-12 16:50:441281 using ReceiverStorageType =
Peter Kasting12f92692023-12-19 04:18:471282 typename MethodReceiverStorage<DecayedReceiver>::Type;
Paul Semel9d2f06972022-12-12 16:50:441283
Peter Kasting2ab0f6a2023-11-30 23:27:371284 template <bool v = !std::is_array_v<std::remove_reference_t<Receiver>>>
1285 struct FirstBoundArgIsNotArray {
1286 static constexpr bool value = [] {
1287 static_assert(v, "First bound argument to a method cannot be an array.");
1288 return v;
1289 }();
1290 };
1291
Peter Kasting5e29968d2024-12-18 04:42:031292 template <bool v = !IsRawRef<DecayedReceiver>>
Peter Kasting2ab0f6a2023-11-30 23:27:371293 struct ReceiverIsNotRawRef {
1294 static constexpr bool value = [] {
1295 static_assert(v, "Receivers may not be raw_ref<T>. If using a raw_ref<T> "
1296 "here is safe and has no lifetime concerns, use "
1297 "base::Unretained() and document why it's safe.");
1298 return v;
1299 }();
1300 };
1301
Peter Kasting5e29968d2024-12-18 04:42:031302 template <bool v = !IsPointerOrRawPtr<DecayedReceiver> ||
1303 IsRefCountedType<RemovePointerT<DecayedReceiver>>>
Peter Kasting2ab0f6a2023-11-30 23:27:371304 struct ReceiverIsNotRawPtr {
1305 static constexpr bool value = [] {
1306 static_assert(v,
1307 "Receivers may not be raw pointers. If using a raw pointer "
1308 "here is safe and has no lifetime concerns, use "
1309 "base::Unretained() and document why it's safe.");
1310 return v;
1311 }();
1312 };
1313
tzik99de02b2016-07-01 05:54:121314 public:
Peter Kasting336972e2024-02-09 22:08:001315 using Type = BindState<true,
1316 is_nullable,
1317 is_callback,
1318 std::decay_t<Functor>,
Paul Semel9d2f06972022-12-12 16:50:441319 ReceiverStorageType,
Peter Kastinga7e941e2023-12-20 01:13:491320 typename ValidateStorageTraits<BoundArgs>::Type...>;
Peter Kasting2ab0f6a2023-11-30 23:27:371321 static constexpr bool value =
1322 std::conjunction_v<FirstBoundArgIsNotArray<>,
1323 ReceiverIsNotRawRef<>,
1324 ReceiverIsNotRawPtr<>,
Peter Kasting5e29968d2024-12-18 04:42:031325 typename ValidateBindStateTypeCommonChecks<
1326 BoundArgs...>::CommonCheckResult>;
tzik99de02b2016-07-01 05:54:121327};
1328
Peter Kasting7419b0be2023-12-19 05:31:191329// Transforms `T` into an unwrapped type, which is passed to the target
1330// function; e.g.:
1331// * `is_once` cases:
1332// ** `TransformToUnwrappedType<true, int&&>` -> `int&&`
1333// ** `TransformToUnwrappedType<true, const int&>` -> `int&&`
1334// ** `TransformToUnwrappedType<true, OwnedWrapper<int>&>` -> `int*&&`
1335// * `!is_once` cases:
1336// ** `TransformToUnwrappedType<false, int&&>` -> `const int&`
1337// ** `TransformToUnwrappedType<false, const int&>` -> `const int&`
1338// ** `TransformToUnwrappedType<false, OwnedWrapper<int>&>` -> `int* const &`
Peter Kastingd077bb22023-12-16 08:40:001339template <bool is_once,
1340 typename T,
1341 typename StoredType = std::decay_t<T>,
1342 typename ForwardedType =
1343 std::conditional_t<is_once, StoredType&&, const StoredType&>>
Daniel Chengcfb972d62021-03-04 13:18:041344using TransformToUnwrappedType =
Peter Kastingd077bb22023-12-16 08:40:001345 decltype(Unwrap(std::declval<ForwardedType>()));
Daniel Chengcfb972d62021-03-04 13:18:041346
Peter Kasting7419b0be2023-12-19 05:31:191347// Used to convert `this` arguments to underlying pointer types; e.g.:
Peter Kastinge5fb81482023-11-17 02:33:211348// `int*` -> `int*`
1349// `std::unique_ptr<int>` -> `int*`
Peter Kasting2ab0f6a2023-11-30 23:27:371350// `int` -> (assertion failure; `this` must be a pointer-like object)
Peter Kastinge5fb81482023-11-17 02:33:211351template <typename T>
Peter Kasting12f92692023-12-19 04:18:471352struct ValidateReceiverType {
Peter Kasting2ab0f6a2023-11-30 23:27:371353 private:
1354 // Pointer-like receivers use a different specialization, so this never
1355 // succeeds.
Avi Drissman421425f2025-08-27 21:54:111356 template <bool v = false>
Peter Kasting2ab0f6a2023-11-30 23:27:371357 struct ReceiverMustBePointerLike {
1358 static constexpr bool value = [] {
1359 static_assert(v,
1360 "Cannot convert `this` argument to address. Method calls "
1361 "must be bound using a pointer-like `this` argument.");
1362 return v;
1363 }();
1364 };
1365
1366 public:
Peter Kastinga7e941e2023-12-20 01:13:491367 // These members are similar in intent to those in `StorageTraits`; see
1368 // comments there.
Peter Kasting2ab0f6a2023-11-30 23:27:371369 using Type = T;
1370 static constexpr bool value = ReceiverMustBePointerLike<>::value;
Peter Kastinge5fb81482023-11-17 02:33:211371};
1372
1373template <typename T>
Nick Diego Yamanecc318b732024-02-27 03:51:451374 requires requires(T&& t) { base::to_address(t); }
Peter Kasting12f92692023-12-19 04:18:471375struct ValidateReceiverType<T> {
Nick Diego Yamanecc318b732024-02-27 03:51:451376 using Type = decltype(base::to_address(std::declval<T>()));
Peter Kasting2ab0f6a2023-11-30 23:27:371377 static constexpr bool value = true;
1378};
1379
Peter Kasting7419b0be2023-12-19 05:31:191380// Transforms `Args` into unwrapped types, and packs them into a `TypeList`. If
1381// `is_method` is true, tries to dereference the first argument to support smart
1382// pointers.
Peter Kasting2ab0f6a2023-11-30 23:27:371383template <bool is_once, bool is_method, typename... Args>
Peter Kasting12f92692023-12-19 04:18:471384struct ValidateUnwrappedTypeList {
Peter Kastinga7e941e2023-12-20 01:13:491385 // These members are similar in intent to those in `StorageTraits`; see
1386 // comments there.
Peter Kasting2ab0f6a2023-11-30 23:27:371387 using Type = TypeList<TransformToUnwrappedType<is_once, Args>...>;
1388 static constexpr bool value = true;
Peter Kastinge5fb81482023-11-17 02:33:211389};
1390
Daniel Chengcfb972d62021-03-04 13:18:041391template <bool is_once, typename Receiver, typename... Args>
Peter Kasting12f92692023-12-19 04:18:471392struct ValidateUnwrappedTypeList<is_once, true, Receiver, Args...> {
Peter Kastinge5fb81482023-11-17 02:33:211393 private:
Paul Semel9d2f06972022-12-12 16:50:441394 using ReceiverStorageType =
Peter Kasting12f92692023-12-19 04:18:471395 typename MethodReceiverStorage<std::decay_t<Receiver>>::Type;
Paul Semel9d2f06972022-12-12 16:50:441396 using UnwrappedReceiver =
1397 TransformToUnwrappedType<is_once, ReceiverStorageType>;
Peter Kasting12f92692023-12-19 04:18:471398 using ValidatedReceiver = ValidateReceiverType<UnwrappedReceiver>;
Daniel Chengcfb972d62021-03-04 13:18:041399
Peter Kastinge5fb81482023-11-17 02:33:211400 public:
Peter Kasting12f92692023-12-19 04:18:471401 using Type = TypeList<typename ValidatedReceiver::Type,
Peter Kasting2ab0f6a2023-11-30 23:27:371402 TransformToUnwrappedType<is_once, Args>...>;
Peter Kasting12f92692023-12-19 04:18:471403 static constexpr bool value = ValidatedReceiver::value;
Peter Kastinge5fb81482023-11-17 02:33:211404};
Daniel Chengcfb972d62021-03-04 13:18:041405
Peter Kasting7419b0be2023-12-19 05:31:191406// `IsUnretainedMayDangle` is true iff `StorageType` is marked with
1407// `unretained_traits::MayDangle`. Note that it is false for
1408// `unretained_traits::MayDangleUntriaged`.
Bartek Nowierski8456cda2023-03-01 10:45:351409template <typename StorageType>
Paul Semelff2ca67b2022-12-21 15:46:021410inline constexpr bool IsUnretainedMayDangle = false;
Peter Kasting7824b6f2023-12-22 03:11:121411
Bartek Nowierskie132f6482023-02-15 20:19:521412template <typename T, RawPtrTraits PtrTraits>
1413inline constexpr bool IsUnretainedMayDangle<
1414 UnretainedWrapper<T, unretained_traits::MayDangle, PtrTraits>> = true;
Paul Semelff2ca67b2022-12-21 15:46:021415
Peter Kasting7419b0be2023-12-19 05:31:191416// `UnretainedAndRawPtrHaveCompatibleTraits` is true iff `StorageType` is marked
1417// with `unretained_traits::MayDangle`, `FunctionParamType` is a `raw_ptr`, and
1418// `StorageType::GetPtrType` is the same type as `FunctionParamType`.
Bartek Nowierski8456cda2023-03-01 10:45:351419template <typename StorageType, typename FunctionParamType>
Paul Semel6cef46d2023-02-28 10:02:211420inline constexpr bool UnretainedAndRawPtrHaveCompatibleTraits = false;
Peter Kasting7824b6f2023-12-22 03:11:121421
Paul Semel6cef46d2023-02-28 10:02:211422template <typename T,
1423 RawPtrTraits PtrTraitsInUnretained,
1424 RawPtrTraits PtrTraitsInReceiver>
1425inline constexpr bool UnretainedAndRawPtrHaveCompatibleTraits<
1426 UnretainedWrapper<T, unretained_traits::MayDangle, PtrTraitsInUnretained>,
1427 raw_ptr<T, PtrTraitsInReceiver>> =
Peter Kastingd077bb22023-12-16 08:40:001428 std::same_as<typename UnretainedWrapper<T,
1429 unretained_traits::MayDangle,
1430 PtrTraitsInUnretained>::GetPtrType,
1431 raw_ptr<T, PtrTraitsInReceiver>>;
Paul Semel6cef46d2023-02-28 10:02:211432
Daniel Chengcfb972d62021-03-04 13:18:041433// Helpers to make error messages slightly more readable.
1434template <int i>
1435struct BindArgument {
1436 template <typename ForwardingType>
1437 struct ForwardedAs {
1438 template <typename FunctorParamType>
1439 struct ToParamWithType {
Peter Kasting5e29968d2024-12-18 04:42:031440 static constexpr bool kRawPtr = IsRawPtr<FunctorParamType>;
Peter Kasting293ce4f2023-12-16 05:26:141441 static constexpr bool kRawPtrMayBeDangling =
Peter Kasting5e29968d2024-12-18 04:42:031442 IsRawPtrMayDangle<FunctorParamType>;
Daniel Chengcfb972d62021-03-04 13:18:041443 static constexpr bool kCanBeForwardedToBoundFunctor =
Peter Kastinga16713f2023-09-02 23:03:101444 std::is_convertible_v<ForwardingType, FunctorParamType>;
kylechar72e6f782021-03-17 17:43:381445
Peter Kasting7419b0be2023-12-19 05:31:191446 // If the bound type can't be forwarded, then test if `FunctorParamType`
1447 // is a non-const lvalue reference and a reference to the unwrapped type
1448 // could have been successfully forwarded.
Peter Kasting293ce4f2023-12-16 05:26:141449 static constexpr bool kIsUnwrappedForwardableNonConstReference =
1450 std::is_lvalue_reference_v<FunctorParamType> &&
1451 !std::is_const_v<std::remove_reference_t<FunctorParamType>> &&
1452 std::is_convertible_v<std::decay_t<ForwardingType>&,
1453 FunctorParamType>;
kylechar72e6f782021-03-17 17:43:381454
Peter Kasting7419b0be2023-12-19 05:31:191455 // Also intentionally drop the `const` qualifier from `ForwardingType`, to
1456 // test if it could have been successfully forwarded if `Passed()` had
1457 // been used.
Peter Kasting293ce4f2023-12-16 05:26:141458 static constexpr bool kWouldBeForwardableWithPassed =
1459 std::is_convertible_v<std::decay_t<ForwardingType>&&,
1460 FunctorParamType>;
Daniel Chengcfb972d62021-03-04 13:18:041461 };
1462 };
1463
1464 template <typename BoundAsType>
1465 struct BoundAs {
1466 template <typename StorageType>
1467 struct StoredAs {
1468 static constexpr bool kBindArgumentCanBeCaptured =
Peter Kastingd077bb22023-12-16 08:40:001469 std::constructible_from<StorageType, BoundAsType>;
Peter Kasting293ce4f2023-12-16 05:26:141470
Peter Kasting7419b0be2023-12-19 05:31:191471 // If the argument can't be captured, intentionally drop the `const`
1472 // qualifier from `BoundAsType`, to test if it could have been
1473 // successfully captured if `std::move()` had been used.
Peter Kasting293ce4f2023-12-16 05:26:141474 static constexpr bool kWouldBeCapturableWithStdMove =
1475 std::constructible_from<StorageType, std::decay_t<BoundAsType>&&>;
Daniel Chengcfb972d62021-03-04 13:18:041476 };
1477 };
Paul Semelff2ca67b2022-12-21 15:46:021478
1479 template <typename FunctionParamType>
1480 struct ToParamWithType {
1481 template <typename StorageType>
1482 struct StoredAs {
Paul Semelff2ca67b2022-12-21 15:46:021483 static constexpr bool kBoundPtrMayDangle =
1484 IsUnretainedMayDangle<StorageType>;
Peter Kasting293ce4f2023-12-16 05:26:141485
1486 static constexpr bool kMayDangleAndMayBeDanglingHaveMatchingTraits =
Paul Semel6cef46d2023-02-28 10:02:211487 UnretainedAndRawPtrHaveCompatibleTraits<StorageType,
1488 FunctionParamType>;
Paul Semelff2ca67b2022-12-21 15:46:021489 };
1490 };
Daniel Chengcfb972d62021-03-04 13:18:041491};
1492
Peter Kasting7419b0be2023-12-19 05:31:191493// Helper to assert that parameter `i` of type `Arg` can be bound, which means:
1494// * `Arg` can be retained internally as `Storage`
1495// * `Arg` can be forwarded as `Unwrapped` to `Param`
Daniel Chengcfb972d62021-03-04 13:18:041496template <int i,
Paul Semelff2ca67b2022-12-21 15:46:021497 bool is_method,
Daniel Chengcfb972d62021-03-04 13:18:041498 typename Arg,
1499 typename Storage,
1500 typename Unwrapped,
1501 typename Param>
Peter Kasting2ab0f6a2023-11-30 23:27:371502struct ParamCanBeBound {
1503 private:
Peter Kasting3b01151f2023-11-27 20:04:211504 using UnwrappedParam = BindArgument<i>::template ForwardedAs<
1505 Unwrapped>::template ToParamWithType<Param>;
1506 using ParamStorage = BindArgument<i>::template ToParamWithType<
1507 Param>::template StoredAs<Storage>;
1508 using BoundStorage =
1509 BindArgument<i>::template BoundAs<Arg>::template StoredAs<Storage>;
1510
Peter Kasting2ab0f6a2023-11-30 23:27:371511 template <bool v = !UnwrappedParam::kRawPtr ||
Peter Kasting293ce4f2023-12-16 05:26:141512 UnwrappedParam::kRawPtrMayBeDangling>
Peter Kasting2ab0f6a2023-11-30 23:27:371513 struct NotRawPtr {
1514 static constexpr bool value = [] {
Peter Kasting36d52e62023-12-11 21:29:331515 static_assert(
1516 v, "Use T* or T& instead of raw_ptr<T> for function parameters, "
1517 "unless you must mark the parameter as MayBeDangling<T>.");
Peter Kasting2ab0f6a2023-11-30 23:27:371518 return v;
1519 }();
1520 };
Peter Kastinga16713f2023-09-02 23:03:101521
Peter Kasting293ce4f2023-12-16 05:26:141522 template <bool v = !ParamStorage::kBoundPtrMayDangle ||
1523 UnwrappedParam::kRawPtrMayBeDangling ||
1524 // Exempt `this` pointer as it is not passed as a regular
1525 // function argument.
1526 (is_method && i == 0)>
Peter Kasting2ab0f6a2023-11-30 23:27:371527 struct MayBeDanglingPtrPassedCorrectly {
1528 static constexpr bool value = [] {
Peter Kasting36d52e62023-12-11 21:29:331529 static_assert(v, "base::UnsafeDangling() pointers should only be passed "
1530 "to parameters marked MayBeDangling<T>.");
Peter Kasting2ab0f6a2023-11-30 23:27:371531 return v;
1532 }();
1533 };
Peter Kastinga16713f2023-09-02 23:03:101534
Peter Kasting2ab0f6a2023-11-30 23:27:371535 template <bool v =
Peter Kasting293ce4f2023-12-16 05:26:141536 !UnwrappedParam::kRawPtrMayBeDangling ||
1537 (ParamStorage::kBoundPtrMayDangle &&
1538 ParamStorage::kMayDangleAndMayBeDanglingHaveMatchingTraits)>
1539 struct MayDangleAndMayBeDanglingHaveMatchingTraits {
Peter Kasting2ab0f6a2023-11-30 23:27:371540 static constexpr bool value = [] {
1541 static_assert(
Peter Kasting36d52e62023-12-11 21:29:331542 v, "Pointers passed to MayBeDangling<T> parameters must be created "
1543 "by base::UnsafeDangling() with the same RawPtrTraits.");
Peter Kasting2ab0f6a2023-11-30 23:27:371544 return v;
1545 }();
1546 };
Peter Kastinga16713f2023-09-02 23:03:101547
Peter Kasting7419b0be2023-12-19 05:31:191548 // With `BindRepeating()`, there are two decision points for how to handle a
Daniel Chengcfb972d62021-03-04 13:18:041549 // move-only type:
1550 //
1551 // 1. Whether the move-only argument should be moved into the internal
Peter Kasting7419b0be2023-12-19 05:31:191552 // `BindState`. Either `std::move()` or `Passed()` is sufficient to trigger
Daniel Chengcfb972d62021-03-04 13:18:041553 // move-only semantics.
1554 // 2. Whether or not the bound, move-only argument should be moved to the
Peter Kasting7419b0be2023-12-19 05:31:191555 // bound functor when invoked. When the argument is bound with `Passed()`,
Daniel Chengcfb972d62021-03-04 13:18:041556 // invoking the callback will destructively move the bound, move-only
1557 // argument to the bound functor. In contrast, if the argument is bound
1558 // with `std::move()`, `RepeatingCallback` will attempt to call the bound
1559 // functor with a constant reference to the bound, move-only argument. This
1560 // will fail if the bound functor accepts that argument by value, since the
1561 // argument cannot be copied. It is this latter case that this
Peter Kasting2ab0f6a2023-11-30 23:27:371562 // assertion aims to catch.
Daniel Chengcfb972d62021-03-04 13:18:041563 //
1564 // In contrast, `BindOnce()` only has one decision point. Once a move-only
1565 // type is captured by value into the internal `BindState`, the bound,
1566 // move-only argument will always be moved to the functor when invoked.
Peter Kasting7419b0be2023-12-19 05:31:191567 // Failure to use `std::move()` will simply fail the
Peter Kasting2ab0f6a2023-11-30 23:27:371568 // `MoveOnlyTypeMustUseStdMove` assertion below instead.
Daniel Chengcfb972d62021-03-04 13:18:041569 //
1570 // Note: `Passed()` is a legacy of supporting move-only types when repeating
1571 // callbacks were the only callback type. A `RepeatingCallback` with a
1572 // `Passed()` argument is really a `OnceCallback` and should eventually be
1573 // migrated.
Peter Kasting293ce4f2023-12-16 05:26:141574 template <bool v = UnwrappedParam::kCanBeForwardedToBoundFunctor ||
1575 !UnwrappedParam::kWouldBeForwardableWithPassed>
Peter Kasting2ab0f6a2023-11-30 23:27:371576 struct MoveOnlyTypeMustUseBasePassed {
1577 static constexpr bool value = [] {
1578 static_assert(v,
1579 "base::BindRepeating() argument is a move-only type. Use "
1580 "base::Passed() instead of std::move() to transfer "
1581 "ownership from the callback to the bound functor.");
1582 return v;
1583 }();
1584 };
Peter Kasting3b01151f2023-11-27 20:04:211585
Peter Kasting293ce4f2023-12-16 05:26:141586 template <bool v = UnwrappedParam::kCanBeForwardedToBoundFunctor ||
1587 !UnwrappedParam::kIsUnwrappedForwardableNonConstReference>
Peter Kasting2ab0f6a2023-11-30 23:27:371588 struct NonConstRefParamMustBeWrapped {
1589 static constexpr bool value = [] {
1590 static_assert(v,
1591 "Bound argument for non-const reference parameter must be "
1592 "wrapped in std::ref() or base::OwnedRef().");
1593 return v;
1594 }();
1595 };
Peter Kasting3b01151f2023-11-27 20:04:211596
Peter Kasting7419b0be2023-12-19 05:31:191597 // Generic failed-to-forward message for cases that didn't match one of the
1598 // two assertions above.
Peter Kasting2ab0f6a2023-11-30 23:27:371599 template <bool v = UnwrappedParam::kCanBeForwardedToBoundFunctor>
1600 struct CanBeForwardedToBoundFunctor {
1601 static constexpr bool value = [] {
1602 static_assert(v,
1603 "Type mismatch between bound argument and bound functor's "
1604 "parameter.");
1605 return v;
1606 }();
1607 };
Daniel Chengcfb972d62021-03-04 13:18:041608
Peter Kasting7419b0be2023-12-19 05:31:191609 // The most common reason for failing to capture a parameter is attempting to
1610 // pass a move-only type as an lvalue.
Peter Kasting293ce4f2023-12-16 05:26:141611 template <bool v = BoundStorage::kBindArgumentCanBeCaptured ||
1612 !BoundStorage::kWouldBeCapturableWithStdMove>
Peter Kasting2ab0f6a2023-11-30 23:27:371613 struct MoveOnlyTypeMustUseStdMove {
1614 static constexpr bool value = [] {
1615 static_assert(v,
1616 "Attempting to bind a move-only type. Use std::move() to "
1617 "transfer ownership to the created callback.");
1618 return v;
1619 }();
1620 };
1621
Peter Kasting7419b0be2023-12-19 05:31:191622 // Any other reason the parameter could not be captured.
Peter Kasting2ab0f6a2023-11-30 23:27:371623 template <bool v = BoundStorage::kBindArgumentCanBeCaptured>
1624 struct BindArgumentCanBeCaptured {
1625 static constexpr bool value = [] {
1626 // In practice, failing this precondition should be rare, as the storage
Peter Kasting7419b0be2023-12-19 05:31:191627 // type is deduced from the arguments passed to `Bind()`.
Peter Kasting2ab0f6a2023-11-30 23:27:371628 static_assert(
1629 v, "Cannot capture argument: is the argument copyable or movable?");
1630 return v;
1631 }();
1632 };
1633
1634 public:
1635 static constexpr bool value =
1636 std::conjunction_v<NotRawPtr<>,
1637 MayBeDanglingPtrPassedCorrectly<>,
Peter Kasting293ce4f2023-12-16 05:26:141638 MayDangleAndMayBeDanglingHaveMatchingTraits<>,
Peter Kasting2ab0f6a2023-11-30 23:27:371639 MoveOnlyTypeMustUseBasePassed<>,
1640 NonConstRefParamMustBeWrapped<>,
1641 CanBeForwardedToBoundFunctor<>,
1642 MoveOnlyTypeMustUseStdMove<>,
1643 BindArgumentCanBeCaptured<>>;
1644};
1645
1646// Takes three same-length `TypeList`s, and checks `ParamCanBeBound` for each
1647// triple.
Paul Semelff2ca67b2022-12-21 15:46:021648template <bool is_method,
1649 typename Index,
Daniel Chengcfb972d62021-03-04 13:18:041650 typename Args,
1651 typename UnwrappedTypeList,
1652 typename ParamsList>
Peter Kasting2ab0f6a2023-11-30 23:27:371653struct ParamsCanBeBound {
1654 static constexpr bool value = false;
1655};
Daniel Chengcfb972d62021-03-04 13:18:041656
Paul Semelff2ca67b2022-12-21 15:46:021657template <bool is_method,
1658 size_t... Ns,
Daniel Chengcfb972d62021-03-04 13:18:041659 typename... Args,
Peter Kasting2ab0f6a2023-11-30 23:27:371660 typename... UnwrappedTypes,
Daniel Chengcfb972d62021-03-04 13:18:041661 typename... Params>
Peter Kasting2ab0f6a2023-11-30 23:27:371662struct ParamsCanBeBound<is_method,
1663 std::index_sequence<Ns...>,
1664 TypeList<Args...>,
1665 TypeList<UnwrappedTypes...>,
1666 TypeList<Params...>> {
1667 static constexpr bool value =
1668 std::conjunction_v<ParamCanBeBound<Ns,
1669 is_method,
1670 Args,
1671 std::decay_t<Args>,
1672 UnwrappedTypes,
1673 Params>...>;
Daniel Chengcfb972d62021-03-04 13:18:041674};
1675
Peter Kasting7419b0be2023-12-19 05:31:191676// Core implementation of `Bind()`, which checks common preconditions before
1677// returning an appropriate callback.
Peter Kasting2ab0f6a2023-11-30 23:27:371678template <template <typename> class CallbackT>
1679struct BindHelper {
1680 private:
Peter Kasting7824b6f2023-12-22 03:11:121681 static constexpr bool kIsOnce =
Daniel Chengfb999fd32025-06-13 02:30:161682 is_instantiation<CallbackT<void()>, OnceCallback>;
Peter Kasting7824b6f2023-12-22 03:11:121683
Peter Kasting336972e2024-02-09 22:08:001684 template <typename Traits, bool v = IsComplete<Traits>>
1685 struct TraitsAreInstantiable {
1686 static constexpr bool value = [] {
Peter Kastingbb8853292024-02-15 19:46:181687 static_assert(
1688 v, "Could not determine how to invoke functor. If this functor has "
1689 "an overloaded operator()(), bind all arguments to it, and ensure "
1690 "the result will select a unique overload.");
Peter Kasting336972e2024-02-09 22:08:001691 return v;
1692 }();
1693 };
1694
Peter Kasting7824b6f2023-12-22 03:11:121695 template <typename Functor,
Daniel Chengfb999fd32025-06-13 02:30:161696 bool v = !is_instantiation<std::decay_t<Functor>, OnceCallback> ||
Peter Kasting7824b6f2023-12-22 03:11:121697 (kIsOnce && std::is_rvalue_reference_v<Functor&&> &&
1698 !std::is_const_v<std::remove_reference_t<Functor>>)>
1699 struct OnceCallbackFunctorIsValid {
1700 static constexpr bool value = [] {
1701 if constexpr (kIsOnce) {
1702 static_assert(v,
1703 "BindOnce() requires non-const rvalue for OnceCallback "
1704 "binding, i.e. base::BindOnce(std::move(callback)).");
1705 } else {
1706 static_assert(v, "BindRepeating() cannot bind OnceCallback. Use "
1707 "BindOnce() with std::move().");
1708 }
1709 return v;
1710 }();
1711 };
1712
1713 template <typename... Args>
1714 struct NoBindArgToOnceCallbackIsBasePassed {
1715 static constexpr bool value = [] {
1716 // Can't use a defaulted template param since it can't come after `Args`.
1717 constexpr bool v =
1718 !kIsOnce ||
Daniel Chengfb999fd32025-06-13 02:30:161719 (... && !is_instantiation<std::decay_t<Args>, PassedWrapper>);
Peter Kasting7824b6f2023-12-22 03:11:121720 static_assert(
1721 v,
1722 "Use std::move() instead of base::Passed() with base::BindOnce().");
1723 return v;
1724 }();
1725 };
1726
Peter Kasting2ab0f6a2023-11-30 23:27:371727 template <
1728 typename Functor,
1729 bool v =
Daniel Chengfb999fd32025-06-13 02:30:161730 !is_instantiation<std::remove_cvref_t<Functor>, FunctionRef> &&
1731 !is_instantiation<std::remove_cvref_t<Functor>, absl::FunctionRef>>
Peter Kasting2ab0f6a2023-11-30 23:27:371732 struct NotFunctionRef {
1733 static constexpr bool value = [] {
Peter Kasting36d52e62023-12-11 21:29:331734 static_assert(
1735 v,
1736 "Functor may not be a FunctionRef, since that is a non-owning "
1737 "reference that may go out of scope before the callback executes.");
Peter Kasting2ab0f6a2023-11-30 23:27:371738 return v;
1739 }();
1740 };
Peter Kastingf871b5e2023-11-17 00:29:271741
Peter Kasting336972e2024-02-09 22:08:001742 template <typename Traits, bool v = Traits::is_stateless>
Peter Kasting2ab0f6a2023-11-30 23:27:371743 struct IsStateless {
1744 static constexpr bool value = [] {
Peter Kasting36d52e62023-12-11 21:29:331745 static_assert(
1746 v, "Capturing lambdas and stateful functors are intentionally not "
1747 "supported. Use a non-capturing lambda or stateless functor (i.e. "
1748 "has no non-static data members) and bind arguments directly.");
Peter Kasting2ab0f6a2023-11-30 23:27:371749 return v;
1750 }();
1751 };
Peter Kasting3b01151f2023-11-27 20:04:211752
Peter Kasting2ab0f6a2023-11-30 23:27:371753 template <typename Functor, typename... Args>
Peter Kasting7824b6f2023-12-22 03:11:121754 static auto BindImpl(Functor&& functor, Args&&... args) {
Peter Kasting26dc0612023-12-13 19:16:481755 // There are a lot of variables and type aliases here. An example will be
1756 // illustrative. Assume we call:
1757 // ```
1758 // struct S {
1759 // double f(int, const std::string&);
1760 // } s;
1761 // int16_t i;
1762 // BindOnce(&S::f, Unretained(&s), i);
1763 // ```
1764 // This means our template params are:
1765 // ```
1766 // template <typename> class CallbackT = OnceCallback
1767 // typename Functor = double (S::*)(int, const std::string&)
1768 // typename... Args =
1769 // UnretainedWrapper<S, unretained_traits::MayNotDangle>, int16_t
1770 // ```
1771 // And the implementation below is effectively:
1772 // ```
Peter Kasting336972e2024-02-09 22:08:001773 // using Traits = struct {
Peter Kasting26dc0612023-12-13 19:16:481774 // using RunType = double(S*, int, const std::string&);
1775 // static constexpr bool is_method = true;
1776 // static constexpr bool is_nullable = true;
1777 // static constexpr bool is_callback = false;
1778 // static constexpr bool is_stateless = true;
1779 // ...
1780 // };
Peter Kasting12f92692023-12-19 04:18:471781 // using ValidatedUnwrappedTypes = struct {
Peter Kasting26dc0612023-12-13 19:16:481782 // using Type = TypeList<S*, int16_t>;
1783 // static constexpr bool value = true;
1784 // };
1785 // using BoundArgsList = TypeList<S*, int16_t>;
1786 // using RunParamsList = TypeList<S*, int, const std::string&>;
1787 // using BoundParamsList = TypeList<S*, int>;
Peter Kasting12f92692023-12-19 04:18:471788 // using ValidatedBindState = struct {
Peter Kasting26dc0612023-12-13 19:16:481789 // using Type =
1790 // BindState<double (S::*)(int, const std::string&),
1791 // UnretainedWrapper<S, unretained_traits::MayNotDangle>,
1792 // int16_t>;
1793 // static constexpr bool value = true;
1794 // };
1795 // if constexpr (true) {
1796 // using UnboundRunType = double(const std::string&);
1797 // using CallbackType = OnceCallback<double(const std::string&)>;
1798 // ...
1799 // ```
Peter Kastingf818a6f2024-02-21 18:21:281800 using Traits = FunctorTraits<TransformToUnwrappedType<kIsOnce, Functor&&>,
1801 TransformToUnwrappedType<kIsOnce, Args&&>...>;
Peter Kasting336972e2024-02-09 22:08:001802 if constexpr (TraitsAreInstantiable<Traits>::value) {
1803 using ValidatedUnwrappedTypes =
1804 ValidateUnwrappedTypeList<kIsOnce, Traits::is_method, Args&&...>;
1805 using BoundArgsList = TypeList<Args...>;
1806 using RunParamsList = ExtractArgs<typename Traits::RunType>;
1807 using BoundParamsList = TakeTypeListItem<sizeof...(Args), RunParamsList>;
1808 using ValidatedBindState =
1809 ValidateBindStateType<Traits::is_method, Traits::is_nullable,
1810 Traits::is_callback, Functor, Args...>;
1811 // This conditional checks if each of the `args` matches to the
1812 // corresponding param of the target function. This check does not affect
1813 // the behavior of `Bind()`, but its error message should be more
1814 // readable.
1815 if constexpr (std::conjunction_v<
1816 NotFunctionRef<Functor>, IsStateless<Traits>,
1817 ValidatedUnwrappedTypes,
1818 ParamsCanBeBound<
1819 Traits::is_method,
1820 std::make_index_sequence<sizeof...(Args)>,
1821 BoundArgsList,
1822 typename ValidatedUnwrappedTypes::Type,
1823 BoundParamsList>,
1824 ValidatedBindState>) {
1825 using UnboundRunType =
1826 MakeFunctionType<ExtractReturnType<typename Traits::RunType>,
1827 DropTypeListItem<sizeof...(Args), RunParamsList>>;
1828 using CallbackType = CallbackT<UnboundRunType>;
Peter Kasting26dc0612023-12-13 19:16:481829
Peter Kasting336972e2024-02-09 22:08:001830 // Store the invoke func into `PolymorphicInvoke` before casting it to
1831 // `InvokeFuncStorage`, so that we can ensure its type matches to
1832 // `PolymorphicInvoke`, to which `CallbackType` will cast back.
1833 typename CallbackType::PolymorphicInvoke invoke_func;
1834 using Invoker =
1835 Invoker<Traits, typename ValidatedBindState::Type, UnboundRunType>;
1836 if constexpr (kIsOnce) {
1837 invoke_func = Invoker::RunOnce;
1838 } else {
1839 invoke_func = Invoker::Run;
1840 }
1841
1842 return CallbackType(ValidatedBindState::Type::Create(
1843 reinterpret_cast<BindStateBase::InvokeFuncStorage>(invoke_func),
1844 std::forward<Functor>(functor), std::forward<Args>(args)...));
Peter Kasting2ab0f6a2023-11-30 23:27:371845 }
Peter Kasting2ab0f6a2023-11-30 23:27:371846 }
Peter Kasting736faa722022-01-12 18:48:451847 }
Daniel Chengcfb972d62021-03-04 13:18:041848
Peter Kasting7419b0be2023-12-19 05:31:191849 // Special cases for binding to a `Callback` without extra bound arguments.
Peter Kastingd077bb22023-12-16 08:40:001850
1851 // `OnceCallback` passed to `OnceCallback`, or `RepeatingCallback` passed to
1852 // `RepeatingCallback`.
1853 template <typename T>
Daniel Chengfb999fd32025-06-13 02:30:161854 requires is_instantiation<T, CallbackT>
Peter Kasting7824b6f2023-12-22 03:11:121855 static T BindImpl(T callback) {
Peter Kasting7419b0be2023-12-19 05:31:191856 // Guard against null pointers accidentally ending up in posted tasks,
1857 // causing hard-to-debug crashes.
Peter Kasting2ab0f6a2023-11-30 23:27:371858 CHECK(callback);
1859 return callback;
1860 }
Daniel Chengcfb972d62021-03-04 13:18:041861
Peter Kastingd077bb22023-12-16 08:40:001862 // `RepeatingCallback` passed to `OnceCallback`. The opposite direction is
1863 // intentionally not supported.
Peter Kasting2ab0f6a2023-11-30 23:27:371864 template <typename Signature>
Daniel Chengfb999fd32025-06-13 02:30:161865 requires is_instantiation<OnceCallback<Signature>, CallbackT>
Peter Kasting7824b6f2023-12-22 03:11:121866 static OnceCallback<Signature> BindImpl(
1867 RepeatingCallback<Signature> callback) {
1868 return BindImpl(OnceCallback<Signature>(callback));
Peter Kasting2ab0f6a2023-11-30 23:27:371869 }
Peter Kastinga7e941e2023-12-20 01:13:491870
Peter Kasting7824b6f2023-12-22 03:11:121871 // Must be defined after `BindImpl()` since it refers to it.
Peter Kastinga7e941e2023-12-20 01:13:491872 template <typename Functor, typename... Args>
Peter Kasting7824b6f2023-12-22 03:11:121873 struct BindImplWouldSucceed {
Peter Kastinga7e941e2023-12-20 01:13:491874 // Can't use a defaulted template param since it can't come after `Args`.
1875 //
Peter Kasting7824b6f2023-12-22 03:11:121876 // Determining if `BindImpl()` would succeed is not as simple as verifying
1877 // any conditions it checks directly; those only control when it's safe to
1878 // call other methods, which in turn may fail. However, ultimately, any
1879 // failure will result in returning `void`, so check for a non-`void` return
1880 // type.
Peter Kastinga7e941e2023-12-20 01:13:491881 static constexpr bool value =
1882 !std::same_as<void,
Peter Kasting7824b6f2023-12-22 03:11:121883 decltype(BindImpl(std::declval<Functor&&>(),
1884 std::declval<Args&&>()...))>;
Peter Kasting2ab0f6a2023-11-30 23:27:371885 };
1886
1887 public:
Peter Kasting7824b6f2023-12-22 03:11:121888 template <typename Functor, typename... Args>
1889 static auto Bind(Functor&& functor, Args&&... args) {
1890 if constexpr (std::conjunction_v<
1891 OnceCallbackFunctorIsValid<Functor>,
1892 NoBindArgToOnceCallbackIsBasePassed<Args...>,
1893 BindImplWouldSucceed<Functor, Args...>>) {
1894 return BindImpl(std::forward<Functor>(functor),
1895 std::forward<Args>(args)...);
Peter Kastinga7e941e2023-12-20 01:13:491896 } else {
1897 return BindFailedCheckPreviousErrors();
Peter Kasting2ab0f6a2023-11-30 23:27:371898 }
1899 }
1900};
Daniel Cheng58ea45b2021-09-17 22:55:081901
[email protected]b38d3572011-02-15 01:27:381902} // namespace internal
tzikcaf1d84b2016-06-28 12:22:211903
Peter Kasting7419b0be2023-12-19 05:31:191904// An injection point to control `this` pointer behavior on a method invocation.
1905// If `IsWeakReceiver<T>::value` is `true` and `T` is used as a method receiver,
1906// `Bind()` cancels the method invocation if the receiver tests as false.
1907// ```
Andrew Williamsbfae87e2024-09-18 13:06:511908// struct S {
Peter Kasting7419b0be2023-12-19 05:31:191909// void f() {}
Peter Kastinga85265e32018-02-15 08:30:231910// };
1911//
Peter Kasting7419b0be2023-12-19 05:31:191912// WeakPtr<S> weak_s = nullptr;
1913// BindOnce(&S::f, weak_s).Run(); // `S::f()` is not called.
1914// ```
Peter Kastinga85265e32018-02-15 08:30:231915template <typename T>
Daniel Chengfb999fd32025-06-13 02:30:161916struct IsWeakReceiver : std::bool_constant<is_instantiation<T, WeakPtr>> {};
Peter Kastinga85265e32018-02-15 08:30:231917
1918template <typename T>
jdoerriec79c2fa22019-02-26 12:35:031919struct IsWeakReceiver<std::reference_wrapper<T>> : IsWeakReceiver<T> {};
Peter Kastinga85265e32018-02-15 08:30:231920
Michael Lippautzbcfc2022021-01-21 17:18:571921// An injection point to control how objects are checked for maybe validity,
1922// which is an optimistic thread-safe check for full validity.
1923template <typename>
1924struct MaybeValidTraits {
1925 template <typename T>
1926 static bool MaybeValid(const T& o) {
1927 return o.MaybeValid();
1928 }
1929};
1930
Peter Kastinga85265e32018-02-15 08:30:231931// An injection point to control how bound objects passed to the target
Peter Kasting7419b0be2023-12-19 05:31:191932// function. `BindUnwrapTraits<>::Unwrap()` is called for each bound object
1933// right before the target function is invoked.
Peter Kastinga85265e32018-02-15 08:30:231934template <typename>
1935struct BindUnwrapTraits {
1936 template <typename T>
1937 static T&& Unwrap(T&& o) {
1938 return std::forward<T>(o);
1939 }
1940};
1941
Daniel Chengadd3e4e2021-10-14 05:04:121942template <typename T>
Peter Kastingd077bb22023-12-16 08:40:001943 requires internal::kIsUnretainedWrapper<internal::UnretainedWrapper, T> ||
1944 internal::kIsUnretainedWrapper<internal::UnretainedRefWrapper, T> ||
Daniel Chengfb999fd32025-06-13 02:30:161945 is_instantiation<T, internal::RetainedRefWrapper> ||
1946 is_instantiation<T, internal::OwnedWrapper> ||
1947 is_instantiation<T, internal::OwnedRefWrapper>
Peter Kastingd077bb22023-12-16 08:40:001948struct BindUnwrapTraits<T> {
1949 static decltype(auto) Unwrap(const T& o) { return o.get(); }
Jan Wilken Dörrie1d011fb2021-03-11 19:37:371950};
1951
1952template <typename T>
Peter Kastinga85265e32018-02-15 08:30:231953struct BindUnwrapTraits<internal::PassedWrapper<T>> {
1954 static T Unwrap(const internal::PassedWrapper<T>& o) { return o.Take(); }
1955};
1956
Xiaohan Wang38e4ebb2022-01-19 06:57:431957#if BUILDFLAG(IS_WIN)
tzikc44f8102018-07-24 09:49:191958template <typename T>
1959struct BindUnwrapTraits<Microsoft::WRL::ComPtr<T>> {
1960 static T* Unwrap(const Microsoft::WRL::ComPtr<T>& ptr) { return ptr.Get(); }
1961};
1962#endif
1963
Peter Kasting7419b0be2023-12-19 05:31:191964// `CallbackCancellationTraits` allows customization of `Callback`'s
1965// cancellation semantics. By default, callbacks are not cancellable. A
1966// specialization should set `is_cancellable` and implement an `IsCancelled()`
1967// that returns whether the callback should be cancelled, as well as a
1968// `MaybeValid()` that returns whether the underlying functor/object is maybe
1969// valid.
Daniel Chengef1375f22023-11-15 00:06:121970template <typename Functor, typename BoundArgsTuple>
Peter Kastinga85265e32018-02-15 08:30:231971struct CallbackCancellationTraits {
1972 static constexpr bool is_cancellable = false;
1973};
1974
Peter Kasting7419b0be2023-12-19 05:31:191975// Specialization for a weak receiver.
Peter Kastinga85265e32018-02-15 08:30:231976template <typename Functor, typename... BoundArgs>
Peter Kasting336972e2024-02-09 22:08:001977 requires internal::kIsWeakMethod<
Peter Kasting8e4901d2024-02-16 16:34:051978 internal::FunctorTraits<Functor, BoundArgs...>::is_method,
Peter Kasting336972e2024-02-09 22:08:001979 BoundArgs...>
Daniel Chengef1375f22023-11-15 00:06:121980struct CallbackCancellationTraits<Functor, std::tuple<BoundArgs...>> {
Peter Kastinga85265e32018-02-15 08:30:231981 static constexpr bool is_cancellable = true;
1982
1983 template <typename Receiver, typename... Args>
1984 static bool IsCancelled(const Functor&,
1985 const Receiver& receiver,
1986 const Args&...) {
1987 return !receiver;
1988 }
Nicolas Ouellet-payeur40f8e9a2018-07-30 16:26:481989
1990 template <typename Receiver, typename... Args>
1991 static bool MaybeValid(const Functor&,
1992 const Receiver& receiver,
1993 const Args&...) {
Michael Lippautzbcfc2022021-01-21 17:18:571994 return MaybeValidTraits<Receiver>::MaybeValid(receiver);
Nicolas Ouellet-payeur40f8e9a2018-07-30 16:26:481995 }
Peter Kastinga85265e32018-02-15 08:30:231996};
1997
Peter Kasting7419b0be2023-12-19 05:31:191998// Specialization for a nested `Bind()`.
Peter Kastingd077bb22023-12-16 08:40:001999template <typename Functor, typename... BoundArgs>
Daniel Chengfb999fd32025-06-13 02:30:162000 requires is_instantiation<Functor, OnceCallback> ||
2001 is_instantiation<Functor, RepeatingCallback>
Peter Kastingd077bb22023-12-16 08:40:002002struct CallbackCancellationTraits<Functor, std::tuple<BoundArgs...>> {
Peter Kastinga85265e32018-02-15 08:30:232003 static constexpr bool is_cancellable = true;
2004
Peter Kastinga85265e32018-02-15 08:30:232005 static bool IsCancelled(const Functor& functor, const BoundArgs&...) {
2006 return functor.IsCancelled();
2007 }
Nicolas Ouellet-payeur40f8e9a2018-07-30 16:26:482008
Nicolas Ouellet-payeur40f8e9a2018-07-30 16:26:482009 static bool MaybeValid(const Functor& functor, const BoundArgs&...) {
Michael Lippautzbcfc2022021-01-21 17:18:572010 return MaybeValidTraits<Functor>::MaybeValid(functor);
Nicolas Ouellet-payeur40f8e9a2018-07-30 16:26:482011 }
Peter Kastinga85265e32018-02-15 08:30:232012};
2013
[email protected]b38d3572011-02-15 01:27:382014} // namespace base
2015
Daniel Cheng91f6fbaf2022-09-16 12:07:482016#endif // BASE_FUNCTIONAL_BIND_INTERNAL_H_