From 98f335690f11649ff8722c49c55c117862ded6dd Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Wed, 14 Sep 2016 01:48:11 +0200 Subject: [PATCH] Fix decorators --- NextGen.cpp | 127 +++++++++++++++++++++++++--------------------------- Test.cpp | 6 +-- 2 files changed, 65 insertions(+), 68 deletions(-) diff --git a/NextGen.cpp b/NextGen.cpp index 8d7f7ed..3a2484c 100644 --- a/NextGen.cpp +++ b/NextGen.cpp @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + /* #include #include #include @@ -46,9 +46,9 @@ namespace detail { template struct ReturnTypeToContinuableConverter; - /*template + template struct -*/ + template class ContinuableBase, CallbackType> { @@ -60,14 +60,14 @@ namespace detail { void via() { } - /*template + template auto then(C&& continuation) { // The type the callback will evaluate to using EvaluatedTo = decltype(std::declval()(std::declval()...)); return EvaluatedTo{ }; - }*/ + } }; } // namespace detail @@ -81,19 +81,19 @@ struct Callback { void operator() (Args... ) { } }; -/*template +template auto make_continuable(Args&&...) { return Continuable<> { }; -}*/ +} -/*auto http_request(std::string url) { +auto http_request(std::string url) { return make_continuable([url](auto& callback) { callback("
hi
"); }); -}*/ +} -/* template + template auto appendHandlerToContinuation(Continuation&& cont, Handler&& handler) { return [cont = std::forward(cont), handler = std::forward(handler)](auto&& continuation) { @@ -108,28 +108,68 @@ auto appendHandlerToContinuation(Continuation&& cont, Handler&& handler) { }; } */ +#include +#include +#include + +auto createEmptyContinuation() { + return [](auto&& callback) { callback(); }; +} + +auto createEmptyCallback() { + return [](auto&&...) { }; +} + +template +struct CallbackResultDecorator { + template + static auto decorate(Callback&& callback) { + return std::forward(callback); + } +}; + +template<> +struct CallbackResultDecorator { + template + static auto decorate(Callback&& callback) { + return [callback = std::forward(callback)](auto&&... args) { + callback(std::forward(args)...); + return createEmptyContinuation(); + }; + } +}; + +// Create the proxy callback that is responsible for invoking +// the real callback and passing the next continuation into +// to the result of the callback. +template +auto createProxyCallback(Callback&& callback, + Next&& next) { + return [callback = std::forward(callback), + next = std::forward(next)] (auto&&... args) mutable { + using Result = decltype(callback(std::forward(args)...)); + using Decorator = CallbackResultDecorator; + Decorator::decorate(std::move(callback)) + (std::forward(args)...)(std::move(next)); + }; +} + 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)); + std::move(continuation)(createProxyCallback( + std::move(callback), std::forward(next))); }; } template void invokeContinuation(Continuation&& continuation) { // Pass an empty callback to the continuation to invoke it - std::forward(continuation)([](auto&&...) {}); + std::forward(continuation)(createEmptyCallback()); } auto makeTestContinuation() { @@ -138,63 +178,20 @@ auto makeTestContinuation() { }; } -void testNextGen() { +int main(int, char**) { auto continuation = makeTestContinuation(); auto then1 = [](std::string) { - int i = 0; - - return makeTestContinuation(); }; - auto then2 = [](std::string) { + auto then2 = []() { 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) { - - }) - .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 1bfea64..366f370 100644 --- a/Test.cpp +++ b/Test.cpp @@ -21,13 +21,13 @@ #include "Continuable.h" -void testNextGen(); +// void testNextGen(); void test_mockup(); void test_incubator(); -int main(int argc, char* const argv[]) +int tttt(int argc, char* const argv[]) { - testNextGen(); + // testNextGen(); return 0; test_mockup();