| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- // 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>
- #include "Header.h"
- #include "Header1.h"
- void _1() {
- cout << "_1" << endl;
- }
- void _2(myData* a) {
- cout << "_2" << " " << a->pchar << endl;
- }
- void _3(myData* a, myOtherData* b) {
- b->myOtherAge = 25;
- cout << "_3" << " " << b->myOtherAge << endl;
- }
- void _4(myData* a, myOtherData* b, int c) {
- b->myOtherAge = c;
- cout << "_3" << " " << b->myOtherAge << endl;
- }
- #if 0
- int main()
- {
- /**
- * Examples using the basic driver
- * This is constructed with a specific value
- * Each callback receives this value
- */
- Client2 c2;
- myData mydata = { 55, "myData struct", myData::MALE };
- myOtherData myotherdata;
- /**
- * Examples using the Event Driver.
- * Note that some callbacks will not be called -
- * their event type is different!
- */
- EventDriver ed;
- printf("Beginning of examples using the EventDriver\n");
- // register a lambda function as a callback
- ed.register_callback(
- [&mydata](myData mydata) {
- printf("lambda callback: %d\n", mydata.myAge);
- },
- VIDEO_START);
- // register client2 cb using std::bind
- ed.register_callback(std::bind(&Client2::func, &c2, std::placeholders::_1), VIDEO_START);
- // register a static class method as a callback
- ed.register_callback(&Client1::func, VIDEO_STOP);
- // register a C function pointer as a callback
- ed.register_callback(&c_client_callback, VIDEO_PAUS);
- ed.callback(mydata, VIDEO_START);
- ed.callback(mydata, VIDEO_STOP);
- ed.callback(myotherdata, VIDEO_PAUS);
- printf("End of examples using the EventDriver\n");
- return 0;
- }
- #else
- /**
- * Alternative storage implementation. Perhaps you want to store
- * callbacks for different event types?
- */
- struct cb_event_t2
- {
- 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 EventDriverNew
- {
- typedef std::function<void(myData)> cb_t;
- // MultiFuncObject<void> funcs;
- // typedef std::function<void(MultiFuncObject)> cb_t2;
- public:
- EventDriverNew() : callbacks_() {};
- // 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
- {
- // my_events_t event = VIDEO_START;
- // 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_ ;
- /// List of callback functions.
- std::vector<cb_event_t> callbacks_;
- };
- class helper {
- EventDriverNew ed;
- myData mydata = { 15, "myData struct", myData::FEMALE };
- public:
- void cb_register() {
- ed.register_callback(
- [this](myData mydata) {
- printf("lambda callback: %d\n", mydata.myAge);
- },
- VIDEO_START);
- };
- void cb_callback() {
- ed.callback(mydata, VIDEO_START);
- }
- };
- int main() {
- helper help;
- myData mydata = { 55, "myData struct", myData::MALE };
- myOtherData myotherdata;
- help.cb_register();
- help.cb_callback();
- MultiFuncObject<void> funcs;
- funcs += &_1;
- funcs += &_2;
- funcs += &_3;
- funcs += &_4;
- funcs();
- funcs(&mydata);
- funcs(&mydata, &myotherdata);
- funcs(&mydata, &myotherdata, 15);
- return 0;
- }
- #endif
|