Index: base/prebind.h |
diff --git a/base/prebind.h b/base/prebind.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3588162c2f65c25aaa4c069625f40135270ceb1f |
--- /dev/null |
+++ b/base/prebind.h |
@@ -0,0 +1,157 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef BASE_PREBIND_H_ |
+#define BASE_PREBIND_H_ |
+ |
+#include "base/callback.h" |
+#include "base/tuple.h" |
+ |
+namespace base { |
+ |
+// TODO(ajwong): Can we somehow reuse Tuple here? |
+ |
+// Invoker is the class where most of the magic happens. It stores the functor, |
+// the bound arguments, and free arguments. This class is specialized based on |
+// the types of all three of those components. |
+// |
+// Currently, the specializations are done by encoding the type information via |
+// nested function signature types. For example, a 2 argument function with 2 |
+// bound parameters is encoded using a type that is a "function of 2 arguments, |
+// that returns a pointer to a function of 2 arguments." The outer function's |
+// signature holds the types of the bound parameters, and the returned function |
+// pointer holds the type of the acutal function. |
+// |
+// For the same 2 argument function with 1 bound parameter, the type of Invoker |
+// is "a function of 1 argument that returns a pointer to a function of 2 |
+// arguments." |
+// |
+// This method of encoding the types allows us to keep the single class name |
+// "Invoker". |
+// |
+// TODO(ajwong): Change this to use the tuple type to avoid having such a crazy |
+// signature. |
+template <typename BoundFuncType> |
+class Invoker; |
+ |
+// 1 -> 0 |
+template <typename R, typename X0, typename P0> |
+class Invoker<R(*(X0))(P0)> : public InvokerBase { |
+ public: |
+ typedef InvokerBase<R(X0)(P0)> SelfType; |
willchan no longer on Chromium
2011/01/19 16:57:43
I'm frankly a bit confused why this is InvokerBase
awong
2011/01/20 20:27:35
Errr...yes, that's just a mistake. I uploaded wit
|
+ |
+ Invoker(R(*f)(X0), const P0& p0) : f_(f), p0_(p0) {} |
+ |
+ static R PolymorphicInvoke(InvokerBase* invoker) { |
+ SelfType* unwrapped = static_cast<SelfType*>(invoker); |
+ return unwrapped->f_(unwrapped->p0_); |
+ } |
+ |
+ private: |
+ Func f_; |
+ P0 p0_; |
+}; |
+ |
+// 2 -> 0 |
+template <typename R, typename X0, typename P0, typename X1, typename P1> |
+class Invoker<R(*(X0,X1))(P0,P1)> : public InvokerBase { |
+ public: |
+ typedef InvokerBase<R(X0,X1)(P0,P1)> SelfType; |
+ |
+ Invoker(R(*f)(X0,X1), const P0& p0, const P1& p1) : f_(f), p0_(p0), p1_(p1) { |
+ } |
+ |
+ static R PolymorphicInvoke(InvokerBase* invoker) { |
+ SelfType* unwrapped = static_cast<SelfType*>(invoker); |
+ return unwrapped->f_(unwrapped->p0_, unwrapped->p1_); |
+ } |
+ |
+ private: |
+ Func f_; |
+ P0 p0_; |
+ P1 p1_; |
+}; |
+ |
+// 3 -> 0 |
+template <typename R, typename X0, typename P0, typename X1, typename P1, |
+ typename X2, typename P2> |
+class Invoker<R(*(X0,X1,X2))(P0,P1,P2)> : public InvokerBase { |
+ public: |
+ typedef InvokerBase<R(X0,X1,X2)(P0,P1,P2)> SelfType; |
+ |
+ Invoker(R(*f)(X0,X1), const P0& p0, const P1& p1) |
+ : f_(f), p0_(p0), p1_(p1), p2_(p2) { |
+ } |
+ |
+ static R PolymorphicInvoke(InvokerBase* invoker) { |
+ SelfType* unwrapped = static_cast<SelfType*>(invoker); |
+ return unwrapped->f_(unwrapped->p0_, unwrapped->p1_); |
+ } |
+ |
+ private: |
+ Func f_; |
+ P0 p0_; |
+ P1 p1_; |
+ P2 p2_; |
+}; |
+ |
+// 2 -> 1 |
+template <typename R, typename X0, typename P0, typename X1, typename A1> |
+class Invoker<R(*(*(X0,X1))(P0))(A1)> : public InvokerBase { |
+ public: |
+ typedef InvokerBase<R(X0,X1)(P0)> SelfType; |
+ |
+ Invoker(R(*f)(X0,X1), const P0& p0, const P1& p1) : f_(f), p0_(p0) { |
+ } |
+ |
+ static R PolymorphicInvoke(InvokerBase* invoker, const A1& a1) { |
+ SelfType* unwrapped = static_cast<SelfType*>(invoker); |
+ return unwrapped->f_(unwrapped->p0_, const_cast<A1&>(a1)); |
willchan no longer on Chromium
2011/01/19 16:57:43
I'm trying to grok everything, but this const_cast
awong
2011/01/20 20:27:35
Actually, I do think it's necessary. If A1 == "in
willchan no longer on Chromium
2011/01/21 02:30:12
Hmmmm, I'm not sure this works. How does it handle
awong
2011/01/21 12:00:36
Ah, I see what you're saying now. Because I used
|
+ } |
+ |
+ private: |
+ Func f_; |
+ P0 p0_; |
+}; |
+ |
+// Function 1 -> 0 |
+template <typename R, typename X0, typename P0> |
+Callback<R(void)> |
+Prebind(R(*f)(X0), const P0& p0) { |
+ typedef Invoker<R(*(X0))(P0)> InvokerType; |
+ return Callback<R(void)>(new InvokerType(f, p0), |
+ &InvokerType::PolymorphicInvoke); |
+} |
+ |
+// Function 2 -> 0 |
+template <typename R, typename X0, typename P0, typename X1, typename P1> |
+Callback<R(void)> |
+Prebind(R(*f)(X0,X1), const P0& p0, const P1& p1) { |
+ typedef Invoker<R(*(X0,X1))(P0,P1)> InvokerType; |
+ return Callback<R(void)>(new InvokerType(f, p0, p1), |
+ &InvokerType::PolymorphicInvoke); |
+} |
+ |
+// Function 3 -> 0 |
+template <typename R, typename X0, typename P0, typename X1, typename P1 |
+ typename X2, typename P2> |
+Callback<R(void)> |
+Prebind(R(*f)(X0,X1,X2), const P0& p0, const P1& p1, const P2& p2) { |
+ typedef Invoker<R(*(X0,X1,X2))(P0,P1,P2)> InvokerType; |
+ return Callback<R(void)>(new InvokerType(f, p0, p1, p2), |
+ &InvokerType::PolymorphicInvoke); |
+} |
+ |
+// Function 2 -> 1 |
+template <typename R, typename X0, typename P0, typename A0> |
+Callback<R(A0)> |
+Prebind(R(*f)(X0,X1), const P0& p0) { |
+ typedef Invoker<R(*(*(X0,X1,X2))(P0))(A0)> InvokerType; |
+ return Callback<R(A0)>(new InvokerType(f, p0), |
+ &InvokerType::PolymorphicInvoke); |
+} |
+ |
+} // namespace base |
+ |
+#endif // BASE_PREBIND_H_ |