Callback_35.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include "Header.h"
  16. #include "Header1.h"
  17. void _1() {
  18. cout << "_1" << endl;
  19. }
  20. void _2(myData* a) {
  21. cout << "_2" << " " << a->pchar << endl;
  22. }
  23. void _3(myData* a, myOtherData* b) {
  24. b->myOtherAge = 25;
  25. cout << "_3" << " " << b->myOtherAge << endl;
  26. }
  27. void _4(myData* a, myOtherData* b, int c) {
  28. b->myOtherAge = c;
  29. cout << "_3" << " " << b->myOtherAge << endl;
  30. }
  31. #if 0
  32. int main()
  33. {
  34. /**
  35. * Examples using the basic driver
  36. * This is constructed with a specific value
  37. * Each callback receives this value
  38. */
  39. Client2 c2;
  40. myData mydata = { 55, "myData struct", myData::MALE };
  41. myOtherData myotherdata;
  42. /**
  43. * Examples using the Event Driver.
  44. * Note that some callbacks will not be called -
  45. * their event type is different!
  46. */
  47. EventDriver ed;
  48. printf("Beginning of examples using the EventDriver\n");
  49. // register a lambda function as a callback
  50. ed.register_callback(
  51. [&mydata](myData mydata) {
  52. printf("lambda callback: %d\n", mydata.myAge);
  53. },
  54. VIDEO_START);
  55. // register client2 cb using std::bind
  56. ed.register_callback(std::bind(&Client2::func, &c2, std::placeholders::_1), VIDEO_START);
  57. // register a static class method as a callback
  58. ed.register_callback(&Client1::func, VIDEO_STOP);
  59. // register a C function pointer as a callback
  60. ed.register_callback(&c_client_callback, VIDEO_PAUS);
  61. ed.callback(mydata, VIDEO_START);
  62. ed.callback(mydata, VIDEO_STOP);
  63. ed.callback(myotherdata, VIDEO_PAUS);
  64. printf("End of examples using the EventDriver\n");
  65. return 0;
  66. }
  67. #else
  68. /**
  69. * Alternative storage implementation. Perhaps you want to store
  70. * callbacks for different event types?
  71. */
  72. struct cb_event_t2
  73. {
  74. std::function<void(myData)> cb;
  75. my_events_t event;
  76. };
  77. /**
  78. * Event based example. Constructed with a uint32_t.
  79. * Callbacks are passed this uint32_t.
  80. * Callbacks are only invoked when their event type matches the
  81. * occuring event.
  82. */
  83. class EventDriverNew
  84. {
  85. typedef std::function<void(myData)> cb_t;
  86. // MultiFuncObject<void> funcs;
  87. // typedef std::function<void(MultiFuncObject)> cb_t2;
  88. public:
  89. EventDriverNew() : callbacks_() {};
  90. // Register a callback.
  91. void register_callback(const cb_t& cb, const my_events_t event)
  92. {
  93. // add callback to end of callback list
  94. callbacks_.push_back({ cb, event });
  95. }
  96. /// Call all the registered callbacks.
  97. void callback(myData& val, my_events_t event) const
  98. {
  99. // my_events_t event = VIDEO_START;
  100. // iterate through callback list and call each one
  101. for (const auto& cb : callbacks_)
  102. {
  103. if (cb.event == event)
  104. {
  105. cb.cb(val);
  106. }
  107. }
  108. }
  109. private:
  110. /// Integer to pass to callbacks.
  111. // myData &val_ ;
  112. /// List of callback functions.
  113. std::vector<cb_event_t> callbacks_;
  114. };
  115. class helper {
  116. EventDriverNew ed;
  117. myData mydata = { 15, "myData struct", myData::FEMALE };
  118. public:
  119. void cb_register() {
  120. ed.register_callback(
  121. [this](myData mydata) {
  122. printf("lambda callback: %d\n", mydata.myAge);
  123. },
  124. VIDEO_START);
  125. };
  126. void cb_callback() {
  127. ed.callback(mydata, VIDEO_START);
  128. }
  129. };
  130. int main() {
  131. helper help;
  132. myData mydata = { 55, "myData struct", myData::MALE };
  133. myOtherData myotherdata;
  134. help.cb_register();
  135. help.cb_callback();
  136. MultiFuncObject<void> funcs;
  137. funcs += &_1;
  138. funcs += &_2;
  139. funcs += &_3;
  140. funcs += &_4;
  141. funcs();
  142. funcs(&mydata);
  143. funcs(&mydata, &myotherdata);
  144. funcs(&mydata, &myotherdata, 15);
  145. return 0;
  146. }
  147. #endif