| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- // Callback_19.cpp : Diese Datei enthält die Funktion "main". Hier beginnt und endet die Ausführung des Programms.
- //
- #include <iostream>
- /**
- * Example code for:
- * - callback storage with std::vector
- * - use of std::function
- * - use of std::bind
- * - use of lambda function as callback
- */
- #include <cstdint>
- #include <cstdio>
- #include <functional>
- #include <vector>
- /**
- * Basic std::function callback example. Templated on a function signature
- * that takes in a uint32_t and returns void.
- */
- struct myData {
- uint8_t myAge = 55;
- const char* pchar = "myData struct";
- enum en_sex { MALE, FEMALE, DIVERS } sex = MALE;
- };
- myData mydata = { 25, "myData struct", myData::MALE };
- typedef std::function<void(myData)> cb_t;
- /**
- * Alternative storage implementation. Perhaps you want to store
- * callbacks for different event types?
- */
- enum my_events_t
- {
- VIDEO_STOP = 0,
- VIDEO_START,
- VIDEO_PAUS,
- EVENT_MAX
- };
- struct cb_event_t
- {
- std::function<void(myData)> cb;
- my_events_t event;
- };
- /**
- * Event based example. Constructed with a uint32_t.
- * Callbacks are passed this uint32_t.
- * Callbacks are only invoked when their event type matches the
- * occuring event.
- */
- class EventDriver
- {
- public:
- // EventDriver(myData& val) : val_(val), callbacks_()
- EventDriver() {};
- // Register a callback.
- void register_callback(const cb_t& cb, const my_events_t event)
- {
- // add callback to end of callback list
- callbacks_.push_back({ cb, event });
- }
- /// Call all the registered callbacks.
- void callback(myData& val, my_events_t event) const
- {
- // iterate through callback list and call each one
- for (const auto& cb : callbacks_)
- {
- if (cb.event == event)
- {
- cb.cb(val);
- }
- }
- }
- private:
- /// Integer to pass to callbacks.
- // myData *val_ = nullptr ;
- /// List of callback functions.
- std::vector<cb_event_t> callbacks_;
- };
- /***********************************************************************************************************************
- * Dummy Client #1
- * Uses a static method for a callback.
- */
- class Client1
- {
- public:
- static void func(myData v)
- {
- printf("static member callback: %d\n", v.myAge);
- }
- };
- /***********************************************************************************************************************
- * Dummy Client #2
- * Uses an instance method as a callback
- */
- class Client2
- {
- public:
- void func(myData v) const
- {
- printf("instance member callback: %d\n", v.myAge);
- }
- };
- /***********************************************************************************************************************
- * Callback on a c function
- */
- extern "C"
- {
- static void c_client_callback(myData v)
- {
- printf("C function callback: %d\n", v.myAge);
- }
- }
- /***********************************************************************************************************************
- * Callback on a c function
- */
- class cButton : public EventDriver {
- public:
- /**
- * Examples using the basic driver
- * This is constructed with a specific value
- * Each callback receives this value
- */
- Client2 c2;
- void init() {
-
- // register a lambda function as a callback
- register_callback(
- [](myData mydata) {
- printf("lambda callback: %d\n", mydata.myAge);
- },
- VIDEO_START);
- // register a lambda function as a callback
- register_callback(
- [](myData mydata) {
- printf("lambda callback: %d\n", mydata.myAge);
- },
- VIDEO_STOP);
- // register client2 cb using std::bind
- register_callback(std::bind(&Client2::func, &c2, std::placeholders::_1), VIDEO_START);
- // register a static class method as a callback
- register_callback(&Client1::func, VIDEO_STOP);
- // register a C function pointer as a callback
- register_callback(&c_client_callback, VIDEO_PAUS);
- };
- void run() {
- // call all the registered callbacks
- printf("------- VIDEO_START -------\n");
- mydata.myAge = 15;
- callback(mydata, VIDEO_START);
- printf("------- VIDEO_STOP -------\n");
- mydata.myAge = 25;
- callback(mydata, VIDEO_STOP);
- printf("------- VIDEO_PAUSE -------\n");
- mydata.myAge = 35;
- callback(mydata, VIDEO_PAUS);
- };
- void readData() {};
- };
- /***********************************************************************************************************************
- * Callback on a c function
- */
- int main()
- {
- /**
- * Examples using the Event Driver.
- * Note that some callbacks will not be called -
- * their event type is different!
- */
- cButton btn;
- printf("Beginning of examples using the EventDriver\n");
- btn.init();
- btn.run();
- printf("\nEnd of examples using the EventDriver\n");
- return 0;
- }
|