OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_PREBIND_H_ | |
6 #define BASE_PREBIND_H_ | |
7 | |
8 #include "base/callback.h" | |
9 #include "base/tuple.h" | |
10 | |
11 namespace base { | |
12 | |
13 // TODO(ajwong): Can we somehow reuse Tuple here? | |
14 | |
15 // Invoker is the class where most of the magic happens. It stores the functor, | |
16 // the bound arguments, and free arguments. This class is specialized based on | |
17 // the types of all three of those components. | |
18 // | |
19 // Currently, the specializations are done by encoding the type information via | |
20 // nested function signature types. For example, a 2 argument function with 2 | |
21 // bound parameters is encoded using a type that is a "function of 2 arguments, | |
22 // that returns a pointer to a function of 2 arguments." The outer function's | |
23 // signature holds the types of the bound parameters, and the returned function | |
24 // pointer holds the type of the acutal function. | |
25 // | |
26 // For the same 2 argument function with 1 bound parameter, the type of Invoker | |
27 // is "a function of 1 argument that returns a pointer to a function of 2 | |
28 // arguments." | |
29 // | |
30 // This method of encoding the types allows us to keep the single class name | |
31 // "Invoker". | |
32 // | |
33 // TODO(ajwong): Change this to use the tuple type to avoid having such a crazy | |
34 // signature. | |
35 template <typename BoundFuncType> | |
36 class Invoker; | |
37 | |
38 // 1 -> 0 | |
39 template <typename R, typename X0, typename P0> | |
40 class Invoker<R(*(X0))(P0)> : public InvokerBase { | |
41 public: | |
42 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
| |
43 | |
44 Invoker(R(*f)(X0), const P0& p0) : f_(f), p0_(p0) {} | |
45 | |
46 static R PolymorphicInvoke(InvokerBase* invoker) { | |
47 SelfType* unwrapped = static_cast<SelfType*>(invoker); | |
48 return unwrapped->f_(unwrapped->p0_); | |
49 } | |
50 | |
51 private: | |
52 Func f_; | |
53 P0 p0_; | |
54 }; | |
55 | |
56 // 2 -> 0 | |
57 template <typename R, typename X0, typename P0, typename X1, typename P1> | |
58 class Invoker<R(*(X0,X1))(P0,P1)> : public InvokerBase { | |
59 public: | |
60 typedef InvokerBase<R(X0,X1)(P0,P1)> SelfType; | |
61 | |
62 Invoker(R(*f)(X0,X1), const P0& p0, const P1& p1) : f_(f), p0_(p0), p1_(p1) { | |
63 } | |
64 | |
65 static R PolymorphicInvoke(InvokerBase* invoker) { | |
66 SelfType* unwrapped = static_cast<SelfType*>(invoker); | |
67 return unwrapped->f_(unwrapped->p0_, unwrapped->p1_); | |
68 } | |
69 | |
70 private: | |
71 Func f_; | |
72 P0 p0_; | |
73 P1 p1_; | |
74 }; | |
75 | |
76 // 3 -> 0 | |
77 template <typename R, typename X0, typename P0, typename X1, typename P1, | |
78 typename X2, typename P2> | |
79 class Invoker<R(*(X0,X1,X2))(P0,P1,P2)> : public InvokerBase { | |
80 public: | |
81 typedef InvokerBase<R(X0,X1,X2)(P0,P1,P2)> SelfType; | |
82 | |
83 Invoker(R(*f)(X0,X1), const P0& p0, const P1& p1) | |
84 : f_(f), p0_(p0), p1_(p1), p2_(p2) { | |
85 } | |
86 | |
87 static R PolymorphicInvoke(InvokerBase* invoker) { | |
88 SelfType* unwrapped = static_cast<SelfType*>(invoker); | |
89 return unwrapped->f_(unwrapped->p0_, unwrapped->p1_); | |
90 } | |
91 | |
92 private: | |
93 Func f_; | |
94 P0 p0_; | |
95 P1 p1_; | |
96 P2 p2_; | |
97 }; | |
98 | |
99 // 2 -> 1 | |
100 template <typename R, typename X0, typename P0, typename X1, typename A1> | |
101 class Invoker<R(*(*(X0,X1))(P0))(A1)> : public InvokerBase { | |
102 public: | |
103 typedef InvokerBase<R(X0,X1)(P0)> SelfType; | |
104 | |
105 Invoker(R(*f)(X0,X1), const P0& p0, const P1& p1) : f_(f), p0_(p0) { | |
106 } | |
107 | |
108 static R PolymorphicInvoke(InvokerBase* invoker, const A1& a1) { | |
109 SelfType* unwrapped = static_cast<SelfType*>(invoker); | |
110 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
| |
111 } | |
112 | |
113 private: | |
114 Func f_; | |
115 P0 p0_; | |
116 }; | |
117 | |
118 // Function 1 -> 0 | |
119 template <typename R, typename X0, typename P0> | |
120 Callback<R(void)> | |
121 Prebind(R(*f)(X0), const P0& p0) { | |
122 typedef Invoker<R(*(X0))(P0)> InvokerType; | |
123 return Callback<R(void)>(new InvokerType(f, p0), | |
124 &InvokerType::PolymorphicInvoke); | |
125 } | |
126 | |
127 // Function 2 -> 0 | |
128 template <typename R, typename X0, typename P0, typename X1, typename P1> | |
129 Callback<R(void)> | |
130 Prebind(R(*f)(X0,X1), const P0& p0, const P1& p1) { | |
131 typedef Invoker<R(*(X0,X1))(P0,P1)> InvokerType; | |
132 return Callback<R(void)>(new InvokerType(f, p0, p1), | |
133 &InvokerType::PolymorphicInvoke); | |
134 } | |
135 | |
136 // Function 3 -> 0 | |
137 template <typename R, typename X0, typename P0, typename X1, typename P1 | |
138 typename X2, typename P2> | |
139 Callback<R(void)> | |
140 Prebind(R(*f)(X0,X1,X2), const P0& p0, const P1& p1, const P2& p2) { | |
141 typedef Invoker<R(*(X0,X1,X2))(P0,P1,P2)> InvokerType; | |
142 return Callback<R(void)>(new InvokerType(f, p0, p1, p2), | |
143 &InvokerType::PolymorphicInvoke); | |
144 } | |
145 | |
146 // Function 2 -> 1 | |
147 template <typename R, typename X0, typename P0, typename A0> | |
148 Callback<R(A0)> | |
149 Prebind(R(*f)(X0,X1), const P0& p0) { | |
150 typedef Invoker<R(*(*(X0,X1,X2))(P0))(A0)> InvokerType; | |
151 return Callback<R(A0)>(new InvokerType(f, p0), | |
152 &InvokerType::PolymorphicInvoke); | |
153 } | |
154 | |
155 } // namespace base | |
156 | |
157 #endif // BASE_PREBIND_H_ | |
OLD | NEW |