callback.h 16 KB

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