From 66b8ec4c66cce6300e20421d3871afb7b2b59c58 Mon Sep 17 00:00:00 2001 From: Naios Date: Tue, 16 Jun 2015 13:37:26 +0200 Subject: [PATCH] more work --- fluent/Continuable.h | 12 +++++++++++- test.cpp | 31 ++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/fluent/Continuable.h b/fluent/Continuable.h index a64da94..a96a9ff 100644 --- a/fluent/Continuable.h +++ b/fluent/Continuable.h @@ -214,12 +214,21 @@ namespace detail } */ + /* + /// Validates the Continuable + inline _ContinuableImpl& Validate() + { + _released = false; + return *this; + } + /// Invalidates the Continuable - _ContinuableImpl& invalidate() + inline _ContinuableImpl& Invalidate() { _released = true; return *this; } + */ }; template<> @@ -249,6 +258,7 @@ namespace detail template static type invoke(Fn functional, Args&&... args) { + // Invoke the functional as usual. return functional(std::forward(args)...); } }; diff --git a/test.cpp b/test.cpp index 6395943..6e6787c 100644 --- a/test.cpp +++ b/test.cpp @@ -10,6 +10,8 @@ #include #include +#include + enum SpellCastResult { SPELL_FAILED_SUCCESS = 0, @@ -20,14 +22,25 @@ enum SpellCastResult SPELL_FAILED_ALREADY_BEING_TAMED = 5 }; -Continuable CastSpell(int id) +template +using Optional = boost::optional; + +// Original method taking an optional callback. +void CastSpell(int id, Optional> const& callback = boost::none) +{ + std::cout << "Casting " << id << std::endl; + + // on success call the callback with SPELL_FAILED_SUCCESS + if (callback) + (*callback)(SPELL_FAILED_SUCCESS); +} + +// Promise wrapped callback decorator. +Continuable CastSpellPromise(int id) { return make_continuable([=](Callback&& callback) { - std::cout << "Cast " << id << std::endl; - - // on success call the callback with SPELL_FAILED_SUCCESS - callback(SPELL_FAILED_SUCCESS); + CastSpell(id, callback); }); } @@ -49,10 +62,10 @@ void test_unwrap(std::string const& msg) int main(int /*argc*/, char** /*argv*/) { - CastSpell(1) + CastSpellPromise(1) .then([](SpellCastResult) { - return CastSpell(2); + return CastSpellPromise(2); }) .then([](SpellCastResult) { @@ -60,11 +73,11 @@ int main(int /*argc*/, char** /*argv*/) }) .then([] { - return CastSpell(3); + return CastSpellPromise(3); }) .then([](SpellCastResult) { - return CastSpell(4); + return CastSpellPromise(4); }) .then([](SpellCastResult) {