callback.h 17 KB

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