Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:04

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 #ifndef GOOGLE_PROTOBUF_STUBS_CALLBACK_H_
0009 #define GOOGLE_PROTOBUF_STUBS_CALLBACK_H_
0010 
0011 #include <type_traits>
0012 
0013 #include "google/protobuf/port_def.inc"
0014 
0015 // ===================================================================
0016 // emulates google3/base/callback.h
0017 
0018 namespace google {
0019 namespace protobuf {
0020 
0021 // Abstract interface for a callback.  When calling an RPC, you must provide
0022 // a Closure to call when the procedure completes.  See the Service interface
0023 // in service.h.
0024 //
0025 // To automatically construct a Closure which calls a particular function or
0026 // method with a particular set of parameters, use the NewCallback() function.
0027 // Example:
0028 //   void FooDone(const FooResponse* response) {
0029 //     ...
0030 //   }
0031 //
0032 //   void CallFoo() {
0033 //     ...
0034 //     // When done, call FooDone() and pass it a pointer to the response.
0035 //     Closure* callback = NewCallback(&FooDone, response);
0036 //     // Make the call.
0037 //     service->Foo(controller, request, response, callback);
0038 //   }
0039 //
0040 // Example that calls a method:
0041 //   class Handler {
0042 //    public:
0043 //     ...
0044 //
0045 //     void FooDone(const FooResponse* response) {
0046 //       ...
0047 //     }
0048 //
0049 //     void CallFoo() {
0050 //       ...
0051 //       // When done, call FooDone() and pass it a pointer to the response.
0052 //       Closure* callback = NewCallback(this, &Handler::FooDone, response);
0053 //       // Make the call.
0054 //       service->Foo(controller, request, response, callback);
0055 //     }
0056 //   };
0057 //
0058 // Currently NewCallback() supports binding zero, one, or two arguments.
0059 //
0060 // Callbacks created with NewCallback() automatically delete themselves when
0061 // executed.  They should be used when a callback is to be called exactly
0062 // once (usually the case with RPC callbacks).  If a callback may be called
0063 // a different number of times (including zero), create it with
0064 // NewPermanentCallback() instead.  You are then responsible for deleting the
0065 // callback (using the "delete" keyword as normal).
0066 //
0067 // Note that NewCallback() is a bit touchy regarding argument types.  Generally,
0068 // the values you provide for the parameter bindings must exactly match the
0069 // types accepted by the callback function.  For example:
0070 //   void Foo(std::string s);
0071 //   NewCallback(&Foo, "foo");          // WON'T WORK:  const char* != string
0072 //   NewCallback(&Foo, std::string("foo"));  // WORKS
0073 // Also note that the arguments cannot be references:
0074 //   void Foo(const std::string& s);
0075 //   std::string my_str;
0076 //   NewCallback(&Foo, my_str);  // WON'T WORK:  Can't use references.
0077 // However, correctly-typed pointers will work just fine.
0078 class PROTOBUF_EXPORT Closure {
0079  public:
0080   Closure() {}
0081   Closure(const Closure&) = delete;
0082   Closure& operator=(const Closure&) = delete;
0083   virtual ~Closure();
0084 
0085   virtual void Run() = 0;
0086 };
0087 
0088 template<typename R>
0089 class ResultCallback {
0090  public:
0091   ResultCallback() {}
0092   ResultCallback(const ResultCallback&) = delete;
0093   ResultCallback& operator=(const ResultCallback&) = delete;
0094   virtual ~ResultCallback() {}
0095 
0096   virtual R Run() = 0;
0097 };
0098 
0099 template <typename R, typename A1>
0100 class PROTOBUF_EXPORT ResultCallback1 {
0101  public:
0102   ResultCallback1() {}
0103   ResultCallback1(const ResultCallback1&) = delete;
0104   ResultCallback1& operator=(const ResultCallback1&) = delete;
0105   virtual ~ResultCallback1() {}
0106 
0107   virtual R Run(A1) = 0;
0108 };
0109 
0110 template <typename R, typename A1, typename A2>
0111 class PROTOBUF_EXPORT ResultCallback2 {
0112  public:
0113   ResultCallback2() {}
0114   ResultCallback2(const ResultCallback2&) = delete;
0115   ResultCallback2& operator=(const ResultCallback2&) = delete;
0116   virtual ~ResultCallback2() {}
0117 
0118   virtual R Run(A1,A2) = 0;
0119 };
0120 
0121 namespace internal {
0122 
0123 class PROTOBUF_EXPORT FunctionClosure0 : public Closure {
0124  public:
0125   typedef void (*FunctionType)();
0126 
0127   FunctionClosure0(FunctionType function, bool self_deleting)
0128     : function_(function), self_deleting_(self_deleting) {}
0129   ~FunctionClosure0();
0130 
0131   void Run() override {
0132     bool needs_delete = self_deleting_;  // read in case callback deletes
0133     function_();
0134     if (needs_delete) delete this;
0135   }
0136 
0137  private:
0138   FunctionType function_;
0139   bool self_deleting_;
0140 };
0141 
0142 template <typename Class>
0143 class MethodClosure0 : public Closure {
0144  public:
0145   typedef void (Class::*MethodType)();
0146 
0147   MethodClosure0(Class* object, MethodType method, bool self_deleting)
0148     : object_(object), method_(method), self_deleting_(self_deleting) {}
0149   ~MethodClosure0() {}
0150 
0151   void Run() override {
0152     bool needs_delete = self_deleting_;  // read in case callback deletes
0153     (object_->*method_)();
0154     if (needs_delete) delete this;
0155   }
0156 
0157  private:
0158   Class* object_;
0159   MethodType method_;
0160   bool self_deleting_;
0161 };
0162 
0163 template <typename Arg1>
0164 class FunctionClosure1 : public Closure {
0165  public:
0166   typedef void (*FunctionType)(Arg1 arg1);
0167 
0168   FunctionClosure1(FunctionType function, bool self_deleting,
0169                    Arg1 arg1)
0170     : function_(function), self_deleting_(self_deleting),
0171       arg1_(arg1) {}
0172   ~FunctionClosure1() {}
0173 
0174   void Run() override {
0175     bool needs_delete = self_deleting_;  // read in case callback deletes
0176     function_(arg1_);
0177     if (needs_delete) delete this;
0178   }
0179 
0180  private:
0181   FunctionType function_;
0182   bool self_deleting_;
0183   Arg1 arg1_;
0184 };
0185 
0186 template <typename Class, typename Arg1>
0187 class MethodClosure1 : public Closure {
0188  public:
0189   typedef void (Class::*MethodType)(Arg1 arg1);
0190 
0191   MethodClosure1(Class* object, MethodType method, bool self_deleting,
0192                  Arg1 arg1)
0193     : object_(object), method_(method), self_deleting_(self_deleting),
0194       arg1_(arg1) {}
0195   ~MethodClosure1() {}
0196 
0197   void Run() override {
0198     bool needs_delete = self_deleting_;  // read in case callback deletes
0199     (object_->*method_)(arg1_);
0200     if (needs_delete) delete this;
0201   }
0202 
0203  private:
0204   Class* object_;
0205   MethodType method_;
0206   bool self_deleting_;
0207   Arg1 arg1_;
0208 };
0209 
0210 template <typename Arg1, typename Arg2>
0211 class FunctionClosure2 : public Closure {
0212  public:
0213   typedef void (*FunctionType)(Arg1 arg1, Arg2 arg2);
0214 
0215   FunctionClosure2(FunctionType function, bool self_deleting,
0216                    Arg1 arg1, Arg2 arg2)
0217     : function_(function), self_deleting_(self_deleting),
0218       arg1_(arg1), arg2_(arg2) {}
0219   ~FunctionClosure2() {}
0220 
0221   void Run() override {
0222     bool needs_delete = self_deleting_;  // read in case callback deletes
0223     function_(arg1_, arg2_);
0224     if (needs_delete) delete this;
0225   }
0226 
0227  private:
0228   FunctionType function_;
0229   bool self_deleting_;
0230   Arg1 arg1_;
0231   Arg2 arg2_;
0232 };
0233 
0234 template <typename Class, typename Arg1, typename Arg2>
0235 class MethodClosure2 : public Closure {
0236  public:
0237   typedef void (Class::*MethodType)(Arg1 arg1, Arg2 arg2);
0238 
0239   MethodClosure2(Class* object, MethodType method, bool self_deleting,
0240                  Arg1 arg1, Arg2 arg2)
0241     : object_(object), method_(method), self_deleting_(self_deleting),
0242       arg1_(arg1), arg2_(arg2) {}
0243   ~MethodClosure2() {}
0244 
0245   void Run() override {
0246     bool needs_delete = self_deleting_;  // read in case callback deletes
0247     (object_->*method_)(arg1_, arg2_);
0248     if (needs_delete) delete this;
0249   }
0250 
0251  private:
0252   Class* object_;
0253   MethodType method_;
0254   bool self_deleting_;
0255   Arg1 arg1_;
0256   Arg2 arg2_;
0257 };
0258 
0259 template<typename R>
0260 class FunctionResultCallback_0_0 : public ResultCallback<R> {
0261  public:
0262   typedef R (*FunctionType)();
0263 
0264   FunctionResultCallback_0_0(FunctionType function, bool self_deleting)
0265       : function_(function), self_deleting_(self_deleting) {}
0266   ~FunctionResultCallback_0_0() {}
0267 
0268   R Run() override {
0269     bool needs_delete = self_deleting_;  // read in case callback deletes
0270     R result = function_();
0271     if (needs_delete) delete this;
0272     return result;
0273   }
0274 
0275  private:
0276   FunctionType function_;
0277   bool self_deleting_;
0278 };
0279 
0280 template<typename R, typename P1>
0281 class FunctionResultCallback_1_0 : public ResultCallback<R> {
0282  public:
0283   typedef R (*FunctionType)(P1);
0284 
0285   FunctionResultCallback_1_0(FunctionType function, bool self_deleting,
0286                              P1 p1)
0287       : function_(function), self_deleting_(self_deleting), p1_(p1) {}
0288   ~FunctionResultCallback_1_0() {}
0289 
0290   R Run() override {
0291     bool needs_delete = self_deleting_;  // read in case callback deletes
0292     R result = function_(p1_);
0293     if (needs_delete) delete this;
0294     return result;
0295   }
0296 
0297  private:
0298   FunctionType function_;
0299   bool self_deleting_;
0300   P1 p1_;
0301 };
0302 
0303 template<typename R, typename Arg1>
0304 class FunctionResultCallback_0_1 : public ResultCallback1<R, Arg1> {
0305  public:
0306   typedef R (*FunctionType)(Arg1 arg1);
0307 
0308   FunctionResultCallback_0_1(FunctionType function, bool self_deleting)
0309       : function_(function), self_deleting_(self_deleting) {}
0310   ~FunctionResultCallback_0_1() {}
0311 
0312   R Run(Arg1 a1) override {
0313     bool needs_delete = self_deleting_;  // read in case callback deletes
0314     R result = function_(a1);
0315     if (needs_delete) delete this;
0316     return result;
0317   }
0318 
0319  private:
0320   FunctionType function_;
0321   bool self_deleting_;
0322 };
0323 
0324 template<typename R, typename P1, typename A1>
0325 class FunctionResultCallback_1_1 : public ResultCallback1<R, A1> {
0326  public:
0327   typedef R (*FunctionType)(P1, A1);
0328 
0329   FunctionResultCallback_1_1(FunctionType function, bool self_deleting,
0330                              P1 p1)
0331       : function_(function), self_deleting_(self_deleting), p1_(p1) {}
0332   ~FunctionResultCallback_1_1() {}
0333 
0334   R Run(A1 a1) override {
0335     bool needs_delete = self_deleting_;  // read in case callback deletes
0336     R result = function_(p1_, a1);
0337     if (needs_delete) delete this;
0338     return result;
0339   }
0340 
0341  private:
0342   FunctionType function_;
0343   bool self_deleting_;
0344   P1 p1_;
0345 };
0346 
0347 template <typename T>
0348 struct InternalConstRef {
0349   typedef typename std::remove_reference<T>::type base_type;
0350   typedef const base_type& type;
0351 };
0352 
0353 template<typename R, typename T>
0354 class MethodResultCallback_0_0 : public ResultCallback<R> {
0355  public:
0356   typedef R (T::*MethodType)();
0357   MethodResultCallback_0_0(T* object, MethodType method, bool self_deleting)
0358       : object_(object),
0359         method_(method),
0360         self_deleting_(self_deleting) {}
0361   ~MethodResultCallback_0_0() {}
0362 
0363   R Run() {
0364     bool needs_delete = self_deleting_;
0365     R result = (object_->*method_)();
0366     if (needs_delete) delete this;
0367     return result;
0368   }
0369 
0370  private:
0371   T* object_;
0372   MethodType method_;
0373   bool self_deleting_;
0374 };
0375 
0376 template <typename R, typename T, typename P1, typename P2, typename P3,
0377           typename P4, typename P5, typename P6, typename A1, typename A2>
0378 class MethodResultCallback_6_2 : public ResultCallback2<R, A1, A2> {
0379  public:
0380   typedef R (T::*MethodType)(P1, P2, P3, P4, P5, P6, A1, A2);
0381   MethodResultCallback_6_2(T* object, MethodType method, bool self_deleting,
0382                            P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6)
0383       : object_(object),
0384         method_(method),
0385         self_deleting_(self_deleting),
0386         p1_(p1),
0387         p2_(p2),
0388         p3_(p3),
0389         p4_(p4),
0390         p5_(p5),
0391         p6_(p6) {}
0392   ~MethodResultCallback_6_2() {}
0393 
0394   R Run(A1 a1, A2 a2) override {
0395     bool needs_delete = self_deleting_;
0396     R result = (object_->*method_)(p1_, p2_, p3_, p4_, p5_, p6_, a1, a2);
0397     if (needs_delete) delete this;
0398     return result;
0399   }
0400 
0401  private:
0402   T* object_;
0403   MethodType method_;
0404   bool self_deleting_;
0405   typename std::remove_reference<P1>::type p1_;
0406   typename std::remove_reference<P2>::type p2_;
0407   typename std::remove_reference<P3>::type p3_;
0408   typename std::remove_reference<P4>::type p4_;
0409   typename std::remove_reference<P5>::type p5_;
0410   typename std::remove_reference<P6>::type p6_;
0411 };
0412 
0413 }  // namespace internal
0414 
0415 // See Closure.
0416 inline Closure* NewCallback(void (*function)()) {
0417   return new internal::FunctionClosure0(function, true);
0418 }
0419 
0420 // See Closure.
0421 inline Closure* NewPermanentCallback(void (*function)()) {
0422   return new internal::FunctionClosure0(function, false);
0423 }
0424 
0425 // See Closure.
0426 template <typename Class>
0427 inline Closure* NewCallback(Class* object, void (Class::*method)()) {
0428   return new internal::MethodClosure0<Class>(object, method, true);
0429 }
0430 
0431 // See Closure.
0432 template <typename Class>
0433 inline Closure* NewPermanentCallback(Class* object, void (Class::*method)()) {
0434   return new internal::MethodClosure0<Class>(object, method, false);
0435 }
0436 
0437 // See Closure.
0438 template <typename Arg1>
0439 inline Closure* NewCallback(void (*function)(Arg1),
0440                             Arg1 arg1) {
0441   return new internal::FunctionClosure1<Arg1>(function, true, arg1);
0442 }
0443 
0444 // See Closure.
0445 template <typename Arg1>
0446 inline Closure* NewPermanentCallback(void (*function)(Arg1),
0447                                      Arg1 arg1) {
0448   return new internal::FunctionClosure1<Arg1>(function, false, arg1);
0449 }
0450 
0451 // See Closure.
0452 template <typename Class, typename Arg1>
0453 inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1),
0454                             Arg1 arg1) {
0455   return new internal::MethodClosure1<Class, Arg1>(object, method, true, arg1);
0456 }
0457 
0458 // See Closure.
0459 template <typename Class, typename Arg1>
0460 inline Closure* NewPermanentCallback(Class* object, void (Class::*method)(Arg1),
0461                                      Arg1 arg1) {
0462   return new internal::MethodClosure1<Class, Arg1>(object, method, false, arg1);
0463 }
0464 
0465 // See Closure.
0466 template <typename Arg1, typename Arg2>
0467 inline Closure* NewCallback(void (*function)(Arg1, Arg2),
0468                             Arg1 arg1, Arg2 arg2) {
0469   return new internal::FunctionClosure2<Arg1, Arg2>(
0470     function, true, arg1, arg2);
0471 }
0472 
0473 // See Closure.
0474 template <typename Arg1, typename Arg2>
0475 inline Closure* NewPermanentCallback(void (*function)(Arg1, Arg2),
0476                                      Arg1 arg1, Arg2 arg2) {
0477   return new internal::FunctionClosure2<Arg1, Arg2>(
0478     function, false, arg1, arg2);
0479 }
0480 
0481 // See Closure.
0482 template <typename Class, typename Arg1, typename Arg2>
0483 inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1, Arg2),
0484                             Arg1 arg1, Arg2 arg2) {
0485   return new internal::MethodClosure2<Class, Arg1, Arg2>(
0486     object, method, true, arg1, arg2);
0487 }
0488 
0489 // See Closure.
0490 template <typename Class, typename Arg1, typename Arg2>
0491 inline Closure* NewPermanentCallback(
0492     Class* object, void (Class::*method)(Arg1, Arg2),
0493     Arg1 arg1, Arg2 arg2) {
0494   return new internal::MethodClosure2<Class, Arg1, Arg2>(
0495     object, method, false, arg1, arg2);
0496 }
0497 
0498 // See ResultCallback
0499 template<typename R>
0500 inline ResultCallback<R>* NewCallback(R (*function)()) {
0501   return new internal::FunctionResultCallback_0_0<R>(function, true);
0502 }
0503 
0504 // See ResultCallback
0505 template<typename R>
0506 inline ResultCallback<R>* NewPermanentCallback(R (*function)()) {
0507   return new internal::FunctionResultCallback_0_0<R>(function, false);
0508 }
0509 
0510 // See ResultCallback
0511 template<typename R, typename P1>
0512 inline ResultCallback<R>* NewCallback(R (*function)(P1), P1 p1) {
0513   return new internal::FunctionResultCallback_1_0<R, P1>(
0514       function, true, p1);
0515 }
0516 
0517 // See ResultCallback
0518 template<typename R, typename P1>
0519 inline ResultCallback<R>* NewPermanentCallback(
0520     R (*function)(P1), P1 p1) {
0521   return new internal::FunctionResultCallback_1_0<R, P1>(
0522       function, false, p1);
0523 }
0524 
0525 // See ResultCallback1
0526 template<typename R, typename A1>
0527 inline ResultCallback1<R, A1>* NewCallback(R (*function)(A1)) {
0528   return new internal::FunctionResultCallback_0_1<R, A1>(function, true);
0529 }
0530 
0531 // See ResultCallback1
0532 template<typename R, typename A1>
0533 inline ResultCallback1<R, A1>* NewPermanentCallback(R (*function)(A1)) {
0534   return new internal::FunctionResultCallback_0_1<R, A1>(function, false);
0535 }
0536 
0537 // See ResultCallback1
0538 template<typename R, typename P1, typename A1>
0539 inline ResultCallback1<R, A1>* NewCallback(R (*function)(P1, A1), P1 p1) {
0540   return new internal::FunctionResultCallback_1_1<R, P1, A1>(
0541       function, true, p1);
0542 }
0543 
0544 // See ResultCallback1
0545 template<typename R, typename P1, typename A1>
0546 inline ResultCallback1<R, A1>* NewPermanentCallback(
0547     R (*function)(P1, A1), P1 p1) {
0548   return new internal::FunctionResultCallback_1_1<R, P1, A1>(
0549       function, false, p1);
0550 }
0551 
0552 // See MethodResultCallback_0_0
0553 template <typename R, typename T1, typename T2>
0554 inline ResultCallback<R>* NewPermanentCallback(
0555     T1* object, R (T2::*function)()) {
0556   return new internal::MethodResultCallback_0_0<R, T1>(object, function, false);
0557 }
0558 
0559 // See MethodResultCallback_6_2
0560 template <typename R, typename T, typename P1, typename P2, typename P3,
0561           typename P4, typename P5, typename P6, typename A1, typename A2>
0562 inline ResultCallback2<R, A1, A2>* NewPermanentCallback(
0563     T* object, R (T::*function)(P1, P2, P3, P4, P5, P6, A1, A2),
0564     typename internal::InternalConstRef<P1>::type p1,
0565     typename internal::InternalConstRef<P2>::type p2,
0566     typename internal::InternalConstRef<P3>::type p3,
0567     typename internal::InternalConstRef<P4>::type p4,
0568     typename internal::InternalConstRef<P5>::type p5,
0569     typename internal::InternalConstRef<P6>::type p6) {
0570   return new internal::MethodResultCallback_6_2<R, T, P1, P2, P3, P4, P5, P6,
0571                                                 A1, A2>(object, function, false,
0572                                                         p1, p2, p3, p4, p5, p6);
0573 }
0574 
0575 // A function which does nothing.  Useful for creating no-op callbacks, e.g.:
0576 //   Closure* nothing = NewCallback(&DoNothing);
0577 void PROTOBUF_EXPORT DoNothing();
0578 
0579 }  // namespace protobuf
0580 }  // namespace google
0581 
0582 #include "google/protobuf/port_undef.inc"
0583 
0584 #endif  // GOOGLE_PROTOBUF_STUBS_CALLBACK_H_