summaryrefslogtreecommitdiffstats
path: root/base/callback.h.pump
diff options
context:
space:
mode:
authorsail@chromium.org <sail@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-14 21:48:54 +0000
committersail@chromium.org <sail@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-14 21:48:54 +0000
commit115288648041898b6d50f7d2e17391e2d1972a76 (patch)
treeeee67d927a7ebb40e87848924a8f26c5c5cb99ca /base/callback.h.pump
parente91ac22d99252095839ca7c82c092972bd56445e (diff)
downloadchromium_src-115288648041898b6d50f7d2e17391e2d1972a76.zip
chromium_src-115288648041898b6d50f7d2e17391e2d1972a76.tar.gz
chromium_src-115288648041898b6d50f7d2e17391e2d1972a76.tar.bz2
Revert 114494 - Remove BindStateHolder and have Bind() return a Callback<> object directly.
This removes some complexity and also fixes a bug where if you call Bind() with the result of Bind(), the resulting Callback would only be valid during the first call. Ouch. BUG=none TEST=new unittests Review URL: http://codereview.chromium.org/8738001 TBR=ajwong@chromium.org Review URL: http://codereview.chromium.org/8914022 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@114495 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/callback.h.pump')
-rw-r--r--base/callback.h.pump58
1 files changed, 28 insertions, 30 deletions
diff --git a/base/callback.h.pump b/base/callback.h.pump
index 2216a3c..91a41e0 100644
--- a/base/callback.h.pump
+++ b/base/callback.h.pump
@@ -134,28 +134,29 @@ $var MAX_ARITY = 7
// The Callback classes represent a generic function pointer. Internally,
// it stores a refcounted piece of state that represents the target function
// and all its bound parameters. Each Callback specialization has a templated
-// constructor that takes an BindState<>*. In the context of the constructor,
-// the static type of this BindState<> pointer uniquely identifies the
-// function it is representing, all its bound parameters, and a Run() method
-// that is capable of invoking the target.
+// constructor that takes an BindStateHolder<> object. In the context of
+// the constructor, the static type of this BindStateHolder<> object
+// uniquely identifies the function it is representing, all its bound
+// parameters, and a DoInvoke() that is capable of invoking the target.
//
-// Callback's constructor takes the BindState<>* that has the full static type
-// and erases the target function type as well as the types of the bound
-// parameters. It does this by storing a pointer to the specific Run()
-// function, and upcasting the state of BindState<>* to a
-// BindStateBase*. This is safe as long as this BindStateBase pointer
-// is only used with the stored Run() pointer.
+// Callback's constructor is takes the BindStateHolder<> that has the
+// full static type and erases the target function type, and the bound
+// parameters. It does this by storing a pointer to the specific DoInvoke()
+// function, and upcasting the state of BindStateHolder<> to a
+// BindStateBase. This is safe as long as this BindStateBase pointer
+// is only used with the stored DoInvoke() pointer.
//
-// To BindState<> objects are created inside the Bind() functions.
-// These functions, along with a set of internal templates, are responsible for
+// To create BindStateHolder<> objects, we use the Bind() functions.
+// These functions, along with a set of internal templates, are reponsible for
//
// - Unwrapping the function signature into return type, and parameters
// - Determining the number of parameters that are bound
-// - Creating the BindState storing the bound parameters
+// - Creating the storage for the bound parameters
// - Performing compile-time asserts to avoid error-prone behavior
-// - Returning an Callback<> with an arity matching the number of unbound
-// parameters and that knows the correct refcounting semantics for the
-// target object if we are binding a method.
+// - Returning an BindStateHolder<> with an DoInvoke() that has an arity
+// matching the number of unbound parameters, and knows the correct
+// refcounting semantics for the target object if we are binding a class
+// method.
//
// The Bind functions do the above using type-inference, and template
// specializations.
@@ -243,11 +244,6 @@ namespace base {
template <typename Sig>
class Callback;
-namespace internal {
-template <typename Runnable, typename RunType, typename BoundArgsType>
-struct BindState;
-} // namespace internal
-
$range ARITY 0..MAX_ARITY
$for ARITY [[
@@ -264,20 +260,22 @@ class Callback<R($for ARG , [[A$(ARG)]])> : public internal::CallbackBase {
public:
typedef R(RunType)($for ARG , [[A$(ARG)]]);
- Callback() : CallbackBase(NULL) { }
+ Callback() : CallbackBase(NULL, NULL) { }
+ // We pass BindStateHolder by const ref to avoid incurring an
+ // unnecessary AddRef/Unref pair even though we will modify the object.
+ // We cannot use a normal reference because the compiler will warn
+ // since this is often used on a return value, which is a temporary.
+ //
// Note that this constructor CANNOT be explicit, and that Bind() CANNOT
// return the exact Callback<> type. See base/bind.h for details.
- template <typename Runnable, typename RunType, typename BoundArgsType>
- Callback(internal::BindState<Runnable, RunType, BoundArgsType>* bind_state)
- : CallbackBase(bind_state) {
-
- // Force the assignment to a local variable of PolymorphicInvoke
+ template <typename T>
+ Callback(const internal::BindStateHolder<T>& bind_state_holder)
+ : CallbackBase(NULL, &bind_state_holder.bind_state_) {
+ // Force the assignment to a location variable of PolymorphicInvoke
// so the compiler will typecheck that the passed in Run() method has
// the correct type.
- PolymorphicInvoke invoke_func =
- &internal::BindState<Runnable, RunType, BoundArgsType>
- ::InvokerType::Run;
+ PolymorphicInvoke invoke_func = &T::InvokerType::Run;
polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
}