OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 GOOGLE_CACHEINVALIDATION_CALLBACK_H_ |
| 6 #define GOOGLE_CACHEINVALIDATION_CALLBACK_H_ |
| 7 |
| 8 #include "base/callback.h" |
| 9 |
| 10 #define INVALIDATION_CALLBACK1_TYPE(Arg1) ::Callback1<Arg1>::Type |
| 11 |
| 12 // Below are a collection of types and functions to make up for the |
| 13 // limited capabilities of Chrome's callback.h. |
| 14 |
| 15 namespace invalidation { |
| 16 |
| 17 typedef ::Callback0::Type Closure; |
| 18 |
| 19 static inline void DoNothing() {} |
| 20 |
| 21 template <class T> |
| 22 bool IsCallbackRepeatable(const T* callback) { |
| 23 return true; |
| 24 } |
| 25 |
| 26 namespace internal { |
| 27 |
| 28 // Identity<T>::type is a typedef of T. Useful for preventing the |
| 29 // compiler from inferring the type of an argument in templates. |
| 30 template <typename T> |
| 31 struct Identity { |
| 32 typedef T type; |
| 33 }; |
| 34 |
| 35 // Specified by TR1 [4.7.2] |
| 36 template <typename T> struct remove_reference { |
| 37 typedef T type; |
| 38 }; |
| 39 |
| 40 template <typename T> struct remove_reference<T&> { |
| 41 typedef T type; |
| 42 }; |
| 43 |
| 44 } // namespace internal |
| 45 |
| 46 // base/callback.h already handles partially applying member functions |
| 47 // to an object, so just route to that. (We only need the one-arg |
| 48 // case.) |
| 49 |
| 50 template <class T, typename Arg1> |
| 51 typename Callback1<Arg1>::Type* NewPermanentCallback( |
| 52 T* object, void (T::*method)(Arg1)) { |
| 53 return new CallbackImpl<T, void (T::*)(Arg1), Tuple1<Arg1> >(object, method); |
| 54 } |
| 55 |
| 56 // Define function runners for the partial application combinations |
| 57 // that we need. |
| 58 |
| 59 class ZeroArgFunctionRunner : public CallbackRunner<Tuple0> { |
| 60 public: |
| 61 ZeroArgFunctionRunner(void (*fn)()) : fn_(fn) {} |
| 62 |
| 63 virtual ~ZeroArgFunctionRunner() {} |
| 64 |
| 65 virtual void RunWithParams(const Tuple0& params) { fn_(); } |
| 66 |
| 67 private: |
| 68 void (*fn_)(); |
| 69 }; |
| 70 |
| 71 template <class T> |
| 72 class ZeroArgMethodRunner : public CallbackRunner<Tuple0> { |
| 73 public: |
| 74 ZeroArgMethodRunner(T* obj, void (T::*meth)()) : obj_(obj), meth_(meth) {} |
| 75 |
| 76 virtual ~ZeroArgMethodRunner() {} |
| 77 |
| 78 virtual void RunWithParams(const Tuple0& params) { |
| 79 (obj_->*meth_)(); |
| 80 } |
| 81 |
| 82 private: |
| 83 T* obj_; |
| 84 void (T::*meth_)(); |
| 85 }; |
| 86 |
| 87 template <class T, typename Arg1> |
| 88 class OneArgCallbackRunner : public CallbackRunner<Tuple0> { |
| 89 public: |
| 90 OneArgCallbackRunner(T* obj, void (T::*meth)(Arg1), |
| 91 Arg1 arg1) |
| 92 : obj_(obj), meth_(meth), arg1_(arg1) {} |
| 93 |
| 94 virtual ~OneArgCallbackRunner() {} |
| 95 |
| 96 virtual void RunWithParams(const Tuple0& params) { |
| 97 (obj_->*meth_)(arg1_); |
| 98 } |
| 99 |
| 100 private: |
| 101 T* obj_; |
| 102 void (T::*meth_)(Arg1); |
| 103 typename internal::remove_reference<Arg1>::type arg1_; |
| 104 }; |
| 105 |
| 106 template <typename Arg1, typename Arg2> |
| 107 class TwoArgFunctionRunner : public CallbackRunner<Tuple0> { |
| 108 public: |
| 109 TwoArgFunctionRunner(void (*fn)(Arg1, Arg2), Arg1 arg1, Arg2 arg2) |
| 110 : fn_(fn), arg1_(arg1), arg2_(arg2) {} |
| 111 |
| 112 virtual ~TwoArgFunctionRunner() {} |
| 113 |
| 114 virtual void RunWithParams(const Tuple0& params) { fn_(arg1_, arg2_); } |
| 115 |
| 116 private: |
| 117 void (*fn_)(Arg1, Arg2); |
| 118 typename internal::remove_reference<Arg1>::type arg1_; |
| 119 typename internal::remove_reference<Arg2>::type arg2_; |
| 120 }; |
| 121 |
| 122 template <class T, typename Arg1, typename Arg2> |
| 123 class TwoArgCallbackRunner : public CallbackRunner<Tuple0> { |
| 124 public: |
| 125 TwoArgCallbackRunner(T* obj, void (T::*meth)(Arg1, Arg2), |
| 126 Arg1 arg1, Arg2 arg2) |
| 127 : obj_(obj), meth_(meth), arg1_(arg1), arg2_(arg2) {} |
| 128 |
| 129 virtual ~TwoArgCallbackRunner() {} |
| 130 |
| 131 virtual void RunWithParams(const Tuple0& params) { |
| 132 (obj_->*meth_)(arg1_, arg2_); |
| 133 } |
| 134 |
| 135 private: |
| 136 T* obj_; |
| 137 void (T::*meth_)(Arg1, Arg2); |
| 138 typename internal::remove_reference<Arg1>::type arg1_; |
| 139 typename internal::remove_reference<Arg2>::type arg2_; |
| 140 }; |
| 141 |
| 142 // Then route the appropriate overloads of NewPermanentCallback() to |
| 143 // use the above. |
| 144 |
| 145 inline Callback0::Type* NewPermanentCallback(void (*fn)()) { |
| 146 return new ZeroArgFunctionRunner(fn); |
| 147 } |
| 148 |
| 149 template <class T1, class T2> |
| 150 typename Callback0::Type* NewPermanentCallback( |
| 151 T1* object, void (T2::*method)()) { |
| 152 return new ZeroArgMethodRunner<T1>(object, method); |
| 153 } |
| 154 |
| 155 template <class T1, class T2, typename Arg1> |
| 156 typename Callback0::Type* NewPermanentCallback( |
| 157 T1* object, |
| 158 void (T2::*method)(Arg1), |
| 159 typename internal::Identity<Arg1>::type arg1) { |
| 160 return new OneArgCallbackRunner<T1, Arg1>(object, method, arg1); |
| 161 } |
| 162 |
| 163 template <typename Arg1, typename Arg2> |
| 164 Callback0::Type* NewPermanentCallback( |
| 165 void (*fn)(Arg1, Arg2), |
| 166 typename internal::Identity<Arg1>::type arg1, |
| 167 typename internal::Identity<Arg2>::type arg2) { |
| 168 return new TwoArgFunctionRunner<Arg1, Arg2>(fn, arg1, arg2); |
| 169 } |
| 170 |
| 171 template <class T1, class T2, typename Arg1, typename Arg2> |
| 172 typename Callback0::Type* NewPermanentCallback( |
| 173 T1* object, |
| 174 void (T2::*method)(Arg1, Arg2), |
| 175 typename internal::Identity<Arg1>::type arg1, |
| 176 typename internal::Identity<Arg2>::type arg2) { |
| 177 return new TwoArgCallbackRunner<T1, Arg1, Arg2>(object, method, arg1, arg2); |
| 178 } |
| 179 |
| 180 } // namespace invalidation |
| 181 |
| 182 #endif // GOOGLE_CACHEINVALIDATION_CALLBACK_H_ |
OLD | NEW |