diff --git a/fluent/Continuable.h b/fluent/Continuable.h index 1b855df..8692587 100644 --- a/fluent/Continuable.h +++ b/fluent/Continuable.h @@ -47,6 +47,7 @@ namespace detail friend class _ContinuableImpl; public: + typedef Callback<_ATy...> CallbackFunction; typedef std::function&&)> ForwardFunction; private: @@ -54,23 +55,35 @@ 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.value()(); } public: // Empty for debugging _ContinuableImpl() - : _released(false), _callback_insert() { } + : _released(false), _callback_insert(), _entry_point() { } /// Deleted copy construct _ContinuableImpl(_ContinuableImpl const&) = delete; /// Move construct _ContinuableImpl(_ContinuableImpl&& right) - : _released(right._released), _callback_insert(std::move(right._callback_insert)) + : _released(right._released), _callback_insert(std::move(right._callback_insert)), + _entry_point(std::move(right._entry_point)) { right._released = true; } @@ -78,7 +91,13 @@ namespace detail // Construct through a ForwardFunction template _ContinuableImpl(_FTy&& callback_insert) - : _callback_insert(std::forward<_FTy>(callback_insert)), _released(false) { } + : _callback_insert(std::forward<_FTy>(callback_insert)), _released(false), _entry_point() { } + + /* + template + _ContinuableImpl(_ContinuableImpl, _RCTy>&& right, _FTy&& callback_insert) + : _callback_insert(std::forward<_FTy>(callback_insert)), _released(false), _entry_point() { } + */ /// Destructor which calls the dispatch chain if needed. ~_ContinuableImpl() @@ -126,12 +145,20 @@ namespace detail template struct unary_chainer<_NextRTy, fu::identity<_NextATy...>> { + typedef typename convert_void_to_continuable<_NextRTy>::type result_t; + + typedef typename result_t::CallbackFunction callback_t; + + /* template - static auto chain(_CTy&& /*functional*//*, _MTy&& me*/) - -> _ContinuableImpl> // typename convert_void_to_continuable::type + static auto chain(_CTy&&) + -> typename convert_void_to_continuable<_NextRTy>::type + // _ContinuableImpl> { - return _ContinuableImpl>(); + return typename convert_void_to_continuable<_NextRTy>::type(); + // _ContinuableImpl>(); } + */ }; template @@ -141,11 +168,27 @@ namespace detail template auto then(_CTy&& functional) - -> decltype(unary_chainer_t<_CTy>::chain(std::declval<_CTy>() - // std::declval<_ContinuableImpl, std::function>>()) - )) + -> typename unary_chainer_t<_CTy>::result_t { - return unary_chainer_t<_CTy>::chain(std::forward<_CTy>(functional)/*, std::move(*this)*/); + // Next callback insert + auto next = [=](typename unary_chainer_t<_CTy>::callback_t&& next_insert_callback) + { + _callback_insert([=](_ATy... args) + { + // typename unary_chainer_t<_CTy>::result_t next = + functional(std::forward<_ATy>(args)...); + + // next_insert_callback(result.); + // next._insert_callback(next_insert_callback); + }); + }; + + // Debugging + // next(unary_chainer_t<_CTy>::callback_t()); + + // decltype(unary_chainer_t<_CTy>::chain(std::declval<_CTy>())) + return typename unary_chainer_t<_CTy>::result_t(); + // unary_chainer_t<_CTy>::chain(std::forward<_CTy>(functional)); } /* diff --git a/test.cpp b/test.cpp index b9a3d3b..970effe 100644 --- a/test.cpp +++ b/test.cpp @@ -97,7 +97,7 @@ int main(int /*argc*/, char** /*argv*/) }); // Wraps a callback function into a continuable - auto cba1 = make_continuable([=](Callback&&) + Continuable cba1 = make_continuable([=](Callback&&) { @@ -120,6 +120,7 @@ int main(int /*argc*/, char** /*argv*/) callback(SPELL_FAILED_AFFECTING_COMBAT); }; + // Implemented by user std::function&&)>(SpellCastResult)> cn1 = [](SpellCastResult) {