You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

callback.h 24 kB

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