From 9c30f38f5229284f8ebc609a129d125a93b27bfd Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Wed, 14 Sep 2016 00:57:42 +0200 Subject: [PATCH] fix the template approach --- NextGen.cpp | 131 +++++++++++++++++++++++++++++++++++++++++++--------- Test.cpp | 4 ++ 2 files changed, 114 insertions(+), 21 deletions(-) diff --git a/NextGen.cpp b/NextGen.cpp index e65dc12..8d7f7ed 100644 --- a/NextGen.cpp +++ b/NextGen.cpp @@ -46,66 +46,155 @@ namespace detail { template struct ReturnTypeToContinuableConverter; - template + /*template struct +*/ template - class ContinuableBase, typename CallbackType> { + class ContinuableBase, CallbackType> { CallbackType callback_; public: explicit ContinuableBase(CallbackType&& callback) : callback_(std::move(callback)) { } - template + void via() { } + + /*template auto then(C&& continuation) { // The type the callback will evaluate to using EvaluatedTo = decltype(std::declval()(std::declval()...)); return EvaluatedTo{ }; - } + }*/ }; } // namespace detail using namespace detail; -template -using Continuable = detail::ContinuableBase>; +// template +// using Continuable = detail::ContinuableBase>; template struct Callback { void operator() (Args... ) { } }; -template +/*template auto make_continuable(Args&&...) { return Continuable<> { }; -} +}*/ -auto http_request(std::string url) { - return make_continuable([url](auto&& callback) { +/*auto http_request(std::string url) { + return make_continuable([url](auto& callback) { callback("
hi
"); }); +}*/ + +/* template +auto appendHandlerToContinuation(Continuation&& cont, Handler&& handler) { + return [cont = std::forward(cont), + handler = std::forward(handler)](auto&& continuation) { + using T = decltype(continuation); + return [continuation = std::forward(continuation)](auto&&... arg) { + continuation(std::forward(arg)...); + }; + + current([continuation = std::forward(continuation)](auto&&... arg) { + continuation(std::forward(arg)...); + }); + }; +} */ + +template +auto appendHandlerToContinuation(Continuation&& continuation, + Callback&& callback) { + return [continuation = std::forward(continuation), + callback = std::forward(callback)](auto&& next) mutable { + + // Create the proxy callback that passes the next continuation into + // the callback. + auto proxy = [callback = std::forward(callback), + next = std::forward(next)] + (auto&&... args) mutable { + callback(std::forward(args)...)(std::move(next)); + }; + // Invoke the next invocation handler + std::move(continuation)(std::move(proxy)); + }; +} + +template +void invokeContinuation(Continuation&& continuation) { + // Pass an empty callback to the continuation to invoke it + std::forward(continuation)([](auto&&...) {}); +} + +auto makeTestContinuation() { + return [](auto&& callback) { + callback("
hi
"); + }; } void testNextGen() { - using t = GetNamedParameterOrDefault< - NamedParameterId::NAMED_PARAMATER_CALLBACK, - Identity<>, - void, - int, - NamedParameter, - float - >; + auto continuation = makeTestContinuation(); - http_request("github.com") + auto then1 = [](std::string) { + + int i = 0; + + return makeTestContinuation(); + }; + + auto then2 = [](std::string) { + + int i = 0; + + return makeTestContinuation(); + }; + + auto f1 = appendHandlerToContinuation(continuation, then1); + auto f2 = appendHandlerToContinuation(f1, then2); + + invokeContinuation(f2); + + return; + + /*http_request("github.com") .then([](std::string content) { - return }) .then([] { - }); + });*/ + + auto c1 = [](auto&& callback) { + + callback(""); + }; + + auto r1 = [](auto&& continuation) { + + }; + + auto u1 = [](std::string str) { + // do sth + (void)str; + + return [](auto&& callback) { // next + callback(0); + }; + }; + + auto proxy = [u1 = std::move(u1)](auto&& tunnel) { + + auto c2 = u1(tunnel); + + + // c2() + }; + + c1(std::move(proxy)); } diff --git a/Test.cpp b/Test.cpp index dbcc749..1bfea64 100644 --- a/Test.cpp +++ b/Test.cpp @@ -21,11 +21,15 @@ #include "Continuable.h" +void testNextGen(); void test_mockup(); void test_incubator(); int main(int argc, char* const argv[]) { + testNextGen(); + return 0; + test_mockup(); test_incubator();