diff --git a/README.md b/README.md index 40497df..8f9683f 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,9 @@ The asynchronous filesystem is provided solely to guide users on how to wrap any Just download the latest release and unzip it into your project. ```c++ -#define NUM_OF_RUNNERS 8 // To change the number of threads used by atask, without this it runs according to the number of cores +#define NUM_OF_RUNNERS 8 // To change the number of threads used by asynco, without this it runs according to the number of cores -#include "asynco/lib/asynco.hpp" // atask(), wait() +#include "asynco/lib/asynco.hpp" // asynco(), wait() #include "asynco/lib/triggers.hpp" // trigger (event emitter) #include "asynco/lib/timers.hpp" // periodic, delayed (like setInterval and setTimeout from JS) #include "asynco/lib/filesystem.hpp" // for async read and write files @@ -85,9 +85,9 @@ Make functions asynchronous * Run an lambda function asynchronously */ -atask( []() { +asynco( []() { sleep_for(2s); // only for simulating long duration function - cout << "atask" << endl; + cout << "asynco" << endl; return 5; }); @@ -100,7 +100,7 @@ void notLambdaFunction() { cout << "Call to not lambda function" << endl; } -atask (notLambdaFunction); +asynco (notLambdaFunction); /** * Run class method @@ -114,7 +114,7 @@ class clm { }; clm classes; -atask( [&classes] () { +asynco( [&classes] () { classes.classMethode(); }); @@ -124,9 +124,9 @@ atask( [&classes] () { * Wait after runned as async */ -auto a = atask( []() { +auto a = asynco( []() { sleep_for(2s); // only for simulating long duration function - cout << "atask" << endl; + cout << "asynco" << endl; return 5; }); @@ -136,7 +136,7 @@ cout << wait(a) << endl; * Wait async function call and use i cout */ -cout << wait(atask( [] () { +cout << wait(asynco( [] () { sleep_for(chrono::seconds(1)); // only for simulating long duration function cout << "wait end" << endl; return 4; diff --git a/lib/asynco.hpp b/lib/asynco.hpp index 9104f27..99023d0 100644 --- a/lib/asynco.hpp +++ b/lib/asynco.hpp @@ -59,7 +59,7 @@ class { * Run the function asynchronously */ template -auto atask(F&& f, Args&&... args) -> future::type> { +auto nonsync(F&& f, Args&&... args) -> future::type> { using return_type = typename result_of::type; future res = _asynco_engine.io_context.post(boost::asio::use_future(bind(forward(f), forward(args)...))); return res; diff --git a/lib/define.hpp b/lib/define.hpp new file mode 100644 index 0000000..e33b870 --- /dev/null +++ b/lib/define.hpp @@ -0,0 +1,19 @@ +#ifndef _ASYNCO_DEFINE_ +#define _ASYNCO_DEFINE_ + +namespace marcelb { +namespace asynco { + +/** + * Alternative names of functions - mostly for the sake of more beautiful coloring of the code + */ + +#define nonsync marcelb::asynco::nonsync +#define wait marcelb::asynco::wait + +} +} + + + +#endif \ No newline at end of file diff --git a/lib/filesystem.hpp b/lib/filesystem.hpp index 18dbb0a..33f01dc 100644 --- a/lib/filesystem.hpp +++ b/lib/filesystem.hpp @@ -19,7 +19,7 @@ namespace fs { */ template void read(string path, Callback&& callback) { - atask( [&path, callback] () { + asynco::nonsync( [&path, callback] () { string content; try { string line; @@ -48,7 +48,7 @@ void read(string path, Callback&& callback) { * Asynchronous file reading */ future read(string path) { - return atask( [&path] () { + return asynco::nonsync( [&path] () { string content; string line; ifstream file (path); @@ -72,7 +72,7 @@ future read(string path) { */ template void write(string path, string content, Callback&& callback) { - atask( [&path, &content, callback] () { + asynco::nonsync( [&path, &content, callback] () { try { ofstream file (path); if (file.is_open()) { @@ -95,7 +95,7 @@ void write(string path, string content, Callback&& callback) { * Asynchronous file writing with callback after write complete */ future write(string path, string content) { - return atask( [&path, &content] () { + return asynco::nonsync( [&path, &content] () { ofstream file (path); if (file.is_open()) { file << content; diff --git a/lib/trigger.hpp b/lib/trigger.hpp index 13ce96a..4361875 100644 --- a/lib/trigger.hpp +++ b/lib/trigger.hpp @@ -42,7 +42,7 @@ class trigger { if (it_eve != triggers.end()) { for (uint i =0; isecond.size(); i++) { auto callback = bind(it_eve->second[i], forward(args)...); - atask(callback); + asynco::nonsync(callback); } } } diff --git a/test/test.cpp b/test/test.cpp index 9e33d89..35bb0d7 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -4,6 +4,7 @@ #include "../lib/trigger.hpp" #include "../lib/filesystem.hpp" #include "../lib/timers.hpp" +#include "../lib/define.hpp" using namespace marcelb::asynco; using namespace triggers; @@ -11,11 +12,13 @@ using namespace triggers; #include #include #include +#include using namespace std; using namespace this_thread; + void sleep_to (int _time) { promise _promise; delayed t( [&]() { @@ -131,31 +134,35 @@ int main () { // * Run an function asyncronic // */ - // atask( []() { - // sleep_for(2s); // only for simulate log duration function - // cout << "atask 1" << endl; - // return 5; - // }); + nonsync ( []() { + sleep_for(2s); // only for simulate log duration function + cout << "asynco 1" << endl; + return 5; + }); - // /** - // * Call not lambda function - // */ + /** + * Call not lambda function + */ + + nonsync (notLambdaFunction); - // atask (notLambdaFunction); + wait ( + nonsync ( + notLambdaFunction + ) + ); - // wait ( - // atask ( - // notLambdaFunction - // ) - // ); + // async(launch::async, [] () { + // cout << "Another thread in async style!" << endl; + // }); // /** // * Call class method // */ // clm classes; - // atask( [&classes] () { + // asynco( [&classes] () { // classes.classMethode(); // }); @@ -165,20 +172,20 @@ int main () { // * Wait after runned as async // */ - // auto a = atask( []() { + // auto a = asynco( []() { // sleep_for(2s); // only for simulate log duration function - // cout << "atask 2" << endl; + // cout << "asynco 2" << endl; // return 5; // }); // cout << wait(a) << endl; - // cout << "print after atask 2" << endl; + // cout << "print after asynco 2" << endl; // /** // * Wait async function call and use i cout // */ - // cout << wait(atask( [] () { + // cout << wait(asynco( [] () { // sleep_for(chrono::seconds(1)); // only for simulate log duration function // cout << "wait end" << endl; // return 4; @@ -209,9 +216,9 @@ int main () { // */ - // atask( [] { + // asynco( [] { // cout << "idemo ..." << endl; - // atask( [] { + // asynco( [] { // cout << "ugdnježdena async funkcija " << endl; // }); // }); @@ -222,68 +229,68 @@ int main () { * initialization of typed events */ - trigger ev2int; - trigger evintString; - trigger<> evoid; + // trigger ev2int; + // trigger evintString; + // trigger<> evoid; - ev2int.on("sum", [](int a, int b) { - cout << "Sum " << a+b << endl; - }); + // ev2int.on("sum", [](int a, int b) { + // cout << "Sum " << a+b << endl; + // }); - ev2int.on("sum", [](int a, int b) { - cout << "Sum done" << endl; - }); + // ev2int.on("sum", [](int a, int b) { + // cout << "Sum done" << endl; + // }); - evintString.on("substract", [](int a, string b) { - cout << "Substract " << a-stoi(b) << endl; - }); + // evintString.on("substract", [](int a, string b) { + // cout << "Substract " << a-stoi(b) << endl; + // }); - evoid.on("void", []() { - cout << "Void emited" << endl; - }); + // evoid.on("void", []() { + // cout << "Void emited" << endl; + // }); - string emited2 = "2"; + // string emited2 = "2"; - evoid.on("void", [&]() { - cout << "Void emited " << emited2 << endl; - }); + // evoid.on("void", [&]() { + // cout << "Void emited " << emited2 << endl; + // }); - evoid.tick("void"); - sleep(1); + // 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; + // });