diff --git a/.gitignore b/.gitignore index b23ba77..14a0c06 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -test/test \ No newline at end of file +test/test +test/*.txt \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 3eeb5bb..7fefd1d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -27,6 +27,7 @@ "bitset": "cpp", "algorithm": "cpp", "string": "cpp", - "string_view": "cpp" + "string_view": "cpp", + "fstream": "cpp" } } \ No newline at end of file diff --git a/README.md b/README.md index c0f529b..6afdc2b 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/lib/filesystem.hpp b/lib/filesystem.hpp new file mode 100644 index 0000000..a0be8a3 --- /dev/null +++ b/lib/filesystem.hpp @@ -0,0 +1,68 @@ +#ifndef _ASYNCO_FS_ +#define _ASYNCO_FS_ + +#include "asynco.hpp" + + +#include +#include + +using namespace std; + +namespace marcelb { + +/** + * Asynchronous file reading +*/ +template +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 +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 \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp index 60ae5ea..f5d1820 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -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 #include @@ -11,29 +12,29 @@ using namespace std; using namespace marcelb; using namespace this_thread; -void sleep_to (int _time) { - promise _promise; - timeout t( [&]() { - _promise.set_value(); - }, _time); - - return _promise.get_future().get(); -} - -void promise_reject (int _time) { - promise _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 _promise; +// timeout t( [&]() { +// _promise.set_value(); +// }, _time); + +// return _promise.get_future().get(); +// } + +// void promise_reject (int _time) { +// promise _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 { - public: - myOwnClass() : event() {}; -}; +// class myOwnClass : public event { +// 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;