From c0d62c822f8e9c99c4abbcf2ce3438d163da6706 Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Wed, 10 Jun 2015 17:09:09 +0200 Subject: [PATCH] Add make_continuable --- fluent++/Callback.h | 26 +++++++++-------- fluent++/Continuable.h | 65 +++++++++++++++++++++++++----------------- test.cpp | 40 +++++++++++++++++++------- 3 files changed, 82 insertions(+), 49 deletions(-) diff --git a/fluent++/Callback.h b/fluent++/Callback.h index b9bba05..c469d63 100644 --- a/fluent++/Callback.h +++ b/fluent++/Callback.h @@ -1,18 +1,19 @@ + /* - * Copyright (C) 2008-2015 TrinityCore + * 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 2 of the License, or (at your - * option) any later version. + * 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. + * 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 . + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ #ifndef _CALLBACK_H_ @@ -51,6 +52,7 @@ namespace detail template using unwrap_callback = do_unwrap_callback<::fu::argument_type_of_t<_CTy>>; + /* template struct WeakProxyFactory { @@ -68,6 +70,7 @@ namespace detail return CreateProxyFromWeak(WeakCallback(shared_callback)); } }; + */ } // detail @@ -80,7 +83,6 @@ using shared_callback_of_t = typename detail::unwrap_callback<_CTy>::SharedCallb template using weak_callback_of_t = typename detail::unwrap_callback<_CTy>::WeakCallbackType; - template inline shared_callback_of_t<_CTy> make_shared_callback(_CTy&& callback) diff --git a/fluent++/Continuable.h b/fluent++/Continuable.h index 39576c9..86fad4a 100644 --- a/fluent++/Continuable.h +++ b/fluent++/Continuable.h @@ -1,18 +1,19 @@ + /* - * Copyright (C) 2008-2015 TrinityCore + * 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 2 of the License, or (at your - * option) any later version. + * 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. + * 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 . + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ #ifndef _CONTINUABLE_H_ @@ -31,27 +32,39 @@ class ContinuableBase template class Continuable; -template -class Continuable, void> -{ -public: - typedef bool type; -}; - template -class Continuable, _WTy> +class Continuable, typename _WTy> { public: - typedef int type; + }; - -/* -template -auto make_continuable(_CTy&& callback) - -> +namespace detail { + template + struct ContinuableFactory; + + template + struct ContinuableFactory<_FTy, _RTy, std::tuple<_ATy...>> + { + static auto CreateFrom(_FTy&& functional) + -> int + { + return 1; + } + }; + + template + using continuable_factory_t = ContinuableFactory< + _FTy, ::fu::return_type_of_t<_FTy>, ::fu::argument_type_of_t<_FTy>>; + +} // detail + +template +inline auto make_continuable(_FTy&& functional) + -> decltype(typename detail::continuable_factory_t<_FTy>::CreateFrom(std::declval<_FTy>())) +{ + return detail::continuable_factory_t<_FTy>::CreateFrom(std::forward<_FTy>(functional)); } -*/ #endif /// _CONTINUABLE_H_ diff --git a/test.cpp b/test.cpp index f338a6c..6617237 100644 --- a/test.cpp +++ b/test.cpp @@ -18,7 +18,7 @@ enum SpellCastResult SPELL_FAILED_ALREADY_BEING_TAMED = 5 }; -ProtoContinueable CastSpell(int id) +ProtoContinueable ProtoCastSpell(int id) { std::cout << "Cast " << id << std::endl; @@ -26,7 +26,7 @@ ProtoContinueable CastSpell(int id) return ProtoContinueable(); } -ProtoContinueable MoveTo(int point) +ProtoContinueable ProtoMoveTo(int point) { std::cout << "Move to point " << point << std::endl; @@ -34,7 +34,7 @@ ProtoContinueable MoveTo(int point) return ProtoContinueable(); } -void CastSpell(int id, Callback const& callback) +void ProtoCastSpell(int id, Callback const& callback) { std::cout << "Cast " << id << std::endl; @@ -42,7 +42,22 @@ void CastSpell(int id, Callback const& callback) callback(SPELL_FAILED_SUCCESS); } -void MoveTo(int point, Callback const& callback) +/*Continuable*/ int CastSpell(int id) +{ + auto lam = [=](Callback const& callback) + { + std::cout << "Cast " << id << std::endl; + + // on success call the callback with SPELL_FAILED_SUCCESS + callback(SPELL_FAILED_SUCCESS); + }; + + auto ct = make_continuable(std::move(lam)); + + return detail::ContinuableFactory const&>>::CreateFrom(std::move(lam)); +} + +void ProtoMoveTo(int point, Callback const& callback) { std::cout << "Move to point " << point << std::endl; @@ -56,7 +71,7 @@ int main(int argc, char** argv) // .then(std::bind(&CastSpell, 71382, std::placeholders::_1)) .then([](SpellCastResult result, Callback const& callback) { - MoveTo(1, callback); + ProtoMoveTo(1, callback); }) .then([](bool success) { @@ -64,12 +79,12 @@ int main(int argc, char** argv) std::cout << "finish everything" << std::endl; }); - + auto cabt = []() { // Do something - return MoveTo(2); + return ProtoMoveTo(2); }; typedef Callback cbd1; @@ -91,7 +106,7 @@ int main(int argc, char** argv) WeakCallbackContainer callback; // Some tests - CastSpell(22872) + ProtoCastSpell(22872) .weak(callback) .then([](bool success) { @@ -101,7 +116,7 @@ int main(int argc, char** argv) } // Do something - /*return*/ MoveTo(2); + return ProtoMoveTo(2); }) .then([] { @@ -161,8 +176,11 @@ int main(int argc, char** argv) typedef Continuable> cont; - typedef Continuable>::type myty1; - typedef Continuable, float>::type myty2; + // typedef Continuable>::type myty1; + // typedef Continuable, float>::type myty2; + + + CastSpell(2); return 0; }