GithubHelp home page GithubHelp logo

plumpmath / xcoroutine Goto Github PK

View Code? Open in Web Editor NEW

This project forked from akzi/xcoroutine

0.0 0.0 0.0 22 KB

simple c++11 coroutine for fixing the async callback hell

License: GNU Lesser General Public License v3.0

C++ 99.17% Makefile 0.83%

xcoroutine's Introduction

xcoroutine

simple coroutine to fix the async callback hell

#include <string>
#include <iostream>
#include <functional>
#include <type_traits>
#include "../include/xcoroutine.hpp"

struct user
{
	user() {}
	user(int id)
		:id_(id) 
	{}
	int id_;
};

std::function<void()> callback0_;
std::function<void(std::string)> callback1;
std::function<void(std::string &&, int)> callback2;
std::function<void(std::string &&, int, bool, user&&)> callbackN;


void async_do0(std::function<void()> callback)
{
	throw std::exception();
	callback0_ = callback;
}
void async_do1(const std::string &str, std::function<void(std::string)> callback)
{
	callback1 = callback;
}
void async_do2(const std::string &str, std::function<void(std::string &&, int)> callback)
{
	callback2 = callback;
}
void async_doN(const std::string &str, std::string && data, std::function<void(std::string &&, int, bool, user&&)> callback)
{
	callbackN = callback;
}
void xroutine_func2()
{
	using xutil::to_function;
	using xcoroutine::apply;
	
	try
	{
		apply(to_function(async_do0));
	}
	catch (const std::exception&)
	{
		std::cout << "catch exception" << std::endl;
	}

	auto res = apply(to_function(async_do1),"hello");
	assert(std::get<0>(res) == "hello world");

	auto res2 = apply(to_function(async_do2), "hello");
	assert(std::get<0>(res2) == "hello world");
	assert(std::get<1>(res2) == 1);

	auto res3 = apply(to_function(async_doN), "hello", "hello2");
	assert(std::get<0>(res3) == "hello world");
	assert(std::get<1>(res3) == 1);
	assert(std::get<2>(res3) == true);
	assert(std::get<3>(res3).id_ == 1);

}
int main()
{
	xcoroutine::create( xroutine_func2);


	if(callback0_)
		callback0_();
	callback1("hello world");
	callback2("hello world", 1);
	callbackN("hello world", 1, true, user(1));
	return 0;
}

xcoroutine's People

Contributors

akzi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.