| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 5 | #ifndef BASE_FUNCTIONAL_BIND_INTERNAL_H_ |
| 6 | #define BASE_FUNCTIONAL_BIND_INTERNAL_H_ |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 7 | |
| avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 10 | #include <concepts> |
| jdoerrie | c79c2fa2 | 2019-02-26 12:35:03 | [diff] [blame] | 11 | #include <functional> |
| jdoerrie | 68f2d51b | 2019-02-28 17:32:44 | [diff] [blame] | 12 | #include <memory> |
| jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 13 | #include <tuple> |
| vmpstr | c52317f | 2015-11-18 08:43:26 | [diff] [blame] | 14 | #include <type_traits> |
| Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 15 | #include <utility> |
| vmpstr | c52317f | 2015-11-18 08:43:26 | [diff] [blame] | 16 | |
| Hans Wennborg | 7b53371 | 2020-06-22 20:52:27 | [diff] [blame] | 17 | #include "base/check.h" |
| Sylvain Defresne | ec3270c | 2018-05-31 17:19:15 | [diff] [blame] | 18 | #include "base/compiler_specific.h" |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 19 | #include "base/functional/callback_internal.h" |
| Daniel Cheng | ef3dad3 | 2022-11-25 03:26:28 | [diff] [blame] | 20 | #include "base/functional/unretained_traits.h" |
| Lukasz Anforowicz | fe7bcbae | 2021-11-03 01:49:56 | [diff] [blame] | 21 | #include "base/memory/raw_ptr.h" |
| danakj | 92061c1 | 2022-07-28 23:53:50 | [diff] [blame] | 22 | #include "base/memory/raw_ref.h" |
| [email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 23 | #include "base/memory/weak_ptr.h" |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 24 | #include "base/types/is_complete.h" |
| Peter Kasting | f871b5e | 2023-11-17 00:29:27 | [diff] [blame] | 25 | #include "base/types/is_instantiation.h" |
| Nick Diego Yamane | cc318b73 | 2024-02-27 03:51:45 | [diff] [blame] | 26 | #include "base/types/to_address.h" |
| [email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 27 | #include "build/build_config.h" |
| Daniel Cheng | 2248b33 | 2022-07-27 06:16:59 | [diff] [blame] | 28 | #include "third_party/abseil-cpp/absl/functional/function_ref.h" |
| [email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 29 | |
| David Sanders | 9f1bb40 | 2025-03-03 19:13:56 | [diff] [blame] | 30 | #if PA_BUILDFLAG(USE_ASAN_BACKUP_REF_PTR) |
| 31 | #include "base/memory/raw_ptr_asan_bound_arg_tracker.h" |
| 32 | #endif |
| 33 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 34 | // See docs/callback.md for user documentation. |
| [email protected] | 2429264 | 2012-07-12 20:06:40 | [diff] [blame] | 35 | // |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 36 | // Concepts: |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 37 | // Functor -- A movable type representing something that should be called. |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 38 | // All function pointers and `Callback<>` are functors even if the |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 39 | // invocation syntax differs. |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 40 | // RunType -- A function type (as opposed to function _pointer_ type) for |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 41 | // a `Callback<>::Run()`. Usually just a convenience typedef. |
| tzik | ce3ecf8 | 2015-12-15 06:41:49 | [diff] [blame] | 42 | // (Bound)Args -- A set of types that stores the arguments. |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 43 | // |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 44 | // Types: |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 45 | // `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] | 4346ef91 | 2011-02-19 00:52:15 | [diff] [blame] | 59 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 60 | #if BUILDFLAG(IS_WIN) |
| tzik | c44f810 | 2018-07-24 09:49:19 | [diff] [blame] | 61 | namespace Microsoft { |
| 62 | namespace WRL { |
| 63 | template <typename> |
| 64 | class ComPtr; |
| 65 | } // namespace WRL |
| 66 | } // namespace Microsoft |
| 67 | #endif |
| 68 | |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 69 | namespace base { |
| 70 | |
| 71 | template <typename T> |
| 72 | struct IsWeakReceiver; |
| 73 | |
| 74 | template <typename> |
| 75 | struct BindUnwrapTraits; |
| 76 | |
| Daniel Cheng | ef1375f2 | 2023-11-15 00:06:12 | [diff] [blame] | 77 | template <typename Functor, typename BoundArgsTuple> |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 78 | struct CallbackCancellationTraits; |
| 79 | |
| Daniel Cheng | 2248b33 | 2022-07-27 06:16:59 | [diff] [blame] | 80 | template <typename Signature> |
| 81 | class FunctionRef; |
| 82 | |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 83 | // 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`. |
| 87 | struct BindFailedCheckPreviousErrors {}; |
| 88 | |
| Bartek Nowierski | 58ce9be8 | 2023-01-06 03:55:47 | [diff] [blame] | 89 | namespace unretained_traits { |
| 90 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 91 | // `UnretainedWrapper` will check and report if pointer is dangling upon |
| Bartek Nowierski | 58ce9be8 | 2023-01-06 03:55:47 | [diff] [blame] | 92 | // invocation. |
| 93 | struct MayNotDangle {}; |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 94 | // `UnretainedWrapper` won't check if pointer is dangling upon invocation. For |
| 95 | // extra safety, the receiver must be of type `MayBeDangling<>`. |
| Bartek Nowierski | 58ce9be8 | 2023-01-06 03:55:47 | [diff] [blame] | 96 | struct MayDangle {}; |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 97 | // `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 Nowierski | 58ce9be8 | 2023-01-06 03:55:47 | [diff] [blame] | 103 | struct MayDangleUntriaged {}; |
| 104 | |
| 105 | } // namespace unretained_traits |
| 106 | |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 107 | namespace internal { |
| 108 | |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 109 | template <typename T, |
| 110 | typename UnretainedTrait, |
| 111 | RawPtrTraits PtrTraits = RawPtrTraits::kEmpty> |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 112 | class UnretainedWrapper { |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 113 | // Note that if `PtrTraits` already includes `MayDangle`, `DanglingRawPtrType` |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 114 | // will be identical to `raw_ptr<T, PtrTraits>`. |
| 115 | using DanglingRawPtrType = MayBeDangling<T, PtrTraits>; |
| 116 | |
| Paul Semel | 6cef46d | 2023-02-28 10:02:21 | [diff] [blame] | 117 | public: |
| Bartek Nowierski | 0ccd76b | 2023-03-22 17:02:58 | [diff] [blame] | 118 | // 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 Kasting | 3b01151f | 2023-11-27 20:04:21 | [diff] [blame] | 123 | // `GetPtrType`=`raw_ptr<T>` would break if e.g. `UnretainedWrapper()` is |
| Bartek Nowierski | 0ccd76b | 2023-03-22 17:02:58 | [diff] [blame] | 124 | // constructed using `char*`, but the receiver is of type `std::string&`. |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 125 | // This is enforced by `static_assert()`s in `ParamCanBeBound`. |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 126 | using GetPtrType = std::conditional_t< |
| Bartek Nowierski | 0ccd76b | 2023-03-22 17:02:58 | [diff] [blame] | 127 | raw_ptr_traits::IsSupportedType<T>::value && |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 128 | std::same_as<UnretainedTrait, unretained_traits::MayDangle>, |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 129 | DanglingRawPtrType, |
| 130 | T*>; |
| Paul Semel | ff2ca67b | 2022-12-21 15:46:02 | [diff] [blame] | 131 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 132 | // Raw pointer makes sense only if there are no `PtrTrait`s. If there are, |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 133 | // it means that a `raw_ptr` is being passed, so use the ctors below instead. |
| Daniel Cheng | ef1375f2 | 2023-11-15 00:06:12 | [diff] [blame] | 134 | explicit UnretainedWrapper(T* o) |
| 135 | requires(PtrTraits == RawPtrTraits::kEmpty) |
| Peter Kasting | 6d4812b7 | 2023-12-19 22:10:21 | [diff] [blame] | 136 | : ptr_(o) { |
| 137 | VerifyPreconditions(); |
| 138 | } |
| Daniel Cheng | ef1375f2 | 2023-11-15 00:06:12 | [diff] [blame] | 139 | |
| 140 | explicit UnretainedWrapper(const raw_ptr<T, PtrTraits>& o) |
| 141 | requires(raw_ptr_traits::IsSupportedType<T>::value) |
| Peter Kasting | 6d4812b7 | 2023-12-19 22:10:21 | [diff] [blame] | 142 | : ptr_(o) { |
| 143 | VerifyPreconditions(); |
| 144 | } |
| 145 | |
| Daniel Cheng | ef1375f2 | 2023-11-15 00:06:12 | [diff] [blame] | 146 | explicit UnretainedWrapper(raw_ptr<T, PtrTraits>&& o) |
| 147 | requires(raw_ptr_traits::IsSupportedType<T>::value) |
| Peter Kasting | 6d4812b7 | 2023-12-19 22:10:21 | [diff] [blame] | 148 | : ptr_(std::move(o)) { |
| 149 | VerifyPreconditions(); |
| 150 | } |
| Bartek Nowierski | ed8072b | 2022-07-29 14:25:53 | [diff] [blame] | 151 | |
| Bartek Nowierski | 3b565eab | 2023-03-13 00:48:11 | [diff] [blame] | 152 | GetPtrType get() const { return GetInternal(ptr_); } |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 153 | |
| Peter Kasting | 6d4812b7 | 2023-12-19 22:10:21 | [diff] [blame] | 154 | // 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 Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 158 | private: |
| Bartek Nowierski | 3b565eab | 2023-03-13 00:48:11 | [diff] [blame] | 159 | // `ptr_` is either a `raw_ptr` or a regular C++ pointer. |
| 160 | template <typename U> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 161 | requires std::same_as<T, U> |
| Bartek Nowierski | 3b565eab | 2023-03-13 00:48:11 | [diff] [blame] | 162 | static GetPtrType GetInternal(U* ptr) { |
| Bartek Nowierski | 3b565eab | 2023-03-13 00:48:11 | [diff] [blame] | 163 | return ptr; |
| 164 | } |
| 165 | template <typename U, RawPtrTraits Traits> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 166 | requires std::same_as<T, U> |
| Bartek Nowierski | 3b565eab | 2023-03-13 00:48:11 | [diff] [blame] | 167 | static GetPtrType GetInternal(const raw_ptr<U, Traits>& ptr) { |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 168 | if constexpr (std::same_as<UnretainedTrait, |
| 169 | unretained_traits::MayNotDangle>) { |
| Bartek Nowierski | 3b565eab | 2023-03-13 00:48:11 | [diff] [blame] | 170 | ptr.ReportIfDangling(); |
| 171 | } |
| 172 | return ptr; |
| 173 | } |
| 174 | |
| Bartek Nowierski | 58ce9be8 | 2023-01-06 03:55:47 | [diff] [blame] | 175 | // `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 Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 179 | // |
| 180 | // If the callback is invoked, dangling pointer detection will be triggered |
| Bartek Nowierski | 58ce9be8 | 2023-01-06 03:55:47 | [diff] [blame] | 181 | // before invoking the bound functor (unless stated otherwise, see |
| 182 | // `UnsafeDangling()` and `UnsafeDanglingUntriaged()`), when retrieving the |
| 183 | // pointer value via `get()` above. |
| Bartek Nowierski | c7d7233 | 2023-01-12 14:06:40 | [diff] [blame] | 184 | using StorageType = |
| 185 | std::conditional_t<raw_ptr_traits::IsSupportedType<T>::value, |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 186 | DanglingRawPtrType, |
| Bartek Nowierski | c7d7233 | 2023-01-12 14:06:40 | [diff] [blame] | 187 | T*>; |
| Bartek Nowierski | 58ce9be8 | 2023-01-06 03:55:47 | [diff] [blame] | 188 | // Avoid converting between different `raw_ptr` types when calling `get()`. |
| Bartek Nowierski | 0ccd76b | 2023-03-22 17:02:58 | [diff] [blame] | 189 | // 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 Nowierski | 58ce9be8 | 2023-01-06 03:55:47 | [diff] [blame] | 191 | static_assert(std::is_pointer_v<GetPtrType> || |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 192 | std::same_as<GetPtrType, StorageType>); |
| Peter Kasting | 6d4812b7 | 2023-12-19 22:10:21 | [diff] [blame] | 193 | |
| 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 Nowierski | 58ce9be8 | 2023-01-06 03:55:47 | [diff] [blame] | 203 | StorageType ptr_; |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 204 | }; |
| 205 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 206 | // Storage type for `std::reference_wrapper` so `BindState` can internally store |
| 207 | // unprotected references using `raw_ref`. |
| Daniel Cheng | add3e4e | 2021-10-14 05:04:12 | [diff] [blame] | 208 | // |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 209 | // `std::reference_wrapper<T>` and `T&` do not work, since the reference |
| 210 | // lifetime is not safely protected by MiraclePtr. |
| Daniel Cheng | add3e4e | 2021-10-14 05:04:12 | [diff] [blame] | 211 | // |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 212 | // `UnretainedWrapper<T>` and `raw_ptr<T>` do not work, since `BindUnwrapTraits` |
| 213 | // would try to pass by `T*` rather than `T&`. |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 214 | template <typename T, |
| 215 | typename UnretainedTrait, |
| 216 | RawPtrTraits PtrTraits = RawPtrTraits::kEmpty> |
| Daniel Cheng | add3e4e | 2021-10-14 05:04:12 | [diff] [blame] | 217 | class UnretainedRefWrapper { |
| 218 | public: |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 219 | // Raw reference makes sense only if there are no `PtrTrait`s. If there are, |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 220 | // it means that a `raw_ref` is being passed, so use the ctors below instead. |
| Daniel Cheng | ef1375f2 | 2023-11-15 00:06:12 | [diff] [blame] | 221 | explicit UnretainedRefWrapper(T& o) |
| 222 | requires(PtrTraits == RawPtrTraits::kEmpty) |
| Peter Kasting | 6d4812b7 | 2023-12-19 22:10:21 | [diff] [blame] | 223 | : ref_(o) { |
| 224 | VerifyPreconditions(); |
| 225 | } |
| Daniel Cheng | ef1375f2 | 2023-11-15 00:06:12 | [diff] [blame] | 226 | |
| 227 | explicit UnretainedRefWrapper(const raw_ref<T, PtrTraits>& o) |
| 228 | requires(raw_ptr_traits::IsSupportedType<T>::value) |
| Peter Kasting | 6d4812b7 | 2023-12-19 22:10:21 | [diff] [blame] | 229 | : ref_(o) { |
| 230 | VerifyPreconditions(); |
| 231 | } |
| Daniel Cheng | ef1375f2 | 2023-11-15 00:06:12 | [diff] [blame] | 232 | |
| Peter Kasting | 6d4812b7 | 2023-12-19 22:10:21 | [diff] [blame] | 233 | explicit UnretainedRefWrapper(raw_ref<T, PtrTraits>&& o) |
| Daniel Cheng | ef1375f2 | 2023-11-15 00:06:12 | [diff] [blame] | 234 | requires(raw_ptr_traits::IsSupportedType<T>::value) |
| Peter Kasting | 6d4812b7 | 2023-12-19 22:10:21 | [diff] [blame] | 235 | : ref_(std::move(o)) { |
| 236 | VerifyPreconditions(); |
| 237 | } |
| Bartek Nowierski | e899ad58 | 2023-01-20 06:21:29 | [diff] [blame] | 238 | |
| Bartek Nowierski | 3b565eab | 2023-03-13 00:48:11 | [diff] [blame] | 239 | T& get() const { return GetInternal(ref_); } |
| danakj | 92061c1 | 2022-07-28 23:53:50 | [diff] [blame] | 240 | |
| Peter Kasting | 6d4812b7 | 2023-12-19 22:10:21 | [diff] [blame] | 241 | // See comments in `UnretainedWrapper` regarding this and |
| 242 | // `VerifyPreconditions()`. |
| 243 | static constexpr bool value = SupportsUnretained<T>; |
| 244 | |
| danakj | 92061c1 | 2022-07-28 23:53:50 | [diff] [blame] | 245 | private: |
| Bartek Nowierski | 3b565eab | 2023-03-13 00:48:11 | [diff] [blame] | 246 | // `ref_` is either a `raw_ref` or a regular C++ reference. |
| 247 | template <typename U> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 248 | requires std::same_as<T, U> |
| Bartek Nowierski | 3b565eab | 2023-03-13 00:48:11 | [diff] [blame] | 249 | static T& GetInternal(U& ref) { |
| Bartek Nowierski | 3b565eab | 2023-03-13 00:48:11 | [diff] [blame] | 250 | return ref; |
| 251 | } |
| 252 | template <typename U, RawPtrTraits Traits> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 253 | requires std::same_as<T, U> |
| Bartek Nowierski | 3b565eab | 2023-03-13 00:48:11 | [diff] [blame] | 254 | static T& GetInternal(const raw_ref<U, Traits>& ref) { |
| Bartek Nowierski | 3b565eab | 2023-03-13 00:48:11 | [diff] [blame] | 255 | // 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 Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 258 | // `PartitionAllocUnretainedDanglingPtr` feature. |
| Bartek Nowierski | 3b565eab | 2023-03-13 00:48:11 | [diff] [blame] | 259 | if constexpr (std::is_same_v<UnretainedTrait, |
| 260 | unretained_traits::MayNotDangle>) { |
| 261 | ref.ReportIfDangling(); |
| 262 | } |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 263 | // 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 Nowierski | 3b565eab | 2023-03-13 00:48:11 | [diff] [blame] | 269 | return ref.get(); |
| 270 | } |
| 271 | |
| Bartek Nowierski | f31d2c5e | 2023-02-14 21:41:43 | [diff] [blame] | 272 | // `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 Nowierski | f31d2c5e | 2023-02-14 21:41:43 | [diff] [blame] | 285 | |
| Peter Kasting | 6d4812b7 | 2023-12-19 22:10:21 | [diff] [blame] | 286 | static constexpr void VerifyPreconditions() { std::ignore = value; } |
| 287 | |
| Bartek Nowierski | f31d2c5e | 2023-02-14 21:41:43 | [diff] [blame] | 288 | StorageType ref_; |
| danakj | 92061c1 | 2022-07-28 23:53:50 | [diff] [blame] | 289 | }; |
| danakj | 92061c1 | 2022-07-28 23:53:50 | [diff] [blame] | 290 | |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 291 | // Can't use `is_instantiation` to detect the unretained wrappers, since they |
| 292 | // have non-type template params. |
| 293 | template <template <typename, typename, RawPtrTraits> typename WrapperT, |
| 294 | typename T> |
| 295 | inline constexpr bool kIsUnretainedWrapper = false; |
| 296 | |
| 297 | template <template <typename, typename, RawPtrTraits> typename WrapperT, |
| 298 | typename T, |
| 299 | typename UnretainedTrait, |
| 300 | RawPtrTraits PtrTraits> |
| 301 | inline constexpr bool |
| 302 | kIsUnretainedWrapper<WrapperT, WrapperT<T, UnretainedTrait, PtrTraits>> = |
| 303 | true; |
| 304 | |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 305 | // 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 Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 310 | template <typename T, typename UnretainedTrait, RawPtrTraits PtrTraits> |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 311 | class UnretainedRefWrapperReceiver { |
| 312 | public: |
| 313 | // NOLINTNEXTLINE(google-explicit-constructor) |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 314 | UnretainedRefWrapperReceiver( |
| Peter Kasting | a16713f | 2023-09-02 23:03:10 | [diff] [blame] | 315 | UnretainedRefWrapper<T, UnretainedTrait, PtrTraits>&& obj) |
| 316 | : obj_(std::move(obj)) {} |
| Peter Kasting | 3b01151f | 2023-11-27 20:04:21 | [diff] [blame] | 317 | |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 318 | T& operator*() const { return obj_.get(); } |
| Peter Kasting | 3b01151f | 2023-11-27 20:04:21 | [diff] [blame] | 319 | T* operator->() const { return &obj_.get(); } |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 320 | |
| 321 | private: |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 322 | UnretainedRefWrapper<T, UnretainedTrait, PtrTraits> obj_; |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 323 | }; |
| 324 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 325 | // `MethodReceiverStorage` converts the current receiver type to its stored |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 326 | // type. For instance, it converts pointers to `scoped_refptr`, and wraps |
| 327 | // `UnretainedRefWrapper` to make it compliant with the internal callback |
| 328 | // invocation mechanism. |
| 329 | template <typename T> |
| Peter Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 330 | struct MethodReceiverStorage { |
| danakj | e2dd9c0 | 2024-04-06 19:18:32 | [diff] [blame] | 331 | using Type = std:: |
| Peter Kasting | 5e29968d | 2024-12-18 04:42:03 | [diff] [blame] | 332 | conditional_t<IsPointerOrRawPtr<T>, scoped_refptr<RemovePointerT<T>>, T>; |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 333 | }; |
| 334 | |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 335 | template <typename T, typename UnretainedTrait, RawPtrTraits PtrTraits> |
| Peter Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 336 | struct MethodReceiverStorage< |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 337 | UnretainedRefWrapper<T, UnretainedTrait, PtrTraits>> { |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 338 | // We can't use `UnretainedRefWrapper` as a receiver directly (see |
| 339 | // `UnretainedRefWrapperReceiver` for why). |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 340 | using Type = UnretainedRefWrapperReceiver<T, UnretainedTrait, PtrTraits>; |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 341 | }; |
| 342 | |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 343 | template <typename T> |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 344 | class 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 | |
| 354 | template <typename T> |
| 355 | struct IgnoreResultHelper { |
| 356 | explicit IgnoreResultHelper(T functor) : functor_(std::move(functor)) {} |
| 357 | explicit operator bool() const { return !!functor_; } |
| 358 | |
| 359 | T functor_; |
| 360 | }; |
| 361 | |
| Marijn Kruisselbrink | c1e775f | 2019-12-19 01:24:22 | [diff] [blame] | 362 | template <typename T, typename Deleter = std::default_delete<T>> |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 363 | class OwnedWrapper { |
| 364 | public: |
| 365 | explicit OwnedWrapper(T* o) : ptr_(o) {} |
| Marijn Kruisselbrink | c1e775f | 2019-12-19 01:24:22 | [diff] [blame] | 366 | explicit OwnedWrapper(std::unique_ptr<T, Deleter>&& ptr) |
| 367 | : ptr_(std::move(ptr)) {} |
| jdoerrie | 68f2d51b | 2019-02-28 17:32:44 | [diff] [blame] | 368 | T* get() const { return ptr_.get(); } |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 369 | |
| 370 | private: |
| Marijn Kruisselbrink | c1e775f | 2019-12-19 01:24:22 | [diff] [blame] | 371 | std::unique_ptr<T, Deleter> ptr_; |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 372 | }; |
| 373 | |
| Jan Wilken Dörrie | 1d011fb | 2021-03-11 19:37:37 | [diff] [blame] | 374 | template <typename T> |
| 375 | class 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 Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 385 | // `PassedWrapper` is a copyable adapter for a scoper that ignores `const`. |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 386 | // |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 387 | // 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 Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 389 | // unnecessary copies, it is incompatible with movable-but-not-copyable |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 390 | // types; doing a destructive "move" of the type into `Bind()` would violate |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 391 | // the const correctness. |
| 392 | // |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 393 | // 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 Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 399 | // |
| 400 | // Two notes: |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 401 | // 1) `PassedWrapper` supports any type that has a move constructor, however |
| Nate Fischer | 04068aa | 2021-03-10 22:20:54 | [diff] [blame] | 402 | // the type will need to be specifically allowed in order for it to be |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 403 | // 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 Gale | 47d1537d | 2024-04-19 21:31:46 | [diff] [blame] | 410 | // TODO(crbug.com/40840557): We have rvalue references and such now. Remove. |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 411 | template <typename T> |
| 412 | class PassedWrapper { |
| 413 | public: |
| danakj | 92061c1 | 2022-07-28 23:53:50 | [diff] [blame] | 414 | explicit PassedWrapper(T&& scoper) : scoper_(std::move(scoper)) {} |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 415 | 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: |
| danakj | 92061c1 | 2022-07-28 23:53:50 | [diff] [blame] | 424 | mutable bool is_valid_ = true; |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 425 | mutable T scoper_; |
| 426 | }; |
| 427 | |
| 428 | template <typename T> |
| 429 | using Unwrapper = BindUnwrapTraits<std::decay_t<T>>; |
| 430 | |
| 431 | template <typename T> |
| Peter Kasting | c2f8749bf | 2018-03-31 03:32:37 | [diff] [blame] | 432 | decltype(auto) Unwrap(T&& o) { |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 433 | return Unwrapper<T>::Unwrap(std::forward<T>(o)); |
| 434 | } |
| 435 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 436 | // `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 Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 440 | // |
| 441 | // The first argument should be the type of the object that will be received by |
| 442 | // the method. |
| 443 | template <bool is_method, typename... Args> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 444 | inline constexpr bool kIsWeakMethod = false; |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 445 | |
| 446 | template <typename T, typename... Args> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 447 | inline constexpr bool kIsWeakMethod<true, T, Args...> = |
| 448 | IsWeakReceiver<T>::value; |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 449 | |
| 450 | // Packs a list of types to hold them in a single type. |
| 451 | template <typename... Types> |
| 452 | struct TypeList {}; |
| 453 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 454 | // Implements `DropTypeListItem`. |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 455 | template <size_t n, typename List> |
| Daniel Cheng | fb999fd3 | 2025-06-13 02:30:16 | [diff] [blame] | 456 | requires is_instantiation<List, TypeList> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 457 | struct DropTypeListItemImpl { |
| 458 | using Type = List; |
| 459 | }; |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 460 | |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 461 | template <size_t n, typename T, typename... List> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 462 | requires(n > 0) |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 463 | struct DropTypeListItemImpl<n, TypeList<T, List...>> |
| 464 | : DropTypeListItemImpl<n - 1, TypeList<List...>> {}; |
| 465 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 466 | // A type-level function that drops `n` list items from a given `TypeList`. |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 467 | template <size_t n, typename List> |
| 468 | using DropTypeListItem = typename DropTypeListItemImpl<n, List>::Type; |
| 469 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 470 | // Implements `TakeTypeListItem`. |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 471 | template <size_t n, typename List, typename... Accum> |
| Daniel Cheng | fb999fd3 | 2025-06-13 02:30:16 | [diff] [blame] | 472 | requires is_instantiation<List, TypeList> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 473 | struct TakeTypeListItemImpl { |
| 474 | using Type = TypeList<Accum...>; |
| 475 | }; |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 476 | |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 477 | template <size_t n, typename T, typename... List, typename... Accum> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 478 | requires(n > 0) |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 479 | struct TakeTypeListItemImpl<n, TypeList<T, List...>, Accum...> |
| 480 | : TakeTypeListItemImpl<n - 1, TypeList<List...>, Accum..., T> {}; |
| 481 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 482 | // 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 Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 484 | template <size_t n, typename List> |
| 485 | using TakeTypeListItem = typename TakeTypeListItemImpl<n, List>::Type; |
| 486 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 487 | // Implements `MakeFunctionType`. |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 488 | template <typename R, typename ArgList> |
| 489 | struct MakeFunctionTypeImpl; |
| 490 | |
| 491 | template <typename R, typename... Args> |
| 492 | struct MakeFunctionTypeImpl<R, TypeList<Args...>> { |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 493 | using Type = R(Args...); |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 494 | }; |
| 495 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 496 | // 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 Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 498 | template <typename R, typename ArgList> |
| 499 | using MakeFunctionType = typename MakeFunctionTypeImpl<R, ArgList>::Type; |
| 500 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 501 | // Implements `ExtractArgs` and `ExtractReturnType`. |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 502 | template <typename Signature> |
| 503 | struct ExtractArgsImpl; |
| 504 | |
| 505 | template <typename R, typename... Args> |
| 506 | struct ExtractArgsImpl<R(Args...)> { |
| 507 | using ReturnType = R; |
| 508 | using ArgsList = TypeList<Args...>; |
| 509 | }; |
| 510 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 511 | // A type-level function that extracts function arguments into a `TypeList`; |
| 512 | // e.g. `ExtractArgs<R(A, B, C)>` -> `TypeList<A, B, C>`. |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 513 | template <typename Signature> |
| 514 | using ExtractArgs = typename ExtractArgsImpl<Signature>::ArgsList; |
| 515 | |
| 516 | // A type-level function that extracts the return type of a function. |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 517 | // e.g. `ExtractReturnType<R(A, B, C)>` -> `R`. |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 518 | template <typename Signature> |
| 519 | using ExtractReturnType = typename ExtractArgsImpl<Signature>::ReturnType; |
| 520 | |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 521 | template <typename Callable, |
| 522 | typename Signature = decltype(&Callable::operator())> |
| 523 | struct ExtractCallableRunTypeImpl; |
| 524 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 525 | #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 | } |
| tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 531 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 532 | BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS(); |
| 533 | BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS(const); |
| 534 | BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS(noexcept); |
| 535 | BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS(const noexcept); |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 536 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 537 | #undef BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS |
| Ian McKellar | 29f262d | 2022-07-20 20:23:41 | [diff] [blame] | 538 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 539 | // Evaluated to the RunType of the given callable type; e.g. |
| 540 | // `ExtractCallableRunType<decltype([](int, char*) { return 0.1; })>` -> |
| 541 | // `double(int, char*)`. |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 542 | template <typename Callable> |
| 543 | using ExtractCallableRunType = |
| 544 | typename ExtractCallableRunTypeImpl<Callable>::Type; |
| 545 | |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 546 | // True when `Functor` has a non-overloaded `operator()()`, e.g.: |
| 547 | // struct S1 { |
| 548 | // int operator()(int); |
| 549 | // }; |
| 550 | // static_assert(HasNonOverloadedCallOp<S1>); |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 551 | // |
| 552 | // int i = 0; |
| Peter Kasting | a16713f | 2023-09-02 23:03:10 | [diff] [blame] | 553 | // auto f = [i] {}; |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 554 | // static_assert(HasNonOverloadedCallOp<decltype(f)>); |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 555 | // |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 556 | // struct S2 { |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 557 | // int operator()(int); |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 558 | // std::string operator()(std::string); |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 559 | // }; |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 560 | // static_assert(!HasNonOverloadedCallOp<S2>); |
| 561 | // |
| 562 | // static_assert(!HasNonOverloadedCallOp<void(*)()>); |
| 563 | // |
| 564 | // struct S3 {}; |
| 565 | // static_assert(!HasNonOverloadedCallOp<S3>); |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 566 | // ``` |
| Daniel Cheng | ef1375f2 | 2023-11-15 00:06:12 | [diff] [blame] | 567 | template <typename Functor> |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 568 | concept HasNonOverloadedCallOp = requires { &Functor::operator(); }; |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 569 | |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 570 | template <typename T> |
| 571 | inline constexpr bool IsObjCArcBlockPointer = false; |
| 572 | |
| 573 | #if __OBJC__ && HAS_FEATURE(objc_arc) |
| 574 | template <typename R, typename... Args> |
| 575 | inline 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 Kasting | 2eef24f4 | 2024-02-21 18:42:22 | [diff] [blame] | 580 | // |
| 581 | // Do not decay `Functor` before testing this, lest it give an incorrect result |
| 582 | // for overloads with different ref-qualifiers. |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 583 | template <typename Functor, typename... BoundArgs> |
| 584 | concept HasOverloadedCallOp = requires { |
| 585 | // The functor must be invocable with the bound args. |
| Peter Kasting | 2eef24f4 | 2024-02-21 18:42:22 | [diff] [blame] | 586 | requires requires(Functor&& f, BoundArgs&&... args) { |
| 587 | std::forward<Functor>(f)(std::forward<BoundArgs>(args)...); |
| 588 | }; |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 589 | // Now exclude invocables that are not cases of overloaded `operator()()`s: |
| 590 | // * `operator()()` exists, but isn't overloaded |
| Peter Kasting | 2eef24f4 | 2024-02-21 18:42:22 | [diff] [blame] | 591 | requires !HasNonOverloadedCallOp<std::decay_t<Functor>>; |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 592 | // * Function pointer (doesn't have `operator()()`) |
| Peter Kasting | 2eef24f4 | 2024-02-21 18:42:22 | [diff] [blame] | 593 | requires !std::is_pointer_v<std::decay_t<Functor>>; |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 594 | // * Block pointer (doesn't have `operator()()`) |
| Peter Kasting | 2eef24f4 | 2024-02-21 18:42:22 | [diff] [blame] | 595 | requires !IsObjCArcBlockPointer<std::decay_t<Functor>>; |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 596 | }; |
| 597 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 598 | // `ForceVoidReturn<>` converts a signature to have a `void` return type. |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 599 | template <typename Sig> |
| 600 | struct ForceVoidReturn; |
| 601 | |
| tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 602 | template <typename R, typename... Args> |
| 603 | struct ForceVoidReturn<R(Args...)> { |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 604 | using RunType = void(Args...); |
| [email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 605 | }; |
| 606 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 607 | // `FunctorTraits<>` |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 608 | // |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 609 | // See description at top of file. This must be declared here so it can be |
| 610 | // referenced in `DecayedFunctorTraits`. |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 611 | template <typename Functor, typename... BoundArgs> |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 612 | struct FunctorTraits; |
| 613 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 614 | // Provides functor traits for pre-decayed functor types. |
| 615 | template <typename Functor, typename... BoundArgs> |
| 616 | struct DecayedFunctorTraits; |
| 617 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 618 | // Callable types. |
| Daniel Cheng | da25e8ac | 2022-07-19 23:02:52 | [diff] [blame] | 619 | // This specialization handles lambdas (captureless and capturing) and functors |
| 620 | // with a call operator. Capturing lambdas and stateful functors are explicitly |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 621 | // disallowed by `BindHelper<>::Bind()`; e.g.: |
| 622 | // ``` |
| 623 | // // Captureless lambda: Allowed |
| Peter Kasting | a16713f | 2023-09-02 23:03:10 | [diff] [blame] | 624 | // [] { return 42; }; |
| tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 625 | // |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 626 | // // Capturing lambda: Disallowed |
| tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 627 | // int x; |
| Peter Kasting | a16713f | 2023-09-02 23:03:10 | [diff] [blame] | 628 | // [x] { return x; }; |
| tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 629 | // |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 630 | // // Empty class with `operator()()`: Allowed |
| tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 631 | // struct Foo { |
| 632 | // void operator()() const {} |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 633 | // // No non-`static` member variables and no virtual functions. |
| tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 634 | // }; |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 635 | // ``` |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 636 | template <typename Functor, typename... BoundArgs> |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 637 | requires HasNonOverloadedCallOp<Functor> |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 638 | struct DecayedFunctorTraits<Functor, BoundArgs...> { |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 639 | using RunType = ExtractCallableRunType<Functor>; |
| 640 | static constexpr bool is_method = false; |
| 641 | static constexpr bool is_nullable = false; |
| Sami Kyostila | 712b4e3 | 2020-04-27 16:51:08 | [diff] [blame] | 642 | static constexpr bool is_callback = false; |
| Daniel Cheng | da25e8ac | 2022-07-19 23:02:52 | [diff] [blame] | 643 | static constexpr bool is_stateless = std::is_empty_v<Functor>; |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 644 | |
| tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 645 | 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)...); |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 649 | } |
| 650 | }; |
| 651 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 652 | // Functions. |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 653 | template <typename R, typename... Args, typename... BoundArgs> |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 654 | struct DecayedFunctorTraits<R (*)(Args...), BoundArgs...> { |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 655 | using RunType = R(Args...); |
| 656 | static constexpr bool is_method = false; |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 657 | static constexpr bool is_nullable = true; |
| Sami Kyostila | 712b4e3 | 2020-04-27 16:51:08 | [diff] [blame] | 658 | static constexpr bool is_callback = false; |
| Daniel Cheng | da25e8ac | 2022-07-19 23:02:52 | [diff] [blame] | 659 | static constexpr bool is_stateless = true; |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 660 | |
| tzik | d58a8923 | 2018-04-26 04:29:53 | [diff] [blame] | 661 | template <typename Function, typename... RunArgs> |
| 662 | static R Invoke(Function&& function, RunArgs&&... args) { |
| 663 | return std::forward<Function>(function)(std::forward<RunArgs>(args)...); |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 664 | } |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 665 | }; |
| 666 | |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 667 | template <typename R, typename... Args, typename... BoundArgs> |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 668 | struct DecayedFunctorTraits<R (*)(Args...) noexcept, BoundArgs...> |
| 669 | : DecayedFunctorTraits<R (*)(Args...), BoundArgs...> {}; |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 670 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 671 | #if BUILDFLAG(IS_WIN) && !defined(ARCH_CPU_64_BITS) |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 672 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 673 | // `__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...> {} |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 678 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 679 | BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS(__stdcall, ); |
| 680 | BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS(__stdcall, noexcept); |
| 681 | BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS(__fastcall, ); |
| 682 | BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS(__fastcall, noexcept); |
| Peter Kasting | 648bb94 | 2023-12-16 15:10:23 | [diff] [blame] | 683 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 684 | #undef BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 685 | #endif // BUILDFLAG(IS_WIN) && !defined(ARCH_CPU_64_BITS) |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 686 | |
| Peter Kasting | 41472f3 | 2023-12-22 03:19:04 | [diff] [blame] | 687 | #if __OBJC__ && HAS_FEATURE(objc_arc) |
| Sylvain Defresne | ec3270c | 2018-05-31 17:19:15 | [diff] [blame] | 688 | |
| Peter Kasting | 41472f3 | 2023-12-22 03:19:04 | [diff] [blame] | 689 | // Objective-C blocks. Blocks can be bound as the compiler will ensure their |
| 690 | // lifetimes will be correctly managed. |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 691 | template <typename R, typename... Args, typename... BoundArgs> |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 692 | struct DecayedFunctorTraits<R (^)(Args...), BoundArgs...> { |
| Sylvain Defresne | ec3270c | 2018-05-31 17:19:15 | [diff] [blame] | 693 | using RunType = R(Args...); |
| 694 | static constexpr bool is_method = false; |
| 695 | static constexpr bool is_nullable = true; |
| Sami Kyostila | 712b4e3 | 2020-04-27 16:51:08 | [diff] [blame] | 696 | static constexpr bool is_callback = false; |
| Daniel Cheng | da25e8ac | 2022-07-19 23:02:52 | [diff] [blame] | 697 | static constexpr bool is_stateless = true; |
| Sylvain Defresne | ec3270c | 2018-05-31 17:19:15 | [diff] [blame] | 698 | |
| 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 Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 702 | // 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 Defresne | ec3270c | 2018-05-31 17:19:15 | [diff] [blame] | 706 | // 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 Kasting | 41472f3 | 2023-12-22 03:19:04 | [diff] [blame] | 712 | #endif // __OBJC__ && HAS_FEATURE(objc_arc) |
| Sylvain Defresne | ec3270c | 2018-05-31 17:19:15 | [diff] [blame] | 713 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 714 | // Methods. |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 715 | template <typename R, |
| 716 | typename Receiver, |
| 717 | typename... Args, |
| 718 | typename... BoundArgs> |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 719 | struct DecayedFunctorTraits<R (Receiver::*)(Args...), BoundArgs...> { |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 720 | using RunType = R(Receiver*, Args...); |
| 721 | static constexpr bool is_method = true; |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 722 | static constexpr bool is_nullable = true; |
| Sami Kyostila | 712b4e3 | 2020-04-27 16:51:08 | [diff] [blame] | 723 | static constexpr bool is_callback = false; |
| Daniel Cheng | da25e8ac | 2022-07-19 23:02:52 | [diff] [blame] | 724 | static constexpr bool is_stateless = true; |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 725 | |
| tzik | d58a8923 | 2018-04-26 04:29:53 | [diff] [blame] | 726 | template <typename Method, typename ReceiverPtr, typename... RunArgs> |
| 727 | static R Invoke(Method method, |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 728 | ReceiverPtr&& receiver_ptr, |
| 729 | RunArgs&&... args) { |
| tzik | 75851f4 | 2017-06-14 06:57:01 | [diff] [blame] | 730 | return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...); |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 731 | } |
| 732 | }; |
| 733 | |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 734 | template <typename R, |
| 735 | typename Receiver, |
| 736 | typename... Args, |
| 737 | typename... BoundArgs> |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 738 | struct DecayedFunctorTraits<R (Receiver::*)(Args...) const, BoundArgs...> |
| 739 | : DecayedFunctorTraits<R (Receiver::*)(Args...), BoundArgs...> { |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 740 | using RunType = R(const Receiver*, Args...); |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 741 | }; |
| 742 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 743 | #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 | |
| 752 | BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONST_AND_QUALS(, noexcept); |
| 753 | BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONST_AND_QUALS(const, noexcept); |
| 754 | |
| 755 | #undef BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONST_AND_QUALS |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 756 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 757 | #if BUILDFLAG(IS_WIN) && !defined(ARCH_CPU_64_BITS) |
| Sorin Jianu | 6bc2e35 | 2020-04-01 18:01:41 | [diff] [blame] | 758 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 759 | // `__stdcall` methods. |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 760 | #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 Jianu | 6bc2e35 | 2020-04-01 18:01:41 | [diff] [blame] | 767 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 768 | BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS(); |
| 769 | BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS(const); |
| 770 | BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS(noexcept); |
| 771 | BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS(const noexcept); |
| Peter Kasting | 648bb94 | 2023-12-16 15:10:23 | [diff] [blame] | 772 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 773 | #undef BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS |
| Peter Kasting | 648bb94 | 2023-12-16 15:10:23 | [diff] [blame] | 774 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 775 | #endif // BUILDFLAG(IS_WIN) && !defined(ARCH_CPU_64_BITS) |
| Sorin Jianu | 6bc2e35 | 2020-04-01 18:01:41 | [diff] [blame] | 776 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 777 | // `IgnoreResult`s. |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 778 | template <typename T, typename... BoundArgs> |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 779 | struct DecayedFunctorTraits<IgnoreResultHelper<T>, BoundArgs...> |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 780 | : FunctorTraits<T, BoundArgs...> { |
| 781 | using RunType = typename ForceVoidReturn< |
| 782 | typename FunctorTraits<T, BoundArgs...>::RunType>::RunType; |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 783 | |
| 784 | template <typename IgnoreResultType, typename... RunArgs> |
| 785 | static void Invoke(IgnoreResultType&& ignore_result_helper, |
| 786 | RunArgs&&... args) { |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 787 | FunctorTraits<T, BoundArgs...>::Invoke( |
| tzik | ff54a5b15 | 2016-08-31 11:50:41 | [diff] [blame] | 788 | std::forward<IgnoreResultType>(ignore_result_helper).functor_, |
| 789 | std::forward<RunArgs>(args)...); |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 790 | } |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 791 | }; |
| 792 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 793 | // `OnceCallback`s. |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 794 | template <typename R, typename... Args, typename... BoundArgs> |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 795 | struct DecayedFunctorTraits<OnceCallback<R(Args...)>, BoundArgs...> { |
| tzik | d4bb5b7d | 2017-08-28 19:08:52 | [diff] [blame] | 796 | using RunType = R(Args...); |
| 797 | static constexpr bool is_method = false; |
| 798 | static constexpr bool is_nullable = true; |
| Sami Kyostila | 712b4e3 | 2020-04-27 16:51:08 | [diff] [blame] | 799 | static constexpr bool is_callback = true; |
| Daniel Cheng | da25e8ac | 2022-07-19 23:02:52 | [diff] [blame] | 800 | static constexpr bool is_stateless = true; |
| tzik | d4bb5b7d | 2017-08-28 19:08:52 | [diff] [blame] | 801 | |
| 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 Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 810 | // `RepeatingCallback`s. |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 811 | template <typename R, typename... Args, typename... BoundArgs> |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 812 | struct DecayedFunctorTraits<RepeatingCallback<R(Args...)>, BoundArgs...> { |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 813 | using RunType = R(Args...); |
| 814 | static constexpr bool is_method = false; |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 815 | static constexpr bool is_nullable = true; |
| Sami Kyostila | 712b4e3 | 2020-04-27 16:51:08 | [diff] [blame] | 816 | static constexpr bool is_callback = true; |
| Daniel Cheng | da25e8ac | 2022-07-19 23:02:52 | [diff] [blame] | 817 | static constexpr bool is_stateless = true; |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 818 | |
| 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] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 825 | }; |
| 826 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 827 | // For most functors, the traits should not depend on how the functor is passed, |
| 828 | // so decay the functor. |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 829 | template <typename Functor, typename... BoundArgs> |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 830 | // 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...>> |
| 835 | struct FunctorTraits<Functor, BoundArgs...> |
| 836 | : DecayedFunctorTraits<std::decay_t<Functor>, BoundArgs...> {}; |
| tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 837 | |
| Peter Kasting | 2eef24f4 | 2024-02-21 18:42:22 | [diff] [blame] | 838 | // For `overloaded operator()()`s, it's possible the ref qualifiers of the |
| 839 | // functor matter, so be careful to use the undecayed type. |
| 840 | template <typename Functor, typename... BoundArgs> |
| 841 | requires HasOverloadedCallOp<Functor, BoundArgs...> |
| 842 | struct 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 Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 860 | // `StorageTraits<>` |
| Daniel Cheng | add3e4e | 2021-10-14 05:04:12 | [diff] [blame] | 861 | // |
| 862 | // See description at top of file. |
| Bartek Nowierski | 6be2e1f0 | 2021-11-26 00:36:33 | [diff] [blame] | 863 | template <typename T> |
| Daniel Cheng | add3e4e | 2021-10-14 05:04:12 | [diff] [blame] | 864 | struct StorageTraits { |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 865 | // The type to use for storing the bound arg inside `BindState`. |
| Daniel Cheng | add3e4e | 2021-10-14 05:04:12 | [diff] [blame] | 866 | using Type = T; |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 867 | |
| 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 Cheng | add3e4e | 2021-10-14 05:04:12 | [diff] [blame] | 872 | }; |
| 873 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 874 | // For `T*`, store as `UnretainedWrapper<T>` for safety, as it internally uses |
| 875 | // `raw_ptr<T>` (when possible). |
| Daniel Cheng | add3e4e | 2021-10-14 05:04:12 | [diff] [blame] | 876 | template <typename T> |
| Bartek Nowierski | 6be2e1f0 | 2021-11-26 00:36:33 | [diff] [blame] | 877 | struct StorageTraits<T*> { |
| Bartek Nowierski | 58ce9be8 | 2023-01-06 03:55:47 | [diff] [blame] | 878 | using Type = UnretainedWrapper<T, unretained_traits::MayNotDangle>; |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 879 | static constexpr bool value = Type::value; |
| Daniel Cheng | add3e4e | 2021-10-14 05:04:12 | [diff] [blame] | 880 | }; |
| 881 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 882 | // For `raw_ptr<T>`, store as `UnretainedWrapper<T>` for safety. This may seem |
| Mark Brand | f48e917 | 2022-09-13 10:58:12 | [diff] [blame] | 883 | // contradictory, but this ensures guaranteed protection for the pointer even |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 884 | // during execution of callbacks with parameters of type `raw_ptr<T>`. |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 885 | template <typename T, RawPtrTraits PtrTraits> |
| 886 | struct StorageTraits<raw_ptr<T, PtrTraits>> { |
| 887 | using Type = UnretainedWrapper<T, unretained_traits::MayNotDangle, PtrTraits>; |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 888 | static constexpr bool value = Type::value; |
| Mark Brand | f48e917 | 2022-09-13 10:58:12 | [diff] [blame] | 889 | }; |
| 890 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 891 | // Unwrap `std::reference_wrapper` and store it in a custom wrapper so that |
| 892 | // references are also protected with `raw_ptr<T>`. |
| Daniel Cheng | add3e4e | 2021-10-14 05:04:12 | [diff] [blame] | 893 | template <typename T> |
| 894 | struct StorageTraits<std::reference_wrapper<T>> { |
| Bartek Nowierski | 58ce9be8 | 2023-01-06 03:55:47 | [diff] [blame] | 895 | using Type = UnretainedRefWrapper<T, unretained_traits::MayNotDangle>; |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 896 | static constexpr bool value = Type::value; |
| Daniel Cheng | add3e4e | 2021-10-14 05:04:12 | [diff] [blame] | 897 | }; |
| 898 | |
| 899 | template <typename T> |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 900 | using ValidateStorageTraits = StorageTraits<std::decay_t<T>>; |
| Daniel Cheng | add3e4e | 2021-10-14 05:04:12 | [diff] [blame] | 901 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 902 | // `InvokeHelper<>` |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 903 | // |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 904 | // There are 2 logical `InvokeHelper<>` specializations: normal, weak. |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 905 | // |
| 906 | // The normal type just calls the underlying runnable. |
| 907 | // |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 908 | // Weak calls need special syntax that is applied to the first argument to check |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 909 | // if they should no-op themselves. |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 910 | template <bool is_weak_call, |
| 911 | typename Traits, |
| 912 | typename ReturnType, |
| 913 | size_t... indices> |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 914 | struct InvokeHelper; |
| 915 | |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 916 | template <typename Traits, typename ReturnType, size_t... indices> |
| 917 | struct InvokeHelper<false, Traits, ReturnType, indices...> { |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 918 | template <typename Functor, typename BoundArgsTuple, typename... RunArgs> |
| 919 | static inline ReturnType MakeItSo(Functor&& functor, |
| 920 | BoundArgsTuple&& bound, |
| 921 | RunArgs&&... args) { |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 922 | return Traits::Invoke( |
| Peter Kasting | f818a6f | 2024-02-21 18:21:28 | [diff] [blame] | 923 | Unwrap(std::forward<Functor>(functor)), |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 924 | Unwrap(std::get<indices>(std::forward<BoundArgsTuple>(bound)))..., |
| 925 | std::forward<RunArgs>(args)...); |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 926 | } |
| 927 | }; |
| 928 | |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 929 | template <typename Traits, |
| 930 | typename ReturnType, |
| 931 | size_t index_target, |
| 932 | size_t... index_tail> |
| 933 | struct InvokeHelper<true, Traits, ReturnType, index_target, index_tail...> { |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 934 | template <typename Functor, typename BoundArgsTuple, typename... RunArgs> |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 935 | static inline void MakeItSo(Functor&& functor, |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 936 | BoundArgsTuple&& bound, |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 937 | RunArgs&&... args) { |
| Andrey Kosyakov | 64b025b | 2023-03-31 21:40:15 | [diff] [blame] | 938 | 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 Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 941 | // that allow cross-thread usage and perform `Lock()` in `Unwrap()` traits. |
| Andrey Kosyakov | 64b025b | 2023-03-31 21:40:15 | [diff] [blame] | 942 | const auto& target = Unwrap(std::get<0>(bound)); |
| 943 | if (!target) { |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 944 | return; |
| Bartek Nowierski | af8d5d2 | 2023-01-11 11:01:02 | [diff] [blame] | 945 | } |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 946 | Traits::Invoke( |
| Peter Kasting | f818a6f | 2024-02-21 18:21:28 | [diff] [blame] | 947 | Unwrap(std::forward<Functor>(functor)), target, |
| Andrey Kosyakov | 64b025b | 2023-03-31 21:40:15 | [diff] [blame] | 948 | Unwrap(std::get<index_tail>(std::forward<BoundArgsTuple>(bound)))..., |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 949 | std::forward<RunArgs>(args)...); |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 950 | } |
| 951 | }; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 952 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 953 | // `Invoker<>` |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 954 | // |
| 955 | // See description at the top of the file. |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 956 | template <typename Traits, typename StorageType, typename UnboundRunType> |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 957 | struct Invoker; |
| 958 | |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 959 | template <typename Traits, |
| 960 | typename StorageType, |
| 961 | typename R, |
| 962 | typename... UnboundArgs> |
| 963 | struct Invoker<Traits, StorageType, R(UnboundArgs...)> { |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 964 | private: |
| 965 | using Indices = std::make_index_sequence< |
| 966 | std::tuple_size_v<decltype(StorageType::bound_args_)>>; |
| 967 | |
| 968 | public: |
| Vladislav Kuzkokov | 6d208e1 | 2017-11-08 21:31:08 | [diff] [blame] | 969 | static R RunOnce(BindStateBase* base, |
| tzik | 9bc6837b | 2018-06-28 20:20:47 | [diff] [blame] | 970 | PassingType<UnboundArgs>... unbound_args) { |
| Peter Kasting | 41472f3 | 2023-12-22 03:19:04 | [diff] [blame] | 971 | auto* const storage = static_cast<StorageType*>(base); |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 972 | return RunImpl(std::move(storage->functor_), |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 973 | std::move(storage->bound_args_), Indices(), |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 974 | std::forward<UnboundArgs>(unbound_args)...); |
| 975 | } |
| 976 | |
| tzik | 9bc6837b | 2018-06-28 20:20:47 | [diff] [blame] | 977 | static R Run(BindStateBase* base, PassingType<UnboundArgs>... unbound_args) { |
| Peter Kasting | 41472f3 | 2023-12-22 03:19:04 | [diff] [blame] | 978 | auto* const storage = static_cast<const StorageType*>(base); |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 979 | return RunImpl(storage->functor_, storage->bound_args_, Indices(), |
| tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 980 | std::forward<UnboundArgs>(unbound_args)...); |
| 981 | } |
| 982 | |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 983 | private: |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 984 | // 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 | |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 1032 | template <typename Functor, typename BoundArgsTuple, size_t... indices> |
| 1033 | static inline R RunImpl(Functor&& functor, |
| tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 1034 | BoundArgsTuple&& bound, |
| Peter Kasting | 41472f3 | 2023-12-22 03:19:04 | [diff] [blame] | 1035 | std::index_sequence<indices...>, |
| tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 1036 | UnboundArgs&&... unbound_args) { |
| Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 1037 | #if PA_BUILDFLAG(USE_ASAN_BACKUP_REF_PTR) |
| Mark Brand | f48e917 | 2022-09-13 10:58:12 | [diff] [blame] | 1038 | 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 Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 1042 | #endif // PA_BUILDFLAG(USE_ASAN_BACKUP_REF_PTR) |
| Mark Brand | f48e917 | 2022-09-13 10:58:12 | [diff] [blame] | 1043 | |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1044 | using DecayedArgsTuple = std::decay_t<BoundArgsTuple>; |
| 1045 | static constexpr bool kIsWeakCall = |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1046 | kIsWeakMethod<Traits::is_method, |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1047 | std::tuple_element_t<indices, DecayedArgsTuple>...>; |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 1048 | 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 Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1059 | return InvokeHelper<kIsWeakCall, Traits, R, indices...>::MakeItSo( |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 1060 | std::forward<Functor>(functor), std::forward<BoundArgsTuple>(bound), |
| 1061 | std::forward<UnboundArgs>(unbound_args)...); |
| 1062 | } |
| [email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 1063 | } |
| 1064 | }; |
| 1065 | |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1066 | // Allow binding a method call with no receiver. |
| Alison Gale | 923a33e | 2024-04-22 23:34:28 | [diff] [blame] | 1067 | // TODO(crbug.com/41484339): Remove or make safe. |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1068 | template <typename... Unused> |
| 1069 | void VerifyMethodReceiver(Unused&&...) {} |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 1070 | |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1071 | template <typename Receiver, typename... Unused> |
| 1072 | void VerifyMethodReceiver(Receiver&& receiver, Unused&&...) { |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1073 | // Asserts that a callback is not the first owner of a ref-counted receiver. |
| Peter Kasting | 5e29968d | 2024-12-18 04:42:03 | [diff] [blame] | 1074 | if constexpr (IsPointerOrRawPtr<std::decay_t<Receiver>> && |
| 1075 | IsRefCountedType<RemovePointerT<std::decay_t<Receiver>>>) { |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1076 | DCHECK(receiver); |
| tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 1077 | |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1078 | // It's error prone to make the implicit first reference to ref-counted |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1079 | // 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 Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1084 | // Foo::Foo() { |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1085 | // ThreadPool::PostTask(FROM_HERE, BindOnce(&Foo::Bar, this)); |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1086 | // } |
| 1087 | // |
| 1088 | // scoped_refptr<Foo> oo = new Foo(); |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1089 | // ``` |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1090 | // |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1091 | // 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 Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1095 | // |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1096 | // Instead, consider adding a static factory, and keeping the first |
| 1097 | // reference alive explicitly. |
| 1098 | // ``` |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1099 | // // static |
| 1100 | // scoped_refptr<Foo> Foo::Create() { |
| 1101 | // auto foo = base::WrapRefCounted(new Foo()); |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1102 | // ThreadPool::PostTask(FROM_HERE, BindOnce(&Foo::Bar, foo)); |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1103 | // return foo; |
| 1104 | // } |
| 1105 | // |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1106 | // scoped_refptr<Foo> oo = Foo::Create(); |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1107 | // ``` |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1108 | DCHECK(receiver->HasAtLeastOneRef()); |
| 1109 | } |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 1110 | } |
| 1111 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1112 | // `BindState<>` |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 1113 | // |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1114 | // This stores all the state passed into `Bind()`. |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1115 | template <bool is_method, |
| 1116 | bool is_nullable, |
| 1117 | bool is_callback, |
| 1118 | typename Functor, |
| 1119 | typename... BoundArgs> |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 1120 | struct BindState final : BindStateBase { |
| Peter Kasting | 2016925 | 2023-12-15 23:52:20 | [diff] [blame] | 1121 | private: |
| 1122 | using BoundArgsTuple = std::tuple<BoundArgs...>; |
| Peter Kasting | 2016925 | 2023-12-15 23:52:20 | [diff] [blame] | 1123 | |
| 1124 | public: |
| tzik | bfe6612 | 2016-07-08 14:14:01 | [diff] [blame] | 1125 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 1126 | static BindState* Create(BindStateBase::InvokeFuncStorage invoke_func, |
| 1127 | ForwardFunctor&& functor, |
| 1128 | ForwardBoundArgs&&... bound_args) { |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1129 | if constexpr (is_method) { |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1130 | VerifyMethodReceiver(bound_args...); |
| 1131 | } |
| 1132 | return new BindState(invoke_func, std::forward<ForwardFunctor>(functor), |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 1133 | std::forward<ForwardBoundArgs>(bound_args)...); |
| 1134 | } |
| tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 1135 | |
| 1136 | Functor functor_; |
| Peter Kasting | 2016925 | 2023-12-15 23:52:20 | [diff] [blame] | 1137 | BoundArgsTuple bound_args_; |
| tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 1138 | |
| 1139 | private: |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1140 | using CancellationTraits = |
| 1141 | CallbackCancellationTraits<Functor, BoundArgsTuple>; |
| Sami Kyostila | 712b4e3 | 2020-04-27 16:51:08 | [diff] [blame] | 1142 | |
| tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 1143 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1144 | requires CancellationTraits::is_cancellable |
| Lei Zhang | b39481fd | 2025-06-26 01:03:40 | [diff] [blame] | 1145 | BindState(BindStateBase::InvokeFuncStorage invoke_func, |
| 1146 | ForwardFunctor&& functor, |
| 1147 | ForwardBoundArgs&&... bound_args) |
| Peter Kasting | 2016925 | 2023-12-15 23:52:20 | [diff] [blame] | 1148 | : BindStateBase(invoke_func, &Destroy, &QueryCancellationTraits), |
| tzik | ff54a5b15 | 2016-08-31 11:50:41 | [diff] [blame] | 1149 | functor_(std::forward<ForwardFunctor>(functor)), |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 1150 | bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { |
| Peter Kasting | 41472f3 | 2023-12-22 03:19:04 | [diff] [blame] | 1151 | CheckFunctorIsNonNull(); |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 1152 | } |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 1153 | |
| tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 1154 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1155 | requires(!CancellationTraits::is_cancellable) |
| Lei Zhang | b39481fd | 2025-06-26 01:03:40 | [diff] [blame] | 1156 | BindState(BindStateBase::InvokeFuncStorage invoke_func, |
| 1157 | ForwardFunctor&& functor, |
| 1158 | ForwardBoundArgs&&... bound_args) |
| tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 1159 | : BindStateBase(invoke_func, &Destroy), |
| 1160 | functor_(std::forward<ForwardFunctor>(functor)), |
| 1161 | bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { |
| Peter Kasting | 41472f3 | 2023-12-22 03:19:04 | [diff] [blame] | 1162 | CheckFunctorIsNonNull(); |
| tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 1163 | } |
| dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 1164 | |
| Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 1165 | ~BindState() = default; |
| tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 1166 | |
| Peter Kasting | 2016925 | 2023-12-15 23:52:20 | [diff] [blame] | 1167 | 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 | |
| tzik | 30e0c31 | 2016-09-21 08:06:54 | [diff] [blame] | 1177 | static void Destroy(const BindStateBase* self) { |
| 1178 | delete static_cast<const BindState*>(self); |
| tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 1179 | } |
| Peter Kasting | 2016925 | 2023-12-15 23:52:20 | [diff] [blame] | 1180 | |
| 1181 | // Helpers to do arg tuple expansion. |
| 1182 | template <size_t... indices> |
| 1183 | bool IsCancelled(std::index_sequence<indices...>) const { |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1184 | return CancellationTraits::IsCancelled(functor_, |
| 1185 | std::get<indices>(bound_args_)...); |
| Peter Kasting | 2016925 | 2023-12-15 23:52:20 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | template <size_t... indices> |
| 1189 | bool MaybeValid(std::index_sequence<indices...>) const { |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1190 | return CancellationTraits::MaybeValid(functor_, |
| 1191 | std::get<indices>(bound_args_)...); |
| Peter Kasting | 2016925 | 2023-12-15 23:52:20 | [diff] [blame] | 1192 | } |
| Peter Kasting | 41472f3 | 2023-12-22 03:19:04 | [diff] [blame] | 1193 | |
| 1194 | void CheckFunctorIsNonNull() const { |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1195 | if constexpr (is_nullable) { |
| Peter Kasting | 41472f3 | 2023-12-22 03:19:04 | [diff] [blame] | 1196 | // 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 Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1200 | if constexpr (is_callback) { |
| Peter Kasting | 41472f3 | 2023-12-22 03:19:04 | [diff] [blame] | 1201 | CHECK(!!functor_); |
| 1202 | } else { |
| 1203 | DCHECK(!!functor_); |
| 1204 | } |
| 1205 | } |
| 1206 | } |
| [email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 1207 | }; |
| 1208 | |
| Peter Kasting | 5e29968d | 2024-12-18 04:42:03 | [diff] [blame] | 1209 | template <typename... BoundArgs> |
| 1210 | struct 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 Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 1234 | // Used to determine and validate the appropriate `BindState`. The |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 1235 | // specializations below cover all cases. The members are similar in intent to |
| 1236 | // those in `StorageTraits`; see comments there. |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1237 | template <bool is_method, |
| 1238 | bool is_nullable, |
| 1239 | bool is_callback, |
| 1240 | typename Functor, |
| 1241 | typename... BoundArgs> |
| Peter Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 1242 | struct ValidateBindStateType; |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 1243 | |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1244 | template <bool is_nullable, |
| 1245 | bool is_callback, |
| 1246 | typename Functor, |
| 1247 | typename... BoundArgs> |
| 1248 | struct ValidateBindStateType<false, |
| 1249 | is_nullable, |
| 1250 | is_callback, |
| 1251 | Functor, |
| 1252 | BoundArgs...> { |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1253 | using Type = BindState<false, |
| 1254 | is_nullable, |
| 1255 | is_callback, |
| 1256 | std::decay_t<Functor>, |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 1257 | typename ValidateStorageTraits<BoundArgs>::Type...>; |
| 1258 | static constexpr bool value = |
| Peter Kasting | 5e29968d | 2024-12-18 04:42:03 | [diff] [blame] | 1259 | ValidateBindStateTypeCommonChecks<BoundArgs...>::CommonCheckResult::value; |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 1260 | }; |
| 1261 | |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1262 | template <bool is_nullable, bool is_callback, typename Functor> |
| 1263 | struct ValidateBindStateType<true, is_nullable, is_callback, Functor> { |
| 1264 | using Type = BindState<true, is_nullable, is_callback, std::decay_t<Functor>>; |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1265 | static constexpr bool value = true; |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 1266 | }; |
| 1267 | |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1268 | template <bool is_nullable, |
| 1269 | bool is_callback, |
| 1270 | typename Functor, |
| 1271 | typename Receiver, |
| 1272 | typename... BoundArgs> |
| 1273 | struct ValidateBindStateType<true, |
| 1274 | is_nullable, |
| 1275 | is_callback, |
| 1276 | Functor, |
| 1277 | Receiver, |
| 1278 | BoundArgs...> { |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 1279 | private: |
| Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 1280 | using DecayedReceiver = std::decay_t<Receiver>; |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 1281 | using ReceiverStorageType = |
| Peter Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 1282 | typename MethodReceiverStorage<DecayedReceiver>::Type; |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 1283 | |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1284 | 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 Kasting | 5e29968d | 2024-12-18 04:42:03 | [diff] [blame] | 1292 | template <bool v = !IsRawRef<DecayedReceiver>> |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1293 | 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 Kasting | 5e29968d | 2024-12-18 04:42:03 | [diff] [blame] | 1302 | template <bool v = !IsPointerOrRawPtr<DecayedReceiver> || |
| 1303 | IsRefCountedType<RemovePointerT<DecayedReceiver>>> |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1304 | 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 | |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 1314 | public: |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1315 | using Type = BindState<true, |
| 1316 | is_nullable, |
| 1317 | is_callback, |
| 1318 | std::decay_t<Functor>, |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 1319 | ReceiverStorageType, |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 1320 | typename ValidateStorageTraits<BoundArgs>::Type...>; |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1321 | static constexpr bool value = |
| 1322 | std::conjunction_v<FirstBoundArgIsNotArray<>, |
| 1323 | ReceiverIsNotRawRef<>, |
| 1324 | ReceiverIsNotRawPtr<>, |
| Peter Kasting | 5e29968d | 2024-12-18 04:42:03 | [diff] [blame] | 1325 | typename ValidateBindStateTypeCommonChecks< |
| 1326 | BoundArgs...>::CommonCheckResult>; |
| tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 1327 | }; |
| 1328 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1329 | // 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 Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1339 | template <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 Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1344 | using TransformToUnwrappedType = |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1345 | decltype(Unwrap(std::declval<ForwardedType>())); |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1346 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1347 | // Used to convert `this` arguments to underlying pointer types; e.g.: |
| Peter Kasting | e5fb8148 | 2023-11-17 02:33:21 | [diff] [blame] | 1348 | // `int*` -> `int*` |
| 1349 | // `std::unique_ptr<int>` -> `int*` |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1350 | // `int` -> (assertion failure; `this` must be a pointer-like object) |
| Peter Kasting | e5fb8148 | 2023-11-17 02:33:21 | [diff] [blame] | 1351 | template <typename T> |
| Peter Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 1352 | struct ValidateReceiverType { |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1353 | private: |
| 1354 | // Pointer-like receivers use a different specialization, so this never |
| 1355 | // succeeds. |
| Avi Drissman | 421425f | 2025-08-27 21:54:11 | [diff] [blame] | 1356 | template <bool v = false> |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1357 | 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 Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 1367 | // These members are similar in intent to those in `StorageTraits`; see |
| 1368 | // comments there. |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1369 | using Type = T; |
| 1370 | static constexpr bool value = ReceiverMustBePointerLike<>::value; |
| Peter Kasting | e5fb8148 | 2023-11-17 02:33:21 | [diff] [blame] | 1371 | }; |
| 1372 | |
| 1373 | template <typename T> |
| Nick Diego Yamane | cc318b73 | 2024-02-27 03:51:45 | [diff] [blame] | 1374 | requires requires(T&& t) { base::to_address(t); } |
| Peter Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 1375 | struct ValidateReceiverType<T> { |
| Nick Diego Yamane | cc318b73 | 2024-02-27 03:51:45 | [diff] [blame] | 1376 | using Type = decltype(base::to_address(std::declval<T>())); |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1377 | static constexpr bool value = true; |
| 1378 | }; |
| 1379 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1380 | // 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 Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1383 | template <bool is_once, bool is_method, typename... Args> |
| Peter Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 1384 | struct ValidateUnwrappedTypeList { |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 1385 | // These members are similar in intent to those in `StorageTraits`; see |
| 1386 | // comments there. |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1387 | using Type = TypeList<TransformToUnwrappedType<is_once, Args>...>; |
| 1388 | static constexpr bool value = true; |
| Peter Kasting | e5fb8148 | 2023-11-17 02:33:21 | [diff] [blame] | 1389 | }; |
| 1390 | |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1391 | template <bool is_once, typename Receiver, typename... Args> |
| Peter Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 1392 | struct ValidateUnwrappedTypeList<is_once, true, Receiver, Args...> { |
| Peter Kasting | e5fb8148 | 2023-11-17 02:33:21 | [diff] [blame] | 1393 | private: |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 1394 | using ReceiverStorageType = |
| Peter Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 1395 | typename MethodReceiverStorage<std::decay_t<Receiver>>::Type; |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 1396 | using UnwrappedReceiver = |
| 1397 | TransformToUnwrappedType<is_once, ReceiverStorageType>; |
| Peter Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 1398 | using ValidatedReceiver = ValidateReceiverType<UnwrappedReceiver>; |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1399 | |
| Peter Kasting | e5fb8148 | 2023-11-17 02:33:21 | [diff] [blame] | 1400 | public: |
| Peter Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 1401 | using Type = TypeList<typename ValidatedReceiver::Type, |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1402 | TransformToUnwrappedType<is_once, Args>...>; |
| Peter Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 1403 | static constexpr bool value = ValidatedReceiver::value; |
| Peter Kasting | e5fb8148 | 2023-11-17 02:33:21 | [diff] [blame] | 1404 | }; |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1405 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1406 | // `IsUnretainedMayDangle` is true iff `StorageType` is marked with |
| 1407 | // `unretained_traits::MayDangle`. Note that it is false for |
| 1408 | // `unretained_traits::MayDangleUntriaged`. |
| Bartek Nowierski | 8456cda | 2023-03-01 10:45:35 | [diff] [blame] | 1409 | template <typename StorageType> |
| Paul Semel | ff2ca67b | 2022-12-21 15:46:02 | [diff] [blame] | 1410 | inline constexpr bool IsUnretainedMayDangle = false; |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1411 | |
| Bartek Nowierski | e132f648 | 2023-02-15 20:19:52 | [diff] [blame] | 1412 | template <typename T, RawPtrTraits PtrTraits> |
| 1413 | inline constexpr bool IsUnretainedMayDangle< |
| 1414 | UnretainedWrapper<T, unretained_traits::MayDangle, PtrTraits>> = true; |
| Paul Semel | ff2ca67b | 2022-12-21 15:46:02 | [diff] [blame] | 1415 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1416 | // `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 Nowierski | 8456cda | 2023-03-01 10:45:35 | [diff] [blame] | 1419 | template <typename StorageType, typename FunctionParamType> |
| Paul Semel | 6cef46d | 2023-02-28 10:02:21 | [diff] [blame] | 1420 | inline constexpr bool UnretainedAndRawPtrHaveCompatibleTraits = false; |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1421 | |
| Paul Semel | 6cef46d | 2023-02-28 10:02:21 | [diff] [blame] | 1422 | template <typename T, |
| 1423 | RawPtrTraits PtrTraitsInUnretained, |
| 1424 | RawPtrTraits PtrTraitsInReceiver> |
| 1425 | inline constexpr bool UnretainedAndRawPtrHaveCompatibleTraits< |
| 1426 | UnretainedWrapper<T, unretained_traits::MayDangle, PtrTraitsInUnretained>, |
| 1427 | raw_ptr<T, PtrTraitsInReceiver>> = |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1428 | std::same_as<typename UnretainedWrapper<T, |
| 1429 | unretained_traits::MayDangle, |
| 1430 | PtrTraitsInUnretained>::GetPtrType, |
| 1431 | raw_ptr<T, PtrTraitsInReceiver>>; |
| Paul Semel | 6cef46d | 2023-02-28 10:02:21 | [diff] [blame] | 1432 | |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1433 | // Helpers to make error messages slightly more readable. |
| 1434 | template <int i> |
| 1435 | struct BindArgument { |
| 1436 | template <typename ForwardingType> |
| 1437 | struct ForwardedAs { |
| 1438 | template <typename FunctorParamType> |
| 1439 | struct ToParamWithType { |
| Peter Kasting | 5e29968d | 2024-12-18 04:42:03 | [diff] [blame] | 1440 | static constexpr bool kRawPtr = IsRawPtr<FunctorParamType>; |
| Peter Kasting | 293ce4f | 2023-12-16 05:26:14 | [diff] [blame] | 1441 | static constexpr bool kRawPtrMayBeDangling = |
| Peter Kasting | 5e29968d | 2024-12-18 04:42:03 | [diff] [blame] | 1442 | IsRawPtrMayDangle<FunctorParamType>; |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1443 | static constexpr bool kCanBeForwardedToBoundFunctor = |
| Peter Kasting | a16713f | 2023-09-02 23:03:10 | [diff] [blame] | 1444 | std::is_convertible_v<ForwardingType, FunctorParamType>; |
| kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 1445 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1446 | // 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 Kasting | 293ce4f | 2023-12-16 05:26:14 | [diff] [blame] | 1449 | 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>; |
| kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 1454 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1455 | // 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 Kasting | 293ce4f | 2023-12-16 05:26:14 | [diff] [blame] | 1458 | static constexpr bool kWouldBeForwardableWithPassed = |
| 1459 | std::is_convertible_v<std::decay_t<ForwardingType>&&, |
| 1460 | FunctorParamType>; |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1461 | }; |
| 1462 | }; |
| 1463 | |
| 1464 | template <typename BoundAsType> |
| 1465 | struct BoundAs { |
| 1466 | template <typename StorageType> |
| 1467 | struct StoredAs { |
| 1468 | static constexpr bool kBindArgumentCanBeCaptured = |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1469 | std::constructible_from<StorageType, BoundAsType>; |
| Peter Kasting | 293ce4f | 2023-12-16 05:26:14 | [diff] [blame] | 1470 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1471 | // 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 Kasting | 293ce4f | 2023-12-16 05:26:14 | [diff] [blame] | 1474 | static constexpr bool kWouldBeCapturableWithStdMove = |
| 1475 | std::constructible_from<StorageType, std::decay_t<BoundAsType>&&>; |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1476 | }; |
| 1477 | }; |
| Paul Semel | ff2ca67b | 2022-12-21 15:46:02 | [diff] [blame] | 1478 | |
| 1479 | template <typename FunctionParamType> |
| 1480 | struct ToParamWithType { |
| 1481 | template <typename StorageType> |
| 1482 | struct StoredAs { |
| Paul Semel | ff2ca67b | 2022-12-21 15:46:02 | [diff] [blame] | 1483 | static constexpr bool kBoundPtrMayDangle = |
| 1484 | IsUnretainedMayDangle<StorageType>; |
| Peter Kasting | 293ce4f | 2023-12-16 05:26:14 | [diff] [blame] | 1485 | |
| 1486 | static constexpr bool kMayDangleAndMayBeDanglingHaveMatchingTraits = |
| Paul Semel | 6cef46d | 2023-02-28 10:02:21 | [diff] [blame] | 1487 | UnretainedAndRawPtrHaveCompatibleTraits<StorageType, |
| 1488 | FunctionParamType>; |
| Paul Semel | ff2ca67b | 2022-12-21 15:46:02 | [diff] [blame] | 1489 | }; |
| 1490 | }; |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1491 | }; |
| 1492 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1493 | // 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 Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1496 | template <int i, |
| Paul Semel | ff2ca67b | 2022-12-21 15:46:02 | [diff] [blame] | 1497 | bool is_method, |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1498 | typename Arg, |
| 1499 | typename Storage, |
| 1500 | typename Unwrapped, |
| 1501 | typename Param> |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1502 | struct ParamCanBeBound { |
| 1503 | private: |
| Peter Kasting | 3b01151f | 2023-11-27 20:04:21 | [diff] [blame] | 1504 | 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 Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1511 | template <bool v = !UnwrappedParam::kRawPtr || |
| Peter Kasting | 293ce4f | 2023-12-16 05:26:14 | [diff] [blame] | 1512 | UnwrappedParam::kRawPtrMayBeDangling> |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1513 | struct NotRawPtr { |
| 1514 | static constexpr bool value = [] { |
| Peter Kasting | 36d52e6 | 2023-12-11 21:29:33 | [diff] [blame] | 1515 | 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 Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1518 | return v; |
| 1519 | }(); |
| 1520 | }; |
| Peter Kasting | a16713f | 2023-09-02 23:03:10 | [diff] [blame] | 1521 | |
| Peter Kasting | 293ce4f | 2023-12-16 05:26:14 | [diff] [blame] | 1522 | 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 Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1527 | struct MayBeDanglingPtrPassedCorrectly { |
| 1528 | static constexpr bool value = [] { |
| Peter Kasting | 36d52e6 | 2023-12-11 21:29:33 | [diff] [blame] | 1529 | static_assert(v, "base::UnsafeDangling() pointers should only be passed " |
| 1530 | "to parameters marked MayBeDangling<T>."); |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1531 | return v; |
| 1532 | }(); |
| 1533 | }; |
| Peter Kasting | a16713f | 2023-09-02 23:03:10 | [diff] [blame] | 1534 | |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1535 | template <bool v = |
| Peter Kasting | 293ce4f | 2023-12-16 05:26:14 | [diff] [blame] | 1536 | !UnwrappedParam::kRawPtrMayBeDangling || |
| 1537 | (ParamStorage::kBoundPtrMayDangle && |
| 1538 | ParamStorage::kMayDangleAndMayBeDanglingHaveMatchingTraits)> |
| 1539 | struct MayDangleAndMayBeDanglingHaveMatchingTraits { |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1540 | static constexpr bool value = [] { |
| 1541 | static_assert( |
| Peter Kasting | 36d52e6 | 2023-12-11 21:29:33 | [diff] [blame] | 1542 | v, "Pointers passed to MayBeDangling<T> parameters must be created " |
| 1543 | "by base::UnsafeDangling() with the same RawPtrTraits."); |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1544 | return v; |
| 1545 | }(); |
| 1546 | }; |
| Peter Kasting | a16713f | 2023-09-02 23:03:10 | [diff] [blame] | 1547 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1548 | // With `BindRepeating()`, there are two decision points for how to handle a |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1549 | // move-only type: |
| 1550 | // |
| 1551 | // 1. Whether the move-only argument should be moved into the internal |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1552 | // `BindState`. Either `std::move()` or `Passed()` is sufficient to trigger |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1553 | // move-only semantics. |
| 1554 | // 2. Whether or not the bound, move-only argument should be moved to the |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1555 | // bound functor when invoked. When the argument is bound with `Passed()`, |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1556 | // 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 Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1562 | // assertion aims to catch. |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1563 | // |
| 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 Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1567 | // Failure to use `std::move()` will simply fail the |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1568 | // `MoveOnlyTypeMustUseStdMove` assertion below instead. |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1569 | // |
| 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 Kasting | 293ce4f | 2023-12-16 05:26:14 | [diff] [blame] | 1574 | template <bool v = UnwrappedParam::kCanBeForwardedToBoundFunctor || |
| 1575 | !UnwrappedParam::kWouldBeForwardableWithPassed> |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1576 | 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 Kasting | 3b01151f | 2023-11-27 20:04:21 | [diff] [blame] | 1585 | |
| Peter Kasting | 293ce4f | 2023-12-16 05:26:14 | [diff] [blame] | 1586 | template <bool v = UnwrappedParam::kCanBeForwardedToBoundFunctor || |
| 1587 | !UnwrappedParam::kIsUnwrappedForwardableNonConstReference> |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1588 | 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 Kasting | 3b01151f | 2023-11-27 20:04:21 | [diff] [blame] | 1596 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1597 | // Generic failed-to-forward message for cases that didn't match one of the |
| 1598 | // two assertions above. |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1599 | 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 Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1608 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1609 | // The most common reason for failing to capture a parameter is attempting to |
| 1610 | // pass a move-only type as an lvalue. |
| Peter Kasting | 293ce4f | 2023-12-16 05:26:14 | [diff] [blame] | 1611 | template <bool v = BoundStorage::kBindArgumentCanBeCaptured || |
| 1612 | !BoundStorage::kWouldBeCapturableWithStdMove> |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1613 | 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 Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1622 | // Any other reason the parameter could not be captured. |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1623 | 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 Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1627 | // type is deduced from the arguments passed to `Bind()`. |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1628 | 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 Kasting | 293ce4f | 2023-12-16 05:26:14 | [diff] [blame] | 1638 | MayDangleAndMayBeDanglingHaveMatchingTraits<>, |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1639 | 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 Semel | ff2ca67b | 2022-12-21 15:46:02 | [diff] [blame] | 1648 | template <bool is_method, |
| 1649 | typename Index, |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1650 | typename Args, |
| 1651 | typename UnwrappedTypeList, |
| 1652 | typename ParamsList> |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1653 | struct ParamsCanBeBound { |
| 1654 | static constexpr bool value = false; |
| 1655 | }; |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1656 | |
| Paul Semel | ff2ca67b | 2022-12-21 15:46:02 | [diff] [blame] | 1657 | template <bool is_method, |
| 1658 | size_t... Ns, |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1659 | typename... Args, |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1660 | typename... UnwrappedTypes, |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1661 | typename... Params> |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1662 | struct 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 Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1674 | }; |
| 1675 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1676 | // Core implementation of `Bind()`, which checks common preconditions before |
| 1677 | // returning an appropriate callback. |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1678 | template <template <typename> class CallbackT> |
| 1679 | struct BindHelper { |
| 1680 | private: |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1681 | static constexpr bool kIsOnce = |
| Daniel Cheng | fb999fd3 | 2025-06-13 02:30:16 | [diff] [blame] | 1682 | is_instantiation<CallbackT<void()>, OnceCallback>; |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1683 | |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1684 | template <typename Traits, bool v = IsComplete<Traits>> |
| 1685 | struct TraitsAreInstantiable { |
| 1686 | static constexpr bool value = [] { |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 1687 | 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 Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1691 | return v; |
| 1692 | }(); |
| 1693 | }; |
| 1694 | |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1695 | template <typename Functor, |
| Daniel Cheng | fb999fd3 | 2025-06-13 02:30:16 | [diff] [blame] | 1696 | bool v = !is_instantiation<std::decay_t<Functor>, OnceCallback> || |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1697 | (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 Cheng | fb999fd3 | 2025-06-13 02:30:16 | [diff] [blame] | 1719 | (... && !is_instantiation<std::decay_t<Args>, PassedWrapper>); |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1720 | static_assert( |
| 1721 | v, |
| 1722 | "Use std::move() instead of base::Passed() with base::BindOnce()."); |
| 1723 | return v; |
| 1724 | }(); |
| 1725 | }; |
| 1726 | |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1727 | template < |
| 1728 | typename Functor, |
| 1729 | bool v = |
| Daniel Cheng | fb999fd3 | 2025-06-13 02:30:16 | [diff] [blame] | 1730 | !is_instantiation<std::remove_cvref_t<Functor>, FunctionRef> && |
| 1731 | !is_instantiation<std::remove_cvref_t<Functor>, absl::FunctionRef>> |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1732 | struct NotFunctionRef { |
| 1733 | static constexpr bool value = [] { |
| Peter Kasting | 36d52e6 | 2023-12-11 21:29:33 | [diff] [blame] | 1734 | 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 Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1738 | return v; |
| 1739 | }(); |
| 1740 | }; |
| Peter Kasting | f871b5e | 2023-11-17 00:29:27 | [diff] [blame] | 1741 | |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1742 | template <typename Traits, bool v = Traits::is_stateless> |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1743 | struct IsStateless { |
| 1744 | static constexpr bool value = [] { |
| Peter Kasting | 36d52e6 | 2023-12-11 21:29:33 | [diff] [blame] | 1745 | 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 Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1749 | return v; |
| 1750 | }(); |
| 1751 | }; |
| Peter Kasting | 3b01151f | 2023-11-27 20:04:21 | [diff] [blame] | 1752 | |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1753 | template <typename Functor, typename... Args> |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1754 | static auto BindImpl(Functor&& functor, Args&&... args) { |
| Peter Kasting | 26dc061 | 2023-12-13 19:16:48 | [diff] [blame] | 1755 | // 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 Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1773 | // using Traits = struct { |
| Peter Kasting | 26dc061 | 2023-12-13 19:16:48 | [diff] [blame] | 1774 | // 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 Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 1781 | // using ValidatedUnwrappedTypes = struct { |
| Peter Kasting | 26dc061 | 2023-12-13 19:16:48 | [diff] [blame] | 1782 | // 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 Kasting | 12f9269 | 2023-12-19 04:18:47 | [diff] [blame] | 1788 | // using ValidatedBindState = struct { |
| Peter Kasting | 26dc061 | 2023-12-13 19:16:48 | [diff] [blame] | 1789 | // 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 Kasting | f818a6f | 2024-02-21 18:21:28 | [diff] [blame] | 1800 | using Traits = FunctorTraits<TransformToUnwrappedType<kIsOnce, Functor&&>, |
| 1801 | TransformToUnwrappedType<kIsOnce, Args&&>...>; |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1802 | 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 Kasting | 26dc061 | 2023-12-13 19:16:48 | [diff] [blame] | 1829 | |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1830 | // 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 Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1845 | } |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1846 | } |
| Peter Kasting | 736faa72 | 2022-01-12 18:48:45 | [diff] [blame] | 1847 | } |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1848 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1849 | // Special cases for binding to a `Callback` without extra bound arguments. |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1850 | |
| 1851 | // `OnceCallback` passed to `OnceCallback`, or `RepeatingCallback` passed to |
| 1852 | // `RepeatingCallback`. |
| 1853 | template <typename T> |
| Daniel Cheng | fb999fd3 | 2025-06-13 02:30:16 | [diff] [blame] | 1854 | requires is_instantiation<T, CallbackT> |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1855 | static T BindImpl(T callback) { |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1856 | // Guard against null pointers accidentally ending up in posted tasks, |
| 1857 | // causing hard-to-debug crashes. |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1858 | CHECK(callback); |
| 1859 | return callback; |
| 1860 | } |
| Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 1861 | |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1862 | // `RepeatingCallback` passed to `OnceCallback`. The opposite direction is |
| 1863 | // intentionally not supported. |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1864 | template <typename Signature> |
| Daniel Cheng | fb999fd3 | 2025-06-13 02:30:16 | [diff] [blame] | 1865 | requires is_instantiation<OnceCallback<Signature>, CallbackT> |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1866 | static OnceCallback<Signature> BindImpl( |
| 1867 | RepeatingCallback<Signature> callback) { |
| 1868 | return BindImpl(OnceCallback<Signature>(callback)); |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1869 | } |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 1870 | |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1871 | // Must be defined after `BindImpl()` since it refers to it. |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 1872 | template <typename Functor, typename... Args> |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1873 | struct BindImplWouldSucceed { |
| Peter Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 1874 | // Can't use a defaulted template param since it can't come after `Args`. |
| 1875 | // |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1876 | // 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 Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 1881 | static constexpr bool value = |
| 1882 | !std::same_as<void, |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1883 | decltype(BindImpl(std::declval<Functor&&>(), |
| 1884 | std::declval<Args&&>()...))>; |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1885 | }; |
| 1886 | |
| 1887 | public: |
| Peter Kasting | 7824b6f | 2023-12-22 03:11:12 | [diff] [blame] | 1888 | 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 Kasting | a7e941e | 2023-12-20 01:13:49 | [diff] [blame] | 1896 | } else { |
| 1897 | return BindFailedCheckPreviousErrors(); |
| Peter Kasting | 2ab0f6a | 2023-11-30 23:27:37 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | }; |
| Daniel Cheng | 58ea45b | 2021-09-17 22:55:08 | [diff] [blame] | 1901 | |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 1902 | } // namespace internal |
| tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 1903 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1904 | // 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 Williams | bfae87e | 2024-09-18 13:06:51 | [diff] [blame] | 1908 | // struct S { |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1909 | // void f() {} |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1910 | // }; |
| 1911 | // |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1912 | // WeakPtr<S> weak_s = nullptr; |
| 1913 | // BindOnce(&S::f, weak_s).Run(); // `S::f()` is not called. |
| 1914 | // ``` |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1915 | template <typename T> |
| Daniel Cheng | fb999fd3 | 2025-06-13 02:30:16 | [diff] [blame] | 1916 | struct IsWeakReceiver : std::bool_constant<is_instantiation<T, WeakPtr>> {}; |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1917 | |
| 1918 | template <typename T> |
| jdoerrie | c79c2fa2 | 2019-02-26 12:35:03 | [diff] [blame] | 1919 | struct IsWeakReceiver<std::reference_wrapper<T>> : IsWeakReceiver<T> {}; |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1920 | |
| Michael Lippautz | bcfc202 | 2021-01-21 17:18:57 | [diff] [blame] | 1921 | // An injection point to control how objects are checked for maybe validity, |
| 1922 | // which is an optimistic thread-safe check for full validity. |
| 1923 | template <typename> |
| 1924 | struct MaybeValidTraits { |
| 1925 | template <typename T> |
| 1926 | static bool MaybeValid(const T& o) { |
| 1927 | return o.MaybeValid(); |
| 1928 | } |
| 1929 | }; |
| 1930 | |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1931 | // An injection point to control how bound objects passed to the target |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1932 | // function. `BindUnwrapTraits<>::Unwrap()` is called for each bound object |
| 1933 | // right before the target function is invoked. |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1934 | template <typename> |
| 1935 | struct BindUnwrapTraits { |
| 1936 | template <typename T> |
| 1937 | static T&& Unwrap(T&& o) { |
| 1938 | return std::forward<T>(o); |
| 1939 | } |
| 1940 | }; |
| 1941 | |
| Daniel Cheng | add3e4e | 2021-10-14 05:04:12 | [diff] [blame] | 1942 | template <typename T> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1943 | requires internal::kIsUnretainedWrapper<internal::UnretainedWrapper, T> || |
| 1944 | internal::kIsUnretainedWrapper<internal::UnretainedRefWrapper, T> || |
| Daniel Cheng | fb999fd3 | 2025-06-13 02:30:16 | [diff] [blame] | 1945 | is_instantiation<T, internal::RetainedRefWrapper> || |
| 1946 | is_instantiation<T, internal::OwnedWrapper> || |
| 1947 | is_instantiation<T, internal::OwnedRefWrapper> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1948 | struct BindUnwrapTraits<T> { |
| 1949 | static decltype(auto) Unwrap(const T& o) { return o.get(); } |
| Jan Wilken Dörrie | 1d011fb | 2021-03-11 19:37:37 | [diff] [blame] | 1950 | }; |
| 1951 | |
| 1952 | template <typename T> |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1953 | struct BindUnwrapTraits<internal::PassedWrapper<T>> { |
| 1954 | static T Unwrap(const internal::PassedWrapper<T>& o) { return o.Take(); } |
| 1955 | }; |
| 1956 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 1957 | #if BUILDFLAG(IS_WIN) |
| tzik | c44f810 | 2018-07-24 09:49:19 | [diff] [blame] | 1958 | template <typename T> |
| 1959 | struct BindUnwrapTraits<Microsoft::WRL::ComPtr<T>> { |
| 1960 | static T* Unwrap(const Microsoft::WRL::ComPtr<T>& ptr) { return ptr.Get(); } |
| 1961 | }; |
| 1962 | #endif |
| 1963 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1964 | // `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 Cheng | ef1375f2 | 2023-11-15 00:06:12 | [diff] [blame] | 1970 | template <typename Functor, typename BoundArgsTuple> |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1971 | struct CallbackCancellationTraits { |
| 1972 | static constexpr bool is_cancellable = false; |
| 1973 | }; |
| 1974 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1975 | // Specialization for a weak receiver. |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1976 | template <typename Functor, typename... BoundArgs> |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1977 | requires internal::kIsWeakMethod< |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1978 | internal::FunctorTraits<Functor, BoundArgs...>::is_method, |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1979 | BoundArgs...> |
| Daniel Cheng | ef1375f2 | 2023-11-15 00:06:12 | [diff] [blame] | 1980 | struct CallbackCancellationTraits<Functor, std::tuple<BoundArgs...>> { |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1981 | 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-payeur | 40f8e9a | 2018-07-30 16:26:48 | [diff] [blame] | 1989 | |
| 1990 | template <typename Receiver, typename... Args> |
| 1991 | static bool MaybeValid(const Functor&, |
| 1992 | const Receiver& receiver, |
| 1993 | const Args&...) { |
| Michael Lippautz | bcfc202 | 2021-01-21 17:18:57 | [diff] [blame] | 1994 | return MaybeValidTraits<Receiver>::MaybeValid(receiver); |
| Nicolas Ouellet-payeur | 40f8e9a | 2018-07-30 16:26:48 | [diff] [blame] | 1995 | } |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1996 | }; |
| 1997 | |
| Peter Kasting | 7419b0be | 2023-12-19 05:31:19 | [diff] [blame] | 1998 | // Specialization for a nested `Bind()`. |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 1999 | template <typename Functor, typename... BoundArgs> |
| Daniel Cheng | fb999fd3 | 2025-06-13 02:30:16 | [diff] [blame] | 2000 | requires is_instantiation<Functor, OnceCallback> || |
| 2001 | is_instantiation<Functor, RepeatingCallback> |
| Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 2002 | struct CallbackCancellationTraits<Functor, std::tuple<BoundArgs...>> { |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 2003 | static constexpr bool is_cancellable = true; |
| 2004 | |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 2005 | static bool IsCancelled(const Functor& functor, const BoundArgs&...) { |
| 2006 | return functor.IsCancelled(); |
| 2007 | } |
| Nicolas Ouellet-payeur | 40f8e9a | 2018-07-30 16:26:48 | [diff] [blame] | 2008 | |
| Nicolas Ouellet-payeur | 40f8e9a | 2018-07-30 16:26:48 | [diff] [blame] | 2009 | static bool MaybeValid(const Functor& functor, const BoundArgs&...) { |
| Michael Lippautz | bcfc202 | 2021-01-21 17:18:57 | [diff] [blame] | 2010 | return MaybeValidTraits<Functor>::MaybeValid(functor); |
| Nicolas Ouellet-payeur | 40f8e9a | 2018-07-30 16:26:48 | [diff] [blame] | 2011 | } |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 2012 | }; |
| 2013 | |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 2014 | } // namespace base |
| 2015 | |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 2016 | #endif // BASE_FUNCTIONAL_BIND_INTERNAL_H_ |