Async_ await_
This commit is contained in:
		
							parent
							
								
									0b94c1e86c
								
							
						
					
					
						commit
						7bf7a7d090
					
				
							
								
								
									
										24
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								README.md
									
									
									
									
									
								
							@ -29,7 +29,7 @@ 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
 | 
			
		||||
 | 
			
		||||
#include "asynco/lib/asynco.hpp"        // asynco(), wait()
 | 
			
		||||
#include "asynco/lib/asynco.hpp"        // async_ (), await_()
 | 
			
		||||
#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,7 +85,7 @@ Make functions asynchronous
 | 
			
		||||
* Run an lambda function asynchronously
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
asynco( []() {
 | 
			
		||||
async_ ( []() {
 | 
			
		||||
    sleep_for(2s);   // only for simulating long duration function
 | 
			
		||||
    cout << "asynco" << endl;
 | 
			
		||||
    return 5;
 | 
			
		||||
@ -100,7 +100,7 @@ void notLambdaFunction() {
 | 
			
		||||
    cout << "Call to not lambda function" << endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
asynco (notLambdaFunction);
 | 
			
		||||
async_ (notLambdaFunction);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Run class method
 | 
			
		||||
@ -114,31 +114,31 @@ class clm {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
clm classes;
 | 
			
		||||
asynco( [&classes] () {
 | 
			
		||||
async_ ( [&classes] () {
 | 
			
		||||
    classes.classMethode();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* Wait after runned as async
 | 
			
		||||
* await_ after runned as async
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
auto a = asynco( []() {
 | 
			
		||||
auto a = async_ ( []() {
 | 
			
		||||
    sleep_for(2s);   // only for simulating long duration function
 | 
			
		||||
    cout << "asynco" << endl;
 | 
			
		||||
    return 5;
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
cout << wait(a) << endl;
 | 
			
		||||
cout << await_(a) << endl;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* Wait async function call and use i cout
 | 
			
		||||
* await_ async function call and use i cout
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
cout << wait(asynco( [] () {
 | 
			
		||||
cout << await_(async_ ( [] () {
 | 
			
		||||
    sleep_for(chrono::seconds(1)); // only for simulating long duration function
 | 
			
		||||
    cout << "wait end" << endl;
 | 
			
		||||
    cout << "await_ end" << endl;
 | 
			
		||||
    return 4;
 | 
			
		||||
})) << endl;
 | 
			
		||||
 | 
			
		||||
@ -279,7 +279,7 @@ fs::write("test1.txt", "Hello world", [] (exception* error) {
 | 
			
		||||
auto future_data = fs::read("test.txt");
 | 
			
		||||
 | 
			
		||||
try {
 | 
			
		||||
    string data = wait(future_data);
 | 
			
		||||
    string data = await_(future_data);
 | 
			
		||||
} catch (exception& err) {
 | 
			
		||||
    cout << err.what() << endl;
 | 
			
		||||
}
 | 
			
		||||
@ -287,7 +287,7 @@ try {
 | 
			
		||||
auto future_status = fs::write("test.txt", "Hello world");
 | 
			
		||||
 | 
			
		||||
try {
 | 
			
		||||
    wait(future_status);
 | 
			
		||||
    await_(future_status);
 | 
			
		||||
} catch (exception& err) {
 | 
			
		||||
    cout << err.what() << endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -11,7 +11,6 @@ namespace asynco {
 | 
			
		||||
 | 
			
		||||
#define HW_CONCURRENCY_MINIMAL 4
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Internal anonymous class for initializing the ASIO context and thread pool
 | 
			
		||||
 * !!! It is anonymous to protect against use in the initialization of other objects of the same type !!!
 | 
			
		||||
@ -59,7 +58,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 async_(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;
 | 
			
		||||
@ -69,7 +68,7 @@ auto nonsync(F&& f, Args&&... args) -> future<typename result_of<F(Args...)>::ty
 | 
			
		||||
 * Block until the asynchronous call completes
 | 
			
		||||
*/
 | 
			
		||||
template<typename T>
 | 
			
		||||
T wait(future<T>& r) {
 | 
			
		||||
T await_(future<T>& r) {
 | 
			
		||||
    return r.get();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -77,7 +76,7 @@ T wait(future<T>& r) {
 | 
			
		||||
 * Block until the asynchronous call completes
 | 
			
		||||
*/
 | 
			
		||||
template<typename T>
 | 
			
		||||
T wait(future<T>&& r) {
 | 
			
		||||
T await_(future<T>&& r) {
 | 
			
		||||
    return move(r).get();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -8,8 +8,8 @@ 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
 | 
			
		||||
#define async_ marcelb::asynco::async_
 | 
			
		||||
#define await_ marcelb::asynco::await_
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -19,7 +19,7 @@ namespace fs {
 | 
			
		||||
*/
 | 
			
		||||
template<typename Callback>
 | 
			
		||||
void read(string path, Callback&& callback) {
 | 
			
		||||
    asynco::nonsync( [&path, callback] () {
 | 
			
		||||
    asynco::async_( [&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 asynco::async_( [&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] () {
 | 
			
		||||
    asynco::async_( [&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 asynco::async_( [&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);
 | 
			
		||||
                asynco::async_(callback);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -134,7 +134,7 @@ int main () {
 | 
			
		||||
    //  * Run an function asyncronic
 | 
			
		||||
    // */
 | 
			
		||||
 | 
			
		||||
    nonsync ( []() {
 | 
			
		||||
    async_ ( []() {
 | 
			
		||||
        sleep_for(2s);   // only for simulate log duration function
 | 
			
		||||
        cout << "asynco 1" << endl;
 | 
			
		||||
        return 5;
 | 
			
		||||
@ -144,11 +144,11 @@ int main () {
 | 
			
		||||
     * Call not lambda function
 | 
			
		||||
    */
 | 
			
		||||
 | 
			
		||||
    nonsync (notLambdaFunction);
 | 
			
		||||
    async_ (notLambdaFunction);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    wait (
 | 
			
		||||
        nonsync (
 | 
			
		||||
    await_ (
 | 
			
		||||
        async_ (
 | 
			
		||||
            notLambdaFunction
 | 
			
		||||
        )
 | 
			
		||||
    );
 | 
			
		||||
@ -169,7 +169,7 @@ int main () {
 | 
			
		||||
    // sleep(5);
 | 
			
		||||
 | 
			
		||||
    // /**
 | 
			
		||||
    //  * Wait after runned as async
 | 
			
		||||
    //  * await_ after runned as async
 | 
			
		||||
    //  */
 | 
			
		||||
 | 
			
		||||
    // auto a = asynco( []() {
 | 
			
		||||
@ -178,16 +178,16 @@ int main () {
 | 
			
		||||
    //     return 5;
 | 
			
		||||
    // });
 | 
			
		||||
 | 
			
		||||
    // cout << wait(a) << endl;
 | 
			
		||||
    // cout << await_(a) << endl;
 | 
			
		||||
    // cout << "print after asynco 2" << endl;
 | 
			
		||||
 | 
			
		||||
    // /**
 | 
			
		||||
    //  * Wait async function call and use i cout
 | 
			
		||||
    //  * await_ async function call and use i cout
 | 
			
		||||
    // */
 | 
			
		||||
 | 
			
		||||
    // cout << wait(asynco( [] () {
 | 
			
		||||
    // cout << await_(asynco( [] () {
 | 
			
		||||
    //     sleep_for(chrono::seconds(1)); // only for simulate log duration function
 | 
			
		||||
    //     cout << "wait end" << endl;
 | 
			
		||||
    //     cout << "await_ end" << endl;
 | 
			
		||||
    //     return 4;
 | 
			
		||||
    // })) << endl;
 | 
			
		||||
 | 
			
		||||
@ -298,7 +298,7 @@ int main () {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // try {
 | 
			
		||||
    //     auto data = wait(status);
 | 
			
		||||
    //     auto data = await_(status);
 | 
			
		||||
    //     cout << data;
 | 
			
		||||
    // } catch (exception& err) {
 | 
			
		||||
    //     cout << err.what() << endl;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user