diff --git a/fluent/Continuable.h b/fluent/Continuable.h index b7d277e..5508535 100644 --- a/fluent/Continuable.h +++ b/fluent/Continuable.h @@ -43,6 +43,10 @@ namespace detail template struct unary_chainer; + // creates an empty callback. + template + struct create_empty_callback_factory; + template struct ContinuableState, _Proxy> { @@ -66,6 +70,18 @@ namespace detail fu::return_type_of_t::type>, fu::argument_type_of_t::type>>; + template + struct create_empty_callback_factory&&)>> + { + static auto create() + -> Callback + { + return [](Args...) + { + }; + } + }; + template class _ContinuableImpl, std::function> { @@ -82,35 +98,28 @@ namespace detail /// to chain everything together ForwardFunction _callback_insert; - boost::optional> _entry_point; - bool _released; - std::function MakeEmptyEntryPoint() - { - return [] - { - }; - } - void Dispatch() { - if (_entry_point) - (*_entry_point)(); + // Set released to true to prevent multiple calls + _released = true; + + // Invoke everything with an empty callback + _callback_insert(create_empty_callback_factory::create()); } public: // Empty for debugging _ContinuableImpl() - : _released(false), _callback_insert(), _entry_point() { } + : _released(false), _callback_insert() { } /// Deleted copy construct _ContinuableImpl(_ContinuableImpl const&) = delete; /// Move construct _ContinuableImpl(_ContinuableImpl&& right) - : _released(right._released), _callback_insert(std::move(right._callback_insert)), - _entry_point(std::move(right._entry_point)) + : _released(right._released), _callback_insert(std::move(right._callback_insert)) { right._released = true; } @@ -118,11 +127,11 @@ namespace detail // Construct through a ForwardFunction template _ContinuableImpl(_FTy&& callback_insert) - : _callback_insert(std::forward<_FTy>(callback_insert)), _released(false), _entry_point() { } + : _callback_insert(std::forward<_FTy>(callback_insert)), _released(false) { } template _ContinuableImpl(_FTy&& callback_insert, _ContinuableImpl<_RSTy, _RCTy>&& right) - : _callback_insert(std::forward<_FTy>(callback_insert)), _released(right._released), _entry_point() + : _callback_insert(std::forward<_FTy>(callback_insert)), _released(right._released) { right._released = true; } @@ -170,14 +179,6 @@ namespace detail }); }, std::move(*this)); - - /* - return typename unary_chainer_t<_CTy>::result_t - (std::move(*this), [=](typename unary_chainer_t<_CTy>::callback_t&& next_insert_callback) - { - - }); - */ } /* diff --git a/test.cpp b/test.cpp index 8d716e4..90c3bdc 100644 --- a/test.cpp +++ b/test.cpp @@ -34,6 +34,8 @@ Continuable Validate() { return make_continuable([=](Callback&& callback) { + std::cout << "Validate " << std::endl; + callback(true); }); } @@ -46,118 +48,112 @@ void test_unwrap(std::string const& msg) int main(int /*argc*/, char** /*argv*/) { - Continuable cb = make_continuable([](Callback&& callback) - { - - callback(true); - }); - - test_unwrap("void()"); - test_unwrap>("std::function"); - test_unwrap>("std::vector"); - - make_continuable([=](Callback<>&&) - { - - }); - - int i = 0; - ++i; - - auto lam = [=](Callback&&) - { - // on success call the callback with SPELL_FAILED_SUCCESS - // callback(SPELL_FAILED_SUCCESS); - }; - - fu::function_type_of_t fun1; - fun1 = lam; - fun1(Callback()); - - fu::function_type_of_t> fun2; - - shared_callback_of_t> sc1; - weak_callback_of_t> sc2; - - make_weak_wrapped_callback(sc1); - make_weak_wrapped_callback(sc2); - - WeakCallbackContainer callback; - - auto weakCallback = callback([] - { - }); - - typedef Continuable cont123; - - typedef Continuable myty1; - typedef Continuable myty2; - - // Convertible test - - // Continuable> spell - { - auto stack = CastSpell(63362) - .then([](SpellCastResult) - { - return CastSpell(35254); - }) - .then([](SpellCastResult) - { - return Validate(); - }); - - int iii = 0; - iii = 1; - } - - // Wraps a callback function into a continuable - Continuable cba1 = make_continuable([=](Callback&&) - { - - - - }); - - cba1.invalidate(); - - CastSpell(63362); - - std::vector myvec; - - typedef fu::requires_functional_constructible>::type test_assert1; - // typedef fu::requires_functional_constructible>::type test_assert2; - - // Brainstorming: this shows an example callback chain - // Given by continuable - std::function&&)> continuable_1 = [](Callback&& callback) - { - callback(SPELL_FAILED_AFFECTING_COMBAT); - }; - - // Implemented by user - std::function&&)>(SpellCastResult)> callback_by_user_1 = [](SpellCastResult) - { - // Given by continuable - // Fn2 - return [](Callback&& callback) + CastSpell(1) + .then([](SpellCastResult) { - callback(true); - }; - }; - - // Implemented by user - std::function&&)>(bool)> cn2 = [](bool val) - { - // Finished - std::cout << "Callback chain finished! -> " << val << std::endl; - - // Given by continuable (auto end) - return [](Callback<>&&) + return CastSpell(2); + }) + .then([](SpellCastResult) { - // Empty callback - }; - }; + return CastSpell(3); + }) + .then([](SpellCastResult) + { + return Validate(); + }); + + //Continuable cb = make_continuable([](Callback&& callback) + //{ + + // callback(true); + //}); + + //test_unwrap("void()"); + //test_unwrap>("std::function"); + //test_unwrap>("std::vector"); + + //make_continuable([=](Callback<>&&) + //{ + + //}); + + //int i = 0; + //++i; + + //auto lam = [=](Callback&&) + //{ + // // on success call the callback with SPELL_FAILED_SUCCESS + // // callback(SPELL_FAILED_SUCCESS); + //}; + + //fu::function_type_of_t fun1; + //fun1 = lam; + //fun1(Callback()); + + //fu::function_type_of_t> fun2; + // + //shared_callback_of_t> sc1; + //weak_callback_of_t> sc2; + // + //make_weak_wrapped_callback(sc1); + //make_weak_wrapped_callback(sc2); + + //WeakCallbackContainer callback; + // + //auto weakCallback = callback([] + //{ + //}); + + //typedef Continuable cont123; + + //typedef Continuable myty1; + //typedef Continuable myty2; + + //// Convertible test + // + //// Continuable> spell + //{ + // auto stack = + + // int iii = 0; + // iii = 1; + //} + + //std::vector myvec; + + //typedef fu::requires_functional_constructible>::type test_assert1; + //// typedef fu::requires_functional_constructible>::type test_assert2; + + //// Brainstorming: this shows an example callback chain + //// Given by continuable + //std::function&&)> continuable_1 = [](Callback&& callback) + //{ + // callback(SPELL_FAILED_AFFECTING_COMBAT); + //}; + + //// Implemented by user + //std::function&&)>(SpellCastResult)> callback_by_user_1 = [](SpellCastResult) + //{ + // // Given by continuable + // // Fn2 + // return [](Callback&& callback) + // { + // callback(true); + // }; + //}; + + //// Implemented by user + //std::function&&)>(bool)> cn2 = [](bool val) + //{ + // // Finished + // std::cout << "Callback chain finished! -> " << val << std::endl; + + // // Given by continuable (auto end) + // return [](Callback<>&&) + // { + // // Empty callback + // }; + //}; //// Entry point //std::function&&>)> entry = [continuable_1 /*= move*/, callback_by_user_1 /*given by the user (::then(...))*/]