#include #include #include "server/uv/uv.hpp" #include "server/http/http.hpp" template class Promise { public: Promise(std::function f, Args&&... args) { result = f(std::forward(args)...); } template Promise then(std::function f) { return Promise(f, std::forward(result)); } T get() { return result; } private: T result; std::atomic completed; }; class TaskPool { template class Task { public: using task_t = Task; using work_f = std::function; using after_work_f = std::function; Task(work_f work, after_work_f callback) : work(work), callback(callback) { req.data = this; } void launch(uv::UvLoop& loop) { uv_queue_work(loop, &req, work_cb, after_work_cb); } private: std::function work; std::function callback; R result; uv_work_t req; static void work_cb(uv_work_t* req) { auto& task = *reinterpret_cast(req->data); } static void after_work_cb(uv_work_t* req, int) { auto task = reinterpret_cast(req->data); delete task; } }; public: TaskPool(uv::UvLoop& loop) : loop(loop) {} template void launch(std::function func, std::function callback) { auto task = new Task(func, callback); task->launch(loop); } private: uv::UvLoop& loop; }; int main(void) { uv::UvLoop loop; TaskPool tp(loop); tp.launch([](void) -> int { return 3 }, [](int x) -> void { std::cout << x << std::endl; }); // http::HttpServer server(loop); // // http::Ipv4 ip("0.0.0.0", 3400); // // server.listen(ip, [](http::Request& req, http::Response& res) { // // // // res.send(req.url); // }); // // loop.run(uv::UvLoop::Mode::Default); return 0; }