Compare commits

..

No commits in common. 'd8e0d0b49d70b1e1e9453d81245f33a811f962e5' and 'e3eddf006b312b66a69e9b67f2d62d26fbc71a7d' have entirely different histories.

  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.
```c++
#define NUM_OF_RUNNERS 8 // To change the number of threads used by asynco, without this it runs according to the number of cores
#define NUM_OF_RUNNERS 8 // To change the number of threads used by atask, without this it runs according to the number of cores
#include "asynco/lib/asynco.hpp" // asynco(), wait()
#include "asynco/lib/asynco.hpp" // atask(), 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
*/
nonsync ( []() {
atask( []() {
sleep_for(2s); // only for simulating long duration function
cout << "nonsync " << endl;
cout << "atask" << endl;
return 5;
});
@ -100,7 +100,7 @@ void notLambdaFunction() {
cout << "Call to not lambda function" << endl;
}
nonsync (notLambdaFunction);
atask (notLambdaFunction);
/**
* Run class method
@ -114,7 +114,7 @@ class clm {
};
clm classes;
nonsync ( [&classes] () {
atask( [&classes] () {
classes.classMethode();
});
@ -124,9 +124,9 @@ nonsync ( [&classes] () {
* Wait after runned as async
*/
auto a = nonsync ( []() {
auto a = atask( []() {
sleep_for(2s); // only for simulating long duration function
cout << "nonsync " << endl;
cout << "atask" << endl;
return 5;
});
@ -136,7 +136,7 @@ cout << wait(a) << endl;
* Wait async function call and use i cout
*/
cout << wait(nonsync ( [] () {
cout << wait(atask( [] () {
sleep_for(chrono::seconds(1)); // only for simulating long duration function
cout << "wait end" << endl;
return 4;

@ -59,7 +59,7 @@ class {
* Run the function asynchronously
*/
template<class F, class... Args>
auto nonsync(F&& f, Args&&... args) -> future<typename result_of<F(Args...)>::type> {
auto atask(F&& f, Args&&... args) -> future<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)...)));
return res;

@ -1,19 +0,0 @@
#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>
void read(string path, Callback&& callback) {
asynco::nonsync( [&path, callback] () {
atask( [&path, callback] () {
string content;
try {
string line;
@ -48,7 +48,7 @@ void read(string path, Callback&& callback) {
* Asynchronous file reading
*/
future<string> read(string path) {
return asynco::nonsync( [&path] () {
return atask( [&path] () {
string content;
string line;
ifstream file (path);
@ -72,7 +72,7 @@ future<string> read(string path) {
*/
template<typename Callback>
void write(string path, string content, Callback&& callback) {
asynco::nonsync( [&path, &content, callback] () {
atask( [&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<void> write(string path, string content) {
return asynco::nonsync( [&path, &content] () {
return atask( [&path, &content] () {
ofstream file (path);
if (file.is_open()) {
file << content;

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

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

Loading…
Cancel
Save