From 8735405c4562df048d7238840775e16634d8cb0f Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Mon, 22 Jun 2015 15:39:19 +0200 Subject: [PATCH] remove convert_void_to_continuable completely --- include/Continuable.h | 114 +++++++++++++----------------------------- 1 file changed, 34 insertions(+), 80 deletions(-) diff --git a/include/Continuable.h b/include/Continuable.h index 9a98ea1..8b4e60a 100644 --- a/include/Continuable.h +++ b/include/Continuable.h @@ -41,12 +41,6 @@ class Continuable; namespace detail { - // convert_void_to_continuable forward declaration. - /// Corrects void return types from functional types which should be - /// Continuable> - template - struct convert_void_to_continuable; - // unary_chainer forward declaration. template struct unary_chainer; @@ -64,28 +58,12 @@ namespace detail struct is_continuable> : public std::true_type { }; - // MSVC 12 has issues to detect the parameter pack otherwise. - template - struct unary_chainer<_NextRTy, fu::identity<_NextATy...>> - { - typedef convert_void_to_continuable<_NextRTy> base; - - typedef typename convert_void_to_continuable<_NextRTy>::type result_t; - - typedef typename result_t::CallbackFunction callback_t; - }; - template struct multiple_all_chainer { }; - template - using unary_chainer_t = unary_chainer< - fu::return_type_of_t::type>, - fu::argument_type_of_t::type>>; - template struct create_empty_callback&&)>> { @@ -186,49 +164,50 @@ public: return *this; } + template + struct unary_chainer_t + { + // Corrected user given functional + typedef decltype(detail::functional_traits<_ATy...>:: + correct(std::declval::type>())) corrected_t; + + typedef fu::return_type_of_t continuable_t; + + typedef fu::argument_type_of_t arguments_t; + + typedef typename continuable_t::CallbackFunction callback_t; + }; + /// Waits for this continuable and invokes the given callback. template auto then(_CTy&& functional) - -> typename std::enable_if< - !detail::is_continuable< - typename std::decay<_CTy>::type - >::value, - typename detail::unary_chainer_t<_CTy>::result_t - >::type + -> typename unary_chainer_t<_CTy>::continuable_t { + static_assert(std::is_same, + typename unary_chainer_t<_CTy>::arguments_t>::value, + "Given function signature isn't correct, for now it must match strictly!"); + // 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. ForwardFunction&& callback = std::move(_callback_insert); - return typename detail::unary_chainer_t<_CTy>::result_t( - [functional, callback](typename detail::unary_chainer_t<_CTy>::callback_t&& call_next) + auto&& corrected = detail::functional_traits<_ATy...>:: + correct(std::forward<_CTy>(functional)); + + return typename unary_chainer_t<_CTy>::continuable_t( + [corrected, callback](typename unary_chainer_t<_CTy>::callback_t&& call_next) { - callback([functional, call_next](_ATy&&... args) mutable + callback([corrected, call_next](_ATy&&... args) mutable { // Invoke the next callback - detail::unary_chainer_t<_CTy>::base::invoke(functional, std::forward<_ATy>(args)...) + corrected(std::forward<_ATy>(args)...) .invoke(std::move(call_next)); }); }, std::move(*this)); } - /// Waits for this continuable and continues with the given one. - template - auto then(_CTy&& continuable) - -> typename std::enable_if< - detail::is_continuable< - typename std::decay<_CTy>::type - >::value, - typename std::decay<_CTy>::type - >::type - { - static_assert(std::is_rvalue_reference<_CTy&&>::value, - "Given continuable must be passed as r-value!"); - - return then(detail::functional_traits<_ATy...>::box_continuable_trait(std::forward<_CTy>(continuable))); - } - + /* template Continuable& _wrap_all(_CTy&&...) { @@ -236,6 +215,7 @@ public: return *this; } + */ /// Placeholder template @@ -408,6 +388,9 @@ namespace detail > >::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)); @@ -429,6 +412,9 @@ namespace detail typename std::decay<_CTy>::type >::type { + static_assert(std::is_rvalue_reference<_CTy&&>::value, + "Given continuable must be passed as r-value!"); + return std::forward<_CTy>(continuable); } @@ -442,38 +428,6 @@ namespace detail return remove_void_trait(box_continuable_trait(std::forward<_CTy>(functional))); } }; - - template<> - struct convert_void_to_continuable - { - typedef Continuable<> type; - - template - static type invoke(Fn functional, Args&&... args) - { - // Invoke the void returning functional - functional(std::forward(args)...); - - // Return a fake void continuable - return type([](Callback<>&& callback) - { - callback(); - }); - } - }; - - template - struct convert_void_to_continuable> - { - typedef Continuable<_CTy...> type; - - template - static type invoke(Fn functional, Args&&... args) - { - // Invoke the functional as usual. - return functional(std::forward(args)...); - } - }; } #endif // _CONTINUABLE_H_