1 #ifndef GOOGLE_PROTOBUF_STUBS_CALLBACK_H_
2 #define GOOGLE_PROTOBUF_STUBS_CALLBACK_H_
3 
4 #include <google/protobuf/stubs/macros.h>
5 #include <google/protobuf/stubs/type_traits.h>
6 
7 // ===================================================================
8 // emulates google3/base/callback.h
9 
10 namespace google {
11 namespace protobuf {
12 
13 // Abstract interface for a callback.  When calling an RPC, you must provide
14 // a Closure to call when the procedure completes.  See the Service interface
15 // in service.h.
16 //
17 // To automatically construct a Closure which calls a particular function or
18 // method with a particular set of parameters, use the NewCallback() function.
19 // Example:
20 //   void FooDone(const FooResponse* response) {
21 //     ...
22 //   }
23 //
24 //   void CallFoo() {
25 //     ...
26 //     // When done, call FooDone() and pass it a pointer to the response.
27 //     Closure* callback = NewCallback(&FooDone, response);
28 //     // Make the call.
29 //     service->Foo(controller, request, response, callback);
30 //   }
31 //
32 // Example that calls a method:
33 //   class Handler {
34 //    public:
35 //     ...
36 //
37 //     void FooDone(const FooResponse* response) {
38 //       ...
39 //     }
40 //
41 //     void CallFoo() {
42 //       ...
43 //       // When done, call FooDone() and pass it a pointer to the response.
44 //       Closure* callback = NewCallback(this, &Handler::FooDone, response);
45 //       // Make the call.
46 //       service->Foo(controller, request, response, callback);
47 //     }
48 //   };
49 //
50 // Currently NewCallback() supports binding zero, one, or two arguments.
51 //
52 // Callbacks created with NewCallback() automatically delete themselves when
53 // executed.  They should be used when a callback is to be called exactly
54 // once (usually the case with RPC callbacks).  If a callback may be called
55 // a different number of times (including zero), create it with
56 // NewPermanentCallback() instead.  You are then responsible for deleting the
57 // callback (using the "delete" keyword as normal).
58 //
59 // Note that NewCallback() is a bit touchy regarding argument types.  Generally,
60 // the values you provide for the parameter bindings must exactly match the
61 // types accepted by the callback function.  For example:
62 //   void Foo(string s);
63 //   NewCallback(&Foo, "foo");          // WON'T WORK:  const char* != string
64 //   NewCallback(&Foo, string("foo"));  // WORKS
65 // Also note that the arguments cannot be references:
66 //   void Foo(const string& s);
67 //   string my_str;
68 //   NewCallback(&Foo, my_str);  // WON'T WORK:  Can't use referecnes.
69 // However, correctly-typed pointers will work just fine.
70 class LIBPROTOBUF_EXPORT Closure {
71  public:
Closure()72   Closure() {}
73   virtual ~Closure();
74 
75   virtual void Run() = 0;
76 
77  private:
78   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Closure);
79 };
80 
81 template<typename R>
82 class ResultCallback {
83  public:
ResultCallback()84   ResultCallback() {}
~ResultCallback()85   virtual ~ResultCallback() {}
86 
87   virtual R Run() = 0;
88 
89  private:
90   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ResultCallback);
91 };
92 
93 template<typename R, typename A1>
94 class LIBPROTOBUF_EXPORT ResultCallback1 {
95  public:
ResultCallback1()96   ResultCallback1() {}
~ResultCallback1()97   virtual ~ResultCallback1() {}
98 
99   virtual R Run(A1) = 0;
100 
101  private:
102   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ResultCallback1);
103 };
104 
105 template<typename R, typename A1, typename A2>
106 class LIBPROTOBUF_EXPORT ResultCallback2 {
107  public:
ResultCallback2()108   ResultCallback2() {}
~ResultCallback2()109   virtual ~ResultCallback2() {}
110 
111   virtual R Run(A1,A2) = 0;
112 
113  private:
114   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ResultCallback2);
115 };
116 
117 namespace internal {
118 
119 class LIBPROTOBUF_EXPORT FunctionClosure0 : public Closure {
120  public:
121   typedef void (*FunctionType)();
122 
FunctionClosure0(FunctionType function,bool self_deleting)123   FunctionClosure0(FunctionType function, bool self_deleting)
124     : function_(function), self_deleting_(self_deleting) {}
125   ~FunctionClosure0();
126 
Run()127   void Run() {
128     bool needs_delete = self_deleting_;  // read in case callback deletes
129     function_();
130     if (needs_delete) delete this;
131   }
132 
133  private:
134   FunctionType function_;
135   bool self_deleting_;
136 };
137 
138 template <typename Class>
139 class MethodClosure0 : public Closure {
140  public:
141   typedef void (Class::*MethodType)();
142 
MethodClosure0(Class * object,MethodType method,bool self_deleting)143   MethodClosure0(Class* object, MethodType method, bool self_deleting)
144     : object_(object), method_(method), self_deleting_(self_deleting) {}
~MethodClosure0()145   ~MethodClosure0() {}
146 
Run()147   void Run() {
148     bool needs_delete = self_deleting_;  // read in case callback deletes
149     (object_->*method_)();
150     if (needs_delete) delete this;
151   }
152 
153  private:
154   Class* object_;
155   MethodType method_;
156   bool self_deleting_;
157 };
158 
159 template <typename Arg1>
160 class FunctionClosure1 : public Closure {
161  public:
162   typedef void (*FunctionType)(Arg1 arg1);
163 
FunctionClosure1(FunctionType function,bool self_deleting,Arg1 arg1)164   FunctionClosure1(FunctionType function, bool self_deleting,
165                    Arg1 arg1)
166     : function_(function), self_deleting_(self_deleting),
167       arg1_(arg1) {}
~FunctionClosure1()168   ~FunctionClosure1() {}
169 
Run()170   void Run() {
171     bool needs_delete = self_deleting_;  // read in case callback deletes
172     function_(arg1_);
173     if (needs_delete) delete this;
174   }
175 
176  private:
177   FunctionType function_;
178   bool self_deleting_;
179   Arg1 arg1_;
180 };
181 
182 template <typename Class, typename Arg1>
183 class MethodClosure1 : public Closure {
184  public:
185   typedef void (Class::*MethodType)(Arg1 arg1);
186 
MethodClosure1(Class * object,MethodType method,bool self_deleting,Arg1 arg1)187   MethodClosure1(Class* object, MethodType method, bool self_deleting,
188                  Arg1 arg1)
189     : object_(object), method_(method), self_deleting_(self_deleting),
190       arg1_(arg1) {}
~MethodClosure1()191   ~MethodClosure1() {}
192 
Run()193   void Run() {
194     bool needs_delete = self_deleting_;  // read in case callback deletes
195     (object_->*method_)(arg1_);
196     if (needs_delete) delete this;
197   }
198 
199  private:
200   Class* object_;
201   MethodType method_;
202   bool self_deleting_;
203   Arg1 arg1_;
204 };
205 
206 template <typename Arg1, typename Arg2>
207 class FunctionClosure2 : public Closure {
208  public:
209   typedef void (*FunctionType)(Arg1 arg1, Arg2 arg2);
210 
FunctionClosure2(FunctionType function,bool self_deleting,Arg1 arg1,Arg2 arg2)211   FunctionClosure2(FunctionType function, bool self_deleting,
212                    Arg1 arg1, Arg2 arg2)
213     : function_(function), self_deleting_(self_deleting),
214       arg1_(arg1), arg2_(arg2) {}
~FunctionClosure2()215   ~FunctionClosure2() {}
216 
Run()217   void Run() {
218     bool needs_delete = self_deleting_;  // read in case callback deletes
219     function_(arg1_, arg2_);
220     if (needs_delete) delete this;
221   }
222 
223  private:
224   FunctionType function_;
225   bool self_deleting_;
226   Arg1 arg1_;
227   Arg2 arg2_;
228 };
229 
230 template <typename Class, typename Arg1, typename Arg2>
231 class MethodClosure2 : public Closure {
232  public:
233   typedef void (Class::*MethodType)(Arg1 arg1, Arg2 arg2);
234 
MethodClosure2(Class * object,MethodType method,bool self_deleting,Arg1 arg1,Arg2 arg2)235   MethodClosure2(Class* object, MethodType method, bool self_deleting,
236                  Arg1 arg1, Arg2 arg2)
237     : object_(object), method_(method), self_deleting_(self_deleting),
238       arg1_(arg1), arg2_(arg2) {}
~MethodClosure2()239   ~MethodClosure2() {}
240 
Run()241   void Run() {
242     bool needs_delete = self_deleting_;  // read in case callback deletes
243     (object_->*method_)(arg1_, arg2_);
244     if (needs_delete) delete this;
245   }
246 
247  private:
248   Class* object_;
249   MethodType method_;
250   bool self_deleting_;
251   Arg1 arg1_;
252   Arg2 arg2_;
253 };
254 
255 template<typename R>
256 class FunctionResultCallback_0_0 : public ResultCallback<R> {
257  public:
258   typedef R (*FunctionType)();
259 
FunctionResultCallback_0_0(FunctionType function,bool self_deleting)260   FunctionResultCallback_0_0(FunctionType function, bool self_deleting)
261       : function_(function), self_deleting_(self_deleting) {}
~FunctionResultCallback_0_0()262   ~FunctionResultCallback_0_0() {}
263 
Run()264   R Run() {
265     bool needs_delete = self_deleting_;  // read in case callback deletes
266     R result = function_();
267     if (needs_delete) delete this;
268     return result;
269   }
270 
271  private:
272   FunctionType function_;
273   bool self_deleting_;
274 };
275 
276 template<typename R, typename P1>
277 class FunctionResultCallback_1_0 : public ResultCallback<R> {
278  public:
279   typedef R (*FunctionType)(P1);
280 
FunctionResultCallback_1_0(FunctionType function,bool self_deleting,P1 p1)281   FunctionResultCallback_1_0(FunctionType function, bool self_deleting,
282                              P1 p1)
283       : function_(function), self_deleting_(self_deleting), p1_(p1) {}
~FunctionResultCallback_1_0()284   ~FunctionResultCallback_1_0() {}
285 
Run()286   R Run() {
287     bool needs_delete = self_deleting_;  // read in case callback deletes
288     R result = function_(p1_);
289     if (needs_delete) delete this;
290     return result;
291   }
292 
293  private:
294   FunctionType function_;
295   bool self_deleting_;
296   P1 p1_;
297 };
298 
299 template<typename R, typename Arg1>
300 class FunctionResultCallback_0_1 : public ResultCallback1<R, Arg1> {
301  public:
302   typedef R (*FunctionType)(Arg1 arg1);
303 
FunctionResultCallback_0_1(FunctionType function,bool self_deleting)304   FunctionResultCallback_0_1(FunctionType function, bool self_deleting)
305       : function_(function), self_deleting_(self_deleting) {}
~FunctionResultCallback_0_1()306   ~FunctionResultCallback_0_1() {}
307 
Run(Arg1 a1)308   R Run(Arg1 a1) {
309     bool needs_delete = self_deleting_;  // read in case callback deletes
310     R result = function_(a1);
311     if (needs_delete) delete this;
312     return result;
313   }
314 
315  private:
316   FunctionType function_;
317   bool self_deleting_;
318 };
319 
320 template<typename R, typename P1, typename A1>
321 class FunctionResultCallback_1_1 : public ResultCallback1<R, A1> {
322  public:
323   typedef R (*FunctionType)(P1, A1);
324 
FunctionResultCallback_1_1(FunctionType function,bool self_deleting,P1 p1)325   FunctionResultCallback_1_1(FunctionType function, bool self_deleting,
326                              P1 p1)
327       : function_(function), self_deleting_(self_deleting), p1_(p1) {}
~FunctionResultCallback_1_1()328   ~FunctionResultCallback_1_1() {}
329 
Run(A1 a1)330   R Run(A1 a1) {
331     bool needs_delete = self_deleting_;  // read in case callback deletes
332     R result = function_(p1_, a1);
333     if (needs_delete) delete this;
334     return result;
335   }
336 
337  private:
338   FunctionType function_;
339   bool self_deleting_;
340   P1 p1_;
341 };
342 
343 template <typename T>
344 struct InternalConstRef {
345   typedef typename remove_reference<T>::type base_type;
346   typedef const base_type& type;
347 };
348 
349 template<typename R, typename T>
350 class MethodResultCallback_0_0 : public ResultCallback<R> {
351  public:
352   typedef R (T::*MethodType)();
MethodResultCallback_0_0(T * object,MethodType method,bool self_deleting)353   MethodResultCallback_0_0(T* object, MethodType method, bool self_deleting)
354       : object_(object),
355         method_(method),
356         self_deleting_(self_deleting) {}
~MethodResultCallback_0_0()357   ~MethodResultCallback_0_0() {}
358 
Run()359   R Run() {
360     bool needs_delete = self_deleting_;
361     R result = (object_->*method_)();
362     if (needs_delete) delete this;
363     return result;
364   }
365 
366  private:
367   T* object_;
368   MethodType method_;
369   bool self_deleting_;
370 };
371 
372 template <typename R, typename T, typename P1, typename P2, typename P3,
373           typename P4, typename P5, typename A1, typename A2>
374 class MethodResultCallback_5_2 : public ResultCallback2<R, A1, A2> {
375  public:
376   typedef R (T::*MethodType)(P1, P2, P3, P4, P5, A1, A2);
MethodResultCallback_5_2(T * object,MethodType method,bool self_deleting,P1 p1,P2 p2,P3 p3,P4 p4,P5 p5)377   MethodResultCallback_5_2(T* object, MethodType method, bool self_deleting,
378                            P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
379       : object_(object),
380         method_(method),
381         self_deleting_(self_deleting),
382         p1_(p1),
383         p2_(p2),
384         p3_(p3),
385         p4_(p4),
386         p5_(p5) {}
~MethodResultCallback_5_2()387   ~MethodResultCallback_5_2() {}
388 
Run(A1 a1,A2 a2)389   R Run(A1 a1, A2 a2) {
390     bool needs_delete = self_deleting_;
391     R result = (object_->*method_)(p1_, p2_, p3_, p4_, p5_, a1, a2);
392     if (needs_delete) delete this;
393     return result;
394   }
395 
396  private:
397   T* object_;
398   MethodType method_;
399   bool self_deleting_;
400   typename remove_reference<P1>::type p1_;
401   typename remove_reference<P2>::type p2_;
402   typename remove_reference<P3>::type p3_;
403   typename remove_reference<P4>::type p4_;
404   typename remove_reference<P5>::type p5_;
405 };
406 
407 }  // namespace internal
408 
409 // See Closure.
NewCallback(void (* function)())410 inline Closure* NewCallback(void (*function)()) {
411   return new internal::FunctionClosure0(function, true);
412 }
413 
414 // See Closure.
NewPermanentCallback(void (* function)())415 inline Closure* NewPermanentCallback(void (*function)()) {
416   return new internal::FunctionClosure0(function, false);
417 }
418 
419 // See Closure.
420 template <typename Class>
NewCallback(Class * object,void (Class::* method)())421 inline Closure* NewCallback(Class* object, void (Class::*method)()) {
422   return new internal::MethodClosure0<Class>(object, method, true);
423 }
424 
425 // See Closure.
426 template <typename Class>
NewPermanentCallback(Class * object,void (Class::* method)())427 inline Closure* NewPermanentCallback(Class* object, void (Class::*method)()) {
428   return new internal::MethodClosure0<Class>(object, method, false);
429 }
430 
431 // See Closure.
432 template <typename Arg1>
NewCallback(void (* function)(Arg1),Arg1 arg1)433 inline Closure* NewCallback(void (*function)(Arg1),
434                             Arg1 arg1) {
435   return new internal::FunctionClosure1<Arg1>(function, true, arg1);
436 }
437 
438 // See Closure.
439 template <typename Arg1>
NewPermanentCallback(void (* function)(Arg1),Arg1 arg1)440 inline Closure* NewPermanentCallback(void (*function)(Arg1),
441                                      Arg1 arg1) {
442   return new internal::FunctionClosure1<Arg1>(function, false, arg1);
443 }
444 
445 // See Closure.
446 template <typename Class, typename Arg1>
NewCallback(Class * object,void (Class::* method)(Arg1),Arg1 arg1)447 inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1),
448                             Arg1 arg1) {
449   return new internal::MethodClosure1<Class, Arg1>(object, method, true, arg1);
450 }
451 
452 // See Closure.
453 template <typename Class, typename Arg1>
NewPermanentCallback(Class * object,void (Class::* method)(Arg1),Arg1 arg1)454 inline Closure* NewPermanentCallback(Class* object, void (Class::*method)(Arg1),
455                                      Arg1 arg1) {
456   return new internal::MethodClosure1<Class, Arg1>(object, method, false, arg1);
457 }
458 
459 // See Closure.
460 template <typename Arg1, typename Arg2>
NewCallback(void (* function)(Arg1,Arg2),Arg1 arg1,Arg2 arg2)461 inline Closure* NewCallback(void (*function)(Arg1, Arg2),
462                             Arg1 arg1, Arg2 arg2) {
463   return new internal::FunctionClosure2<Arg1, Arg2>(
464     function, true, arg1, arg2);
465 }
466 
467 // See Closure.
468 template <typename Arg1, typename Arg2>
NewPermanentCallback(void (* function)(Arg1,Arg2),Arg1 arg1,Arg2 arg2)469 inline Closure* NewPermanentCallback(void (*function)(Arg1, Arg2),
470                                      Arg1 arg1, Arg2 arg2) {
471   return new internal::FunctionClosure2<Arg1, Arg2>(
472     function, false, arg1, arg2);
473 }
474 
475 // See Closure.
476 template <typename Class, typename Arg1, typename Arg2>
NewCallback(Class * object,void (Class::* method)(Arg1,Arg2),Arg1 arg1,Arg2 arg2)477 inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1, Arg2),
478                             Arg1 arg1, Arg2 arg2) {
479   return new internal::MethodClosure2<Class, Arg1, Arg2>(
480     object, method, true, arg1, arg2);
481 }
482 
483 // See Closure.
484 template <typename Class, typename Arg1, typename Arg2>
NewPermanentCallback(Class * object,void (Class::* method)(Arg1,Arg2),Arg1 arg1,Arg2 arg2)485 inline Closure* NewPermanentCallback(
486     Class* object, void (Class::*method)(Arg1, Arg2),
487     Arg1 arg1, Arg2 arg2) {
488   return new internal::MethodClosure2<Class, Arg1, Arg2>(
489     object, method, false, arg1, arg2);
490 }
491 
492 // See ResultCallback
493 template<typename R>
NewCallback(R (* function)())494 inline ResultCallback<R>* NewCallback(R (*function)()) {
495   return new internal::FunctionResultCallback_0_0<R>(function, true);
496 }
497 
498 // See ResultCallback
499 template<typename R>
NewPermanentCallback(R (* function)())500 inline ResultCallback<R>* NewPermanentCallback(R (*function)()) {
501   return new internal::FunctionResultCallback_0_0<R>(function, false);
502 }
503 
504 // See ResultCallback
505 template<typename R, typename P1>
NewCallback(R (* function)(P1),P1 p1)506 inline ResultCallback<R>* NewCallback(R (*function)(P1), P1 p1) {
507   return new internal::FunctionResultCallback_1_0<R, P1>(
508       function, true, p1);
509 }
510 
511 // See ResultCallback
512 template<typename R, typename P1>
NewPermanentCallback(R (* function)(P1),P1 p1)513 inline ResultCallback<R>* NewPermanentCallback(
514     R (*function)(P1), P1 p1) {
515   return new internal::FunctionResultCallback_1_0<R, P1>(
516       function, false, p1);
517 }
518 
519 // See ResultCallback1
520 template<typename R, typename A1>
NewCallback(R (* function)(A1))521 inline ResultCallback1<R, A1>* NewCallback(R (*function)(A1)) {
522   return new internal::FunctionResultCallback_0_1<R, A1>(function, true);
523 }
524 
525 // See ResultCallback1
526 template<typename R, typename A1>
NewPermanentCallback(R (* function)(A1))527 inline ResultCallback1<R, A1>* NewPermanentCallback(R (*function)(A1)) {
528   return new internal::FunctionResultCallback_0_1<R, A1>(function, false);
529 }
530 
531 // See ResultCallback1
532 template<typename R, typename P1, typename A1>
NewCallback(R (* function)(P1,A1),P1 p1)533 inline ResultCallback1<R, A1>* NewCallback(R (*function)(P1, A1), P1 p1) {
534   return new internal::FunctionResultCallback_1_1<R, P1, A1>(
535       function, true, p1);
536 }
537 
538 // See ResultCallback1
539 template<typename R, typename P1, typename A1>
NewPermanentCallback(R (* function)(P1,A1),P1 p1)540 inline ResultCallback1<R, A1>* NewPermanentCallback(
541     R (*function)(P1, A1), P1 p1) {
542   return new internal::FunctionResultCallback_1_1<R, P1, A1>(
543       function, false, p1);
544 }
545 
546 // See MethodResultCallback_0_0
547 template <typename R, typename T1, typename T2>
NewPermanentCallback(T1 * object,R (T2::* function)())548 inline ResultCallback<R>* NewPermanentCallback(
549     T1* object, R (T2::*function)()) {
550   return new internal::MethodResultCallback_0_0<R, T1>(object, function, false);
551 }
552 
553 // See MethodResultCallback_5_2
554 template <typename R, typename T, typename P1, typename P2, typename P3,
555           typename P4, typename P5, typename A1, typename A2>
NewPermanentCallback(T * object,R (T::* function)(P1,P2,P3,P4,P5,A1,A2),typename internal::InternalConstRef<P1>::type p1,typename internal::InternalConstRef<P2>::type p2,typename internal::InternalConstRef<P3>::type p3,typename internal::InternalConstRef<P4>::type p4,typename internal::InternalConstRef<P5>::type p5)556 inline ResultCallback2<R, A1, A2>* NewPermanentCallback(
557     T* object, R (T::*function)(P1, P2, P3, P4, P5, A1, A2),
558     typename internal::InternalConstRef<P1>::type p1,
559     typename internal::InternalConstRef<P2>::type p2,
560     typename internal::InternalConstRef<P3>::type p3,
561     typename internal::InternalConstRef<P4>::type p4,
562     typename internal::InternalConstRef<P5>::type p5) {
563   return new internal::MethodResultCallback_5_2<R, T, P1, P2, P3, P4, P5, A1,
564                                                 A2>(object, function, false, p1,
565                                                     p2, p3, p4, p5);
566 }
567 
568 // A function which does nothing.  Useful for creating no-op callbacks, e.g.:
569 //   Closure* nothing = NewCallback(&DoNothing);
570 void LIBPROTOBUF_EXPORT DoNothing();
571 
572 
573 }  // namespace protobuf
574 }  // namespace google
575 
576 #endif  // GOOGLE_PROTOBUF_STUBS_CALLBACK_H_
577