mirror of
https://github.com/Naios/continuable.git
synced 2026-02-09 11:16:40 +08:00
Add trait to test unwrappable
This commit is contained in:
parent
78e7d7c1f5
commit
0e22df8faf
@ -31,8 +31,10 @@ struct Continuable
|
|||||||
ForwardFunction _callback_insert;
|
ForwardFunction _callback_insert;
|
||||||
|
|
||||||
Continuable<_ATy...>() { }
|
Continuable<_ATy...>() { }
|
||||||
Continuable<_ATy...>(ForwardFunction&& callback_insert)
|
|
||||||
: _callback_insert(std::forward<ForwardFunction>(callback_insert)) { }
|
template<typename _FTy>
|
||||||
|
Continuable<_ATy...>(_FTy&& callback_insert)
|
||||||
|
: _callback_insert(std::forward<_FTy>(callback_insert)) { }
|
||||||
|
|
||||||
template <typename _CTy>
|
template <typename _CTy>
|
||||||
Continuable<_ATy...>& then(_CTy&&)
|
Continuable<_ATy...>& then(_CTy&&)
|
||||||
|
|||||||
@ -25,6 +25,7 @@
|
|||||||
#define _FUNCTIONAL_UNWRAP_HPP_
|
#define _FUNCTIONAL_UNWRAP_HPP_
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
namespace fu
|
namespace fu
|
||||||
{
|
{
|
||||||
@ -106,6 +107,20 @@ namespace fu
|
|||||||
typedef unwrap_function_impl<_RTy(_ATy...)> type;
|
typedef unwrap_function_impl<_RTy(_ATy...)> type;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename>
|
||||||
|
struct to_true
|
||||||
|
: std::true_type { };
|
||||||
|
|
||||||
|
/// std::true_type if unwrappable
|
||||||
|
template<typename... Function>
|
||||||
|
static auto test_unwrappable(int)
|
||||||
|
-> to_true<typename select_best_unwrap<Function...>::type::function_type>;
|
||||||
|
|
||||||
|
/// std::false_type if not unwrappable
|
||||||
|
template<typename... Function>
|
||||||
|
static auto test_unwrappable(long)
|
||||||
|
-> std::false_type;
|
||||||
|
|
||||||
} // detail
|
} // detail
|
||||||
|
|
||||||
/// Trait to unwrap function parameters of various types:
|
/// Trait to unwrap function parameters of various types:
|
||||||
@ -140,6 +155,11 @@ namespace fu
|
|||||||
using function_ptr_of_t =
|
using function_ptr_of_t =
|
||||||
typename detail::select_best_unwrap<Function...>::type::function_ptr;
|
typename detail::select_best_unwrap<Function...>::type::function_ptr;
|
||||||
|
|
||||||
|
/// Trait which defines if the given function is unwrappable or not.
|
||||||
|
template<typename... Function>
|
||||||
|
struct is_unwrappable
|
||||||
|
: decltype(detail::test_unwrappable<Function...>(0)) { };
|
||||||
|
|
||||||
} // fu
|
} // fu
|
||||||
|
|
||||||
#endif // _FUNCTIONAL_UNWRAP_HPP_
|
#endif // _FUNCTIONAL_UNWRAP_HPP_
|
||||||
|
|||||||
21
test.cpp
21
test.cpp
@ -6,6 +6,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
enum SpellCastResult
|
enum SpellCastResult
|
||||||
{
|
{
|
||||||
@ -28,9 +30,24 @@ Continuable<SpellCastResult> CastSpell(int id)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
template <typename... T>
|
||||||
|
void test_unwrap(std::string const& msg)
|
||||||
{
|
{
|
||||||
auto lam = [=](Callback<SpellCastResult>&& callback)
|
std::cout << msg << " is unwrappable: " << (fu::is_unwrappable<T...>::value ? "true" : "false") << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int /*argc*/, char** /*argv*/)
|
||||||
|
{
|
||||||
|
test_unwrap<void()>("void()");
|
||||||
|
test_unwrap<std::function<void()>>("std::function<void()>");
|
||||||
|
test_unwrap<std::vector<std::string>>("std::vector<std::string>");
|
||||||
|
|
||||||
|
auto voidcontinue = make_continuable([=](Callback<>&& /*callback*/)
|
||||||
|
{
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
auto lam = [=](Callback<SpellCastResult>&& /*callback*/)
|
||||||
{
|
{
|
||||||
// on success call the callback with SPELL_FAILED_SUCCESS
|
// on success call the callback with SPELL_FAILED_SUCCESS
|
||||||
// callback(SPELL_FAILED_SUCCESS);
|
// callback(SPELL_FAILED_SUCCESS);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user