|
|
|
@ -4,7 +4,6 @@ |
|
|
|
|
#include "../lib/trigger.hpp" |
|
|
|
|
#include "../lib/filesystem.hpp" |
|
|
|
|
#include "../lib/timers.hpp" |
|
|
|
|
#include "../lib/define.hpp" |
|
|
|
|
|
|
|
|
|
using namespace marcelb::asynco; |
|
|
|
|
using namespace triggers; |
|
|
|
@ -12,14 +11,11 @@ using namespace triggers; |
|
|
|
|
#include <iostream> |
|
|
|
|
#include <unistd.h> |
|
|
|
|
#include <thread> |
|
|
|
|
#include <future> |
|
|
|
|
#include <vector> |
|
|
|
|
|
|
|
|
|
using namespace std; |
|
|
|
|
using namespace this_thread; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sleep_to (int _time) { |
|
|
|
|
promise<void> _promise; |
|
|
|
|
delayed t( [&]() { |
|
|
|
@ -69,9 +65,9 @@ int main () { |
|
|
|
|
|
|
|
|
|
// --------------- TIME ASYNCHRONOUS FUNCTIONS --------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Init periodic and delayed; clear periodic and delayed |
|
|
|
|
*/ |
|
|
|
|
// /**
|
|
|
|
|
// * Init periodic and delayed; clear periodic and delayed
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
// periodic inter1 ([&]() {
|
|
|
|
|
// cout << "periodic prvi " << rtime_ms() - start << endl;
|
|
|
|
@ -129,34 +125,15 @@ int main () { |
|
|
|
|
// cout << "nije isteko " << endl;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// auto d = Delayed( [](){
|
|
|
|
|
// cout << "Delayed" << endl;
|
|
|
|
|
// }, 2000);
|
|
|
|
|
|
|
|
|
|
// auto p = Periodic( [](){
|
|
|
|
|
// cout << "Periodic" << endl;
|
|
|
|
|
// }, 700);
|
|
|
|
|
// // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS -------------------------
|
|
|
|
|
|
|
|
|
|
// Periodic( [&] (){
|
|
|
|
|
// cout << "Delayed expire " << d->expired() << endl;
|
|
|
|
|
// cout << "Periodic ticks " << p->ticks() << endl;
|
|
|
|
|
// cout << "Delayed stoped " << d->stoped() << endl;
|
|
|
|
|
// cout << "Periodic stoped " << p->stoped() << endl;
|
|
|
|
|
// }, 1000);
|
|
|
|
|
|
|
|
|
|
// Delayed( [&](){
|
|
|
|
|
// p->stop();
|
|
|
|
|
// }, 10000);
|
|
|
|
|
|
|
|
|
|
// // // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS -------------------------
|
|
|
|
|
|
|
|
|
|
// // /**
|
|
|
|
|
// // * Run an function asyncronic
|
|
|
|
|
// // */
|
|
|
|
|
// /**
|
|
|
|
|
// * Run an function asyncronic
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
// async_ ( []() {
|
|
|
|
|
// atask( []() {
|
|
|
|
|
// sleep_for(2s); // only for simulate log duration function
|
|
|
|
|
// cout << "asynco 1" << endl;
|
|
|
|
|
// cout << "atask 1" << endl;
|
|
|
|
|
// return 5;
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
@ -164,51 +141,46 @@ int main () { |
|
|
|
|
// * Call not lambda function
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
// async_ (notLambdaFunction);
|
|
|
|
|
// atask (notLambdaFunction);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// await_ (
|
|
|
|
|
// async_ (
|
|
|
|
|
// wait (
|
|
|
|
|
// atask (
|
|
|
|
|
// notLambdaFunction
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // async(launch::async, [] () {
|
|
|
|
|
// // cout << "Another thread in async style!" << endl;
|
|
|
|
|
// // });
|
|
|
|
|
|
|
|
|
|
// // /**
|
|
|
|
|
// // * Call class method
|
|
|
|
|
// // */
|
|
|
|
|
// /**
|
|
|
|
|
// * Call class method
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
// clm classes;
|
|
|
|
|
// async_ ( [&classes] () {
|
|
|
|
|
// atask( [&classes] () {
|
|
|
|
|
// classes.classMethode();
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// sleep(5);
|
|
|
|
|
|
|
|
|
|
// // /**
|
|
|
|
|
// // * await_ after runned as async
|
|
|
|
|
// // */
|
|
|
|
|
// /**
|
|
|
|
|
// * Wait after runned as async
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
// auto aa = async_ ( []() {
|
|
|
|
|
// auto a = atask( []() {
|
|
|
|
|
// sleep_for(2s); // only for simulate log duration function
|
|
|
|
|
// cout << "async_ 2" << endl;
|
|
|
|
|
// cout << "atask 2" << endl;
|
|
|
|
|
// return 5;
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// cout << await_(aa) << endl;
|
|
|
|
|
// cout << "print after async_ 2" << endl;
|
|
|
|
|
// cout << wait(a) << endl;
|
|
|
|
|
// cout << "print after atask 2" << endl;
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * await_ async function call and use i cout
|
|
|
|
|
// * Wait async function call and use i cout
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
// cout << await_(async_ ( [] () {
|
|
|
|
|
// cout << wait(atask( [] () {
|
|
|
|
|
// sleep_for(chrono::seconds(1)); // only for simulate log duration function
|
|
|
|
|
// cout << "await_ end" << endl;
|
|
|
|
|
// cout << "wait end" << endl;
|
|
|
|
|
// return 4;
|
|
|
|
|
// })) << endl;
|
|
|
|
|
|
|
|
|
@ -237,135 +209,81 @@ int main () { |
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// async_ ( [] {
|
|
|
|
|
// atask( [] {
|
|
|
|
|
// cout << "idemo ..." << endl;
|
|
|
|
|
// async_ ( [] {
|
|
|
|
|
// atask( [] {
|
|
|
|
|
// cout << "ugdnježdena async funkcija " << endl;
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// --------------- EVENTS -------------------
|
|
|
|
|
|
|
|
|
|
// // -------------------------- AWAIT ALL ----------------------------------
|
|
|
|
|
|
|
|
|
|
// auto a = async_ ( []() {
|
|
|
|
|
// cout << "A" << endl;
|
|
|
|
|
// return 3;
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// auto b = async_ ( []() {
|
|
|
|
|
// cout << "B" << endl;
|
|
|
|
|
// throw runtime_error("Test exception");
|
|
|
|
|
// return;
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// auto c = async_ ( []() {
|
|
|
|
|
// cout << "C" << endl;
|
|
|
|
|
// return "Hello";
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// int a_;
|
|
|
|
|
// string c_;
|
|
|
|
|
|
|
|
|
|
// auto await_all = [&] () {
|
|
|
|
|
// a_ = await_(a);
|
|
|
|
|
// await_(b);
|
|
|
|
|
// c_ = await_(c);
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
// await_all();
|
|
|
|
|
// cout << "a_ " << a_ << " c_ " << c_ << endl;
|
|
|
|
|
// } catch (const exception& exc) {
|
|
|
|
|
// cout << exc.what() << endl;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // // same type
|
|
|
|
|
|
|
|
|
|
// vector<future<void>> fut_vec;
|
|
|
|
|
// for (int i=0; i<5; i++) {
|
|
|
|
|
// fut_vec.push_back(
|
|
|
|
|
// async_ ( [i]() {
|
|
|
|
|
// cout << "Async_ " << i << endl;
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// auto await_all2 = [&] () {
|
|
|
|
|
// for (int i=0; i<fut_vec.size(); i++) {
|
|
|
|
|
// await_ (fut_vec[i]);
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
/**
|
|
|
|
|
* initialization of typed events |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
// await_all2();
|
|
|
|
|
trigger<int, int> ev2int; |
|
|
|
|
trigger<int, string> evintString; |
|
|
|
|
trigger<> evoid; |
|
|
|
|
|
|
|
|
|
// // --------------- EVENTS -------------------
|
|
|
|
|
ev2int.on("sum", [](int a, int b) { |
|
|
|
|
cout << "Sum " << a+b << endl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * initialization of typed events
|
|
|
|
|
// */
|
|
|
|
|
ev2int.on("sum", [](int a, int b) { |
|
|
|
|
cout << "Sum done" << endl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// trigger<int, int> ev2int;
|
|
|
|
|
// trigger<int, string> evintString;
|
|
|
|
|
// trigger<> evoid;
|
|
|
|
|
evintString.on("substract", [](int a, string b) { |
|
|
|
|
cout << "Substract " << a-stoi(b) << endl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// ev2int.on("sum", [](int a, int b) {
|
|
|
|
|
// cout << "Sum " << a+b << endl;
|
|
|
|
|
// });
|
|
|
|
|
evoid.on("void", []() { |
|
|
|
|
cout << "Void emited" << endl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// ev2int.on("sum", [](int a, int b) {
|
|
|
|
|
// cout << "Sum done" << endl;
|
|
|
|
|
// });
|
|
|
|
|
string emited2 = "2"; |
|
|
|
|
|
|
|
|
|
// evintString.on("substract", [](int a, string b) {
|
|
|
|
|
// cout << "Substract " << a-stoi(b) << endl;
|
|
|
|
|
// });
|
|
|
|
|
evoid.on("void", [&]() { |
|
|
|
|
cout << "Void emited " << emited2 << endl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// evoid.on("void", []() {
|
|
|
|
|
// cout << "Void emited" << endl;
|
|
|
|
|
// });
|
|
|
|
|
evoid.tick("void"); |
|
|
|
|
sleep(1); |
|
|
|
|
|
|
|
|
|
// string emited2 = "2";
|
|
|
|
|
|
|
|
|
|
// evoid.on("void", [&]() {
|
|
|
|
|
// cout << "Void emited " << emited2 << endl;
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// evoid.tick("void");
|
|
|
|
|
// sleep(1);
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * Emit
|
|
|
|
|
// */
|
|
|
|
|
/**
|
|
|
|
|
* Emit |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
// ev2int.tick("sum", 5, 8);
|
|
|
|
|
ev2int.tick("sum", 5, 8); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// sleep(1);
|
|
|
|
|
// evintString.tick("substract", 3, to_string(2));
|
|
|
|
|
sleep(1); |
|
|
|
|
evintString.tick("substract", 3, to_string(2)); |
|
|
|
|
|
|
|
|
|
// sleep(1);
|
|
|
|
|
// evoid.off("void");
|
|
|
|
|
// evoid.tick("void");
|
|
|
|
|
sleep(1); |
|
|
|
|
evoid.off("void"); |
|
|
|
|
evoid.tick("void"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// cout << "Ukupno 2 int " << ev2int.listeners() << endl;
|
|
|
|
|
// cout << "Ukupno evintString " << evintString.listeners() << endl;
|
|
|
|
|
// cout << "Ukupno evoid " << evoid.listeners() << endl;
|
|
|
|
|
// cout << "Ukupno 2 int " << ev2int.listeners("sum") << endl;
|
|
|
|
|
cout << "Ukupno 2 int " << ev2int.listeners() << endl; |
|
|
|
|
cout << "Ukupno evintString " << evintString.listeners() << endl; |
|
|
|
|
cout << "Ukupno evoid " << evoid.listeners() << endl; |
|
|
|
|
cout << "Ukupno 2 int " << ev2int.listeners("sum") << endl; |
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * Own class
|
|
|
|
|
// */
|
|
|
|
|
/**
|
|
|
|
|
* Own class
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
// myOwnClass myclass;
|
|
|
|
|
myOwnClass myclass; |
|
|
|
|
|
|
|
|
|
// delayed t( [&] {
|
|
|
|
|
// myclass.tick("constructed", 1);
|
|
|
|
|
// }, 200);
|
|
|
|
|
delayed t( [&] { |
|
|
|
|
myclass.tick("constructed", 1); |
|
|
|
|
}, 200); |
|
|
|
|
|
|
|
|
|
// myclass.on("constructed", [] (int i) {
|
|
|
|
|
// cout << "Constructed " << i << endl;
|
|
|
|
|
// });
|
|
|
|
|
myclass.on("constructed", [] (int i) { |
|
|
|
|
cout << "Constructed " << i << endl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -373,7 +291,7 @@ int main () { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
// auto data = await_(status);
|
|
|
|
|
// auto data = wait(status);
|
|
|
|
|
// cout << data;
|
|
|
|
|
// } catch (exception& err) {
|
|
|
|
|
// cout << err.what() << endl;
|
|
|
|
|