#ifndef BASE_FUNCTIONAL_BIND_INTERNAL_H_
#define BASE_FUNCTIONAL_BIND_INTERNAL_H_
#include <stddef.h>
#include <concepts>
#include <functional>
#include <memory>
#include <tuple>
#include <type_traits>
#include <utility>
#include "base/check.h"
#include "base/compiler_specific.h"
#include "base/functional/callback_internal.h"
#include "base/functional/unretained_traits.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/memory/weak_ptr.h"
#include "base/types/is_complete.h"
#include "base/types/is_instantiation.h"
#include "base/types/to_address.h"
#include "build/build_config.h"
#include "third_party/abseil-cpp/absl/functional/function_ref.h"
#if PA_BUILDFLAG(USE_ASAN_BACKUP_REF_PTR)
#include "base/memory/raw_ptr_asan_bound_arg_tracker.h"
#endif
#if BUILDFLAG(IS_WIN)
namespace Microsoft {
namespace WRL {
template <typename>
class ComPtr;
}
}
#endif
namespace base {
template <typename T>
struct IsWeakReceiver;
template <typename>
struct BindUnwrapTraits;
template <typename Functor, typename BoundArgsTuple>
struct CallbackCancellationTraits;
template <typename Signature>
class FunctionRef;
struct BindFailedCheckPreviousErrors {};
namespace unretained_traits {
struct MayNotDangle {};
struct MayDangle {};
struct MayDangleUntriaged {};
}
namespace internal {
template <typename T,
typename UnretainedTrait,
RawPtrTraits PtrTraits = RawPtrTraits::kEmpty>
class UnretainedWrapper {
using DanglingRawPtrType = MayBeDangling<T, PtrTraits>;
public:
using GetPtrType = std::conditional_t<
raw_ptr_traits::IsSupportedType<T>::value &&
std::same_as<UnretainedTrait, unretained_traits::MayDangle>,
DanglingRawPtrType,
T*>;
explicit UnretainedWrapper(T* o)
requires(PtrTraits == RawPtrTraits::kEmpty)
: ptr_(o) {
VerifyPreconditions();
}
explicit UnretainedWrapper(const raw_ptr<T, PtrTraits>& o)
requires(raw_ptr_traits::IsSupportedType<T>::value)
: ptr_(o) {
VerifyPreconditions();
}
explicit UnretainedWrapper(raw_ptr<T, PtrTraits>&& o)
requires(raw_ptr_traits::IsSupportedType<T>::value)
: ptr_(std::move(o)) {
VerifyPreconditions();
}
GetPtrType get() const { return GetInternal(ptr_); }
static constexpr bool value = SupportsUnretained<T>;
private:
template <typename U>
requires std::same_as<T, U>
static GetPtrType GetInternal(U* ptr) {
return ptr;
}
template <typename U, RawPtrTraits Traits>
requires std::same_as<T, U>
static GetPtrType GetInternal(const raw_ptr<U, Traits>& ptr) {
if constexpr (std::same_as<UnretainedTrait,
unretained_traits::MayNotDangle>) {
ptr.ReportIfDangling();
}
return ptr;
}
using StorageType =
std::conditional_t<raw_ptr_traits::IsSupportedType<T>::value,
DanglingRawPtrType,
T*>;
static_assert(std::is_pointer_v<GetPtrType> ||
std::same_as<GetPtrType, StorageType>);
static constexpr void VerifyPreconditions() {
std::ignore = value;
}
StorageType ptr_;
};
template <typename T,
typename UnretainedTrait,
RawPtrTraits PtrTraits = RawPtrTraits::kEmpty>
class UnretainedRefWrapper {
public:
explicit UnretainedRefWrapper(T& o)
requires(PtrTraits == RawPtrTraits::kEmpty)
: ref_(o) {
VerifyPreconditions();
}
explicit UnretainedRefWrapper(const raw_ref<T, PtrTraits>& o)
requires(raw_ptr_traits::IsSupportedType<T>::value)
: ref_(o) {
VerifyPreconditions();
}
explicit UnretainedRefWrapper(raw_ref<T, PtrTraits>&& o)
requires(raw_ptr_traits::IsSupportedType<T>::value)
: ref_(std::move(o)) {
VerifyPreconditions();
}
T& get() const { return GetInternal(ref_); }
static constexpr bool value = SupportsUnretained<T>;
private:
template <typename U>
requires std::same_as<T, U>
static T& GetInternal(U& ref) {
return ref;
}
template <typename U, RawPtrTraits Traits>
requires std::same_as<T, U>
static T& GetInternal(const raw_ref<U, Traits>& ref) {
if constexpr (std::is_same_v<UnretainedTrait,
unretained_traits::MayNotDangle>) {
ref.ReportIfDangling();
}
return ref.get();
}
using StorageType =
std::conditional_t<raw_ptr_traits::IsSupportedType<T>::value,
raw_ref<T, DisableDanglingPtrDetection>,
T&>;
static constexpr void VerifyPreconditions() { std::ignore = value; }
StorageType ref_;
};
template <template <typename, typename, RawPtrTraits> typename WrapperT,
typename T>
inline constexpr bool kIsUnretainedWrapper = false;
template <template <typename, typename, RawPtrTraits> typename WrapperT,
typename T,
typename UnretainedTrait,
RawPtrTraits PtrTraits>
inline constexpr bool
kIsUnretainedWrapper<WrapperT, WrapperT<T, UnretainedTrait, PtrTraits>> =
true;
template <typename T, typename UnretainedTrait, RawPtrTraits PtrTraits>
class UnretainedRefWrapperReceiver {
public:
UnretainedRefWrapperReceiver(
UnretainedRefWrapper<T, UnretainedTrait, PtrTraits>&& obj)
: obj_(std::move(obj)) {}
T& operator*() const { return obj_.get(); }
T* operator->() const { return &obj_.get(); }
private:
UnretainedRefWrapper<T, UnretainedTrait, PtrTraits> obj_;
};
template <typename T>
struct MethodReceiverStorage {
using Type = std::
conditional_t<IsPointerOrRawPtr<T>, scoped_refptr<RemovePointerT<T>>, T>;
};
template <typename T, typename UnretainedTrait, RawPtrTraits PtrTraits>
struct MethodReceiverStorage<
UnretainedRefWrapper<T, UnretainedTrait, PtrTraits>> {
using Type = UnretainedRefWrapperReceiver<T, UnretainedTrait, PtrTraits>;
};
template <typename T>
class RetainedRefWrapper {
public:
explicit RetainedRefWrapper(T* o) : ptr_(o) {}
explicit RetainedRefWrapper(scoped_refptr<T> o) : ptr_(std::move(o)) {}
T* get() const { return ptr_.get(); }
private:
scoped_refptr<T> ptr_;
};
template <typename T>
struct IgnoreResultHelper {
explicit IgnoreResultHelper(T functor) : functor_(std::move(functor)) {}
explicit operator bool() const { return !!functor_; }
T functor_;
};
template <typename T, typename Deleter = std::default_delete<T>>
class OwnedWrapper {
public:
explicit OwnedWrapper(T* o) : ptr_(o) {}
explicit OwnedWrapper(std::unique_ptr<T, Deleter>&& ptr)
: ptr_(std::move(ptr)) {}
T* get() const { return ptr_.get(); }
private:
std::unique_ptr<T, Deleter> ptr_;
};
template <typename T>
class OwnedRefWrapper {
public:
explicit OwnedRefWrapper(const T& t) : t_(t) {}
explicit OwnedRefWrapper(T&& t) : t_(std::move(t)) {}
T& get() const { return t_; }
private:
mutable T t_;
};
template <typename T>
class PassedWrapper {
public:
explicit PassedWrapper(T&& scoper) : scoper_(std::move(scoper)) {}
PassedWrapper(PassedWrapper&& other)
: is_valid_(other.is_valid_), scoper_(std::move(other.scoper_)) {}
T Take() const {
CHECK(is_valid_);
is_valid_ = false;
return std::move(scoper_);
}
private:
mutable bool is_valid_ = true;
mutable T scoper_;
};
template <typename T>
using Unwrapper = BindUnwrapTraits<std::decay_t<T>>;
template <typename T>
decltype(auto) Unwrap(T&& o) {
return Unwrapper<T>::Unwrap(std::forward<T>(o));
}
template <bool is_method, typename... Args>
inline constexpr bool kIsWeakMethod = false;
template <typename T, typename... Args>
inline constexpr bool kIsWeakMethod<true, T, Args...> =
IsWeakReceiver<T>::value;
template <typename... Types>
struct TypeList {};
template <size_t n, typename List>
requires is_instantiation<List, TypeList>
struct DropTypeListItemImpl {
using Type = List;
};
template <size_t n, typename T, typename... List>
requires(n > 0)
struct DropTypeListItemImpl<n, TypeList<T, List...>>
: DropTypeListItemImpl<n - 1, TypeList<List...>> {};
template <size_t n, typename List>
using DropTypeListItem = typename DropTypeListItemImpl<n, List>::Type;
template <size_t n, typename List, typename... Accum>
requires is_instantiation<List, TypeList>
struct TakeTypeListItemImpl {
using Type = TypeList<Accum...>;
};
template <size_t n, typename T, typename... List, typename... Accum>
requires(n > 0)
struct TakeTypeListItemImpl<n, TypeList<T, List...>, Accum...>
: TakeTypeListItemImpl<n - 1, TypeList<List...>, Accum..., T> {};
template <size_t n, typename List>
using TakeTypeListItem = typename TakeTypeListItemImpl<n, List>::Type;
template <typename R, typename ArgList>
struct MakeFunctionTypeImpl;
template <typename R, typename... Args>
struct MakeFunctionTypeImpl<R, TypeList<Args...>> {
using Type = R(Args...);
};
template <typename R, typename ArgList>
using MakeFunctionType = typename MakeFunctionTypeImpl<R, ArgList>::Type;
template <typename Signature>
struct ExtractArgsImpl;
template <typename R, typename... Args>
struct ExtractArgsImpl<R(Args...)> {
using ReturnType = R;
using ArgsList = TypeList<Args...>;
};
template <typename Signature>
using ExtractArgs = typename ExtractArgsImpl<Signature>::ArgsList;
template <typename Signature>
using ExtractReturnType = typename ExtractArgsImpl<Signature>::ReturnType;
template <typename Callable,
typename Signature = decltype(&Callable::operator())>
struct ExtractCallableRunTypeImpl;
#define BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS(quals) \
template <typename Callable, typename R, typename... Args> \
struct ExtractCallableRunTypeImpl<Callable, \
R (Callable::*)(Args...) quals> { \
using Type = R(Args...); \
}
BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS();
BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS(const);
BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS(noexcept);
BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS(const noexcept);
#undef BIND_INTERNAL_EXTRACT_CALLABLE_RUN_TYPE_WITH_QUALS
template <typename Callable>
using ExtractCallableRunType =
typename ExtractCallableRunTypeImpl<Callable>::Type;
template <typename Functor>
concept HasNonOverloadedCallOp = requires { &Functor::operator(); };
template <typename T>
inline constexpr bool IsObjCArcBlockPointer = false;
#if __OBJC__ && HAS_FEATURE(objc_arc)
template <typename R, typename... Args>
inline constexpr bool IsObjCArcBlockPointer<R (^)(Args...)> = true;
#endif
template <typename Functor, typename... BoundArgs>
concept HasOverloadedCallOp = requires {
requires requires(Functor&& f, BoundArgs&&... args) {
std::forward<Functor>(f)(std::forward<BoundArgs>(args)...);
};
requires !HasNonOverloadedCallOp<std::decay_t<Functor>>;
requires !std::is_pointer_v<std::decay_t<Functor>>;
requires !IsObjCArcBlockPointer<std::decay_t<Functor>>;
};
template <typename Sig>
struct ForceVoidReturn;
template <typename R, typename... Args>
struct ForceVoidReturn<R(Args...)> {
using RunType = void(Args...);
};
template <typename Functor, typename... BoundArgs>
struct FunctorTraits;
template <typename Functor, typename... BoundArgs>
struct DecayedFunctorTraits;
template <typename Functor, typename... BoundArgs>
requires HasNonOverloadedCallOp<Functor>
struct DecayedFunctorTraits<Functor, BoundArgs...> {
using RunType = ExtractCallableRunType<Functor>;
static constexpr bool is_method = false;
static constexpr bool is_nullable = false;
static constexpr bool is_callback = false;
static constexpr bool is_stateless = std::is_empty_v<Functor>;
template <typename RunFunctor, typename... RunArgs>
static ExtractReturnType<RunType> Invoke(RunFunctor&& functor,
RunArgs&&... args) {
return std::forward<RunFunctor>(functor)(std::forward<RunArgs>(args)...);
}
};
template <typename R, typename... Args, typename... BoundArgs>
struct DecayedFunctorTraits<R (*)(Args...), BoundArgs...> {
using RunType = R(Args...);
static constexpr bool is_method = false;
static constexpr bool is_nullable = true;
static constexpr bool is_callback = false;
static constexpr bool is_stateless = true;
template <typename Function, typename... RunArgs>
static R Invoke(Function&& function, RunArgs&&... args) {
return std::forward<Function>(function)(std::forward<RunArgs>(args)...);
}
};
template <typename R, typename... Args, typename... BoundArgs>
struct DecayedFunctorTraits<R (*)(Args...) noexcept, BoundArgs...>
: DecayedFunctorTraits<R (*)(Args...), BoundArgs...> {};
#if BUILDFLAG(IS_WIN) && !defined(ARCH_CPU_64_BITS)
#define BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS(conv, quals) \
template <typename R, typename... Args, typename... BoundArgs> \
struct DecayedFunctorTraits<R(conv*)(Args...) quals, BoundArgs...> \
: DecayedFunctorTraits<R (*)(Args...) quals, BoundArgs...> {}
BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS(__stdcall, );
BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS(__stdcall, noexcept);
BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS(__fastcall, );
BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS(__fastcall, noexcept);
#undef BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONV_AND_QUALS
#endif
#if __OBJC__ && HAS_FEATURE(objc_arc)
template <typename R, typename... Args, typename... BoundArgs>
struct DecayedFunctorTraits<R (^)(Args...), BoundArgs...> {
using RunType = R(Args...);
static constexpr bool is_method = false;
static constexpr bool is_nullable = true;
static constexpr bool is_callback = false;
static constexpr bool is_stateless = true;
template <typename BlockType, typename... RunArgs>
static R Invoke(BlockType&& block, RunArgs&&... args) {
__attribute__((objc_precise_lifetime)) R (^scoped_block)(Args...) = block;
return scoped_block(std::forward<RunArgs>(args)...);
}
};
#endif
template <typename R,
typename Receiver,
typename... Args,
typename... BoundArgs>
struct DecayedFunctorTraits<R (Receiver::*)(Args...), BoundArgs...> {
using RunType = R(Receiver*, Args...);
static constexpr bool is_method = true;
static constexpr bool is_nullable = true;
static constexpr bool is_callback = false;
static constexpr bool is_stateless = true;
template <typename Method, typename ReceiverPtr, typename... RunArgs>
static R Invoke(Method method,
ReceiverPtr&& receiver_ptr,
RunArgs&&... args) {
return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...);
}
};
template <typename R,
typename Receiver,
typename... Args,
typename... BoundArgs>
struct DecayedFunctorTraits<R (Receiver::*)(Args...) const, BoundArgs...>
: DecayedFunctorTraits<R (Receiver::*)(Args...), BoundArgs...> {
using RunType = R(const Receiver*, Args...);
};
#define BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONST_AND_QUALS(constqual, \
quals) \
template <typename R, typename Receiver, typename... Args, \
typename... BoundArgs> \
struct DecayedFunctorTraits<R (Receiver::*)(Args...) constqual quals, \
BoundArgs...> \
: DecayedFunctorTraits<R (Receiver::*)(Args...) constqual, \
BoundArgs...> {}
BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONST_AND_QUALS(, noexcept);
BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONST_AND_QUALS(const, noexcept);
#undef BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_WITH_CONST_AND_QUALS
#if BUILDFLAG(IS_WIN) && !defined(ARCH_CPU_64_BITS)
#define BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS(quals) \
template <typename R, typename Receiver, typename... Args, \
typename... BoundArgs> \
struct DecayedFunctorTraits<R (__stdcall Receiver::*)(Args...) quals, \
BoundArgs...> \
: public DecayedFunctorTraits<R (Receiver::*)(Args...) quals, \
BoundArgs...> {}
BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS();
BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS(const);
BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS(noexcept);
BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS(const noexcept);
#undef BIND_INTERNAL_DECAYED_FUNCTOR_TRAITS_STDCALL_WITH_QUALS
#endif
template <typename T, typename... BoundArgs>
struct DecayedFunctorTraits<IgnoreResultHelper<T>, BoundArgs...>
: FunctorTraits<T, BoundArgs...> {
using RunType = typename ForceVoidReturn<
typename FunctorTraits<T, BoundArgs...>::RunType>::RunType;
template <typename IgnoreResultType, typename... RunArgs>
static void Invoke(IgnoreResultType&& ignore_result_helper,
RunArgs&&... args) {
FunctorTraits<T, BoundArgs...>::Invoke(
std::forward<IgnoreResultType>(ignore_result_helper).functor_,
std::forward<RunArgs>(args)...);
}
};
template <typename R, typename... Args, typename... BoundArgs>
struct DecayedFunctorTraits<OnceCallback<R(Args...)>, BoundArgs...> {
using RunType = R(Args...);
static constexpr bool is_method = false;
static constexpr bool is_nullable = true;
static constexpr bool is_callback = true;
static constexpr bool is_stateless = true;
template <typename CallbackType, typename... RunArgs>
static R Invoke(CallbackType&& callback, RunArgs&&... args) {
DCHECK(!callback.is_null());
return std::forward<CallbackType>(callback).Run(
std::forward<RunArgs>(args)...);
}
};
template <typename R, typename... Args, typename... BoundArgs>
struct DecayedFunctorTraits<RepeatingCallback<R(Args...)>, BoundArgs...> {
using RunType = R(Args...);
static constexpr bool is_method = false;
static constexpr bool is_nullable = true;
static constexpr bool is_callback = true;
static constexpr bool is_stateless = true;
template <typename CallbackType, typename... RunArgs>
static R Invoke(CallbackType&& callback, RunArgs&&... args) {
DCHECK(!callback.is_null());
return std::forward<CallbackType>(callback).Run(
std::forward<RunArgs>(args)...);
}
};
template <typename Functor, typename... BoundArgs>
requires IsComplete<DecayedFunctorTraits<std::decay_t<Functor>, BoundArgs...>>
struct FunctorTraits<Functor, BoundArgs...>
: DecayedFunctorTraits<std::decay_t<Functor>, BoundArgs...> {};
template <typename Functor, typename... BoundArgs>
requires HasOverloadedCallOp<Functor, BoundArgs...>
struct FunctorTraits<Functor, BoundArgs...> {
using RunType = decltype(std::declval<Functor>()(
std::declval<BoundArgs>()...))(std::decay_t<BoundArgs>...);
static constexpr bool is_method = false;
static constexpr bool is_nullable = false;
static constexpr bool is_callback = false;
static constexpr bool is_stateless = std::is_empty_v<std::decay_t<Functor>>;
template <typename RunFunctor, typename... RunArgs>
static ExtractReturnType<RunType> Invoke(RunFunctor&& functor,
RunArgs&&... args) {
return std::forward<RunFunctor>(functor)(std::forward<RunArgs>(args)...);
}
};
template <typename T>
struct StorageTraits {
using Type = T;
static constexpr bool value = true;
};
template <typename T>
struct StorageTraits<T*> {
using Type = UnretainedWrapper<T, unretained_traits::MayNotDangle>;
static constexpr bool value = Type::value;
};
template <typename T, RawPtrTraits PtrTraits>
struct StorageTraits<raw_ptr<T, PtrTraits>> {
using Type = UnretainedWrapper<T, unretained_traits::MayNotDangle, PtrTraits>;
static constexpr bool value = Type::value;
};
template <typename T>
struct StorageTraits<std::reference_wrapper<T>> {
using Type = UnretainedRefWrapper<T, unretained_traits::MayNotDangle>;
static constexpr bool value = Type::value;
};
template <typename T>
using ValidateStorageTraits = StorageTraits<std::decay_t<T>>;
template <bool is_weak_call,
typename Traits,
typename ReturnType,
size_t... indices>
struct InvokeHelper;
template <typename Traits, typename ReturnType, size_t... indices>
struct InvokeHelper<false, Traits, ReturnType, indices...> {
template <typename Functor, typename BoundArgsTuple, typename... RunArgs>
static inline ReturnType MakeItSo(Functor&& functor,
BoundArgsTuple&& bound,
RunArgs&&... args) {
return Traits::Invoke(
Unwrap(std::forward<Functor>(functor)),
Unwrap(std::get<indices>(std::forward<BoundArgsTuple>(bound)))...,
std::forward<RunArgs>(args)...);
}
};
template <typename Traits,
typename ReturnType,
size_t index_target,
size_t... index_tail>
struct InvokeHelper<true, Traits, ReturnType, index_target, index_tail...> {
template <typename Functor, typename BoundArgsTuple, typename... RunArgs>
static inline void MakeItSo(Functor&& functor,
BoundArgsTuple&& bound,
RunArgs&&... args) {
static_assert(index_target == 0);
const auto& target = Unwrap(std::get<0>(bound));
if (!target) {
return;
}
Traits::Invoke(
Unwrap(std::forward<Functor>(functor)), target,
Unwrap(std::get<index_tail>(std::forward<BoundArgsTuple>(bound)))...,
std::forward<RunArgs>(args)...);
}
};
template <typename Traits, typename StorageType, typename UnboundRunType>
struct Invoker;
template <typename Traits,
typename StorageType,
typename R,
typename... UnboundArgs>
struct Invoker<Traits, StorageType, R(UnboundArgs...)> {
private:
using Indices = std::make_index_sequence<
std::tuple_size_v<decltype(StorageType::bound_args_)>>;
public:
static R RunOnce(BindStateBase* base,
PassingType<UnboundArgs>... unbound_args) {
auto* const storage = static_cast<StorageType*>(base);
return RunImpl(std::move(storage->functor_),
std::move(storage->bound_args_), Indices(),
std::forward<UnboundArgs>(unbound_args)...);
}
static R Run(BindStateBase* base, PassingType<UnboundArgs>... unbound_args) {
auto* const storage = static_cast<const StorageType*>(base);
return RunImpl(storage->functor_, storage->bound_args_, Indices(),
std::forward<UnboundArgs>(unbound_args)...);
}
private:
template <bool is_weak_call, bool v = !is_weak_call || std::is_void_v<R>>
struct WeakCallReturnsVoid {
static constexpr bool value = [] {
static_assert(v,
"WeakPtrs can only bind to methods without return values.");
return v;
}();
};
template <typename Functor, typename BoundArgsTuple, size_t... indices>
static inline R RunImpl(Functor&& functor,
BoundArgsTuple&& bound,
std::index_sequence<indices...>,
UnboundArgs&&... unbound_args) {
#if PA_BUILDFLAG(USE_ASAN_BACKUP_REF_PTR)
RawPtrAsanBoundArgTracker raw_ptr_asan_bound_arg_tracker;
raw_ptr_asan_bound_arg_tracker.AddArgs(
std::get<indices>(std::forward<BoundArgsTuple>(bound))...,
std::forward<UnboundArgs>(unbound_args)...);
#endif
using DecayedArgsTuple = std::decay_t<BoundArgsTuple>;
static constexpr bool kIsWeakCall =
kIsWeakMethod<Traits::is_method,
std::tuple_element_t<indices, DecayedArgsTuple>...>;
if constexpr (WeakCallReturnsVoid<kIsWeakCall>::value) {
return InvokeHelper<kIsWeakCall, Traits, R, indices...>::MakeItSo(
std::forward<Functor>(functor), std::forward<BoundArgsTuple>(bound),
std::forward<UnboundArgs>(unbound_args)...);
}
}
};
template <typename... Unused>
void VerifyMethodReceiver(Unused&&...) {}
template <typename Receiver, typename... Unused>
void VerifyMethodReceiver(Receiver&& receiver, Unused&&...) {
if constexpr (IsPointerOrRawPtr<std::decay_t<Receiver>> &&
IsRefCountedType<RemovePointerT<std::decay_t<Receiver>>>) {
DCHECK(receiver);
DCHECK(receiver->HasAtLeastOneRef());
}
}
template <bool is_method,
bool is_nullable,
bool is_callback,
typename Functor,
typename... BoundArgs>
struct BindState final : BindStateBase {
private:
using BoundArgsTuple = std::tuple<BoundArgs...>;
public:
template <typename ForwardFunctor, typename... ForwardBoundArgs>
static BindState* Create(BindStateBase::InvokeFuncStorage invoke_func,
ForwardFunctor&& functor,
ForwardBoundArgs&&... bound_args) {
if constexpr (is_method) {
VerifyMethodReceiver(bound_args...);
}
return new BindState(invoke_func, std::forward<ForwardFunctor>(functor),
std::forward<ForwardBoundArgs>(bound_args)...);
}
Functor functor_;
BoundArgsTuple bound_args_;
private:
using CancellationTraits =
CallbackCancellationTraits<Functor, BoundArgsTuple>;
template <typename ForwardFunctor, typename... ForwardBoundArgs>
requires CancellationTraits::is_cancellable
BindState(BindStateBase::InvokeFuncStorage invoke_func,
ForwardFunctor&& functor,
ForwardBoundArgs&&... bound_args)
: BindStateBase(invoke_func, &Destroy, &QueryCancellationTraits),
functor_(std::forward<ForwardFunctor>(functor)),
bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) {
CheckFunctorIsNonNull();
}
template <typename ForwardFunctor, typename... ForwardBoundArgs>
requires(!CancellationTraits::is_cancellable)
BindState(BindStateBase::InvokeFuncStorage invoke_func,
ForwardFunctor&& functor,
ForwardBoundArgs&&... bound_args)
: BindStateBase(invoke_func, &Destroy),
functor_(std::forward<ForwardFunctor>(functor)),
bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) {
CheckFunctorIsNonNull();
}
~BindState() = default;
static bool QueryCancellationTraits(
const BindStateBase* base,
BindStateBase::CancellationQueryMode mode) {
auto* const storage = static_cast<const BindState*>(base);
static constexpr std::make_index_sequence<sizeof...(BoundArgs)> kIndices;
return (mode == BindStateBase::CancellationQueryMode::kIsCancelled)
? storage->IsCancelled(kIndices)
: storage->MaybeValid(kIndices);
}
static void Destroy(const BindStateBase* self) {
delete static_cast<const BindState*>(self);
}
template <size_t... indices>
bool IsCancelled(std::index_sequence<indices...>) const {
return CancellationTraits::IsCancelled(functor_,
std::get<indices>(bound_args_)...);
}
template <size_t... indices>
bool MaybeValid(std::index_sequence<indices...>) const {
return CancellationTraits::MaybeValid(functor_,
std::get<indices>(bound_args_)...);
}
void CheckFunctorIsNonNull() const {
if constexpr (is_nullable) {
if constexpr (is_callback) {
CHECK(!!functor_);
} else {
DCHECK(!!functor_);
}
}
}
};
template <typename... BoundArgs>
struct ValidateBindStateTypeCommonChecks {
private:
template <typename T,
bool v =
(IsRawRef<T> && IsRefCountedType<base::RemoveRawRefT<T>>) ||
(IsPointerOrRawPtr<T> &&
IsRefCountedType<base::RemovePointerT<T>>)>
struct RefCountedTypeNotPassedByRawPointer {
static constexpr bool value = [] {
static_assert(
!v, "A parameter is a refcounted type and needs scoped_refptr.");
return !v;
}();
};
public:
using CommonCheckResult = std::conjunction<
RefCountedTypeNotPassedByRawPointer<std::decay_t<BoundArgs>>...,
ValidateStorageTraits<BoundArgs>...>;
};
template <bool is_method,
bool is_nullable,
bool is_callback,
typename Functor,
typename... BoundArgs>
struct ValidateBindStateType;
template <bool is_nullable,
bool is_callback,
typename Functor,
typename... BoundArgs>
struct ValidateBindStateType<false,
is_nullable,
is_callback,
Functor,
BoundArgs...> {
using Type = BindState<false,
is_nullable,
is_callback,
std::decay_t<Functor>,
typename ValidateStorageTraits<BoundArgs>::Type...>;
static constexpr bool value =
ValidateBindStateTypeCommonChecks<BoundArgs...>::CommonCheckResult::value;
};
template <bool is_nullable, bool is_callback, typename Functor>
struct ValidateBindStateType<true, is_nullable, is_callback, Functor> {
using Type = BindState<true, is_nullable, is_callback, std::decay_t<Functor>>;
static constexpr bool value = true;
};
template <bool is_nullable,
bool is_callback,
typename Functor,
typename Receiver,
typename... BoundArgs>
struct ValidateBindStateType<true,
is_nullable,
is_callback,
Functor,
Receiver,
BoundArgs...> {
private:
using DecayedReceiver = std::decay_t<Receiver>;
using ReceiverStorageType =
typename MethodReceiverStorage<DecayedReceiver>::Type;
template <bool v = !std::is_array_v<std::remove_reference_t<Receiver>>>
struct FirstBoundArgIsNotArray {
static constexpr bool value = [] {
static_assert(v, "First bound argument to a method cannot be an array.");
return v;
}();
};
template <bool v = !IsRawRef<DecayedReceiver>>
struct ReceiverIsNotRawRef {
static constexpr bool value = [] {
static_assert(v, "Receivers may not be raw_ref<T>. If using a raw_ref<T> "
"here is safe and has no lifetime concerns, use "
"base::Unretained() and document why it's safe.");
return v;
}();
};
template <bool v = !IsPointerOrRawPtr<DecayedReceiver> ||
IsRefCountedType<RemovePointerT<DecayedReceiver>>>
struct ReceiverIsNotRawPtr {
static constexpr bool value = [] {
static_assert(v,
"Receivers may not be raw pointers. If using a raw pointer "
"here is safe and has no lifetime concerns, use "
"base::Unretained() and document why it's safe.");
return v;
}();
};
public:
using Type = BindState<true,
is_nullable,
is_callback,
std::decay_t<Functor>,
ReceiverStorageType,
typename ValidateStorageTraits<BoundArgs>::Type...>;
static constexpr bool value =
std::conjunction_v<FirstBoundArgIsNotArray<>,
ReceiverIsNotRawRef<>,
ReceiverIsNotRawPtr<>,
typename ValidateBindStateTypeCommonChecks<
BoundArgs...>::CommonCheckResult>;
};
template <bool is_once,
typename T,
typename StoredType = std::decay_t<T>,
typename ForwardedType =
std::conditional_t<is_once, StoredType&&, const StoredType&>>
using TransformToUnwrappedType =
decltype(Unwrap(std::declval<ForwardedType>()));
template <typename T>
struct ValidateReceiverType {
private:
template <bool v = false>
struct ReceiverMustBePointerLike {
static constexpr bool value = [] {
static_assert(v,
"Cannot convert `this` argument to address. Method calls "
"must be bound using a pointer-like `this` argument.");
return v;
}();
};
public:
using Type = T;
static constexpr bool value = ReceiverMustBePointerLike<>::value;
};
template <typename T>
requires requires(T&& t) { base::to_address(t); }
struct ValidateReceiverType<T> {
using Type = decltype(base::to_address(std::declval<T>()));
static constexpr bool value = true;
};
template <bool is_once, bool is_method, typename... Args>
struct ValidateUnwrappedTypeList {
using Type = TypeList<TransformToUnwrappedType<is_once, Args>...>;
static constexpr bool value = true;
};
template <bool is_once, typename Receiver, typename... Args>
struct ValidateUnwrappedTypeList<is_once, true, Receiver, Args...> {
private:
using ReceiverStorageType =
typename MethodReceiverStorage<std::decay_t<Receiver>>::Type;
using UnwrappedReceiver =
TransformToUnwrappedType<is_once, ReceiverStorageType>;
using ValidatedReceiver = ValidateReceiverType<UnwrappedReceiver>;
public:
using Type = TypeList<typename ValidatedReceiver::Type,
TransformToUnwrappedType<is_once, Args>...>;
static constexpr bool value = ValidatedReceiver::value;
};
template <typename StorageType>
inline constexpr bool IsUnretainedMayDangle = false;
template <typename T, RawPtrTraits PtrTraits>
inline constexpr bool IsUnretainedMayDangle<
UnretainedWrapper<T, unretained_traits::MayDangle, PtrTraits>> = true;
template <typename StorageType, typename FunctionParamType>
inline constexpr bool UnretainedAndRawPtrHaveCompatibleTraits = false;
template <typename T,
RawPtrTraits PtrTraitsInUnretained,
RawPtrTraits PtrTraitsInReceiver>
inline constexpr bool UnretainedAndRawPtrHaveCompatibleTraits<
UnretainedWrapper<T, unretained_traits::MayDangle, PtrTraitsInUnretained>,
raw_ptr<T, PtrTraitsInReceiver>> =
std::same_as<typename UnretainedWrapper<T,
unretained_traits::MayDangle,
PtrTraitsInUnretained>::GetPtrType,
raw_ptr<T, PtrTraitsInReceiver>>;
template <int i>
struct BindArgument {
template <typename ForwardingType>
struct ForwardedAs {
template <typename FunctorParamType>
struct ToParamWithType {
static constexpr bool kRawPtr = IsRawPtr<FunctorParamType>;
static constexpr bool kRawPtrMayBeDangling =
IsRawPtrMayDangle<FunctorParamType>;
static constexpr bool kCanBeForwardedToBoundFunctor =
std::is_convertible_v<ForwardingType, FunctorParamType>;
static constexpr bool kIsUnwrappedForwardableNonConstReference =
std::is_lvalue_reference_v<FunctorParamType> &&
!std::is_const_v<std::remove_reference_t<FunctorParamType>> &&
std::is_convertible_v<std::decay_t<ForwardingType>&,
FunctorParamType>;
static constexpr bool kWouldBeForwardableWithPassed =
std::is_convertible_v<std::decay_t<ForwardingType>&&,
FunctorParamType>;
};
};
template <typename BoundAsType>
struct BoundAs {
template <typename StorageType>
struct StoredAs {
static constexpr bool kBindArgumentCanBeCaptured =
std::constructible_from<StorageType, BoundAsType>;
static constexpr bool kWouldBeCapturableWithStdMove =
std::constructible_from<StorageType, std::decay_t<BoundAsType>&&>;
};
};
template <typename FunctionParamType>
struct ToParamWithType {
template <typename StorageType>
struct StoredAs {
static constexpr bool kBoundPtrMayDangle =
IsUnretainedMayDangle<StorageType>;
static constexpr bool kMayDangleAndMayBeDanglingHaveMatchingTraits =
UnretainedAndRawPtrHaveCompatibleTraits<StorageType,
FunctionParamType>;
};
};
};
template <int i,
bool is_method,
typename Arg,
typename Storage,
typename Unwrapped,
typename Param>
struct ParamCanBeBound {
private:
using UnwrappedParam = BindArgument<i>::template ForwardedAs<
Unwrapped>::template ToParamWithType<Param>;
using ParamStorage = BindArgument<i>::template ToParamWithType<
Param>::template StoredAs<Storage>;
using BoundStorage =
BindArgument<i>::template BoundAs<Arg>::template StoredAs<Storage>;
template <bool v = !UnwrappedParam::kRawPtr ||
UnwrappedParam::kRawPtrMayBeDangling>
struct NotRawPtr {
static constexpr bool value = [] {
static_assert(
v, "Use T* or T& instead of raw_ptr<T> for function parameters, "
"unless you must mark the parameter as MayBeDangling<T>.");
return v;
}();
};
template <bool v = !ParamStorage::kBoundPtrMayDangle ||
UnwrappedParam::kRawPtrMayBeDangling ||
(is_method && i == 0)>
struct MayBeDanglingPtrPassedCorrectly {
static constexpr bool value = [] {
static_assert(v, "base::UnsafeDangling() pointers should only be passed "
"to parameters marked MayBeDangling<T>.");
return v;
}();
};
template <bool v =
!UnwrappedParam::kRawPtrMayBeDangling ||
(ParamStorage::kBoundPtrMayDangle &&
ParamStorage::kMayDangleAndMayBeDanglingHaveMatchingTraits)>
struct MayDangleAndMayBeDanglingHaveMatchingTraits {
static constexpr bool value = [] {
static_assert(
v, "Pointers passed to MayBeDangling<T> parameters must be created "
"by base::UnsafeDangling() with the same RawPtrTraits.");
return v;
}();
};
template <bool v = UnwrappedParam::kCanBeForwardedToBoundFunctor ||
!UnwrappedParam::kWouldBeForwardableWithPassed>
struct MoveOnlyTypeMustUseBasePassed {
static constexpr bool value = [] {
static_assert(v,
"base::BindRepeating() argument is a move-only type. Use "
"base::Passed() instead of std::move() to transfer "
"ownership from the callback to the bound functor.");
return v;
}();
};
template <bool v = UnwrappedParam::kCanBeForwardedToBoundFunctor ||
!UnwrappedParam::kIsUnwrappedForwardableNonConstReference>
struct NonConstRefParamMustBeWrapped {
static constexpr bool value = [] {
static_assert(v,
"Bound argument for non-const reference parameter must be "
"wrapped in std::ref() or base::OwnedRef().");
return v;
}();
};
template <bool v = UnwrappedParam::kCanBeForwardedToBoundFunctor>
struct CanBeForwardedToBoundFunctor {
static constexpr bool value = [] {
static_assert(v,
"Type mismatch between bound argument and bound functor's "
"parameter.");
return v;
}();
};
template <bool v = BoundStorage::kBindArgumentCanBeCaptured ||
!BoundStorage::kWouldBeCapturableWithStdMove>
struct MoveOnlyTypeMustUseStdMove {
static constexpr bool value = [] {
static_assert(v,
"Attempting to bind a move-only type. Use std::move() to "
"transfer ownership to the created callback.");
return v;
}();
};
template <bool v = BoundStorage::kBindArgumentCanBeCaptured>
struct BindArgumentCanBeCaptured {
static constexpr bool value = [] {
static_assert(
v, "Cannot capture argument: is the argument copyable or movable?");
return v;
}();
};
public:
static constexpr bool value =
std::conjunction_v<NotRawPtr<>,
MayBeDanglingPtrPassedCorrectly<>,
MayDangleAndMayBeDanglingHaveMatchingTraits<>,
MoveOnlyTypeMustUseBasePassed<>,
NonConstRefParamMustBeWrapped<>,
CanBeForwardedToBoundFunctor<>,
MoveOnlyTypeMustUseStdMove<>,
BindArgumentCanBeCaptured<>>;
};
template <bool is_method,
typename Index,
typename Args,
typename UnwrappedTypeList,
typename ParamsList>
struct ParamsCanBeBound {
static constexpr bool value = false;
};
template <bool is_method,
size_t... Ns,
typename... Args,
typename... UnwrappedTypes,
typename... Params>
struct ParamsCanBeBound<is_method,
std::index_sequence<Ns...>,
TypeList<Args...>,
TypeList<UnwrappedTypes...>,
TypeList<Params...>> {
static constexpr bool value =
std::conjunction_v<ParamCanBeBound<Ns,
is_method,
Args,
std::decay_t<Args>,
UnwrappedTypes,
Params>...>;
};
template <template <typename> class CallbackT>
struct BindHelper {
private:
static constexpr bool kIsOnce =
is_instantiation<CallbackT<void()>, OnceCallback>;
template <typename Traits, bool v = IsComplete<Traits>>
struct TraitsAreInstantiable {
static constexpr bool value = [] {
static_assert(
v, "Could not determine how to invoke functor. If this functor has "
"an overloaded operator()(), bind all arguments to it, and ensure "
"the result will select a unique overload.");
return v;
}();
};
template <typename Functor,
bool v = !is_instantiation<std::decay_t<Functor>, OnceCallback> ||
(kIsOnce && std::is_rvalue_reference_v<Functor&&> &&
!std::is_const_v<std::remove_reference_t<Functor>>)>
struct OnceCallbackFunctorIsValid {
static constexpr bool value = [] {
if constexpr (kIsOnce) {
static_assert(v,
"BindOnce() requires non-const rvalue for OnceCallback "
"binding, i.e. base::BindOnce(std::move(callback)).");
} else {
static_assert(v, "BindRepeating() cannot bind OnceCallback. Use "
"BindOnce() with std::move().");
}
return v;
}();
};
template <typename... Args>
struct NoBindArgToOnceCallbackIsBasePassed {
static constexpr bool value = [] {
constexpr bool v =
!kIsOnce ||
(... && !is_instantiation<std::decay_t<Args>, PassedWrapper>);
static_assert(
v,
"Use std::move() instead of base::Passed() with base::BindOnce().");
return v;
}();
};
template <
typename Functor,
bool v =
!is_instantiation<std::remove_cvref_t<Functor>, FunctionRef> &&
!is_instantiation<std::remove_cvref_t<Functor>, absl::FunctionRef>>
struct NotFunctionRef {
static constexpr bool value = [] {
static_assert(
v,
"Functor may not be a FunctionRef, since that is a non-owning "
"reference that may go out of scope before the callback executes.");
return v;
}();
};
template <typename Traits, bool v = Traits::is_stateless>
struct IsStateless {
static constexpr bool value = [] {
static_assert(
v, "Capturing lambdas and stateful functors are intentionally not "
"supported. Use a non-capturing lambda or stateless functor (i.e. "
"has no non-static data members) and bind arguments directly.");
return v;
}();
};
template <typename Functor, typename... Args>
static auto BindImpl(Functor&& functor, Args&&... args) {
using Traits = FunctorTraits<TransformToUnwrappedType<kIsOnce, Functor&&>,
TransformToUnwrappedType<kIsOnce, Args&&>...>;
if constexpr (TraitsAreInstantiable<Traits>::value) {
using ValidatedUnwrappedTypes =
ValidateUnwrappedTypeList<kIsOnce, Traits::is_method, Args&&...>;
using BoundArgsList = TypeList<Args...>;
using RunParamsList = ExtractArgs<typename Traits::RunType>;
using BoundParamsList = TakeTypeListItem<sizeof...(Args), RunParamsList>;
using ValidatedBindState =
ValidateBindStateType<Traits::is_method, Traits::is_nullable,
Traits::is_callback, Functor, Args...>;
if constexpr (std::conjunction_v<
NotFunctionRef<Functor>, IsStateless<Traits>,
ValidatedUnwrappedTypes,
ParamsCanBeBound<
Traits::is_method,
std::make_index_sequence<sizeof...(Args)>,
BoundArgsList,
typename ValidatedUnwrappedTypes::Type,
BoundParamsList>,
ValidatedBindState>) {
using UnboundRunType =
MakeFunctionType<ExtractReturnType<typename Traits::RunType>,
DropTypeListItem<sizeof...(Args), RunParamsList>>;
using CallbackType = CallbackT<UnboundRunType>;
typename CallbackType::PolymorphicInvoke invoke_func;
using Invoker =
Invoker<Traits, typename ValidatedBindState::Type, UnboundRunType>;
if constexpr (kIsOnce) {
invoke_func = Invoker::RunOnce;
} else {
invoke_func = Invoker::Run;
}
return CallbackType(ValidatedBindState::Type::Create(
reinterpret_cast<BindStateBase::InvokeFuncStorage>(invoke_func),
std::forward<Functor>(functor), std::forward<Args>(args)...));
}
}
}
template <typename T>
requires is_instantiation<T, CallbackT>
static T BindImpl(T callback) {
CHECK(callback);
return callback;
}
template <typename Signature>
requires is_instantiation<OnceCallback<Signature>, CallbackT>
static OnceCallback<Signature> BindImpl(
RepeatingCallback<Signature> callback) {
return BindImpl(OnceCallback<Signature>(callback));
}
template <typename Functor, typename... Args>
struct BindImplWouldSucceed {
static constexpr bool value =
!std::same_as<void,
decltype(BindImpl(std::declval<Functor&&>(),
std::declval<Args&&>()...))>;
};
public:
template <typename Functor, typename... Args>
static auto Bind(Functor&& functor, Args&&... args) {
if constexpr (std::conjunction_v<
OnceCallbackFunctorIsValid<Functor>,
NoBindArgToOnceCallbackIsBasePassed<Args...>,
BindImplWouldSucceed<Functor, Args...>>) {
return BindImpl(std::forward<Functor>(functor),
std::forward<Args>(args)...);
} else {
return BindFailedCheckPreviousErrors();
}
}
};
}
template <typename T>
struct IsWeakReceiver : std::bool_constant<is_instantiation<T, WeakPtr>> {};
template <typename T>
struct IsWeakReceiver<std::reference_wrapper<T>> : IsWeakReceiver<T> {};
template <typename>
struct MaybeValidTraits {
template <typename T>
static bool MaybeValid(const T& o) {
return o.MaybeValid();
}
};
template <typename>
struct BindUnwrapTraits {
template <typename T>
static T&& Unwrap(T&& o) {
return std::forward<T>(o);
}
};
template <typename T>
requires internal::kIsUnretainedWrapper<internal::UnretainedWrapper, T> ||
internal::kIsUnretainedWrapper<internal::UnretainedRefWrapper, T> ||
is_instantiation<T, internal::RetainedRefWrapper> ||
is_instantiation<T, internal::OwnedWrapper> ||
is_instantiation<T, internal::OwnedRefWrapper>
struct BindUnwrapTraits<T> {
static decltype(auto) Unwrap(const T& o) { return o.get(); }
};
template <typename T>
struct BindUnwrapTraits<internal::PassedWrapper<T>> {
static T Unwrap(const internal::PassedWrapper<T>& o) { return o.Take(); }
};
#if BUILDFLAG(IS_WIN)
template <typename T>
struct BindUnwrapTraits<Microsoft::WRL::ComPtr<T>> {
static T* Unwrap(const Microsoft::WRL::ComPtr<T>& ptr) { return ptr.Get(); }
};
#endif
template <typename Functor, typename BoundArgsTuple>
struct CallbackCancellationTraits {
static constexpr bool is_cancellable = false;
};
template <typename Functor, typename... BoundArgs>
requires internal::kIsWeakMethod<
internal::FunctorTraits<Functor, BoundArgs...>::is_method,
BoundArgs...>
struct CallbackCancellationTraits<Functor, std::tuple<BoundArgs...>> {
static constexpr bool is_cancellable = true;
template <typename Receiver, typename... Args>
static bool IsCancelled(const Functor&,
const Receiver& receiver,
const Args&...) {
return !receiver;
}
template <typename Receiver, typename... Args>
static bool MaybeValid(const Functor&,
const Receiver& receiver,
const Args&...) {
return MaybeValidTraits<Receiver>::MaybeValid(receiver);
}
};
template <typename Functor, typename... BoundArgs>
requires is_instantiation<Functor, OnceCallback> ||
is_instantiation<Functor, RepeatingCallback>
struct CallbackCancellationTraits<Functor, std::tuple<BoundArgs...>> {
static constexpr bool is_cancellable = true;
static bool IsCancelled(const Functor& functor, const BoundArgs&...) {
return functor.IsCancelled();
}
static bool MaybeValid(const Functor& functor, const BoundArgs&...) {
return MaybeValidTraits<Functor>::MaybeValid(functor);
}
};
}
#endif