Merge and namespace edit

dev-own-engine 0.1
marcelb 8 months ago
parent 8eff5ef1ea
commit 105c15b06b
  1. 43
      .vscode/settings.json
  2. 41
      README.md
  3. 4
      lib/asynco.hpp
  4. 4
      lib/event.hpp
  5. 23
      lib/filesystem.hpp
  6. 6
      lib/rotor.hpp
  7. 2
      lib/runner.hpp
  8. 254
      test/test.cpp

@ -28,6 +28,47 @@
"algorithm": "cpp", "algorithm": "cpp",
"string": "cpp", "string": "cpp",
"string_view": "cpp", "string_view": "cpp",
"fstream": "cpp" "fstream": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwctype": "cpp",
"any": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"cstdint": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"system_error": "cpp",
"iomanip": "cpp",
"istream": "cpp",
"limits": "cpp",
"numbers": "cpp",
"semaphore": "cpp",
"sstream": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"variant": "cpp"
} }
} }

@ -20,15 +20,17 @@ A C++ library for event-driven asynchronous multi-threaded programming.
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 2 // To change the number of threads used by asynco #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/event.hpp" // event #include "asynco/lib/event.hpp" // event
#include "asynco/lib/rotor.hpp" // interval, timeout #include "asynco/lib/rotor.hpp" // interval, timeout
#include "asynco/lib/runner.hpp" // for own loop #include "asynco/lib/runner.hpp" // for own loop
#include "asynco/lib/filesystem.hpp" // for async read and write files #include "asynco/lib/filesystem.hpp" // for async read and write files
using namespace marcelb; using namespace marcelb;
using namespace asynco;
using namespace events;
``` ```
@ -60,9 +62,9 @@ Make functions asynchronous
* Run an lambda function asynchronously * Run an lambda function asynchronously
*/ */
asynco( []() { atask( []() {
sleep_for(2s); // only for simulating long duration function sleep_for(2s); // only for simulating long duration function
cout << "asynco" << endl; cout << "atask" << endl;
return 5; return 5;
}); });
@ -75,7 +77,7 @@ void notLambdaFunction() {
cout << "Call to not lambda function" << endl; cout << "Call to not lambda function" << endl;
} }
asynco (notLambdaFunction); atask (notLambdaFunction);
/** /**
* Run class method * Run class method
@ -89,7 +91,7 @@ class clm {
}; };
clm classes; clm classes;
asynco( [&classes] () { atask( [&classes] () {
classes.classMethode(); classes.classMethode();
}); });
@ -99,9 +101,9 @@ asynco( [&classes] () {
* Wait after runned as async * Wait after runned as async
*/ */
auto a = asynco( []() { auto a = atask( []() {
sleep_for(2s); // only for simulating long duration function sleep_for(2s); // only for simulating long duration function
cout << "asynco" << endl; cout << "atask" << endl;
return 5; return 5;
}); });
@ -111,7 +113,7 @@ cout << wait(a) << endl;
* Wait async function call and use i cout * Wait async function call and use i cout
*/ */
cout << wait(asynco( [] () { cout << wait(atask( [] () {
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;
@ -233,7 +235,7 @@ Asynchronous file IO
```c++ ```c++
string data_; string data_;
asynco_read("test.txt", [&data_] (string data, exception* error) { fs::read("test.txt", [&data_] (string data, exception* error) {
if (error) { if (error) {
cout << "Error " << error->what() << endl; cout << "Error " << error->what() << endl;
} else { } else {
@ -243,13 +245,30 @@ asynco_read("test.txt", [&data_] (string data, exception* error) {
} }
}); });
asynco_write("test1.txt", "Hello night", [] (exception* error) { fs::write("test1.txt", "Hello world", [] (exception* error) {
if (error) { if (error) {
cout << "Error " << error->what() << endl; cout << "Error " << error->what() << endl;
} else { } else {
cout << "Write successfuly" << endl; cout << "Write successfuly" << endl;
} }
}); });
auto future_data = fs::read("test.txt");
try {
string data = wait(future_data);
} catch (exception& err) {
cout << err.what() << endl;
}
auto future_status = fs::write("test.txt", "Hello world");
try {
wait(future_status);
} catch (exception& err) {
cout << err.what() << endl;
}
``` ```
## License ## License

@ -6,12 +6,13 @@
using namespace std; using namespace std;
namespace marcelb { namespace marcelb {
namespace asynco {
/** /**
* Run the function asynchronously * Run the function asynchronously
*/ */
template<class F, class... Args> template<class F, class... Args>
auto asynco(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; using return_type = typename result_of<F(Args...)>::type;
future<return_type> res = _asyncon.put_task(bind(forward<F>(f), forward<Args>(args)...)); future<return_type> res = _asyncon.put_task(bind(forward<F>(f), forward<Args>(args)...));
@ -34,6 +35,7 @@ T wait(future<T>&& r) {
return move(r).get(); return move(r).get();
} }
}
} }
#endif #endif

@ -11,6 +11,8 @@
using namespace std; using namespace std;
namespace marcelb { namespace marcelb {
namespace asynco {
namespace events {
/** /**
* Event class, for event-driven programming. * Event class, for event-driven programming.
@ -57,6 +59,8 @@ class event {
}; };
}
}
} }
#endif #endif

@ -8,15 +8,19 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
using namespace marcelb;
using namespace asynco;
namespace marcelb { namespace marcelb {
namespace asynco {
namespace fs {
/** /**
* Asynchronous file reading with callback after read complete * Asynchronous file reading with callback after read complete
*/ */
template<typename Callback> template<typename Callback>
void asynco_read(string path, Callback&& callback) { void read(string path, Callback&& callback) {
asynco( [&path, callback] () { async( [&path, callback] () {
string content; string content;
try { try {
string line; string line;
@ -44,8 +48,8 @@ void asynco_read(string path, Callback&& callback) {
/** /**
* Asynchronous file reading * Asynchronous file reading
*/ */
future<string> asynco_read(string path) { future<string> read(string path) {
return asynco( [&path] () { return async( [&path] () {
string content; string content;
string line; string line;
ifstream file (path); ifstream file (path);
@ -68,8 +72,8 @@ future<string> asynco_read(string path) {
* Asynchronous file writing with callback after write complete * Asynchronous file writing with callback after write complete
*/ */
template<typename Callback> template<typename Callback>
void asynco_write(string path, string content, Callback&& callback) { void write(string path, string content, Callback&& callback) {
asynco( [&path, &content, callback] () { async( [&path, &content, callback] () {
try { try {
ofstream file (path); ofstream file (path);
if (file.is_open()) { if (file.is_open()) {
@ -91,8 +95,8 @@ void asynco_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> asynco_write(string path, string content) { future<void> write(string path, string content) {
return asynco( [&path, &content] () { return async( [&path, &content] () {
ofstream file (path); ofstream file (path);
if (file.is_open()) { if (file.is_open()) {
file << content; file << content;
@ -105,7 +109,8 @@ future<void> asynco_write(string path, string content) {
}); });
} }
}
}
} }
#endif #endif

@ -9,8 +9,10 @@
using namespace std; using namespace std;
using namespace marcelb; using namespace marcelb;
using namespace asynco;
namespace marcelb { namespace marcelb {
namespace asynco {
/** /**
* Get the time in ms from the epoch * Get the time in ms from the epoch
@ -28,6 +30,8 @@ int64_t rtime_us() {
.count(); .count();
} }
namespace {
/** /**
* Intern class for timer async loop * Intern class for timer async loop
*/ */
@ -177,6 +181,7 @@ class rotor {
* It is intended that there is only one global declaration * It is intended that there is only one global declaration
*/ */
static rotor _rotor; static rotor _rotor;
}
/** /**
* Core class for pure async timer functions * Core class for pure async timer functions
@ -228,6 +233,7 @@ class timeout : public _timer_intern {
}; };
}
} }
#endif #endif

@ -12,6 +12,7 @@
using namespace std; using namespace std;
namespace marcelb { namespace marcelb {
namespace asynco {
#define HW_CONCURRENCY_MINIMAL 4 #define HW_CONCURRENCY_MINIMAL 4
@ -129,6 +130,7 @@ class runner {
*/ */
static runner _asyncon; static runner _asyncon;
}
} }
#endif #endif

@ -1,40 +1,42 @@
#define NUM_OF_RUNNERS 2 #define NUM_OF_RUNNERS 2
// #include "../lib/asynco.hpp" #include "../lib/asynco.hpp"
// #include "../lib/event.hpp" #include "../lib/event.hpp"
// #include "../lib/rotor.hpp" #include "../lib/rotor.hpp"
#include "../lib/filesystem.hpp" // #include "../lib/filesystem.hpp"
#include <iostream> #include <iostream>
#include <unistd.h> #include <unistd.h>
using namespace std; using namespace std;
using namespace marcelb; using namespace marcelb::asynco;
using namespace events;
// using namespace asynco;
using namespace this_thread; using namespace this_thread;
// void sleep_to (int _time) { void sleep_to (int _time) {
// promise<void> _promise; promise<void> _promise;
// timeout t( [&]() { timeout t( [&]() {
// _promise.set_value(); _promise.set_value();
// }, _time); }, _time);
// return _promise.get_future().get(); return _promise.get_future().get();
// } }
// void promise_reject (int _time) { void promise_reject (int _time) {
// promise<void> _promise; promise<void> _promise;
// timeout t( [&]() { timeout t( [&]() {
// try { try {
// // simulate except // simulate except
// throw runtime_error("Error simulation"); throw runtime_error("Error simulation");
// _promise.set_value(); _promise.set_value();
// } catch (...) { } catch (...) {
// _promise.set_exception(current_exception()); _promise.set_exception(current_exception());
// } }
// }, _time); }, _time);
// return _promise.get_future().get(); return _promise.get_future().get();
// } }
void notLambdaFunction() { void notLambdaFunction() {
cout << "Call to not lambda function" << endl; cout << "Call to not lambda function" << endl;
@ -49,42 +51,42 @@ class clm {
// ------------------ EXTEND OWN CLASS WITH EVENTS ------------------- // ------------------ EXTEND OWN CLASS WITH EVENTS -------------------
// class myOwnClass : public event<int> { class myOwnClass : public event<int> {
// public: public:
// myOwnClass() : event() {}; myOwnClass() : event() {};
// }; };
int main () { int main () {
// auto start = rtime_ms(); auto start = rtime_ms();
// --------------- TIME ASYNCHRONOUS FUNCTIONS -------------- // // --------------- TIME ASYNCHRONOUS FUNCTIONS --------------
/** // /**
* Init interval and timeout; clear interval and timeout // * Init interval and timeout; clear interval and timeout
*/ // */
interval( [&] () { // interval( [&] () {
cout << "interval 1: " << rtime_ms() - start << endl; // cout << "interval 1: " << rtime_ms() - start << endl;
}, 50); // }, 50);
interval( [&] () { // interval( [&] () {
cout << "interval 1: " << rtime_ms() - start << endl; // cout << "interval 1: " << rtime_ms() - start << endl;
}, 100); // }, 100);
interval( [&] () { // interval( [&] () {
cout << "interval 2: " << rtime_ms() - start << endl; // cout << "interval 2: " << rtime_ms() - start << endl;
}, 200); // }, 200);
interval( [&] () { // interval( [&] () {
cout << "interval 3: " << rtime_ms() - start << endl; // cout << "interval 3: " << rtime_ms() - start << endl;
}, 300); // }, 300);
interval( [&] () { // interval( [&] () {
cout << "interval 4: " << rtime_ms() - start << endl; // cout << "interval 4: " << rtime_ms() - start << endl;
}, 400); // }, 400);
// interval inter1 ([&]() { // interval inter1 ([&]() {
// cout << "interval prvi " << rtime_ms() - start << endl; // cout << "interval prvi " << rtime_ms() - start << endl;
@ -127,13 +129,13 @@ int main () {
// // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS ------------------------- // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS -------------------------
// /** /**
// * Run an function asyncronic * Run an function asyncronic
// */ */
// asynco( []() { // atask( []() {
// sleep_for(2s); // only for simulate log duration function // sleep_for(2s); // only for simulate log duration function
// cout << "asynco 1" << endl; // cout << "atask 1" << endl;
// return 5; // return 5;
// }); // });
@ -141,11 +143,11 @@ int main () {
// * Call not lambda function // * Call not lambda function
// */ // */
// asynco (notLambdaFunction); // atask (notLambdaFunction);
// wait ( // wait (
// asynco ( // atask (
// notLambdaFunction // notLambdaFunction
// ) // )
// ); // );
@ -155,7 +157,7 @@ int main () {
// */ // */
// clm classes; // clm classes;
// asynco( [&classes] () { // atask( [&classes] () {
// classes.classMethode(); // classes.classMethode();
// }); // });
@ -165,20 +167,20 @@ int main () {
// * Wait after runned as async // * Wait after runned as async
// */ // */
// auto a = asynco( []() { // auto a = atask( []() {
// sleep_for(2s); // only for simulate log duration function // sleep_for(2s); // only for simulate log duration function
// cout << "asynco 2" << endl; // cout << "atask 2" << endl;
// return 5; // return 5;
// }); // });
// cout << wait(a) << endl; // cout << wait(a) << endl;
// cout << "print after asynco 2" << endl; // cout << "print after atask 2" << endl;
// /** // /**
// * Wait async function call and use i cout // * 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 // sleep_for(chrono::seconds(1)); // only for simulate log duration function
// cout << "wait end" << endl; // cout << "wait end" << endl;
// return 4; // return 4;
@ -191,9 +193,9 @@ int main () {
// sleep_to(3000); // sleep_to(3000);
// cout << "sleep_to " << rtime_ms() - start << endl; // cout << "sleep_to " << rtime_ms() - start << endl;
/** // /**
* Catch promise reject // * Catch promise reject
*/ // */
// try { // try {
// promise_reject(3000); // promise_reject(3000);
@ -209,110 +211,84 @@ int main () {
// */ // */
// asynco( [] { // atask( [] {
// cout << "idemo ..." << endl; // cout << "idemo ..." << endl;
// asynco( [] { // atask( [] {
// cout << "ugdnježdena async funkcija " << endl; // cout << "ugdnježdena async funkcija " << endl;
// }); // });
// }); // });
// // --------------- EVENTS ------------------- // --------------- EVENTS -------------------
// /**
// * initialization of typed events
// */
// event<int, int> ev2int;
// event<int, string> evintString;
// event<> evoid;
// ev2int.on("sum", [](int a, int b) { /**
// cout << "Sum " << a+b << endl; * initialization of typed events
// }); */
// ev2int.on("sum", [](int a, int b) { event<int, int> ev2int;
// cout << "Sum done" << endl; event<int, string> evintString;
// }); event<> evoid;
// evintString.on("substract", [](int a, string b) { ev2int.on("sum", [](int a, int b) {
// cout << "Substract " << a-stoi(b) << endl; cout << "Sum " << a+b << endl;
// }); });
// evoid.on("void", []() { ev2int.on("sum", [](int a, int b) {
// cout << "Void emited" << endl; cout << "Sum done" << endl;
// }); });
// string emited2 = "2"; evintString.on("substract", [](int a, string b) {
cout << "Substract " << a-stoi(b) << endl;
});
// evoid.on("void", [&]() { evoid.on("void", []() {
// cout << "Void emited " << emited2 << endl; cout << "Void emited" << endl;
// }); });
// evoid.emit("void"); string emited2 = "2";
// sleep(1);
// /** evoid.on("void", [&]() {
// * Emit cout << "Void emited " << emited2 << endl;
// */ });
// ev2int.emit("sum", 5, 8); evoid.emit("void");
sleep(1);
/**
* Emit
*/
// sleep(1); ev2int.emit("sum", 5, 8);
// evintString.emit("substract", 3, to_string(2));
// sleep(1);
// evoid.off("void");
// evoid.emit("void");
// /** sleep(1);
// * Own class evintString.emit("substract", 3, to_string(2));
// */
// myOwnClass myclass; sleep(1);
evoid.off("void");
evoid.emit("void");
// timeout t( [&] { /**
// myclass.emit("constructed", 1); * Own class
// }, 200); */
// myclass.on("constructed", [] (int i) { myOwnClass myclass;
// cout << "Constructed " << i << endl;
// });
string data_; timeout t( [&] {
myclass.emit("constructed", 1);
}, 200);
// asynco_read("test.txt", [&data_] (string data, exception* error) { myclass.on("constructed", [] (int i) {
// if (error) { cout << "Constructed " << i << endl;
// cout << "Error " << error->what() << endl; });
// } else {
// cout << "Data " << endl << data << endl;
// data_ = data;
// cout << "Data_" << data_ << endl;
// }
// });
// auto data = asynco_read("test4.txt");
// try {
// data_ = wait(data);
// cout << "data" << data_ << endl;
// } catch (exception& err) {
// cout << err.what() << endl;
// }
// asynco_write("test1.txt", "Hello night", [] (exception* error) {
// if (error) {
// cout << "Error " << error->what() << endl;
// } else {
// cout << "Write successfuly" << endl;
// }
// });
auto status = asynco_write("test1.txt", "Hello night"); // auto status = fs::read("test1.txt");
// try { // try {
// wait(status); // auto data = wait(status);
// cout << data;
// } catch (exception& err) { // } catch (exception& err) {
// cout << err.what() << endl; // cout << err.what() << endl;
// } // }

Loading…
Cancel
Save