Add make_continuable

This commit is contained in:
Naios 2015-06-10 17:09:09 +02:00
parent 06df1c4313
commit 9f9c901aa5
3 changed files with 82 additions and 49 deletions

View File

@ -1,18 +1,19 @@
/* /*
* Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> * Copyright (C) 2015 Naios <naios-dev@live.de>
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software: you can redistribute it and/or modify
* under the terms of the GNU General Public License as published by the * it under the terms of the GNU General Public License as published by
* Free Software Foundation; either version 2 of the License, or (at your * the Free Software Foundation, either version 3 of the License, or
* option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, but WITHOUT * This program is distributed in the hope that it will be useful,
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * but WITHOUT ANY WARRANTY; without even the implied warranty of
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License
* with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef _CALLBACK_H_ #ifndef _CALLBACK_H_
@ -51,6 +52,7 @@ namespace detail
template<typename _CTy> template<typename _CTy>
using unwrap_callback = do_unwrap_callback<::fu::argument_type_of_t<_CTy>>; using unwrap_callback = do_unwrap_callback<::fu::argument_type_of_t<_CTy>>;
/*
template<typename... Args> template<typename... Args>
struct WeakProxyFactory struct WeakProxyFactory
{ {
@ -68,6 +70,7 @@ namespace detail
return CreateProxyFromWeak(WeakCallback<Args...>(shared_callback)); return CreateProxyFromWeak(WeakCallback<Args...>(shared_callback));
} }
}; };
*/
} // detail } // detail
@ -80,7 +83,6 @@ using shared_callback_of_t = typename detail::unwrap_callback<_CTy>::SharedCallb
template<typename _CTy> template<typename _CTy>
using weak_callback_of_t = typename detail::unwrap_callback<_CTy>::WeakCallbackType; using weak_callback_of_t = typename detail::unwrap_callback<_CTy>::WeakCallbackType;
template<typename _CTy> template<typename _CTy>
inline shared_callback_of_t<_CTy> inline shared_callback_of_t<_CTy>
make_shared_callback(_CTy&& callback) make_shared_callback(_CTy&& callback)

View File

@ -1,18 +1,19 @@
/* /*
* Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> * Copyright (C) 2015 Naios <naios-dev@live.de>
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software: you can redistribute it and/or modify
* under the terms of the GNU General Public License as published by the * it under the terms of the GNU General Public License as published by
* Free Software Foundation; either version 2 of the License, or (at your * the Free Software Foundation, either version 3 of the License, or
* option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, but WITHOUT * This program is distributed in the hope that it will be useful,
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * but WITHOUT ANY WARRANTY; without even the implied warranty of
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License
* with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef _CONTINUABLE_H_ #ifndef _CONTINUABLE_H_
@ -31,27 +32,39 @@ class ContinuableBase
template <typename _CTy, typename _WTy = void> template <typename _CTy, typename _WTy = void>
class Continuable; class Continuable;
template <typename... _ATy>
class Continuable<Callback<_ATy...>, void>
{
public:
typedef bool type;
};
template <typename... _ATy, typename _WTy> template <typename... _ATy, typename _WTy>
class Continuable<Callback<_ATy...>, _WTy> class Continuable<Callback<_ATy...>, typename _WTy>
{ {
public: public:
typedef int type;
}; };
namespace detail
/*
template <typename _CTy>
auto make_continuable(_CTy&& callback)
->
{ {
template <typename _FTy, typename _RTy, typename... _ATy>
struct ContinuableFactory;
template <typename _FTy, typename _RTy, typename... _ATy>
struct ContinuableFactory<_FTy, _RTy, std::tuple<_ATy...>>
{
static auto CreateFrom(_FTy&& functional)
-> int
{
return 1;
}
};
template <typename _FTy>
using continuable_factory_t = ContinuableFactory<
_FTy, ::fu::return_type_of_t<_FTy>, ::fu::argument_type_of_t<_FTy>>;
} // detail
template <typename _FTy>
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_ #endif /// _CONTINUABLE_H_

View File

@ -18,7 +18,7 @@ enum SpellCastResult
SPELL_FAILED_ALREADY_BEING_TAMED = 5 SPELL_FAILED_ALREADY_BEING_TAMED = 5
}; };
ProtoContinueable CastSpell(int id) ProtoContinueable ProtoCastSpell(int id)
{ {
std::cout << "Cast " << id << std::endl; std::cout << "Cast " << id << std::endl;
@ -26,7 +26,7 @@ ProtoContinueable CastSpell(int id)
return ProtoContinueable(); return ProtoContinueable();
} }
ProtoContinueable MoveTo(int point) ProtoContinueable ProtoMoveTo(int point)
{ {
std::cout << "Move to point " << point << std::endl; std::cout << "Move to point " << point << std::endl;
@ -34,7 +34,7 @@ ProtoContinueable MoveTo(int point)
return ProtoContinueable(); return ProtoContinueable();
} }
void CastSpell(int id, Callback<SpellCastResult> const& callback) void ProtoCastSpell(int id, Callback<SpellCastResult> const& callback)
{ {
std::cout << "Cast " << id << std::endl; std::cout << "Cast " << id << std::endl;
@ -42,7 +42,22 @@ void CastSpell(int id, Callback<SpellCastResult> const& callback)
callback(SPELL_FAILED_SUCCESS); callback(SPELL_FAILED_SUCCESS);
} }
void MoveTo(int point, Callback<bool> const& callback) /*Continuable<SpellCastResult>*/ int CastSpell(int id)
{
auto lam = [=](Callback<SpellCastResult> 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<decltype(lam), void, std::tuple<Callback<SpellCastResult> const&>>::CreateFrom(std::move(lam));
}
void ProtoMoveTo(int point, Callback<bool> const& callback)
{ {
std::cout << "Move to point " << point << std::endl; 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(std::bind(&CastSpell, 71382, std::placeholders::_1))
.then([](SpellCastResult result, Callback<bool> const& callback) .then([](SpellCastResult result, Callback<bool> const& callback)
{ {
MoveTo(1, callback); ProtoMoveTo(1, callback);
}) })
.then([](bool success) .then([](bool success)
{ {
@ -64,12 +79,12 @@ int main(int argc, char** argv)
std::cout << "finish everything" << std::endl; std::cout << "finish everything" << std::endl;
}); });
auto cabt = []() auto cabt = []()
{ {
// Do something // Do something
return MoveTo(2); return ProtoMoveTo(2);
}; };
typedef Callback<bool> cbd1; typedef Callback<bool> cbd1;
@ -91,7 +106,7 @@ int main(int argc, char** argv)
WeakCallbackContainer callback; WeakCallbackContainer callback;
// Some tests // Some tests
CastSpell(22872) ProtoCastSpell(22872)
.weak(callback) .weak(callback)
.then([](bool success) .then([](bool success)
{ {
@ -101,7 +116,7 @@ int main(int argc, char** argv)
} }
// Do something // Do something
/*return*/ MoveTo(2); return ProtoMoveTo(2);
}) })
.then([] .then([]
{ {
@ -161,8 +176,11 @@ int main(int argc, char** argv)
typedef Continuable<Callback<bool>> cont; typedef Continuable<Callback<bool>> cont;
typedef Continuable<Callback<bool>>::type myty1; // typedef Continuable<Callback<bool>>::type myty1;
typedef Continuable<Callback<bool>, float>::type myty2; // typedef Continuable<Callback<bool>, float>::type myty2;
CastSpell(2);
return 0; return 0;
} }