I would like to bind any kind of event to functions of any type for the (G)UI system of a game I am working on.
I would like to store any kind of functions and its initial arguments in a Functor template class, which I can bind to a signal binding index. Events bound to a signal binding will trigger it once they appear in the event queue, which will cause all Functors bound to this binding to be called.
I have found that the way to store the function arguments was to use an std::tuple
in the functor template, but I need help initializing it correctly and unpacking it properly when calling the function.
This is what I have so far:
template<typename R, typename... Args>
class FuncBinding {
private:
std::tuple<Args...> args;
R(*fun)(Args...);
public:
FuncBinding(R(*pF)(Args...) , Args... pArgs)
:fun(pF), args(std::make_tuple<Args...>(pArgs)), signalBindings(std::vector<unsigned int>()){}
std::vector<unsigned int> signalBindings;
static std::vector<FuncBinding<R, Args...>> allInstances;
void bind(unsigned int pSignalBinding) {
signalBindings.push_back(pSignalBinding);
}
void fix() {
allInstances.push_back(*this);
}
R invoke() {
return fun(args...);
}
R callFunc(Args... pArgs) {
return fun(pArgs...);
}
};
template<typename R, typename... Args>
std::vector<FuncBinding<R, Args...>> FuncBinding<R, Args...>::allInstances = std::vector<FuncBinding<R, Args...>>();
How do I use the parameter pack Args
correctly in order to:
- create the template class
std::tuple
appropiately - initialize the
std::tuple<Args...>
instanceargs
with the functor arguments - unpack the tuple when calling the function through the function pointer
R(*fun)(Args...)
Aucun commentaire:
Enregistrer un commentaire