Add trait to test unwrappable

This commit is contained in:
Denis Blank 2015-06-11 15:33:01 +02:00 committed by Naios
parent 78e7d7c1f5
commit 0e22df8faf
3 changed files with 43 additions and 4 deletions

View File

@ -31,8 +31,10 @@ struct Continuable
ForwardFunction _callback_insert;
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>
Continuable<_ATy...>& then(_CTy&&)

View File

@ -25,6 +25,7 @@
#define _FUNCTIONAL_UNWRAP_HPP_
#include <functional>
#include <type_traits>
namespace fu
{
@ -106,6 +107,20 @@ namespace fu
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
/// Trait to unwrap function parameters of various types:
@ -140,6 +155,11 @@ namespace fu
using function_ptr_of_t =
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
#endif // _FUNCTIONAL_UNWRAP_HPP_

View File

@ -6,6 +6,8 @@
#include <iostream>
#include <exception>
#include <type_traits>
#include <string>
#include <vector>
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
// callback(SPELL_FAILED_SUCCESS);