#include "../lib/runner.hpp" #include "../lib/asynco.hpp" #include "../lib/event.hpp" #include "../lib/rotor.hpp" #include #include using namespace std; using namespace marcelb; using namespace this_thread; #ifndef ON_RUNNER #define ON_RUNNER runner on_async; #endif void sleep_to (int _time) { promise _promise; timeout t( [&]() { _promise.set_value(); }, _time); return _promise.get_future().get(); } void promise_reject (int _time) { promise _promise; timeout t( [&]() { try { // simulate except throw runtime_error("Error simulation"); _promise.set_value(); } catch (...) { _promise.set_exception(current_exception()); } }, _time); return _promise.get_future().get(); } void notLambdaFunction() { cout << "Call to not lambda function" << endl; } class clm { public: void classMethode() { cout << "Call class method" << endl; } }; // ------------------ EXTEND OWN CLASS WITH EVENTS ------------------- class myOwnClass : public event { public: myOwnClass() : event() {}; }; int main () { on_async.change_runners(64); auto start = rtime_ms(); // --------------- TIME ASYNCHRONOUS FUNCTIONS -------------- /** * Init interval and timeout; clear interval and timeout */ // ovo ne radi // vector interv; // vector tmout; // for (int i=0; i< 20; i++) { // interv.push_back( interval( [i] () { // cout << "interval " << i << endl; // }, 1000)); // tmout.push_back( timeout( [i] () { // cout << "timeout " << i << endl; // }, 1000*i)); // } // ovo valja popravit // interval( [] () { // cout << "interval " << endl; // }, 1000); // interval inter1 ([&]() { // cout << "interval prvi " << rtime_ms() - start << endl; // }, 1000); // interval inter2 ([&]() { // cout << "interval drugi " << rtime_ms() - start << endl; // }, 2000); // interval inter3 ([&]() { // cout << "interval treći " << rtime_ms() - start << endl; // }, 3000); // timeout time1 ( [&] () { // cout << "Close interval 1 i 2 " << rtime_ms() - start << endl; // inter1.clear(); // inter2.clear(); // }, 10000); // timeout time2 ([&] () { // cout << "Close interval 3 " << rtime_ms() - start << endl; // inter3.clear(); // time1.clear(); // }, 2000); // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS ------------------------- // /** // * Put task directly and get returned value - it is not recommended to use it // */ // auto res1 = on_async.put_task( [] () { // cout << "Jebiga " < ev2int; // event evintString; // event<> evoid; // ev2int.on("sum", [](int a, int b) { // cout << "Sum " << a+b << endl; // }); // evintString.on("substract", [](int a, string b) { // cout << "Substract " << a-stoi(b) << endl; // }); // evoid.on("void", []() { // cout << "Void emited" << endl; // }); // // sleep(1); // /** // * Emit // */ // ev2int.emit("sum", 5, 8); // sleep(1); // evintString.emit("substract", 3, to_string(2)); // sleep(1); // evoid.emit("void"); // /** // * Own class // */ // myOwnClass myclass; // timeout t( [&] { // myclass.emit("constructed", 1); // }, 200); // myclass.on("constructed", [] (int i) { // cout << "Constructed " << i << endl; // }); sleep(100000); // only for testing return 0; }