#ifndef BASE_FUNCTIONAL_CALLBACK_INTERNAL_H_
#define BASE_FUNCTIONAL_CALLBACK_INTERNAL_H_
#include <type_traits>
#include <utility>
#include "base/base_export.h"
#include "base/compiler_specific.h"
#include "base/functional/callback_forward.h"
#include "base/memory/ref_counted.h"
namespace base {
struct FakeBindState;
namespace internal {
class BindStateBase;
template <bool is_method,
bool is_nullable,
bool is_callback,
typename Functor,
typename... BoundArgs>
struct BindState;
struct BASE_EXPORT BindStateBaseRefCountTraits {
static void Destruct(const BindStateBase*);
};
template <typename T>
using PassingType = std::conditional_t<std::is_scalar_v<T>, T, T&&>;
class BASE_EXPORT BindStateBase
: public RefCountedThreadSafe<BindStateBase, BindStateBaseRefCountTraits> {
public:
REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
enum class CancellationQueryMode : bool {
kIsCancelled = false,
kMaybeValid = true,
};
using InvokeFuncStorage = void (*)();
BindStateBase(const BindStateBase&) = delete;
BindStateBase& operator=(const BindStateBase&) = delete;
private:
using DestructorPtr = void (*)(const BindStateBase*);
using QueryCancellationTraitsPtr = bool (*)(const BindStateBase*,
CancellationQueryMode mode);
BindStateBase(InvokeFuncStorage polymorphic_invoke, DestructorPtr destructor);
BindStateBase(InvokeFuncStorage polymorphic_invoke,
DestructorPtr destructor,
QueryCancellationTraitsPtr query_cancellation_traits);
~BindStateBase() = default;
friend struct BindStateBaseRefCountTraits;
friend class RefCountedThreadSafe<BindStateBase, BindStateBaseRefCountTraits>;
friend class BindStateHolder;
template <bool is_method,
bool is_nullable,
bool is_callback,
typename Functor,
typename... BoundArgs>
friend struct BindState;
friend struct ::base::FakeBindState;
bool IsCancelled() const {
return query_cancellation_traits_(this,
CancellationQueryMode::kIsCancelled);
}
bool MaybeValid() const {
return query_cancellation_traits_(this, CancellationQueryMode::kMaybeValid);
}
InvokeFuncStorage polymorphic_invoke_;
DestructorPtr destructor_;
QueryCancellationTraitsPtr query_cancellation_traits_;
};
class BASE_EXPORT TRIVIAL_ABI BindStateHolder {
public:
using InvokeFuncStorage = BindStateBase::InvokeFuncStorage;
inline constexpr BindStateHolder() noexcept;
inline explicit BindStateHolder(BindStateBase* bind_state);
BindStateHolder(const BindStateHolder&);
BindStateHolder& operator=(const BindStateHolder&);
inline BindStateHolder(BindStateHolder&&) noexcept;
BindStateHolder& operator=(BindStateHolder&&) noexcept;
~BindStateHolder();
bool is_null() const { return !bind_state_; }
explicit operator bool() const { return !is_null(); }
bool IsCancelled() const;
bool MaybeValid() const;
void Reset();
friend bool operator==(const BindStateHolder&,
const BindStateHolder&) = default;
const scoped_refptr<BindStateBase>& bind_state() const { return bind_state_; }
InvokeFuncStorage polymorphic_invoke() const {
return bind_state_->polymorphic_invoke_;
}
private:
scoped_refptr<BindStateBase> bind_state_;
};
constexpr BindStateHolder::BindStateHolder() noexcept = default;
BindStateHolder::BindStateHolder(BindStateBase* bind_state)
: bind_state_(AdoptRef(bind_state)) {}
BindStateHolder::BindStateHolder(BindStateHolder&&) noexcept = default;
template <typename OriginalCallback, typename ThenCallback>
struct ThenHelper;
template <template <typename> class OriginalCallback,
template <typename>
class ThenCallback,
typename... OriginalArgs,
typename ThenR,
typename... ThenArgs>
struct ThenHelper<OriginalCallback<void(OriginalArgs...)>,
ThenCallback<ThenR(ThenArgs...)>> {
private:
template <bool v = sizeof...(ThenArgs) == 0>
struct CorrectNumberOfArgs {
static constexpr bool value = [] {
static_assert(v,
"|then| callback cannot accept parameters if |this| has a "
"void return type.");
return v;
}();
};
public:
static auto CreateTrampoline() {
return [](OriginalCallback<void(OriginalArgs...)> c1,
ThenCallback<ThenR(ThenArgs...)> c2,
OriginalArgs... c1_args) -> ThenR {
if constexpr (CorrectNumberOfArgs<>::value) {
std::move(c1).Run(std::forward<OriginalArgs>(c1_args)...);
return std::move(c2).Run();
}
};
}
};
template <template <typename> class OriginalCallback,
template <typename>
class ThenCallback,
typename OriginalR,
typename... OriginalArgs,
typename ThenR,
typename... ThenArgs>
struct ThenHelper<OriginalCallback<OriginalR(OriginalArgs...)>,
ThenCallback<ThenR(ThenArgs...)>> {
private:
template <bool v = sizeof...(ThenArgs) == 1>
struct CorrectNumberOfArgs {
static constexpr bool value = [] {
static_assert(
v,
"|then| callback must accept exactly one parameter if |this| has a "
"non-void return type.");
return v;
}();
};
template <bool v =
std::is_constructible_v<ThenArgs..., OriginalR&&>>
struct ArgsAreConvertible {
static constexpr bool value = [] {
static_assert(v,
"|then| callback's parameter must be constructible from "
"return type of |this|.");
return v;
}();
};
public:
static auto CreateTrampoline() {
return [](OriginalCallback<OriginalR(OriginalArgs...)> c1,
ThenCallback<ThenR(ThenArgs...)> c2,
OriginalArgs... c1_args) -> ThenR {
if constexpr (std::conjunction_v<CorrectNumberOfArgs<>,
ArgsAreConvertible<>>) {
return std::move(c2).Run(
std::move(c1).Run(std::forward<OriginalArgs>(c1_args)...));
}
};
}
};
}
}
#endif