mirror of
https://github.com/Naios/continuable.git
synced 2025-12-08 01:36:46 +08:00
first progress ,-)
This commit is contained in:
parent
13e1aedeaa
commit
0b770f9474
@ -43,6 +43,10 @@ namespace detail
|
||||
template<typename _NextRTy, typename... _NextATy>
|
||||
struct unary_chainer;
|
||||
|
||||
// creates an empty callback.
|
||||
template<typename _FTy>
|
||||
struct create_empty_callback_factory;
|
||||
|
||||
template<typename... _Cain, typename _Proxy>
|
||||
struct ContinuableState<std::tuple<_Cain...>, _Proxy>
|
||||
{
|
||||
@ -66,6 +70,18 @@ namespace detail
|
||||
fu::return_type_of_t<typename std::decay<_CTy>::type>,
|
||||
fu::argument_type_of_t<typename std::decay<_CTy>::type>>;
|
||||
|
||||
template<typename... Args>
|
||||
struct create_empty_callback_factory<std::function<void(std::function<void(Args...)>&&)>>
|
||||
{
|
||||
static auto create()
|
||||
-> Callback<Args...>
|
||||
{
|
||||
return [](Args...)
|
||||
{
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... _STy, typename... _ATy>
|
||||
class _ContinuableImpl<ContinuableState<_STy...>, std::function<void(_ATy...)>>
|
||||
{
|
||||
@ -82,35 +98,28 @@ namespace detail
|
||||
/// to chain everything together
|
||||
ForwardFunction _callback_insert;
|
||||
|
||||
boost::optional<std::function<void()>> _entry_point;
|
||||
|
||||
bool _released;
|
||||
|
||||
std::function<void()> MakeEmptyEntryPoint()
|
||||
{
|
||||
return []
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
void Dispatch()
|
||||
{
|
||||
if (_entry_point)
|
||||
(*_entry_point)();
|
||||
// Set released to true to prevent multiple calls
|
||||
_released = true;
|
||||
|
||||
// Invoke everything with an empty callback
|
||||
_callback_insert(create_empty_callback_factory<ForwardFunction>::create());
|
||||
}
|
||||
|
||||
public:
|
||||
// Empty for debugging
|
||||
_ContinuableImpl()
|
||||
: _released(false), _callback_insert(), _entry_point() { }
|
||||
: _released(false), _callback_insert() { }
|
||||
|
||||
/// Deleted copy construct
|
||||
_ContinuableImpl(_ContinuableImpl const&) = delete;
|
||||
|
||||
/// Move construct
|
||||
_ContinuableImpl(_ContinuableImpl&& right)
|
||||
: _released(right._released), _callback_insert(std::move(right._callback_insert)),
|
||||
_entry_point(std::move(right._entry_point))
|
||||
: _released(right._released), _callback_insert(std::move(right._callback_insert))
|
||||
{
|
||||
right._released = true;
|
||||
}
|
||||
@ -118,11 +127,11 @@ namespace detail
|
||||
// Construct through a ForwardFunction
|
||||
template<typename _FTy>
|
||||
_ContinuableImpl(_FTy&& callback_insert)
|
||||
: _callback_insert(std::forward<_FTy>(callback_insert)), _released(false), _entry_point() { }
|
||||
: _callback_insert(std::forward<_FTy>(callback_insert)), _released(false) { }
|
||||
|
||||
template<typename _RSTy, typename _RCTy, typename _FTy>
|
||||
_ContinuableImpl(_FTy&& callback_insert, _ContinuableImpl<_RSTy, _RCTy>&& right)
|
||||
: _callback_insert(std::forward<_FTy>(callback_insert)), _released(right._released), _entry_point()
|
||||
: _callback_insert(std::forward<_FTy>(callback_insert)), _released(right._released)
|
||||
{
|
||||
right._released = true;
|
||||
}
|
||||
@ -170,14 +179,6 @@ namespace detail
|
||||
});
|
||||
|
||||
}, std::move(*this));
|
||||
|
||||
/*
|
||||
return typename unary_chainer_t<_CTy>::result_t
|
||||
(std::move(*this), [=](typename unary_chainer_t<_CTy>::callback_t&& next_insert_callback)
|
||||
{
|
||||
|
||||
});
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
216
test.cpp
216
test.cpp
@ -34,6 +34,8 @@ Continuable<bool> Validate()
|
||||
{
|
||||
return make_continuable([=](Callback<bool>&& callback)
|
||||
{
|
||||
std::cout << "Validate " << std::endl;
|
||||
|
||||
callback(true);
|
||||
});
|
||||
}
|
||||
@ -46,118 +48,112 @@ void test_unwrap(std::string const& msg)
|
||||
|
||||
int main(int /*argc*/, char** /*argv*/)
|
||||
{
|
||||
Continuable<bool> cb = make_continuable([](Callback<bool>&& callback)
|
||||
{
|
||||
|
||||
callback(true);
|
||||
});
|
||||
|
||||
test_unwrap<void()>("void()");
|
||||
test_unwrap<std::function<void()>>("std::function<void()>");
|
||||
test_unwrap<std::vector<std::string>>("std::vector<std::string>");
|
||||
|
||||
make_continuable([=](Callback<>&&)
|
||||
{
|
||||
|
||||
});
|
||||
|
||||
int i = 0;
|
||||
++i;
|
||||
|
||||
auto lam = [=](Callback<SpellCastResult>&&)
|
||||
{
|
||||
// on success call the callback with SPELL_FAILED_SUCCESS
|
||||
// callback(SPELL_FAILED_SUCCESS);
|
||||
};
|
||||
|
||||
fu::function_type_of_t<decltype(lam)> fun1;
|
||||
fun1 = lam;
|
||||
fun1(Callback<SpellCastResult>());
|
||||
|
||||
fu::function_type_of_t<Callback<int>> fun2;
|
||||
|
||||
shared_callback_of_t<std::function<void(int)>> sc1;
|
||||
weak_callback_of_t<Callback<int>> sc2;
|
||||
|
||||
make_weak_wrapped_callback(sc1);
|
||||
make_weak_wrapped_callback(sc2);
|
||||
|
||||
WeakCallbackContainer callback;
|
||||
|
||||
auto weakCallback = callback([]
|
||||
{
|
||||
});
|
||||
|
||||
typedef Continuable<bool> cont123;
|
||||
|
||||
typedef Continuable<bool> myty1;
|
||||
typedef Continuable<bool, float> myty2;
|
||||
|
||||
// Convertible test
|
||||
|
||||
// Continuable<Callback<SpellCastResult>> spell
|
||||
{
|
||||
auto stack = CastSpell(63362)
|
||||
.then([](SpellCastResult)
|
||||
{
|
||||
return CastSpell(35254);
|
||||
})
|
||||
.then([](SpellCastResult)
|
||||
{
|
||||
return Validate();
|
||||
});
|
||||
|
||||
int iii = 0;
|
||||
iii = 1;
|
||||
}
|
||||
|
||||
// Wraps a callback function into a continuable
|
||||
Continuable<SpellCastResult> cba1 = make_continuable([=](Callback<SpellCastResult>&&)
|
||||
{
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
cba1.invalidate();
|
||||
|
||||
CastSpell(63362);
|
||||
|
||||
std::vector<int> myvec;
|
||||
|
||||
typedef fu::requires_functional_constructible<std::function<void()>>::type test_assert1;
|
||||
// typedef fu::requires_functional_constructible<std::vector<int>>::type test_assert2;
|
||||
|
||||
// Brainstorming: this shows an example callback chain
|
||||
// Given by continuable
|
||||
std::function<void(Callback<SpellCastResult>&&)> continuable_1 = [](Callback<SpellCastResult>&& callback)
|
||||
{
|
||||
callback(SPELL_FAILED_AFFECTING_COMBAT);
|
||||
};
|
||||
|
||||
// Implemented by user
|
||||
std::function<std::function<void(Callback<bool>&&)>(SpellCastResult)> callback_by_user_1 = [](SpellCastResult)
|
||||
{
|
||||
// Given by continuable
|
||||
// Fn2
|
||||
return [](Callback<bool>&& callback)
|
||||
CastSpell(1)
|
||||
.then([](SpellCastResult)
|
||||
{
|
||||
callback(true);
|
||||
};
|
||||
};
|
||||
|
||||
// Implemented by user
|
||||
std::function<std::function<void(Callback<>&&)>(bool)> cn2 = [](bool val)
|
||||
{
|
||||
// Finished
|
||||
std::cout << "Callback chain finished! -> " << val << std::endl;
|
||||
|
||||
// Given by continuable (auto end)
|
||||
return [](Callback<>&&)
|
||||
return CastSpell(2);
|
||||
})
|
||||
.then([](SpellCastResult)
|
||||
{
|
||||
// Empty callback
|
||||
};
|
||||
};
|
||||
return CastSpell(3);
|
||||
})
|
||||
.then([](SpellCastResult)
|
||||
{
|
||||
return Validate();
|
||||
});
|
||||
|
||||
//Continuable<bool> cb = make_continuable([](Callback<bool>&& callback)
|
||||
//{
|
||||
|
||||
// callback(true);
|
||||
//});
|
||||
|
||||
//test_unwrap<void()>("void()");
|
||||
//test_unwrap<std::function<void()>>("std::function<void()>");
|
||||
//test_unwrap<std::vector<std::string>>("std::vector<std::string>");
|
||||
|
||||
//make_continuable([=](Callback<>&&)
|
||||
//{
|
||||
|
||||
//});
|
||||
|
||||
//int i = 0;
|
||||
//++i;
|
||||
|
||||
//auto lam = [=](Callback<SpellCastResult>&&)
|
||||
//{
|
||||
// // on success call the callback with SPELL_FAILED_SUCCESS
|
||||
// // callback(SPELL_FAILED_SUCCESS);
|
||||
//};
|
||||
|
||||
//fu::function_type_of_t<decltype(lam)> fun1;
|
||||
//fun1 = lam;
|
||||
//fun1(Callback<SpellCastResult>());
|
||||
|
||||
//fu::function_type_of_t<Callback<int>> fun2;
|
||||
//
|
||||
//shared_callback_of_t<std::function<void(int)>> sc1;
|
||||
//weak_callback_of_t<Callback<int>> sc2;
|
||||
//
|
||||
//make_weak_wrapped_callback(sc1);
|
||||
//make_weak_wrapped_callback(sc2);
|
||||
|
||||
//WeakCallbackContainer callback;
|
||||
//
|
||||
//auto weakCallback = callback([]
|
||||
//{
|
||||
//});
|
||||
|
||||
//typedef Continuable<bool> cont123;
|
||||
|
||||
//typedef Continuable<bool> myty1;
|
||||
//typedef Continuable<bool, float> myty2;
|
||||
|
||||
//// Convertible test
|
||||
//
|
||||
//// Continuable<Callback<SpellCastResult>> spell
|
||||
//{
|
||||
// auto stack =
|
||||
|
||||
// int iii = 0;
|
||||
// iii = 1;
|
||||
//}
|
||||
|
||||
//std::vector<int> myvec;
|
||||
|
||||
//typedef fu::requires_functional_constructible<std::function<void()>>::type test_assert1;
|
||||
//// typedef fu::requires_functional_constructible<std::vector<int>>::type test_assert2;
|
||||
|
||||
//// Brainstorming: this shows an example callback chain
|
||||
//// Given by continuable
|
||||
//std::function<void(Callback<SpellCastResult>&&)> continuable_1 = [](Callback<SpellCastResult>&& callback)
|
||||
//{
|
||||
// callback(SPELL_FAILED_AFFECTING_COMBAT);
|
||||
//};
|
||||
|
||||
//// Implemented by user
|
||||
//std::function<std::function<void(Callback<bool>&&)>(SpellCastResult)> callback_by_user_1 = [](SpellCastResult)
|
||||
//{
|
||||
// // Given by continuable
|
||||
// // Fn2
|
||||
// return [](Callback<bool>&& callback)
|
||||
// {
|
||||
// callback(true);
|
||||
// };
|
||||
//};
|
||||
|
||||
//// Implemented by user
|
||||
//std::function<std::function<void(Callback<>&&)>(bool)> cn2 = [](bool val)
|
||||
//{
|
||||
// // Finished
|
||||
// std::cout << "Callback chain finished! -> " << val << std::endl;
|
||||
|
||||
// // Given by continuable (auto end)
|
||||
// return [](Callback<>&&)
|
||||
// {
|
||||
// // Empty callback
|
||||
// };
|
||||
//};
|
||||
|
||||
//// Entry point
|
||||
//std::function<void(Callback<bool>&&>)> entry = [continuable_1 /*= move*/, callback_by_user_1 /*given by the user (::then(...))*/]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user