Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
callback.h
Go to the documentation of this file.
1 // Copyright 2013 Google Inc. All Rights Reserved.
16 
17 #ifndef PAGESPEED_KERNEL_BASE_CALLBACK_H_
18 #define PAGESPEED_KERNEL_BASE_CALLBACK_H_
19 
20 
21 
22 namespace net_instaweb {
23 
43 template<class A1>
44 class Callback1 {
45  public:
46  Callback1() {}
47  virtual ~Callback1() {}
48  virtual void Run(A1) = 0;
49 };
50 
53 
55 template<class C, class A1, bool DeleteAfterRun>
56 class _MemberCallback_0_1 : public Callback1<A1> {
57  public:
58  typedef void (C::*MemberSignature)(A1);
59  typedef Callback1<A1> base;
60 
61  _MemberCallback_0_1(C* object, MemberSignature member)
62  : object_(object),
63  member_(member) {
64  }
65 
66  void Run(A1 t) {
67  (object_->*member_)(t);
68  if (DeleteAfterRun) {
69  delete this;
70  }
71  }
72 
73  private:
74  C* object_;
75  MemberSignature member_;
76 };
77 
79 template <class T1, class T2, class A1>
81 NewCallback(T1* obj, void (T2::*member)(A1)) {
82  return new _MemberCallback_0_1<T1, A1, true>(obj, member);
83 }
84 
86 template <class T1, class T2, class A1>
87 typename _MemberCallback_0_1<T1, A1, false>::base*
88 NewPermanentCallback(T1* obj, void (T2::*member)(A1)) {
89  return new _MemberCallback_0_1<T1, A1, false>(obj, member);
90 }
91 
93 template<typename T> struct remove_reference { typedef T type; };
94 template<typename T> struct remove_reference<T&> { typedef T type; };
95 
96 template <typename T>
97 struct ConstRef {
98  typedef typename remove_reference<T>::type base_type;
99  typedef const base_type& type;
100 };
101 
102 template <class T, class P1, class A1, bool DeleteAfterRun>
103 class _MemberCallback_1_1 : public Callback1<A1> {
104  public:
105  typedef Callback1<A1> base;
106  typedef void (T::*MemberSignature)(P1, A1);
107 
108  private:
109  T* object_;
110  MemberSignature member_;
111  typename remove_reference<P1>::type p1_;
112 
113  public:
114  _MemberCallback_1_1(T* object,
115  MemberSignature member,
116  typename ConstRef<P1>::type p1)
117  : object_(object),
118  member_(member),
119  p1_(p1) { }
120 
121  virtual void Run(A1 a1) {
122  (object_->*member_)(p1_, a1);
123  if (DeleteAfterRun) {
124  delete this;
125  }
126  }
127 };
128 
130 template <class T1, class T2, class P1, class A1>
132 NewCallback(T1* obj,
133  void (T2::*member)(P1, A1),
134  typename ConstRef<P1>::type p1) {
135  return new _MemberCallback_1_1<T1, P1, A1, true>(obj, member, p1);
136 }
137 
139 template <class T1, class T2, class P1, class A1>
140 inline typename _MemberCallback_1_1<T1, P1, A1, false>::base*
142  void (T2::*member)(P1, A1),
143  typename ConstRef<P1>::type p1) {
144  return new _MemberCallback_1_1<T1, P1, A1, false>(obj, member, p1);
145 }
146 
166 template<class A1, class A2>
167 class Callback2 {
168  public:
169  Callback2() {}
170  virtual ~Callback2() {}
171  virtual void Run(A1, A2) = 0;
172 };
173 
176 
178 template<class C, class A1, class A2, bool DeleteAfterRun>
179 class _MemberCallback_0_2 : public Callback2<A1, A2> {
180  public:
181  typedef void (C::*MemberSignature)(A1, A2);
182  typedef Callback2<A1, A2> base;
183 
184  _MemberCallback_0_2(C* object, MemberSignature member)
185  : object_(object),
186  member_(member) {
187  }
188 
189  void Run(A1 t1, A2 t2) {
190  (object_->*member_)(t1, t2);
191  if (DeleteAfterRun) {
192  delete this;
193  }
194  }
195 
196  private:
197  C* object_;
198  MemberSignature member_;
199 };
200 
202 template <class T1, class T2, class A1, class A2>
204 NewCallback(T1* obj, void (T2::*member)(A1, A2)) {
205  return new _MemberCallback_0_2<T1, A1, A2, true>(obj, member);
206 }
207 
209 template <class T1, class T2, class A1, class A2>
210 typename _MemberCallback_0_2<T1, A1, A2, false>::base*
211 NewPermanentCallback(T1* obj, void (T2::*member)(A1, A2)) {
212  return new _MemberCallback_0_2<T1, A1, A2, false>(obj, member);
213 }
214 
215 template <class T, class P1, class A1, class A2, bool DeleteAfterRun>
216 class _MemberCallback_2_1 : public Callback2<A1, A2> {
217  public:
218  typedef Callback2<A1, A2> base;
219  typedef void (T::*MemberSignature)(P1, A1, A2);
220 
221  private:
222  T* object_;
223  MemberSignature member_;
224  typename remove_reference<P1>::type p1_;
225 
226  public:
227  _MemberCallback_2_1(T* object,
228  MemberSignature member,
229  typename ConstRef<P1>::type p1)
230  : object_(object),
231  member_(member),
232  p1_(p1) { }
233 
234  virtual void Run(A1 a1, A2 a2) {
235  (object_->*member_)(p1_, a1, a2);
236  if (DeleteAfterRun) {
237  delete this;
238  }
239  }
240 };
241 
243 template <class T1, class T2, class P1, class A1, class A2>
245 NewCallback(T1* obj,
246  void (T2::*member)(P1, A1, A2),
247  typename ConstRef<P1>::type p1) {
248  return new _MemberCallback_2_1<T1, P1, A1, A2, true>(obj, member, p1);
249 }
250 
252 template <class T1, class T2, class P1, class A1, class A2>
253 inline typename _MemberCallback_2_1<T1, P1, A1, A2, false>::base*
255  void (T2::*member)(P1, A1, A2),
256  typename ConstRef<P1>::type p1) {
257  return new _MemberCallback_2_1<T1, P1, A1, A2, false>(obj, member, p1);
258 }
259 
260 }
261 
262 
263 #endif
Definition: callback.h:216
Definition: callback.h:103
Definition: callback.h:167
Definition: callback.h:56
Definition: callback.h:97
_MemberCallback_0_1< T1, A1, true >::base * NewCallback(T1 *obj, void(T2::*member)(A1))
Creates a callback that automatically gets deleted after being run.
Definition: callback.h:81
_MemberCallback_0_1< T1, A1, false >::base * NewPermanentCallback(T1 *obj, void(T2::*member)(A1))
Creates a callback that does not get deleted after being run.
Definition: callback.h:88
Specified by TR1 [4.7.2] Reference modifications.
Definition: callback.h:93
Definition: callback.h:44
Definition: callback.h:179