Callback_20.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. myData mydata = { 25, "myData struct", myData::MALE };
  25. typedef std::function<void(myData)> cb_t;
  26. /**
  27. * Alternative storage implementation. Perhaps you want to store
  28. * callbacks for different event types?
  29. */
  30. enum my_events_t
  31. {
  32. VIDEO_STOP = 0,
  33. VIDEO_START,
  34. VIDEO_PAUS,
  35. EVENT_MAX
  36. };
  37. struct cb_event_t
  38. {
  39. std::function<void(myData)> cb;
  40. my_events_t event;
  41. };
  42. /**
  43. * Event based example. Constructed with a uint32_t.
  44. * Callbacks are passed this uint32_t.
  45. * Callbacks are only invoked when their event type matches the
  46. * occuring event.
  47. */
  48. class EventDriver
  49. {
  50. public:
  51. // EventDriver(myData& val) : val_(val), callbacks_()
  52. EventDriver() {};
  53. // Register a callback.
  54. void register_callback(const cb_t& cb, const my_events_t event)
  55. {
  56. // add callback to end of callback list
  57. callbacks_.push_back({ cb, event });
  58. }
  59. /// Call all the registered callbacks.
  60. void callback(myData& val, my_events_t event) const
  61. {
  62. // iterate through callback list and call each one
  63. for (const auto& cb : callbacks_)
  64. {
  65. if (cb.event == event)
  66. {
  67. cb.cb(val);
  68. }
  69. }
  70. }
  71. private:
  72. /// Integer to pass to callbacks.
  73. // myData *val_ = nullptr ;
  74. /// List of callback functions.
  75. std::vector<cb_event_t> callbacks_;
  76. };
  77. /***********************************************************************************************************************
  78. * Dummy Client #1
  79. * Uses a static method for a callback.
  80. */
  81. class Client1
  82. {
  83. public:
  84. static void func(myData v)
  85. {
  86. printf("static member callback: %d\n", v.myAge);
  87. }
  88. };
  89. /***********************************************************************************************************************
  90. * Dummy Client #2
  91. * Uses an instance method as a callback
  92. */
  93. class Client2
  94. {
  95. public:
  96. void func(myData v) const
  97. {
  98. printf("instance member callback: %d\n", v.myAge);
  99. }
  100. };
  101. /***********************************************************************************************************************
  102. * Callback on a c function
  103. */
  104. extern "C"
  105. {
  106. static void c_client_callback(myData v)
  107. {
  108. printf("C function callback: %d\n", v.myAge);
  109. }
  110. }
  111. /***********************************************************************************************************************
  112. * Callback on a c function
  113. */
  114. class cButton : public EventDriver {
  115. public:
  116. /**
  117. * Examples using the basic driver
  118. * This is constructed with a specific value
  119. * Each callback receives this value
  120. */
  121. Client2 c2;
  122. void init() {
  123. // register a lambda function as a callback
  124. register_callback(
  125. [](myData mydata) {
  126. printf("lambda callback: %d\n", mydata.myAge);
  127. },
  128. VIDEO_START);
  129. // register a lambda function as a callback
  130. register_callback(
  131. [](myData mydata) {
  132. printf("lambda callback: %d\n", mydata.myAge);
  133. },
  134. VIDEO_STOP);
  135. // register client2 cb using std::bind
  136. register_callback(std::bind(&Client2::func, &c2, std::placeholders::_1), VIDEO_START);
  137. // register a static class method as a callback
  138. register_callback(&Client1::func, VIDEO_STOP);
  139. // register a C function pointer as a callback
  140. register_callback(&c_client_callback, VIDEO_PAUS);
  141. };
  142. void run() {
  143. // call all the registered callbacks
  144. printf("------- VIDEO_START -------\n");
  145. mydata.myAge = 15;
  146. callback(mydata, VIDEO_START);
  147. printf("------- VIDEO_STOP -------\n");
  148. mydata.myAge = 25;
  149. callback(mydata, VIDEO_STOP);
  150. printf("------- VIDEO_PAUSE -------\n");
  151. mydata.myAge = 35;
  152. callback(mydata, VIDEO_PAUS);
  153. };
  154. void readData() {};
  155. };
  156. /***********************************************************************************************************************
  157. * Callback on a c function
  158. */
  159. int main()
  160. {
  161. /**
  162. * Examples using the Event Driver.
  163. * Note that some callbacks will not be called -
  164. * their event type is different!
  165. */
  166. cButton btn;
  167. printf("Beginning of examples using the EventDriver\n");
  168. btn.init();
  169. btn.run();
  170. printf("\nEnd of examples using the EventDriver\n");
  171. return 0;
  172. }