Compare commits

..

2 Commits

  1. 18
      README.md
  2. 2
      lib/asynco.hpp
  3. 19
      lib/define.hpp
  4. 8
      lib/filesystem.hpp
  5. 2
      lib/trigger.hpp
  6. 137
      test/test.cpp

@ -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. Just download the latest release and unzip it into your project.
```c++ ```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/triggers.hpp" // trigger (event emitter)
#include "asynco/lib/timers.hpp" // periodic, delayed (like setInterval and setTimeout from JS) #include "asynco/lib/timers.hpp" // periodic, delayed (like setInterval and setTimeout from JS)
#include "asynco/lib/filesystem.hpp" // for async read and write files #include "asynco/lib/filesystem.hpp" // for async read and write files
@ -85,9 +85,9 @@ Make functions asynchronous
* Run an lambda function asynchronously * Run an lambda function asynchronously
*/ */
atask( []() { nonsync ( []() {
sleep_for(2s); // only for simulating long duration function sleep_for(2s); // only for simulating long duration function
cout << "atask" << endl; cout << "nonsync " << endl;
return 5; return 5;
}); });
@ -100,7 +100,7 @@ void notLambdaFunction() {
cout << "Call to not lambda function" << endl; cout << "Call to not lambda function" << endl;
} }
atask (notLambdaFunction); nonsync (notLambdaFunction);
/** /**
* Run class method * Run class method
@ -114,7 +114,7 @@ class clm {
}; };
clm classes; clm classes;
atask( [&classes] () { nonsync ( [&classes] () {
classes.classMethode(); classes.classMethode();
}); });
@ -124,9 +124,9 @@ atask( [&classes] () {
* Wait after runned as async * Wait after runned as async
*/ */
auto a = atask( []() { auto a = nonsync ( []() {
sleep_for(2s); // only for simulating long duration function sleep_for(2s); // only for simulating long duration function
cout << "atask" << endl; cout << "nonsync " << endl;
return 5; return 5;
}); });
@ -136,7 +136,7 @@ cout << wait(a) << endl;
* Wait async function call and use i cout * Wait async function call and use i cout
*/ */
cout << wait(atask( [] () { cout << wait(nonsync ( [] () {
sleep_for(chrono::seconds(1)); // only for simulating long duration function sleep_for(chrono::seconds(1)); // only for simulating long duration function
cout << "wait end" << endl; cout << "wait end" << endl;
return 4; return 4;

@ -59,7 +59,7 @@ class {
* Run the function asynchronously * Run the function asynchronously
*/ */
template<class F, class... Args> template<class F, class... Args>
auto atask(F&& f, Args&&... args) -> future<typename result_of<F(Args...)>::type> { auto nonsync(F&& f, Args&&... args) -> future<typename result_of<F(Args...)>::type> {
using return_type = typename result_of<F(Args...)>::type; using return_type = typename result_of<F(Args...)>::type;
future<return_type> res = _asynco_engine.io_context.post(boost::asio::use_future(bind(forward<F>(f), forward<Args>(args)...))); future<return_type> res = _asynco_engine.io_context.post(boost::asio::use_future(bind(forward<F>(f), forward<Args>(args)...)));
return res; return res;

@ -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

@ -19,7 +19,7 @@ namespace fs {
*/ */
template<typename Callback> template<typename Callback>
void read(string path, Callback&& callback) { void read(string path, Callback&& callback) {
atask( [&path, callback] () { asynco::nonsync( [&path, callback] () {
string content; string content;
try { try {
string line; string line;
@ -48,7 +48,7 @@ void read(string path, Callback&& callback) {
* Asynchronous file reading * Asynchronous file reading
*/ */
future<string> read(string path) { future<string> read(string path) {
return atask( [&path] () { return asynco::nonsync( [&path] () {
string content; string content;
string line; string line;
ifstream file (path); ifstream file (path);
@ -72,7 +72,7 @@ future<string> read(string path) {
*/ */
template<typename Callback> template<typename Callback>
void write(string path, string content, Callback&& callback) { void write(string path, string content, Callback&& callback) {
atask( [&path, &content, callback] () { asynco::nonsync( [&path, &content, callback] () {
try { try {
ofstream file (path); ofstream file (path);
if (file.is_open()) { if (file.is_open()) {
@ -95,7 +95,7 @@ void write(string path, string content, Callback&& callback) {
* Asynchronous file writing with callback after write complete * Asynchronous file writing with callback after write complete
*/ */
future<void> write(string path, string content) { future<void> write(string path, string content) {
return atask( [&path, &content] () { return asynco::nonsync( [&path, &content] () {
ofstream file (path); ofstream file (path);
if (file.is_open()) { if (file.is_open()) {
file << content; file << content;

@ -42,7 +42,7 @@ class trigger {
if (it_eve != triggers.end()) { if (it_eve != triggers.end()) {
for (uint i =0; i<it_eve->second.size(); i++) { for (uint i =0; i<it_eve->second.size(); i++) {
auto callback = bind(it_eve->second[i], forward<Args>(args)...); auto callback = bind(it_eve->second[i], forward<Args>(args)...);
atask(callback); asynco::nonsync(callback);
} }
} }
} }

@ -4,6 +4,7 @@
#include "../lib/trigger.hpp" #include "../lib/trigger.hpp"
#include "../lib/filesystem.hpp" #include "../lib/filesystem.hpp"
#include "../lib/timers.hpp" #include "../lib/timers.hpp"
#include "../lib/define.hpp"
using namespace marcelb::asynco; using namespace marcelb::asynco;
using namespace triggers; using namespace triggers;
@ -11,11 +12,13 @@ using namespace triggers;
#include <iostream> #include <iostream>
#include <unistd.h> #include <unistd.h>
#include <thread> #include <thread>
#include <future>
using namespace std; using namespace std;
using namespace this_thread; using namespace this_thread;
void sleep_to (int _time) { void sleep_to (int _time) {
promise<void> _promise; promise<void> _promise;
delayed t( [&]() { delayed t( [&]() {
@ -131,31 +134,35 @@ int main () {
// * Run an function asyncronic // * Run an function asyncronic
// */ // */
// atask( []() { nonsync ( []() {
// sleep_for(2s); // only for simulate log duration function sleep_for(2s); // only for simulate log duration function
// cout << "atask 1" << endl; cout << "asynco 1" << endl;
// return 5; return 5;
// }); });
// /** /**
// * Call not lambda function * Call not lambda function
// */ */
nonsync (notLambdaFunction);
// atask (notLambdaFunction);
wait (
nonsync (
notLambdaFunction
)
);
// wait ( // async(launch::async, [] () {
// atask ( // cout << "Another thread in async style!" << endl;
// notLambdaFunction // });
// )
// );
// /** // /**
// * Call class method // * Call class method
// */ // */
// clm classes; // clm classes;
// atask( [&classes] () { // asynco( [&classes] () {
// classes.classMethode(); // classes.classMethode();
// }); // });
@ -165,20 +172,20 @@ int main () {
// * Wait after runned as async // * Wait after runned as async
// */ // */
// auto a = atask( []() { // auto a = asynco( []() {
// sleep_for(2s); // only for simulate log duration function // sleep_for(2s); // only for simulate log duration function
// cout << "atask 2" << endl; // cout << "asynco 2" << endl;
// return 5; // return 5;
// }); // });
// cout << wait(a) << endl; // cout << wait(a) << endl;
// cout << "print after atask 2" << endl; // cout << "print after asynco 2" << endl;
// /** // /**
// * Wait async function call and use i cout // * 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 // sleep_for(chrono::seconds(1)); // only for simulate log duration function
// cout << "wait end" << endl; // cout << "wait end" << endl;
// return 4; // return 4;
@ -209,9 +216,9 @@ int main () {
// */ // */
// atask( [] { // asynco( [] {
// cout << "idemo ..." << endl; // cout << "idemo ..." << endl;
// atask( [] { // asynco( [] {
// cout << "ugdnježdena async funkcija " << endl; // cout << "ugdnježdena async funkcija " << endl;
// }); // });
// }); // });
@ -222,68 +229,68 @@ int main () {
* 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