From 074859d14f87c6e7cd96fade884fe7ce5262b14a Mon Sep 17 00:00:00 2001 From: Naios Date: Tue, 16 Jun 2015 20:02:00 +0200 Subject: [PATCH] Add the possible to direct use the continuable --- fluent/Continuable.h | 24 +++++++++++++++++++++++- test.cpp | 11 +++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/fluent/Continuable.h b/fluent/Continuable.h index f56b9d9..eb41140 100644 --- a/fluent/Continuable.h +++ b/fluent/Continuable.h @@ -136,9 +136,11 @@ namespace detail } // Then implementation of eval functionals. + // Enable if the given type isn't a continuable. template auto _then(_CTy&& functional) - -> typename unary_chainer_t<_CTy>::result_t + -> typename std::enable_if::type>::value, + typename unary_chainer_t<_CTy>::result_t>::type { // Transfer the insert function to the local scope. // Also use it as an r-value reference to try to get move semantics with c++11 lambdas. @@ -157,6 +159,26 @@ namespace detail }, std::move(*this)); } + // The continuable itself + template + auto _then(_CTy&& continuable) + -> typename std::enable_if::type>::value, + typename std::decay<_CTy>::type>::type + { + static_assert(std::is_rvalue_reference<_CTy&&>::value, + "Given continuable must be passed as r-value!"); + + // Trick C++11 lambda capture rules for non copyable but moveable continuables. + std::shared_ptr::type> shared_continuable = + std::make_shared::type>(std::forward<_CTy>(continuable)); + + // Create a fake function which returns the value on invoke. + return _then([shared_continuable](_ATy...) + { + return std::move(*shared_continuable); + }); + } + public: /// Deleted copy construct _ContinuableImpl(_ContinuableImpl const&) = delete; diff --git a/test.cpp b/test.cpp index 39eabcd..c930a7e 100644 --- a/test.cpp +++ b/test.cpp @@ -83,14 +83,9 @@ int main(int /*argc*/, char** /*argv*/) { std::cout << "Pause a callback (void test) " << std::endl; }) - .then([] - { - return CastSpellPromise(3); - }) - .then([](SpellCastResult) - { - return CastSpellPromise(4); - }) + .then(CastSpellPromise(3)) + .then(CastSpellPromise(4)) + .then(CastSpellPromise(5)) .then([](SpellCastResult) { return Validate();