// Callback_19.cpp : Diese Datei enthält die Funktion "main". Hier beginnt und endet die Ausführung des Programms. // #include /** * Example code for: * - callback storage with std::vector * - use of std::function * - use of std::bind * - use of lambda function as callback */ #include #include #include #include /** * 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 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 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 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; }