I'm working on creating a simple reflector in C 11, it stores function pointers of instances functions as:
static std::unordered_map<std::string, std::pair<void(EmptyClass::*)(void), int>>* methods;
template<typename ClassType, typename returnType, typename... Args>
static void RegistFunction(std::string name, returnType(ClassType::* func)(Args... args)) {
(*methods)[name] = std::make_pair((void(EmptyClass::*)())func, sizeof...(Args));
}
template<typename ReturnType, typename ClassType, typename... Args>
static ReturnType ExecuteFunction(ClassType* object, std::string name, Args... args) {
if (object == NULL) return;
ReturnType(ClassType:: * func)(Args...) = (ReturnType(ClassType::*)(Args...))(*methods)[name].first;
return (object->*func)(std::forward<Args>(args)...);
}
But when I want to call ExecuteFunction, the number of arguments may be more than the number that function pointer actually accepts. So I need to remove some arguments from the tail of argument list, but it seems I can only remove from head.
template<typename ReturnType, typename ClassType, typename Arg, typename... Args>
static ReturnType ExecuteFunction(ClassType* object, std::string name, Arg arg, Args... args) {
if (sizeof...(Args) 1 > (*methods)[name].second) {
return ExecuteFunction<ReturnType>(std::forward<ClassType*>(object), std::forward<std::string>(name), std::forward<Args>(args)...);
}
if (object == NULL) return;
ReturnType(ClassType:: * func)( Arg, Args...) = (ReturnType(ClassType::*)(Arg, Args...))(*methods)[name].first;
return (object->*func)(std::forward<Arg>(arg), std::forward<Args>(args)...);
}
Is there any solution to remove arguments at the tail of variadic method template?
CodePudding user response:
Here's a C 11 implementation depending only on std::string
and std::unordered_map
. Some mandatory remarks:
- As mentioned, this is extremely brittle due to inferring the function type by the provided arguments. This is UB waiting to happen.
method
really shouldn't be a pointer.- If your return type is not assignable, this will break spectacularly.
- The class pointer really should be a reference instead.
- If you think the implementation is insane, then yes, it is indeed, and you should give up on being fully generic.
A C 11 implementation of std::index_sequence
and friends can be found here.
template<typename...>
struct typelist {};
template<size_t, typename, typename, typename, typename>
struct call;
template<size_t N, typename R, typename C, typename... Accum, typename Head, typename... Tail>
struct call<N, R, C, typelist<Accum...>, typelist<Head, Tail...>>
: call<N, R, C, typelist<Accum..., Head>, typelist<Tail...>>
{
};
template<typename R, typename C, typename... Accum, typename Head, typename... Tail>
struct call<sizeof...(Accum), R, C, typelist<Accum...>, typelist<Head, Tail...>>
{
template<typename... Ts>
int operator()(Ts&&...)
{
return 0;
}
template<typename... Ts>
int operator()(R& ret, void (EmptyClass::* g)(), C& obj, Accum&... args, Ts&&...)
{
auto f = (R (C::*)(Accum...))g;
ret = (obj.*f)(std::move(args)...);
return 0;
}
};
template<typename R, typename C, typename... Args, size_t... Is>
R switcher(int i, index_sequence<Is...>, void (EmptyClass::* g)(), C& obj, Args&... args)
{
R ret{};
int unused[] = {(i == Is ?
call<Is, R, C, typelist<>, typelist<Args..., void>>{}(ret, g, obj, args...)
: 0)...};
(void)unused;
return ret;
}
template<typename C, typename R, typename... Args>
void reg(std::string name, R (C::* func)(Args... args)) {
(*methods)[name] = std::make_pair((void (EmptyClass::*)())func, sizeof...(Args));
}
template<typename R, typename C, typename... Args>
R exec(C* obj, std::string name, Args... args) {
if(obj == nullptr)
throw "a tantrum";
auto& info = (*methods)[name];
auto g = info.first;
size_t i = info.second;
if(i > sizeof...(Args))
throw "a fit";
return switcher<R>(i, make_index_sequence<sizeof...(Args) 1>{}, g, *obj, args...);
}
CodePudding user response:
To continue the old-way, you might do it pre-C 11 with hard-coded limit:
// No args
template<typename ReturnType, typename ClassType>
static ReturnType ExecuteFunction(ClassType* object, std::string name)
{
switch ((*methods)[name].second) {
case 0: {
if (object == NULL) return {};
auto func = (ReturnType(ClassType::*)())((*methods)[name].first);
return (object->*func)();
}
default: throw std::runtime_error("Wrong argument");
}
}
// One arg
template<typename ReturnType, typename ClassType, typename T1>
static ReturnType ExecuteFunction(ClassType* object, std::string name, T1 arg1)
{
switch ((*methods)[name].second) {
case 0: return ExecuteFunction(object, name);
case 1: {
if (object == NULL) return {};
auto func = (ReturnType(ClassType::*)(T1))((*methods)[name].first);
return (object->*func)(std::forward<T1>(arg1));
}
default: throw std::runtime_error("Wrong argument");
}
}
// Two args
template<typename ReturnType, typename ClassType, typename T1, typename T2>
static ReturnType ExecuteFunction(ClassType* object, std::string name, T1 arg1, T2 arg2)
{
switch ((*methods)[name].second) {
case 0: return ExecuteFunction(object, name);
case 1: return ExecuteFunction(object, name, std::forward<T1>(arg1));
case 2: {
if (object == NULL) return {};
auto func = (ReturnType(ClassType::*)(T1, T2))((*methods)[name].first);
return (object->*func)(std::forward<T1>(arg1), std::forward<T2>(arg2));
}
default: throw std::runtime_error("Wrong argument");
}
}
// .. up to reasonable limit.