/* * Copyright (C) 2015 Naios * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef _CONTINUABLE_H_ #define _CONTINUABLE_H_ #include "Callback.h" namespace detail { } // detail template struct ContinuableState { }; template > class Continuable; template class Continuable, _State> { public: typedef std::function&&)> ForwardFunction; private: // Function which expects a callback that is inserted from the Continuable // to chain everything together ForwardFunction _callback_insert; bool released; void Dispatch() { } public: /// Deleted copy construct template Continuable(Continuable<_RCTy, _RState> const&) = delete; /// Move construct template Continuable(Continuable<_RCTy, _RState>&& right) : released(right.released) { right.released = true; } // Construct through a ForwardFunction template Continuable(_FTy&& callback_insert) : _callback_insert(std::forward<_FTy>(callback_insert)), released(false) { } /// Destructor which calls the dispatch chain if needed. ~Continuable() { if (!released) { released = true; Dispatch(); } } /// Deleted copy assign template Continuable& operator= (Continuable<_RCTy, _RState> const&) = delete; /// Move construct assign template Continuable& operator= (Continuable<_RCTy, _RState>&& right) { released = right.released; right.released = true; return *this; } // TODO Accept only correct callbacks template Continuable> then(_CTy&&) { // TODO Transmute the returned callback here. return Continuable>(std::move(*this)); } }; namespace detail { template struct ContinuableFactory; template struct ContinuableFactory<_FTy, _RTy, ::fu::identity&&>> { static auto CreateFrom(_FTy&& functional) -> Continuable> { return Continuable>( typename Continuable>::ForwardFunction(std::forward<_FTy>(functional))); } }; template using continuable_factory_t = ContinuableFactory< _FTy, ::fu::return_type_of_t<_FTy>, ::fu::argument_type_of_t<_FTy>>; } // detail /// Wraps a functional object which expects a r-value callback as argument into a continuable. /// The callable is invoked when the continuable shall continue. /// For example: /// make_continuable([](Callback&& callback) /// { /// /* Continue here */ /// callback(5); /// }); template inline auto make_continuable(_FTy&& functional) -> decltype(detail::continuable_factory_t<_FTy>::CreateFrom(std::declval<_FTy>())) { return detail::continuable_factory_t<_FTy>::CreateFrom(std::forward<_FTy>(functional)); } #endif // _CONTINUABLE_H_