mirror of
https://github.com/Naios/continuable.git
synced 2026-02-09 11:16:40 +08:00
first progress ,-)
This commit is contained in:
parent
62937050c8
commit
101fef23f8
@ -43,6 +43,10 @@ namespace detail
|
|||||||
template<typename _NextRTy, typename... _NextATy>
|
template<typename _NextRTy, typename... _NextATy>
|
||||||
struct unary_chainer;
|
struct unary_chainer;
|
||||||
|
|
||||||
|
// creates an empty callback.
|
||||||
|
template<typename _FTy>
|
||||||
|
struct create_empty_callback_factory;
|
||||||
|
|
||||||
template<typename... _Cain, typename _Proxy>
|
template<typename... _Cain, typename _Proxy>
|
||||||
struct ContinuableState<std::tuple<_Cain...>, _Proxy>
|
struct ContinuableState<std::tuple<_Cain...>, _Proxy>
|
||||||
{
|
{
|
||||||
@ -66,6 +70,18 @@ namespace detail
|
|||||||
fu::return_type_of_t<typename std::decay<_CTy>::type>,
|
fu::return_type_of_t<typename std::decay<_CTy>::type>,
|
||||||
fu::argument_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>
|
template<typename... _STy, typename... _ATy>
|
||||||
class _ContinuableImpl<ContinuableState<_STy...>, std::function<void(_ATy...)>>
|
class _ContinuableImpl<ContinuableState<_STy...>, std::function<void(_ATy...)>>
|
||||||
{
|
{
|
||||||
@ -82,35 +98,28 @@ namespace detail
|
|||||||
/// to chain everything together
|
/// to chain everything together
|
||||||
ForwardFunction _callback_insert;
|
ForwardFunction _callback_insert;
|
||||||
|
|
||||||
boost::optional<std::function<void()>> _entry_point;
|
|
||||||
|
|
||||||
bool _released;
|
bool _released;
|
||||||
|
|
||||||
std::function<void()> MakeEmptyEntryPoint()
|
|
||||||
{
|
|
||||||
return []
|
|
||||||
{
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
void Dispatch()
|
void Dispatch()
|
||||||
{
|
{
|
||||||
if (_entry_point)
|
// Set released to true to prevent multiple calls
|
||||||
(*_entry_point)();
|
_released = true;
|
||||||
|
|
||||||
|
// Invoke everything with an empty callback
|
||||||
|
_callback_insert(create_empty_callback_factory<ForwardFunction>::create());
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Empty for debugging
|
// Empty for debugging
|
||||||
_ContinuableImpl()
|
_ContinuableImpl()
|
||||||
: _released(false), _callback_insert(), _entry_point() { }
|
: _released(false), _callback_insert() { }
|
||||||
|
|
||||||
/// Deleted copy construct
|
/// Deleted copy construct
|
||||||
_ContinuableImpl(_ContinuableImpl const&) = delete;
|
_ContinuableImpl(_ContinuableImpl const&) = delete;
|
||||||
|
|
||||||
/// Move construct
|
/// Move construct
|
||||||
_ContinuableImpl(_ContinuableImpl&& right)
|
_ContinuableImpl(_ContinuableImpl&& right)
|
||||||
: _released(right._released), _callback_insert(std::move(right._callback_insert)),
|
: _released(right._released), _callback_insert(std::move(right._callback_insert))
|
||||||
_entry_point(std::move(right._entry_point))
|
|
||||||
{
|
{
|
||||||
right._released = true;
|
right._released = true;
|
||||||
}
|
}
|
||||||
@ -118,11 +127,11 @@ namespace detail
|
|||||||
// Construct through a ForwardFunction
|
// Construct through a ForwardFunction
|
||||||
template<typename _FTy>
|
template<typename _FTy>
|
||||||
_ContinuableImpl(_FTy&& callback_insert)
|
_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>
|
template<typename _RSTy, typename _RCTy, typename _FTy>
|
||||||
_ContinuableImpl(_FTy&& callback_insert, _ContinuableImpl<_RSTy, _RCTy>&& right)
|
_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;
|
right._released = true;
|
||||||
}
|
}
|
||||||
@ -170,14 +179,6 @@ namespace detail
|
|||||||
});
|
});
|
||||||
|
|
||||||
}, std::move(*this));
|
}, 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)
|
return make_continuable([=](Callback<bool>&& callback)
|
||||||
{
|
{
|
||||||
|
std::cout << "Validate " << std::endl;
|
||||||
|
|
||||||
callback(true);
|
callback(true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -46,118 +48,112 @@ void test_unwrap(std::string const& msg)
|
|||||||
|
|
||||||
int main(int /*argc*/, char** /*argv*/)
|
int main(int /*argc*/, char** /*argv*/)
|
||||||
{
|
{
|
||||||
Continuable<bool> cb = make_continuable([](Callback<bool>&& callback)
|
CastSpell(1)
|
||||||
{
|
.then([](SpellCastResult)
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
callback(true);
|
return CastSpell(2);
|
||||||
};
|
})
|
||||||
};
|
.then([](SpellCastResult)
|
||||||
|
|
||||||
// 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
|
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
|
//// Entry point
|
||||||
//std::function<void(Callback<bool>&&>)> entry = [continuable_1 /*= move*/, callback_by_user_1 /*given by the user (::then(...))*/]
|
//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