diff --git a/NextGen.cpp b/NextGen.cpp index 8699319..14478af 100644 --- a/NextGen.cpp +++ b/NextGen.cpp @@ -120,7 +120,7 @@ struct SelfDispatcher { } }; -template +template class ContinuableBase; static auto createEmptyContinuation() { @@ -239,27 +239,49 @@ void invokeContinuation(Continuation&& continuation) { std::forward(continuation)(createEmptyCallback()); } +template +struct ContinuableConfig { + using Continuation = ContinuationType; + using Dispatcher = DispatcherType; + + template + using ChangeContinuationTo = ContinuableConfig< + NewType, Dispatcher + >; + + template + using ChangeDispatcherTo = ContinuableConfig< + Continuation, NewType + >; +}; + template auto make_continuable(Continuation&& continuation, Dispatcher&& dispatcher = SelfDispatcher{}) noexcept { - return ContinuableBase, std::decay_t> { - std::forward(continuation), std::forward(dispatcher) + using Config = ContinuableConfig< + std::decay_t, + std::decay_t + >; + return ContinuableBase { + std::forward(continuation), + std::forward(dispatcher) }; } -template +template class ContinuableBase { - template + template friend class ContinuableBase; - ContinuableBase(Continuation continuation_, Ownership ownership_, - Dispatcher dispatcher_) noexcept + ContinuableBase(typename Config::Continuation continuation_, + Ownership ownership_, + typename Config::Dispatcher dispatcher_) noexcept : continuation(std::move(continuation_)), dispatcher(std::move(dispatcher_)), ownership(std::move(ownership_)) { } public: - ContinuableBase(Continuation continuation_, - Dispatcher dispatcher_ = Dispatcher{}) noexcept + ContinuableBase(typename Config::Continuation continuation_, + typename Config::Dispatcher dispatcher_) noexcept : continuation(std::move(continuation_)), dispatcher(std::move(dispatcher_)) { } ~ContinuableBase() { @@ -274,7 +296,10 @@ public: auto then(Callback&& callback)&& { auto next = appendHandlerToContinuation(std::move(continuation), std::forward(callback)); - return ContinuableBase, Dispatcher> { + using Transformed = ContinuableBase< + typename Config::template ChangeContinuationTo + >; + return Transformed { std::move(next), std::move(ownership), std::move(dispatcher) }; } @@ -283,51 +308,62 @@ public: auto then(Callback&& callback) const& { auto next = appendHandlerToContinuation(continuation, std::forward(callback)); - return ContinuableBase, Dispatcher> { + using Transformed = ContinuableBase< + typename Config::template ChangeContinuationTo + >; + return Transformed { std::move(next), ownership, dispatcher }; } - template - auto post(NewStrand&& newStrand)&& { - return ContinuableBase { + template + auto post(NewDispatcher&& newDispatcher)&& { + using Transformed = ContinuableBase< + typename Config::template ChangeDispatcherTo> + >; + return Transformed { std::move(continuation), std::move(ownership), - std::forward(newStrand) + std::forward(newDispatcher) }; } - template - auto post(NewStrand&& newStrand) const& { - return ContinuableBase { + template + auto post(NewDispatcher&& newDispatcher) const& { + using Transformed = ContinuableBase< + typename Config::template ChangeDispatcherTo> + >; + return Transformed { continuation, ownership, - std::forward(newStrand) + std::forward(newDispatcher) }; } private: - Continuation continuation; - Dispatcher dispatcher; + typename Config::Continuation continuation; + typename Config::Dispatcher dispatcher; Ownership ownership; }; static auto makeTestContinuation() { - return make_continuable([i = std::make_unique(0)](auto&& callback) { + return make_continuable([](auto&& callback) { callback("
hi
"); }); } int main(int, char**) { - auto dispatcher = [](auto callable) { - - }; + auto dispatcher = SelfDispatcher{}; int res = 0; makeTestContinuation() .post(dispatcher) .then([](std::string) { + + return std::make_tuple(47, 46, 45); }) .then([&](int val1, int val2, int val3) { + + res += val1 + val2 + val3; int i = 0; });