Page Speed Optimization Libraries  1.4.26.1
net/instaweb/util/public/function.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00018 
00019 #ifndef NET_INSTAWEB_UTIL_PUBLIC_FUNCTION_H_
00020 #define NET_INSTAWEB_UTIL_PUBLIC_FUNCTION_H_
00021 
00022 #include "net/instaweb/util/public/atomic_bool.h"
00023 #include "net/instaweb/util/public/basictypes.h"
00024 
00025 namespace net_instaweb {
00026 
00047 class Function {
00048  public:
00049   Function();
00050   virtual ~Function();
00051 
00060   void set_quit_requested_pointer(AtomicBool* x) { quit_requested_ = x; }
00061 
00066   bool quit_requested() const {
00067     return (quit_requested_ != NULL) && quit_requested_->value();
00068   }
00069 
00073   void CallRun();
00074   void CallCancel();
00075 
00080   void set_delete_after_callback(bool x) { delete_after_callback_ = x; }
00081 
00085   void Reset();
00086 
00087  protected:
00092   virtual void Run() = 0;
00093 
00100   virtual void Cancel() {}
00101 
00102  private:
00103   AtomicBool* quit_requested_;
00104   bool run_called_;
00105   bool cancel_called_;
00106   bool delete_after_callback_;
00107 
00108   DISALLOW_COPY_AND_ASSIGN(Function);
00109 };
00110 
00114 #define CALL_MEMBER_FN(object, ptrToMember) ((this->object)->*(ptrToMember))
00115 
00118 template<class C>
00119 class MemberFunctionBase : public Function {
00120  public:
00121   explicit MemberFunctionBase(C* c) : object_(c) {}
00122 
00123  protected:
00124   C* object_;
00125 };
00126 
00128 template<class C>
00129 class MemberFunction0 : public MemberFunctionBase<C> {
00130  public:
00131   typedef void (C::*Func)();
00132 
00134   MemberFunction0(Func f, C* c) : MemberFunctionBase<C>(c), run_(f),
00135                                   cancel_(NULL) {
00136   }
00137 
00139   MemberFunction0(Func f, Func cancel, C* c)
00140       : MemberFunctionBase<C>(c), run_(f), cancel_(cancel) {}
00141 
00142  protected:
00143   virtual void Run() { CALL_MEMBER_FN(object_, run_)(); }
00144   virtual void Cancel() {
00145     if (cancel_ != NULL) {
00146       CALL_MEMBER_FN(object_, cancel_)();
00147     }
00148   }
00149 
00150  private:
00151   Func run_;
00152   Func cancel_;
00153 };
00154 
00156 template<class C, typename T1>
00157 class MemberFunction1 : public MemberFunctionBase<C> {
00158  public:
00159   typedef void (C::*Func)(T1);
00160 
00162   MemberFunction1(Func f, C* c, T1 v1)
00163       : MemberFunctionBase<C>(c), run_(f), cancel_(NULL), v1_(v1) {}
00164 
00166   MemberFunction1(Func f, Func cancel,
00167                   C* c, T1 v1)
00168       : MemberFunctionBase<C>(c), run_(f), cancel_(cancel), v1_(v1)  {}
00169 
00170  protected:
00171   virtual void Run() { CALL_MEMBER_FN(object_, run_)(v1_); }
00172   virtual void Cancel() {
00173     if (cancel_ != NULL) {
00174       CALL_MEMBER_FN(object_, cancel_)(v1_);
00175     }
00176   }
00177 
00178  private:
00179   Func run_;
00180   Func cancel_;
00181   T1 v1_;
00182 };
00183 
00185 template<class C, typename T1, typename T2>
00186 class MemberFunction2 : public MemberFunctionBase<C> {
00187  public:
00188   typedef void (C::*Func)(T1, T2);
00189 
00191   MemberFunction2(Func f, C* c, T1 v1, T2 v2)
00192       : MemberFunctionBase<C>(c), run_(f), cancel_(NULL), v1_(v1), v2_(v2) {}
00193 
00195   MemberFunction2(Func f, Func cancel,
00196                   C* c, T1 v1, T2 v2)
00197       : MemberFunctionBase<C>(c),
00198         run_(f), cancel_(cancel),
00199         v1_(v1), v2_(v2)  {}
00200 
00201  protected:
00202   virtual void Run() { CALL_MEMBER_FN(object_, run_)(v1_, v2_); }
00203   virtual void Cancel() {
00204     if (cancel_ != NULL) {
00205       CALL_MEMBER_FN(object_, cancel_)(v1_, v2_);
00206     }
00207   }
00208 
00209  private:
00210   Func run_;
00211   Func cancel_;
00212   T1 v1_;
00213   T2 v2_;
00214 };
00215 
00217 template<class C, typename T1, typename T2, typename T3>
00218 class MemberFunction3 : public MemberFunctionBase<C> {
00219  public:
00220   typedef void (C::*Func)(T1, T2, T3);
00221 
00223   MemberFunction3(Func f, C* c, T1 v1, T2 v2, T3 v3)
00224       : MemberFunctionBase<C>(c), run_(f), cancel_(NULL), v1_(v1), v2_(v2),
00225         v3_(v3) {
00226   }
00227 
00229   MemberFunction3(Func f, Func cancel,
00230                   C* c, T1 v1, T2 v2, T3 v3)
00231       : MemberFunctionBase<C>(c),
00232         run_(f), cancel_(cancel),
00233         v1_(v1), v2_(v2), v3_(v3)  {}
00234 
00235  protected:
00236   virtual void Run() { CALL_MEMBER_FN(object_, run_)(v1_, v2_, v3_); }
00237   virtual void Cancel() {
00238     if (cancel_ != NULL) {
00239       CALL_MEMBER_FN(object_, cancel_)(v1_, v2_, v3_);
00240     }
00241   }
00242 
00243  private:
00244   Func run_;
00245   Func cancel_;
00246   T1 v1_;
00247   T2 v2_;
00248   T3 v3_;
00249 };
00250 
00251 #undef CALL_MEMBER_FN
00252 
00254 template<class C>
00255 Function* MakeFunction(C* object, void (C::*run)()) {
00256   return new MemberFunction0<C>(run, object);
00257 }
00258 
00261 template<class C>
00262 Function* MakeFunction(C* object, void (C::*run)(), void (C::*cancel)()) {
00263   return new MemberFunction0<C>(run, cancel, object);
00264 }
00265 
00267 template<class C, class T>
00268 Function* MakeFunction(C* object, void (C::*run)(T), T t) {
00269   return new MemberFunction1<C, T>(run, object, t);
00270 }
00271 
00274 template<class C, class T>
00275 Function* MakeFunction(C* object,
00276                        void (C::*run)(T),
00277                        void (C::*cancel)(T), T t) {
00278   return new MemberFunction1<C, T>(run, cancel, object, t);
00279 }
00280 
00282 template<class C, class T, class U>
00283 Function* MakeFunction(C* object, void (C::*run)(T, U), T t, U u) {
00284   return new MemberFunction2<C, T, U>(run, object, t, u);
00285 }
00286 
00289 template<class C, class T, class U>
00290 Function* MakeFunction(C* object,
00291                        void (C::*run)(T, U),
00292                        void (C::*cancel)(T, U),
00293                        T t, U u) {
00294   return new MemberFunction2<C, T, U>(run, cancel, object, t, u);
00295 }
00296 
00298 template<class C, class T, class U, class V>
00299 Function* MakeFunction(C* object,
00300                        void (C::*run)(T, U, V),
00301                        T t, U u, V v) {
00302   return new MemberFunction3<C, T, U, V>(run, object, t, u, v);
00303 }
00304 
00307 template<class C, class T, class U, class V>
00308 Function* MakeFunction(C* object,
00309                        void (C::*run)(T, U, V),
00310                        void (C::*cancel)(T, U, V),
00311                        T t, U u, V v) {
00312   return new MemberFunction3<C, T, U, V>(run, cancel, object, t, u, v);
00313 }
00314 
00315 }  
00316 
00317 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_FUNCTION_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines