Add async read and write files

dev-own-engine
marcelb 8 months ago
parent 4ab7c4af18
commit daec3d7f00
  1. 3
      .gitignore
  2. 3
      .vscode/settings.json
  3. 28
      README.md
  4. 68
      lib/filesystem.hpp
  5. 109
      test/test.cpp

3
.gitignore vendored

@ -1 +1,2 @@
test/test
test/test
test/*.txt

@ -27,6 +27,7 @@
"bitset": "cpp",
"algorithm": "cpp",
"string": "cpp",
"string_view": "cpp"
"string_view": "cpp",
"fstream": "cpp"
}
}

@ -14,6 +14,7 @@ A C++ library for event-driven asynchronous multi-threaded programming.
- Typed events (on, emit, off)
- Event loops
- Multiple parallel execution loops
- Asynchronous file IO
## Installation
Just download the latest release and unzip it into your project.
@ -25,6 +26,8 @@ Just download the latest release and unzip it into your project.
#include "asynco/lib/event.hpp" // event
#include "asynco/lib/rotor.hpp" // interval, timeout
#include "asynco/lib/runner.hpp" // for own loop
#include "asynco/lib/filesystem.hpp"// for async read and write files
using namespace marcelb;
```
@ -224,6 +227,31 @@ myclass.on("constructed", [] (int i) {
});
```
Asynchronous file IO
```c++
string data_;
asynco_read("test.txt", [&data_] (string data, exception* error) {
if (error) {
cout << "Error " << error->what() << endl;
} else {
cout << "Data " << endl << data << endl;
data_ = data;
cout << "Data_" << data_ << endl;
}
});
asynco_write("test1.txt", "Hello night", [] (exception* error) {
if (error) {
cout << "Error " << error->what() << endl;
} else {
cout << "Write successfuly" << endl;
}
});
```
## License
[APACHE 2.0](http://www.apache.org/licenses/LICENSE-2.0/)

@ -0,0 +1,68 @@
#ifndef _ASYNCO_FS_
#define _ASYNCO_FS_
#include "asynco.hpp"
#include <fstream>
#include <iostream>
using namespace std;
namespace marcelb {
/**
* Asynchronous file reading
*/
template<typename Callback>
void asynco_read(string path, Callback&& callback) {
asynco( [&path, callback] () {
string content;
try {
string line;
ifstream file (path);
if (file.is_open()) {
line.clear();
while ( getline (file,line) ) {
content += line + "\n";
}
file.close();
}
else {
throw runtime_error("Unable to open file");
}
callback(content, nullptr);
} catch(exception& error) {
callback(content, &error);
}
});
}
/**
* Asynchronous file writing
*/
template<typename Callback>
void asynco_write(string path, string content, Callback&& callback) {
asynco( [&path, &content, callback] () {
try {
ofstream file (path);
if (file.is_open()) {
file << content;
file.close();
}
else {
throw runtime_error("Unable to open file");
}
callback(nullptr);
} catch(exception& error) {
callback(&error);
}
});
}
}
#endif

@ -1,8 +1,9 @@
#define NUM_OF_RUNNERS 2
#include "../lib/asynco.hpp"
#include "../lib/event.hpp"
#include "../lib/rotor.hpp"
// #include "../lib/asynco.hpp"
// #include "../lib/event.hpp"
// #include "../lib/rotor.hpp"
#include "../lib/filesystem.hpp"
#include <iostream>
#include <unistd.h>
@ -11,29 +12,29 @@ using namespace std;
using namespace marcelb;
using namespace this_thread;
void sleep_to (int _time) {
promise<void> _promise;
timeout t( [&]() {
_promise.set_value();
}, _time);
return _promise.get_future().get();
}
void promise_reject (int _time) {
promise<void> _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 sleep_to (int _time) {
// promise<void> _promise;
// timeout t( [&]() {
// _promise.set_value();
// }, _time);
// return _promise.get_future().get();
// }
// void promise_reject (int _time) {
// promise<void> _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;
@ -48,15 +49,15 @@ class clm {
// ------------------ EXTEND OWN CLASS WITH EVENTS -------------------
class myOwnClass : public event<int> {
public:
myOwnClass() : event() {};
};
// class myOwnClass : public event<int> {
// public:
// myOwnClass() : event() {};
// };
int main () {
auto start = rtime_ms();
// auto start = rtime_ms();
// --------------- TIME ASYNCHRONOUS FUNCTIONS --------------
@ -64,22 +65,22 @@ int main () {
* Init interval and timeout; clear interval and timeout
*/
interval( [&] () {
cout << "interval " << rtime_ms() - start << endl;
}, 200);
// interval( [&] () {
// cout << "interval " << rtime_ms() - start << endl;
// }, 200);
interval( [&] () {
cout << "interval " << rtime_ms() - start << endl;
}, 200);
// interval( [&] () {
// cout << "interval " << rtime_ms() - start << endl;
// }, 200);
interval( [&] () {
cout << "interval " << rtime_ms() - start << endl;
}, 200);
// interval( [&] () {
// cout << "interval " << rtime_ms() - start << endl;
// }, 200);
interval( [&] () {
cout << "interval " << rtime_ms() - start << endl;
}, 200);
// interval( [&] () {
// cout << "interval " << rtime_ms() - start << endl;
// }, 200);
// interval inter1 ([&]() {
// cout << "interval prvi " << rtime_ms() - start << endl;
@ -274,6 +275,28 @@ int main () {
// cout << "Constructed " << i << endl;
// });
// string data_;
// asynco_read("test.txt", [&data_] (string data, exception* error) {
// if (error) {
// cout << "Error " << error->what() << endl;
// } else {
// cout << "Data " << endl << data << endl;
// data_ = data;
// cout << "Data_" << data_ << endl;
// }
// });
// asynco_write("test1.txt", "Hello night", [] (exception* error) {
// if (error) {
// cout << "Error " << error->what() << endl;
// } else {
// cout << "Write successfuly" << endl;
// }
// });
cout << "Sleep" << endl;
sleep(100000); // only for testing
return 0;

Loading…
Cancel
Save