Callback_19.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // Callback_19.cpp : Diese Datei enthält die Funktion "main". Hier beginnt und endet die Ausführung des Programms.
  2. //
  3. #include <iostream>
  4. /**
  5. * Example code for:
  6. * - callback storage with std::vector
  7. * - use of std::function
  8. * - use of std::bind
  9. * - use of lambda function as callback
  10. */
  11. #include <cstdint>
  12. #include <cstdio>
  13. #include <functional>
  14. #include <vector>
  15. /**
  16. * Basic std::function callback example. Templated on a function signature
  17. * that takes in a uint32_t and returns void.
  18. */
  19. struct myData {
  20. uint8_t myAge = 55;
  21. const char* pchar = "myData struct";
  22. enum en_sex { MALE, FEMALE, DIVERS } sex = MALE;
  23. };
  24. typedef std::function<void(myData)> cb_t;
  25. #if BASICDRIVER
  26. /**
  27. * Basic example. Constructed with a uint32_t.
  28. * Callbacks are passed this uint32_t.
  29. */
  30. class BasicDriver
  31. {
  32. public:
  33. BasicDriver(uint32_t val) : val_(val), callbacks_()
  34. {
  35. }
  36. // Register a callback.
  37. void register_callback(const cb_t& cb)
  38. {
  39. // add callback to end of callback list
  40. callbacks_.push_back(cb);
  41. }
  42. /// Call all the registered callbacks.
  43. void callback() const
  44. {
  45. // iterate through callback list and call each one
  46. for (const auto& cb : callbacks_)
  47. {
  48. cb(val_);
  49. }
  50. }
  51. private:
  52. /// Integer to pass to callbacks.
  53. uint32_t val_;
  54. /// List of callback functions.
  55. std::vector<cb_t> callbacks_;
  56. };
  57. #endif
  58. #if ARGDRIVER
  59. /**
  60. * Here's an example type that stores a std::function and an argument
  61. * to pass with that callback. Passing a uint32_t input to your callback
  62. * is a pretty common implementation. Also useful if you need to store a
  63. * pointer for use with a BOUNCE function.
  64. */
  65. struct cb_arg_t
  66. {
  67. // the callback - takes a uint32_t input.
  68. std::function<void(uint32_t)> cb;
  69. // value to return with the callback.
  70. uint32_t arg;
  71. };
  72. /**
  73. * Arg based example.
  74. * Callbacks register with a uint32_t that they want returned
  75. * Callbacks always passed their specific uint32_t value
  76. */
  77. class ArgDriver
  78. {
  79. public:
  80. ArgDriver() : callbacks_()
  81. {
  82. }
  83. // Register a callback.
  84. void register_callback(const cb_t& cb, const uint32_t val)
  85. {
  86. // add callback to end of callback list
  87. callbacks_.push_back({ cb, val });
  88. }
  89. /// Call all the registered callbacks.
  90. void callback() const
  91. {
  92. // iterate through callback list and call each one
  93. for (const auto& cb : callbacks_)
  94. {
  95. cb.cb(cb.arg);
  96. }
  97. }
  98. private:
  99. /// List of callback functions.
  100. std::vector<cb_arg_t> callbacks_;
  101. };
  102. #endif
  103. /**
  104. * Alternative storage implementation. Perhaps you want to store
  105. * callbacks for different event types?
  106. */
  107. enum my_events_t
  108. {
  109. VIDEO_STOP = 0,
  110. VIDEO_START,
  111. VIDEO_PAUS,
  112. EVENT_MAX
  113. };
  114. struct cb_event_t
  115. {
  116. std::function<void(myData)> cb;
  117. my_events_t event;
  118. };
  119. /**
  120. * Event based example. Constructed with a uint32_t.
  121. * Callbacks are passed this uint32_t.
  122. * Callbacks are only invoked when their event type matches the
  123. * occuring event.
  124. */
  125. class EventDriver
  126. {
  127. public:
  128. EventDriver(myData& val) : val_(val), callbacks_()
  129. {
  130. }
  131. // Register a callback.
  132. void register_callback(const cb_t& cb, const my_events_t event)
  133. {
  134. // add callback to end of callback list
  135. callbacks_.push_back({ cb, event });
  136. }
  137. /// Call all the registered callbacks.
  138. void callback(myData& val, my_events_t event) const
  139. {
  140. // my_events_t event = VIDEO_START;
  141. // iterate through callback list and call each one
  142. for (const auto& cb : callbacks_)
  143. {
  144. if (cb.event == event)
  145. {
  146. cb.cb(val_);
  147. }
  148. }
  149. }
  150. private:
  151. /// Integer to pass to callbacks.
  152. myData& val_;
  153. /// List of callback functions.
  154. std::vector<cb_event_t> callbacks_;
  155. };
  156. /***********************************************************************************************************************
  157. * Dummy Client #1
  158. * Uses a static method for a callback.
  159. */
  160. class Client1
  161. {
  162. public:
  163. static void func(myData v)
  164. {
  165. printf("static member callback: %d\n", v.myAge);
  166. }
  167. };
  168. /***********************************************************************************************************************
  169. * Dummy Client #2
  170. * Uses an instance method as a callback
  171. */
  172. class Client2
  173. {
  174. public:
  175. void func(myData v) const
  176. {
  177. printf("instance member callback: %d\n", v.myAge);
  178. }
  179. };
  180. /***********************************************************************************************************************
  181. * Callback on a c function
  182. */
  183. extern "C"
  184. {
  185. static void c_client_callback(myData v)
  186. {
  187. printf("C function callback: %d\n", v.myAge);
  188. }
  189. }
  190. int main()
  191. {
  192. /**
  193. * Examples using the basic driver
  194. * This is constructed with a specific value
  195. * Each callback receives this value
  196. */
  197. Client2 c2;
  198. myData mydata = { 55, "myData struct", myData::MALE };
  199. #if BASICDRIVER
  200. BasicDriver bd(0xDEADBEEF); // create basic driver instance
  201. printf("Starting examples using the BasicDriver\n");
  202. // register a lambda function as a callback
  203. bd.register_callback([](uint32_t v) {
  204. printf("lambda callback: 0x%x\n", v);
  205. });
  206. /**
  207. * std::bind is used to create a function object using
  208. * both the instance and method pointers. This gives you the
  209. * callback into the specific object - very useful!
  210. *
  211. * std::bind keeps specific arguments at a fixed value (or a placeholder)
  212. * For our argument here, we keep as a placeholder.
  213. */
  214. bd.register_callback(std::bind(&Client2::func, &c2, std::placeholders::_1));
  215. // register a static class method as a callback
  216. bd.register_callback(&Client1::func);
  217. // register a C function pointer as a callback
  218. bd.register_callback(&c_client_callback);
  219. // call all the registered callbacks
  220. bd.callback();
  221. printf("End of examples using the BasicDriver\n");
  222. #endif
  223. /**
  224. * Examples using the Event Driver.
  225. * Note that some callbacks will not be called -
  226. * their event type is different!
  227. */
  228. EventDriver ed(mydata);
  229. printf("Beginning of examples using the EventDriver\n");
  230. // register a lambda function as a callback
  231. ed.register_callback(
  232. [&mydata](myData mydata) {
  233. printf("lambda callback: %d\n", mydata.myAge);
  234. },
  235. VIDEO_START);
  236. // register client2 cb using std::bind
  237. ed.register_callback(std::bind(&Client2::func, &c2, std::placeholders::_1), VIDEO_START);
  238. // register a static class method as a callback
  239. ed.register_callback(&Client1::func, VIDEO_STOP);
  240. // register a C function pointer as a callback
  241. ed.register_callback(&c_client_callback, VIDEO_PAUS);
  242. // call all the registered callbacks
  243. ed.callback(mydata, VIDEO_START);
  244. ed.callback(mydata, VIDEO_STOP);
  245. ed.callback(mydata, VIDEO_PAUS);
  246. printf("End of examples using the EventDriver\n");
  247. #if ARGDRIVER
  248. /**
  249. * Examples using the Arg Driver.
  250. * Note that each callback registers for its own value
  251. */
  252. ArgDriver ad;
  253. printf("Beginning of examples using the ArgDriver\n");
  254. // register a lambda function as a callback
  255. ad.register_callback(
  256. [](uint32_t v) {
  257. printf("lambda callback: 0x%x\n", v);
  258. },
  259. 0x0);
  260. // register client2 cb using std::bind
  261. ad.register_callback(std::bind(&Client2::func, &c2, std::placeholders::_1), 0x1);
  262. // register a static class method as a callback
  263. ad.register_callback(&Client1::func, 0x2);
  264. // register a C function pointer as a callback
  265. ad.register_callback(&c_client_callback, 0x3);
  266. // call all the registered callbacks
  267. ad.callback();
  268. printf("End of examples using the ArgDriver\n");
  269. #endif
  270. return 0;
  271. }