Skip to content

Instantly share code, notes, and snippets.

@isc30 isc30/callable.hpp
Last active Sep 2, 2018

Embed
What would you like to do?
template-based wrapper for any callable with signature + metadata traits
#pragma once
#include <type_traits>
#include <functional>
template<typename TCallable, typename TSignature>
constexpr bool is_callable_as_v = std::is_constructible<std::function<TSignature>, TCallable>::value;
template<
typename TCallable,
typename TSignature,
typename = std::enable_if_t<is_callable_as_v<TCallable, TSignature>>>
using Callable = TCallable;
template<typename TCallable>
struct CallableMetadata
: CallableMetadata<decltype(&TCallable::operator())>
{
};
template<class TClass, typename TReturn, typename... TArgs>
struct CallableMetadata<TReturn(TClass::*)(TArgs...)>
{
using class_t = TClass;
using return_t = TReturn;
using args_tuple_t = std::tuple<TArgs...>;
using ptr_t = TReturn(*)(TArgs...);
// Beware! this function makes a copy of the closure! and of the arguments when called!
static ptr_t generatePointer(const TClass& closure)
{
static TClass staticClosureCopy = closure;
return [](TArgs... args)
{
return staticClosureCopy(args...);
};
}
};
template<class TClass, typename TReturn, typename... TArgs>
struct CallableMetadata<TReturn(TClass::*)(TArgs...) const>
{
using class_t = TClass;
using return_t = TReturn;
using args_tuple_t = std::tuple<TArgs...>;
using ptr_t = TReturn(*)(TArgs...) const;
// Beware! this function makes a copy of the closure! and of the arguments when called!
static ptr_t generatePointer(const TClass& closure)
{
static TClass staticClosureCopy = closure;
return [](TArgs... args)
{
return staticClosureCopy(args...);
};
}
};
// Beware! this function makes a copy of the closure! and of the arguments when called!
template<typename TCallable>
auto* callableToPointer(const TCallable& callable)
{
return CallableMetadata<TCallable>::generatePointer(callable);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.