Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
function.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_BASE_FUNCTION_H_
20 #define PAGESPEED_KERNEL_BASE_FUNCTION_H_
21 
24 
25 namespace net_instaweb {
26 
47 class Function {
48  public:
49  Function();
50  virtual ~Function();
51 
60  void set_quit_requested_pointer(AtomicBool* x) { quit_requested_ = x; }
61 
66  bool quit_requested() const {
67  return (quit_requested_ != NULL) && quit_requested_->value();
68  }
69 
73  void CallRun();
74  void CallCancel();
75 
80  void set_delete_after_callback(bool x) { delete_after_callback_ = x; }
81 
85  void Reset();
86 
87  protected:
92  virtual void Run() = 0;
93 
100  virtual void Cancel() {}
101 
102  private:
103  AtomicBool* quit_requested_;
104  bool run_called_;
105  bool cancel_called_;
106  bool delete_after_callback_;
107 
108 
109 };
110 
114 #define CALL_MEMBER_FN(object, ptrToMember) ((this->object)->*(ptrToMember))
115 
118 template<class C>
119 class MemberFunctionBase : public Function {
120  public:
121  explicit MemberFunctionBase(C* c) : object_(c) {}
122 
123  protected:
124  C* object_;
125 };
126 
128 template<class C>
130  public:
131  typedef void (C::*Func)();
132 
134  MemberFunction0(Func f, C* c) : MemberFunctionBase<C>(c), run_(f),
135  cancel_(NULL) {
136  }
137 
139  MemberFunction0(Func f, Func cancel, C* c)
140  : MemberFunctionBase<C>(c), run_(f), cancel_(cancel) {}
141 
142  protected:
143  virtual void Run() { CALL_MEMBER_FN(object_, run_)(); }
144  virtual void Cancel() {
145  if (cancel_ != NULL) {
146  CALL_MEMBER_FN(object_, cancel_)();
147  }
148  }
149 
150  private:
151  Func run_;
152  Func cancel_;
153 };
154 
156 template<class C, typename T1>
158  public:
159  typedef void (C::*Func)(T1);
160 
162  MemberFunction1(Func f, C* c, T1 v1)
163  : MemberFunctionBase<C>(c), run_(f), cancel_(NULL), v1_(v1) {}
164 
166  MemberFunction1(Func f, Func cancel,
167  C* c, T1 v1)
168  : MemberFunctionBase<C>(c), run_(f), cancel_(cancel), v1_(v1) {}
169 
170  protected:
171  virtual void Run() { CALL_MEMBER_FN(object_, run_)(v1_); }
172  virtual void Cancel() {
173  if (cancel_ != NULL) {
174  CALL_MEMBER_FN(object_, cancel_)(v1_);
175  }
176  }
177 
178  private:
179  Func run_;
180  Func cancel_;
181  T1 v1_;
182 };
183 
185 template<class C, typename T1, typename T2>
187  public:
188  typedef void (C::*Func)(T1, T2);
189 
191  MemberFunction2(Func f, C* c, T1 v1, T2 v2)
192  : MemberFunctionBase<C>(c), run_(f), cancel_(NULL), v1_(v1), v2_(v2) {}
193 
195  MemberFunction2(Func f, Func cancel,
196  C* c, T1 v1, T2 v2)
197  : MemberFunctionBase<C>(c),
198  run_(f), cancel_(cancel),
199  v1_(v1), v2_(v2) {}
200 
201  protected:
202  virtual void Run() { CALL_MEMBER_FN(object_, run_)(v1_, v2_); }
203  virtual void Cancel() {
204  if (cancel_ != NULL) {
205  CALL_MEMBER_FN(object_, cancel_)(v1_, v2_);
206  }
207  }
208 
209  private:
210  Func run_;
211  Func cancel_;
212  T1 v1_;
213  T2 v2_;
214 };
215 
217 template<class C, typename T1, typename T2, typename T3>
219  public:
220  typedef void (C::*Func)(T1, T2, T3);
221 
223  MemberFunction3(Func f, C* c, T1 v1, T2 v2, T3 v3)
224  : MemberFunctionBase<C>(c), run_(f), cancel_(NULL), v1_(v1), v2_(v2),
225  v3_(v3) {
226  }
227 
229  MemberFunction3(Func f, Func cancel,
230  C* c, T1 v1, T2 v2, T3 v3)
231  : MemberFunctionBase<C>(c),
232  run_(f), cancel_(cancel),
233  v1_(v1), v2_(v2), v3_(v3) {}
234 
235  protected:
236  virtual void Run() { CALL_MEMBER_FN(object_, run_)(v1_, v2_, v3_); }
237  virtual void Cancel() {
238  if (cancel_ != NULL) {
239  CALL_MEMBER_FN(object_, cancel_)(v1_, v2_, v3_);
240  }
241  }
242 
243  private:
244  Func run_;
245  Func cancel_;
246  T1 v1_;
247  T2 v2_;
248  T3 v3_;
249 };
250 
252 template<class C, typename T1, typename T2, typename T3, typename T4>
254  public:
255  typedef void (C::*Func)(T1, T2, T3, T4);
256 
258  MemberFunction4(Func f, C* c, T1 v1, T2 v2, T3 v3, T4 v4)
259  : MemberFunctionBase<C>(c), run_(f), cancel_(NULL), v1_(v1), v2_(v2),
260  v3_(v3), v4_(v4) {
261  }
262 
264  MemberFunction4(Func f, Func cancel,
265  C* c, T1 v1, T2 v2, T3 v3, T4 v4)
266  : MemberFunctionBase<C>(c),
267  run_(f), cancel_(cancel),
268  v1_(v1), v2_(v2), v3_(v3), v4_(v4) {}
269 
270  protected:
271  virtual void Run() { CALL_MEMBER_FN(object_, run_)(v1_, v2_, v3_, v4_); }
272  virtual void Cancel() {
273  if (cancel_ != NULL) {
274  CALL_MEMBER_FN(object_, cancel_)(v1_, v2_, v3_, v4_);
275  }
276  }
277 
278  private:
279  Func run_;
280  Func cancel_;
281  T1 v1_;
282  T2 v2_;
283  T3 v3_;
284  T4 v4_;
285 };
286 
287 #undef CALL_MEMBER_FN
288 
290 template<class C>
291 Function* MakeFunction(C* object, void (C::*run)()) {
292  return new MemberFunction0<C>(run, object);
293 }
294 
297 template<class C>
298 Function* MakeFunction(C* object, void (C::*run)(), void (C::*cancel)()) {
299  return new MemberFunction0<C>(run, cancel, object);
300 }
301 
303 template<class C, class T>
304 Function* MakeFunction(C* object, void (C::*run)(T), T t) {
305  return new MemberFunction1<C, T>(run, object, t);
306 }
307 
310 template<class C, class T>
312  void (C::*run)(T),
313  void (C::*cancel)(T), T t) {
314  return new MemberFunction1<C, T>(run, cancel, object, t);
315 }
316 
318 template<class C, class T, class U>
319 Function* MakeFunction(C* object, void (C::*run)(T, U), T t, U u) {
320  return new MemberFunction2<C, T, U>(run, object, t, u);
321 }
322 
325 template<class C, class T, class U>
327  void (C::*run)(T, U),
328  void (C::*cancel)(T, U),
329  T t, U u) {
330  return new MemberFunction2<C, T, U>(run, cancel, object, t, u);
331 }
332 
334 template<class C, class T, class U, class V>
336  void (C::*run)(T, U, V),
337  T t, U u, V v) {
338  return new MemberFunction3<C, T, U, V>(run, object, t, u, v);
339 }
340 
343 template<class C, class T, class U, class V>
345  void (C::*run)(T, U, V),
346  void (C::*cancel)(T, U, V),
347  T t, U u, V v) {
348  return new MemberFunction3<C, T, U, V>(run, cancel, object, t, u, v);
349 }
350 
352 template<class C, class T, class U, class V, class W>
354  void (C::*run)(T, U, V, W),
355  T t, U u, V v, W w) {
356  return new MemberFunction4<C, T, U, V, W>(run, object, t, u, v, w);
357 }
358 
361 template<class C, class T, class U, class V, class W>
363  void (C::*run)(T, U, V, W),
364  void (C::*cancel)(T, U, V, W),
365  T t, U u, V v, W w) {
366  return new MemberFunction4<C, T, U, V, W>(run, cancel, object, t, u, v, w);
367 }
368 
369 }
370 
371 #endif
bool quit_requested() const
Definition: function.h:66
Function * MakeFunction(C *object, void(C::*run)())
Makes a Function* that calls a 0-arg class method.
Definition: function.h:291
Captures a delayed call to a 2-arg member function as a closure.
Definition: function.h:186
Definition: atomic_bool.h:31
virtual void Run()
Definition: function.h:171
Captures a delayed call to a 4-arg member function as a closure.
Definition: function.h:253
virtual void Run()
Definition: function.h:202
MemberFunction3(Func f, C *c, T1 v1, T2 v2, T3 v3)
Constructor supplying a Run method, but no Cancel method.
Definition: function.h:223
virtual void Cancel()
Definition: function.h:272
virtual void Cancel()
Definition: function.h:237
MemberFunction0(Func f, C *c)
Constructor supplying a Run method, but no Cancel method.
Definition: function.h:134
virtual void Run()=0
Definition: function.h:47
Captures a delayed call to a 0-arg member function as a closure.
Definition: function.h:129
MemberFunction0(Func f, Func cancel, C *c)
Constructor supplying a Run method and a Cancel method.
Definition: function.h:139
virtual void Run()
Definition: function.h:143
virtual void Run()
Definition: function.h:236
MemberFunction3(Func f, Func cancel, C *c, T1 v1, T2 v2, T3 v3)
Constructor supplying a Run method and a Cancel method.
Definition: function.h:229
virtual void Cancel()
Definition: function.h:100
virtual void Cancel()
Definition: function.h:144
virtual void Cancel()
Definition: function.h:172
void set_delete_after_callback(bool x)
Definition: function.h:80
MemberFunction2(Func f, Func cancel, C *c, T1 v1, T2 v2)
Constructor supplying a Run method and a Cancel method.
Definition: function.h:195
Definition: function.h:119
MemberFunction2(Func f, C *c, T1 v1, T2 v2)
Constructor supplying a Run method, but no Cancel method.
Definition: function.h:191
Captures a delayed call to a 1-arg member function as a closure.
Definition: function.h:157
#define CALL_MEMBER_FN(object, ptrToMember)
Definition: function.h:114
virtual void Run()
Definition: function.h:271
Captures a delayed call to a 3-arg member function as a closure.
Definition: function.h:218
MemberFunction4(Func f, C *c, T1 v1, T2 v2, T3 v3, T4 v4)
Constructor supplying a Run method, but no Cancel method.
Definition: function.h:258
MemberFunction1(Func f, Func cancel, C *c, T1 v1)
Constructor supplying a Run method and a Cancel method.
Definition: function.h:166
MemberFunction1(Func f, C *c, T1 v1)
Constructor supplying a Run method, but no Cancel method.
Definition: function.h:162
void set_quit_requested_pointer(AtomicBool *x)
Definition: function.h:60
MemberFunction4(Func f, Func cancel, C *c, T1 v1, T2 v2, T3 v3, T4 v4)
Constructor supplying a Run method and a Cancel method.
Definition: function.h:264
virtual void Cancel()
Definition: function.h:203