Skip to content

Instantly share code, notes, and snippets.

@gelldur
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gelldur/2bc3f99c59d344a315c6 to your computer and use it in GitHub Desktop.
Save gelldur/2bc3f99c59d344a315c6 to your computer and use it in GitHub Desktop.
Chcking for onEnter method
template<typename T>
struct has_onEnter
{
private:
template<typename U>
static auto test(int) -> decltype(std::declval<U>().onEnter(), std::true_type
{});
template<typename >
static std::false_type test(...);
public:
static const bool value = std::is_same<decltype(test<T>(0)), std::true_type>::value;
};
template<typename T>
typename std::enable_if<has_onEnter<T>::value, std::function<void()>>::type check_onEnter(T* value)
{
return [=]()
{
value->onEnter();
};
}
template<typename T>
typename std::enable_if<has_onEnter<T>::value == false, std::function<void()>>::type check_onEnter(T* value)
{
return
{};
}
// example
/*
void addListener(const int tag, T* listener)
{
if (has_onEnter<T>::value || has_onExit<T>::value)
{
_listeners.emplace_back( { tag, check_onEnter<T>(*listener), check_onExit<T>(listener) });
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment