diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2017-11-20 10:33:36 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2017-11-22 11:45:12 +0000 |
commit | be59a35641616a4cf23c4a13fa0632624b021c1b (patch) | |
tree | 9da183258bdf9cc413f7562079d25ace6955467f /chromium/base/bind_internal.h | |
parent | d702e4b6a64574e97fc7df8fe3238cde70242080 (diff) |
BASELINE: Update Chromium to 62.0.3202.101
Change-Id: I2d5eca8117600df6d331f6166ab24d943d9814ac
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Diffstat (limited to 'chromium/base/bind_internal.h')
-rw-r--r-- | chromium/base/bind_internal.h | 182 |
1 files changed, 87 insertions, 95 deletions
diff --git a/chromium/base/bind_internal.h b/chromium/base/bind_internal.h index 5aab7f408a4..c19f75915a6 100644 --- a/chromium/base/bind_internal.h +++ b/chromium/base/bind_internal.h @@ -7,15 +7,14 @@ #include <stddef.h> -#include <tuple> #include <type_traits> +#include <utility> #include "base/bind_helpers.h" #include "base/callback_internal.h" #include "base/memory/raw_scoped_refptr_mismatch_checker.h" #include "base/memory/weak_ptr.h" #include "base/template_util.h" -#include "base/tuple.h" #include "build/build_config.h" namespace base { @@ -46,19 +45,6 @@ namespace internal { // BindState<> -- Stores the curried parameters, and is the main entry point // into the Bind() system. -template <typename...> -struct make_void { - using type = void; -}; - -// A clone of C++17 std::void_t. -// Unlike the original version, we need |make_void| as a helper struct to avoid -// a C++14 defect. -// ref: http://en.cppreference.com/w/cpp/types/void_t -// ref: http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1558 -template <typename... Ts> -using void_t = typename make_void<Ts...>::type; - template <typename Callable, typename Signature = decltype(&Callable::operator())> struct ExtractCallableRunTypeImpl; @@ -112,9 +98,9 @@ struct HasRefCountedTypeAsRawPtr : std::false_type {}; // parameters recursively. template <typename T, typename... Args> struct HasRefCountedTypeAsRawPtr<T, Args...> - : std::conditional<NeedsScopedRefptrButGetsRawPtr<T>::value, - std::true_type, - HasRefCountedTypeAsRawPtr<Args...>>::type {}; + : std::conditional_t<NeedsScopedRefptrButGetsRawPtr<T>::value, + std::true_type, + HasRefCountedTypeAsRawPtr<Args...>> {}; // ForceVoidReturn<> // @@ -138,9 +124,8 @@ struct FunctorTraits; // base::Bind(), based on the fact that captureless lambdas can be convertible // to the function type while capturing lambdas can't. template <typename Functor> -struct FunctorTraits< - Functor, - typename std::enable_if<IsConvertibleToRunType<Functor>::value>::type> { +struct FunctorTraits<Functor, + std::enable_if_t<IsConvertibleToRunType<Functor>::value>> { using RunType = ExtractCallableRunType<Functor>; static constexpr bool is_method = false; static constexpr bool is_nullable = false; @@ -240,10 +225,9 @@ struct FunctorTraits<IgnoreResultHelper<T>> : FunctorTraits<T> { } }; -// For Callbacks. -template <typename R, typename... Args, - CopyMode copy_mode, RepeatMode repeat_mode> -struct FunctorTraits<Callback<R(Args...), copy_mode, repeat_mode>> { +// For OnceCallbacks. +template <typename R, typename... Args> +struct FunctorTraits<OnceCallback<R(Args...)>> { using RunType = R(Args...); static constexpr bool is_method = false; static constexpr bool is_nullable = true; @@ -256,6 +240,24 @@ struct FunctorTraits<Callback<R(Args...), copy_mode, repeat_mode>> { } }; +// For RepeatingCallbacks. +template <typename R, typename... Args> +struct FunctorTraits<RepeatingCallback<R(Args...)>> { + using RunType = R(Args...); + static constexpr bool is_method = false; + static constexpr bool is_nullable = 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> +using MakeFunctorTraits = FunctorTraits<std::decay_t<Functor>>; + // InvokeHelper<> // // There are 2 logical InvokeHelper<> specializations: normal, WeakCalls. @@ -271,7 +273,7 @@ template <typename ReturnType> struct InvokeHelper<false, ReturnType> { template <typename Functor, typename... RunArgs> static inline ReturnType MakeItSo(Functor&& functor, RunArgs&&... args) { - using Traits = FunctorTraits<typename std::decay<Functor>::type>; + using Traits = MakeFunctorTraits<Functor>; return Traits::Invoke(std::forward<Functor>(functor), std::forward<RunArgs>(args)...); } @@ -291,7 +293,7 @@ struct InvokeHelper<true, ReturnType> { RunArgs&&... args) { if (!weak_ptr) return; - using Traits = FunctorTraits<typename std::decay<Functor>::type>; + using Traits = MakeFunctorTraits<Functor>; Traits::Invoke(std::forward<Functor>(functor), std::forward<BoundWeakPtr>(weak_ptr), std::forward<RunArgs>(args)...); @@ -315,7 +317,7 @@ struct Invoker<StorageType, R(UnboundArgs...)> { std::tuple_size<decltype(storage->bound_args_)>::value; return RunImpl(std::move(storage->functor_), std::move(storage->bound_args_), - MakeIndexSequence<num_bound_args>(), + std::make_index_sequence<num_bound_args>(), std::forward<UnboundArgs>(unbound_args)...); } @@ -326,9 +328,8 @@ struct Invoker<StorageType, R(UnboundArgs...)> { const StorageType* storage = static_cast<StorageType*>(base); static constexpr size_t num_bound_args = std::tuple_size<decltype(storage->bound_args_)>::value; - return RunImpl(storage->functor_, - storage->bound_args_, - MakeIndexSequence<num_bound_args>(), + return RunImpl(storage->functor_, storage->bound_args_, + std::make_index_sequence<num_bound_args>(), std::forward<UnboundArgs>(unbound_args)...); } @@ -336,17 +337,14 @@ struct Invoker<StorageType, R(UnboundArgs...)> { template <typename Functor, typename BoundArgsTuple, size_t... indices> static inline R RunImpl(Functor&& functor, BoundArgsTuple&& bound, - IndexSequence<indices...>, + std::index_sequence<indices...>, UnboundArgs&&... unbound_args) { - static constexpr bool is_method = - FunctorTraits<typename std::decay<Functor>::type>::is_method; + static constexpr bool is_method = MakeFunctorTraits<Functor>::is_method; - using DecayedArgsTuple = typename std::decay<BoundArgsTuple>::type; + using DecayedArgsTuple = std::decay_t<BoundArgsTuple>; static constexpr bool is_weak_call = IsWeakMethod<is_method, - typename std::tuple_element< - indices, - DecayedArgsTuple>::type...>::value; + std::tuple_element_t<indices, DecayedArgsTuple>...>(); return InvokeHelper<is_weak_call, R>::MakeItSo( std::forward<Functor>(functor), @@ -355,25 +353,44 @@ struct Invoker<StorageType, R(UnboundArgs...)> { } }; -// Used to implement MakeUnboundRunType. +// Extracts necessary type info from Functor and BoundArgs. +// Used to implement MakeUnboundRunType, BindOnce and BindRepeating. template <typename Functor, typename... BoundArgs> -struct MakeUnboundRunTypeImpl { - using RunType = - typename FunctorTraits<typename std::decay<Functor>::type>::RunType; +struct BindTypeHelper { + static constexpr size_t num_bounds = sizeof...(BoundArgs); + using FunctorTraits = MakeFunctorTraits<Functor>; + + // Example: + // When Functor is `double (Foo::*)(int, const std::string&)`, and BoundArgs + // is a template pack of `Foo*` and `int16_t`: + // - RunType is `double(Foo*, int, const std::string&)`, + // - ReturnType is `double`, + // - RunParamsList is `TypeList<Foo*, int, const std::string&>`, + // - BoundParamsList is `TypeList<Foo*, int>`, + // - UnboundParamsList is `TypeList<const std::string&>`, + // - BoundArgsList is `TypeList<Foo*, int16_t>`, + // - UnboundRunType is `double(const std::string&)`. + using RunType = typename FunctorTraits::RunType; using ReturnType = ExtractReturnType<RunType>; - using Args = ExtractArgs<RunType>; - using UnboundArgs = DropTypeListItem<sizeof...(BoundArgs), Args>; - using Type = MakeFunctionType<ReturnType, UnboundArgs>; + + using RunParamsList = ExtractArgs<RunType>; + using BoundParamsList = TakeTypeListItem<num_bounds, RunParamsList>; + using UnboundParamsList = DropTypeListItem<num_bounds, RunParamsList>; + + using BoundArgsList = TypeList<BoundArgs...>; + + using UnboundRunType = MakeFunctionType<ReturnType, UnboundParamsList>; }; + template <typename Functor> -typename std::enable_if<FunctorTraits<Functor>::is_nullable, bool>::type -IsNull(const Functor& functor) { +std::enable_if_t<FunctorTraits<Functor>::is_nullable, bool> IsNull( + const Functor& functor) { return !functor; } template <typename Functor> -typename std::enable_if<!FunctorTraits<Functor>::is_nullable, bool>::type -IsNull(const Functor&) { +std::enable_if_t<!FunctorTraits<Functor>::is_nullable, bool> IsNull( + const Functor&) { return false; } @@ -381,7 +398,7 @@ IsNull(const Functor&) { template <typename Functor, typename BoundArgsTuple, size_t... indices> bool ApplyCancellationTraitsImpl(const Functor& functor, const BoundArgsTuple& bound_args, - IndexSequence<indices...>) { + std::index_sequence<indices...>) { return CallbackCancellationTraits<Functor, BoundArgsTuple>::IsCancelled( functor, std::get<indices>(bound_args)...); } @@ -393,25 +410,9 @@ bool ApplyCancellationTraits(const BindStateBase* base) { const BindStateType* storage = static_cast<const BindStateType*>(base); static constexpr size_t num_bound_args = std::tuple_size<decltype(storage->bound_args_)>::value; - return ApplyCancellationTraitsImpl(storage->functor_, storage->bound_args_, - MakeIndexSequence<num_bound_args>()); -}; - -// Template helpers to detect using Bind() on a base::Callback without any -// additional arguments. In that case, the original base::Callback object should -// just be directly used. -template <typename Functor, typename... BoundArgs> -struct BindingCallbackWithNoArgs { - static constexpr bool value = false; -}; - -template <typename Signature, - typename... BoundArgs, - CopyMode copy_mode, - RepeatMode repeat_mode> -struct BindingCallbackWithNoArgs<Callback<Signature, copy_mode, repeat_mode>, - BoundArgs...> { - static constexpr bool value = sizeof...(BoundArgs) == 0; + return ApplyCancellationTraitsImpl( + storage->functor_, storage->bound_args_, + std::make_index_sequence<num_bound_args>()); }; // BindState<> @@ -435,10 +436,6 @@ struct BindState final : BindStateBase { invoke_func, std::forward<ForwardFunctor>(functor), std::forward<ForwardBoundArgs>(bound_args)...) { - static_assert(!BindingCallbackWithNoArgs<Functor, BoundArgs...>::value, - "Attempting to bind a base::Callback with no additional " - "arguments: save a heap allocation and use the original " - "base::Callback object"); } Functor functor_; @@ -482,45 +479,40 @@ struct MakeBindStateTypeImpl; template <typename Functor, typename... BoundArgs> struct MakeBindStateTypeImpl<false, Functor, BoundArgs...> { - static_assert(!HasRefCountedTypeAsRawPtr< - typename std::decay<BoundArgs>::type...>::value, + static_assert(!HasRefCountedTypeAsRawPtr<std::decay_t<BoundArgs>...>::value, "A parameter is a refcounted type and needs scoped_refptr."); - using Type = BindState<typename std::decay<Functor>::type, - typename std::decay<BoundArgs>::type...>; + using Type = BindState<std::decay_t<Functor>, std::decay_t<BoundArgs>...>; }; template <typename Functor> struct MakeBindStateTypeImpl<true, Functor> { - using Type = BindState<typename std::decay<Functor>::type>; + using Type = BindState<std::decay_t<Functor>>; }; template <typename Functor, typename Receiver, typename... BoundArgs> struct MakeBindStateTypeImpl<true, Functor, Receiver, BoundArgs...> { - static_assert( - !std::is_array<typename std::remove_reference<Receiver>::type>::value, - "First bound argument to a method cannot be an array."); - static_assert(!HasRefCountedTypeAsRawPtr< - typename std::decay<BoundArgs>::type...>::value, + static_assert(!std::is_array<std::remove_reference_t<Receiver>>::value, + "First bound argument to a method cannot be an array."); + static_assert(!HasRefCountedTypeAsRawPtr<std::decay_t<BoundArgs>...>::value, "A parameter is a refcounted type and needs scoped_refptr."); private: - using DecayedReceiver = typename std::decay<Receiver>::type; + using DecayedReceiver = std::decay_t<Receiver>; public: using Type = BindState< - typename std::decay<Functor>::type, - typename std::conditional< - std::is_pointer<DecayedReceiver>::value, - scoped_refptr<typename std::remove_pointer<DecayedReceiver>::type>, - DecayedReceiver>::type, - typename std::decay<BoundArgs>::type...>; + std::decay_t<Functor>, + std::conditional_t<std::is_pointer<DecayedReceiver>::value, + scoped_refptr<std::remove_pointer_t<DecayedReceiver>>, + DecayedReceiver>, + std::decay_t<BoundArgs>...>; }; template <typename Functor, typename... BoundArgs> -using MakeBindStateType = typename MakeBindStateTypeImpl< - FunctorTraits<typename std::decay<Functor>::type>::is_method, - Functor, - BoundArgs...>::Type; +using MakeBindStateType = + typename MakeBindStateTypeImpl<MakeFunctorTraits<Functor>::is_method, + Functor, + BoundArgs...>::Type; } // namespace internal @@ -528,7 +520,7 @@ using MakeBindStateType = typename MakeBindStateTypeImpl< // E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C). template <typename Functor, typename... BoundArgs> using MakeUnboundRunType = - typename internal::MakeUnboundRunTypeImpl<Functor, BoundArgs...>::Type; + typename internal::BindTypeHelper<Functor, BoundArgs...>::UnboundRunType; } // namespace base |