Commit c8214992 authored by tzik's avatar tzik Committed by Commit bot

[Base] Use variadic template for helpers in bind_internal.h

This CL removes generated code for
* RunnableAdapter
* ForceVoidReturn
* FunctorTraits
* InvokeHelper
from base/bind_internal.h.

BUG=433164

Review URL: https://codereview.chromium.org/621773002

Cr-Commit-Position: refs/heads/master@{#304997}
parent 11bdcd08
This diff is collapsed.
...@@ -57,18 +57,15 @@ namespace internal { ...@@ -57,18 +57,15 @@ namespace internal {
// Types: // Types:
// RunnableAdapter<> -- Wraps the various "function" pointer types into an // RunnableAdapter<> -- Wraps the various "function" pointer types into an
// object that adheres to the Runnable interface. // object that adheres to the Runnable interface.
// There are |3*ARITY| RunnableAdapter types.
// FunctionTraits<> -- Type traits that unwrap a function signature into a // FunctionTraits<> -- Type traits that unwrap a function signature into a
// a set of easier to use typedefs. Used mainly for // a set of easier to use typedefs. Used mainly for
// compile time asserts. // compile time asserts.
// There are |ARITY| FunctionTraits types. // There are |ARITY| FunctionTraits types.
// ForceVoidReturn<> -- Helper class for translating function signatures to // ForceVoidReturn<> -- Helper class for translating function signatures to
// equivalent forms with a "void" return type. // equivalent forms with a "void" return type.
// There are |ARITY| ForceVoidReturn types.
// FunctorTraits<> -- Type traits used determine the correct RunType and // FunctorTraits<> -- Type traits used determine the correct RunType and
// RunnableType for a Functor. This is where function // RunnableType for a Functor. This is where function
// signature adapters are applied. // signature adapters are applied.
// There are |ARITY| ForceVoidReturn types.
// MakeRunnable<> -- Takes a Functor and returns an object in the Runnable // MakeRunnable<> -- Takes a Functor and returns an object in the Runnable
// type class that represents the underlying Functor. // type class that represents the underlying Functor.
// There are |O(1)| MakeRunnable types. // There are |O(1)| MakeRunnable types.
...@@ -77,7 +74,6 @@ namespace internal { ...@@ -77,7 +74,6 @@ namespace internal {
// and for ignoring return values. This is separate from // and for ignoring return values. This is separate from
// Invoker to avoid creating multiple version of Invoker<> // Invoker to avoid creating multiple version of Invoker<>
// which grows at O(n^2) with the arity. // which grows at O(n^2) with the arity.
// There are |k*ARITY| InvokeHelper types.
// Invoker<> -- Unwraps the curried parameters and executes the Runnable. // Invoker<> -- Unwraps the curried parameters and executes the Runnable.
// There are |(ARITY^2 + ARITY)/2| Invoketypes. // There are |(ARITY^2 + ARITY)/2| Invoketypes.
// BindState<> -- Stores the curried parameters, and is the main entry point // BindState<> -- Stores the curried parameters, and is the main entry point
...@@ -107,75 +103,64 @@ namespace internal { ...@@ -107,75 +103,64 @@ namespace internal {
template <typename Functor> template <typename Functor>
class RunnableAdapter; class RunnableAdapter;
$for ARITY [[ // Function.
$range ARG 1..ARITY template <typename R, typename... Args>
class RunnableAdapter<R(*)(Args...)> {
// Function: Arity $(ARITY).
template <typename R[[]]
$if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]>
class RunnableAdapter<R(*)($for ARG , [[A$(ARG)]])> {
public: public:
typedef R (RunType)($for ARG , [[A$(ARG)]]); typedef R (RunType)(Args...);
explicit RunnableAdapter(R(*function)($for ARG , [[A$(ARG)]])) explicit RunnableAdapter(R(*function)(Args...))
: function_(function) { : function_(function) {
} }
R Run($for ARG , [[typename CallbackParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) { R Run(typename CallbackParamTraits<Args>::ForwardType... args) {
return function_($for ARG , [[CallbackForward(a$(ARG))]]); return function_(CallbackForward(args)...);
} }
private: private:
R (*function_)($for ARG , [[A$(ARG)]]); R (*function_)(Args...);
}; };
// Method: Arity $(ARITY). // Method.
template <typename R, typename T[[]] template <typename R, typename T, typename... Args>
$if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]> class RunnableAdapter<R(T::*)(Args...)> {
class RunnableAdapter<R(T::*)($for ARG , [[A$(ARG)]])> {
public: public:
typedef R (RunType)(T*[[]] typedef R (RunType)(T*, Args...);
$if ARITY > 0[[, ]] $for ARG , [[A$(ARG)]]);
typedef true_type IsMethod; typedef true_type IsMethod;
explicit RunnableAdapter(R(T::*method)($for ARG , [[A$(ARG)]])) explicit RunnableAdapter(R(T::*method)(Args...))
: method_(method) { : method_(method) {
} }
R Run(T* object[[]] R Run(T* object, typename CallbackParamTraits<Args>::ForwardType... args) {
$if ARITY > 0[[, ]] $for ARG, [[typename CallbackParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) { return (object->*method_)(CallbackForward(args)...);
return (object->*method_)($for ARG , [[CallbackForward(a$(ARG))]]);
} }
private: private:
R (T::*method_)($for ARG , [[A$(ARG)]]); R (T::*method_)(Args...);
}; };
// Const Method: Arity $(ARITY). // Const Method.
template <typename R, typename T[[]] template <typename R, typename T, typename... Args>
$if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]> class RunnableAdapter<R(T::*)(Args...) const> {
class RunnableAdapter<R(T::*)($for ARG , [[A$(ARG)]]) const> {
public: public:
typedef R (RunType)(const T*[[]] typedef R (RunType)(const T*, Args...);
$if ARITY > 0[[, ]] $for ARG , [[A$(ARG)]]);
typedef true_type IsMethod; typedef true_type IsMethod;
explicit RunnableAdapter(R(T::*method)($for ARG , [[A$(ARG)]]) const) explicit RunnableAdapter(R(T::*method)(Args...) const)
: method_(method) { : method_(method) {
} }
R Run(const T* object[[]] R Run(const T* object,
$if ARITY > 0[[, ]] $for ARG, [[typename CallbackParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) { typename CallbackParamTraits<Args>::ForwardType... args) {
return (object->*method_)($for ARG , [[CallbackForward(a$(ARG))]]); return (object->*method_)(CallbackForward(args)...);
} }
private: private:
R (T::*method_)($for ARG , [[A$(ARG)]]) const; R (T::*method_)(Args...) const;
}; };
]] $$ for ARITY // TODO(tzik): Remove FunctionTraits after we finish removing bind.pump.
// FunctionTraits<> // FunctionTraits<>
// //
// Breaks a function signature apart into typedefs for easier introspection. // Breaks a function signature apart into typedefs for easier introspection.
...@@ -205,17 +190,11 @@ $for ARG [[ ...@@ -205,17 +190,11 @@ $for ARG [[
template <typename Sig> template <typename Sig>
struct ForceVoidReturn; struct ForceVoidReturn;
$for ARITY [[ template <typename R, typename... Args>
$range ARG 1..ARITY struct ForceVoidReturn<R(Args...)> {
typedef void(RunType)(Args...);
template <typename R[[]]
$if ARITY > 0[[, ]] $for ARG , [[typename A$(ARG)]]>
struct ForceVoidReturn<R($for ARG , [[A$(ARG)]])> {
typedef void(RunType)($for ARG , [[A$(ARG)]]);
}; };
]] $$ for ARITY
// FunctorTraits<> // FunctorTraits<>
// //
...@@ -284,51 +263,31 @@ template <bool IsWeakCall, typename ReturnType, typename Runnable, ...@@ -284,51 +263,31 @@ template <bool IsWeakCall, typename ReturnType, typename Runnable,
typename ArgsType> typename ArgsType>
struct InvokeHelper; struct InvokeHelper;
$for ARITY [[ template <typename ReturnType, typename Runnable, typename... Args>
$range ARG 1..ARITY
$range WEAKCALL_ARG 2..ARITY
template <typename ReturnType, typename Runnable[[]]
$if ARITY > 0 [[,]] $for ARG , [[typename A$(ARG)]]>
struct InvokeHelper<false, ReturnType, Runnable, struct InvokeHelper<false, ReturnType, Runnable,
void($for ARG , [[A$(ARG)]])> { void(Args...)> {
static ReturnType MakeItSo(Runnable runnable[[]] static ReturnType MakeItSo(Runnable runnable, Args... args) {
$if ARITY > 0[[, ]] $for ARG , [[A$(ARG) a$(ARG)]]) { return runnable.Run(CallbackForward(args)...);
return runnable.Run($for ARG , [[CallbackForward(a$(ARG))]]);
} }
}; };
template <typename Runnable[[]] template <typename Runnable, typename... Args>
$if ARITY > 0 [[,]] $for ARG , [[typename A$(ARG)]]> struct InvokeHelper<false, void, Runnable, void(Args...)> {
struct InvokeHelper<false, void, Runnable, static void MakeItSo(Runnable runnable, Args... args) {
void($for ARG , [[A$(ARG)]])> { runnable.Run(CallbackForward(args)...);
static void MakeItSo(Runnable runnable[[]]
$if ARITY > 0[[, ]] $for ARG , [[A$(ARG) a$(ARG)]]) {
runnable.Run($for ARG , [[CallbackForward(a$(ARG))]]);
} }
}; };
$if ARITY > 0 [[ template <typename Runnable, typename BoundWeakPtr, typename... Args>
struct InvokeHelper<true, void, Runnable, void(BoundWeakPtr, Args...)> {
template <typename Runnable[[]], typename BoundWeakPtr static void MakeItSo(Runnable runnable, BoundWeakPtr weak_ptr, Args... args) {
$if ARITY > 1[[, ]] $for WEAKCALL_ARG , [[typename A$(WEAKCALL_ARG)]]>
struct InvokeHelper<true, void, Runnable,
void(BoundWeakPtr
$if ARITY > 1[[, ]] $for WEAKCALL_ARG , [[A$(WEAKCALL_ARG)]])> {
static void MakeItSo(Runnable runnable, BoundWeakPtr weak_ptr
$if ARITY > 1[[, ]] $for WEAKCALL_ARG , [[A$(WEAKCALL_ARG) a$(WEAKCALL_ARG)]]) {
if (!weak_ptr.get()) { if (!weak_ptr.get()) {
return; return;
} }
runnable.Run(weak_ptr.get() runnable.Run(weak_ptr.get(), CallbackForward(args)...);
$if ARITY > 1[[, ]] $for WEAKCALL_ARG , [[CallbackForward(a$(WEAKCALL_ARG))]]);
} }
}; };
]]
]] $$ for ARITY
#if !defined(_MSC_VER) #if !defined(_MSC_VER)
template <typename ReturnType, typename Runnable, typename ArgsType> template <typename ReturnType, typename Runnable, typename ArgsType>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment