Add make_continuable

This commit is contained in:
Denis Blank 2015-06-10 17:09:09 +02:00 committed by Naios
parent 51aa0df82c
commit c0d62c822f
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
* 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 <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _CALLBACK_H_
@ -51,6 +52,7 @@ namespace detail
template<typename _CTy>
using unwrap_callback = do_unwrap_callback<::fu::argument_type_of_t<_CTy>>;
/*
template<typename... Args>
struct WeakProxyFactory
{
@ -68,6 +70,7 @@ namespace detail
return CreateProxyFromWeak(WeakCallback<Args...>(shared_callback));
}
};
*/
} // detail
@ -80,7 +83,6 @@ using shared_callback_of_t = typename detail::unwrap_callback<_CTy>::SharedCallb
template<typename _CTy>
using weak_callback_of_t = typename detail::unwrap_callback<_CTy>::WeakCallbackType;
template<typename _CTy>
inline shared_callback_of_t<_CTy>
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
* 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 <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _CONTINUABLE_H_
@ -31,27 +32,39 @@ class ContinuableBase
template <typename _CTy, typename _WTy = void>
class Continuable;
template <typename... _ATy>
class Continuable<Callback<_ATy...>, void>
{
public:
typedef bool type;
};
template <typename... _ATy, typename _WTy>
class Continuable<Callback<_ATy...>, _WTy>
class Continuable<Callback<_ATy...>, typename _WTy>
{
public:
typedef int type;
};
/*
template <typename _CTy>
auto make_continuable(_CTy&& callback)
->
namespace detail
{
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_

View File

@ -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<SpellCastResult> const& callback)
void ProtoCastSpell(int id, Callback<SpellCastResult> const& callback)
{
std::cout << "Cast " << id << std::endl;
@ -42,7 +42,22 @@ void CastSpell(int id, Callback<SpellCastResult> const& callback)
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;
@ -56,7 +71,7 @@ int main(int argc, char** argv)
// .then(std::bind(&CastSpell, 71382, std::placeholders::_1))
.then([](SpellCastResult result, Callback<bool> 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<bool> 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<Callback<bool>> cont;
typedef Continuable<Callback<bool>>::type myty1;
typedef Continuable<Callback<bool>, float>::type myty2;
// typedef Continuable<Callback<bool>>::type myty1;
// typedef Continuable<Callback<bool>, float>::type myty2;
CastSpell(2);
return 0;
}