Add giveup function

giveup
marcelb 3 months ago
parent e3eddf006b
commit 7bd269bab9
  1. 17
      lib/timers.hpp
  2. 117
      test/test.cpp

@ -217,6 +217,23 @@ class delayed {
}; };
delayed giveup(function<void()> callback, uint64_t time) {
delayed timeout ( []() {
cout << "Timeout " << endl;
throw runtime_error("Time expired");
}, time);
_asynco_engine.io_context.post([&] () {
cout << "execute " << endl;
callback();
cout << "clear " << endl;
timeout.stop();
});
return timeout;
}
} }
} }

@ -70,7 +70,9 @@ int main () {
// */ // */
// periodic inter1 ([&]() { // periodic inter1 ([&]() {
// cout << "periodic prvi " << rtime_ms() - start << endl; // static uint a=0;
// cout << "periodic " << a++ << ", duriation: " << rtime_ms() - start << endl;
// cout << "ticks " << inter1.ticks() << endl;
// }, 1000); // }, 1000);
// periodic inter2 ([&]() { // periodic inter2 ([&]() {
@ -125,6 +127,23 @@ int main () {
// cout << "nije isteko " << endl; // cout << "nije isteko " << endl;
// } // }
// delayed timeout ( []() {
// cout << "Timeout " << endl;
// throw runtime_error("Time expired");
// }, 2000);
// try {
auto a = giveup([]() {
cout << "givup" << endl;
sleep(5);
cout << "return" << endl;
}, 2000);
// } catch (exception e) {
// cout << e.what() << endl;
// }
// // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS ------------------------- // // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS -------------------------
// /** // /**
@ -216,74 +235,74 @@ int main () {
// }); // });
// }); // });
// --------------- EVENTS ------------------- // // --------------- EVENTS -------------------
/** // /**
* initialization of typed events // * initialization of typed events
*/ // */
trigger<int, int> ev2int; // trigger<int, int> ev2int;
trigger<int, string> evintString; // trigger<int, string> evintString;
trigger<> evoid; // trigger<> evoid;
ev2int.on("sum", [](int a, int b) { // ev2int.on("sum", [](int a, int b) {
cout << "Sum " << a+b << endl; // cout << "Sum " << a+b << endl;
}); // });
ev2int.on("sum", [](int a, int b) { // ev2int.on("sum", [](int a, int b) {
cout << "Sum done" << endl; // cout << "Sum done" << endl;
}); // });
evintString.on("substract", [](int a, string b) { // evintString.on("substract", [](int a, string b) {
cout << "Substract " << a-stoi(b) << endl; // cout << "Substract " << a-stoi(b) << endl;
}); // });
evoid.on("void", []() { // evoid.on("void", []() {
cout << "Void emited" << endl; // cout << "Void emited" << endl;
}); // });
string emited2 = "2"; // string emited2 = "2";
evoid.on("void", [&]() { // evoid.on("void", [&]() {
cout << "Void emited " << emited2 << endl; // cout << "Void emited " << emited2 << endl;
}); // });
evoid.tick("void"); // evoid.tick("void");
sleep(1); // sleep(1);
/** // /**
* Emit // * Emit
*/ // */
ev2int.tick("sum", 5, 8); // ev2int.tick("sum", 5, 8);
sleep(1); // sleep(1);
evintString.tick("substract", 3, to_string(2)); // evintString.tick("substract", 3, to_string(2));
sleep(1); // sleep(1);
evoid.off("void"); // evoid.off("void");
evoid.tick("void"); // evoid.tick("void");
cout << "Ukupno 2 int " << ev2int.listeners() << endl; // cout << "Ukupno 2 int " << ev2int.listeners() << endl;
cout << "Ukupno evintString " << evintString.listeners() << endl; // cout << "Ukupno evintString " << evintString.listeners() << endl;
cout << "Ukupno evoid " << evoid.listeners() << endl; // cout << "Ukupno evoid " << evoid.listeners() << endl;
cout << "Ukupno 2 int " << ev2int.listeners("sum") << endl; // cout << "Ukupno 2 int " << ev2int.listeners("sum") << endl;
/** // /**
* Own class // * Own class
*/ // */
myOwnClass myclass; // myOwnClass myclass;
delayed t( [&] { // delayed t( [&] {
myclass.tick("constructed", 1); // myclass.tick("constructed", 1);
}, 200); // }, 200);
myclass.on("constructed", [] (int i) { // myclass.on("constructed", [] (int i) {
cout << "Constructed " << i << endl; // cout << "Constructed " << i << endl;
}); // });

Loading…
Cancel
Save