Reputation: 2508
I have a class that is supposed to forward some arguments to a functor, but doing some additional stuff.
class SemaphoredEventRouterObserver {
My_Semaphore m_semaphore;
mywrapper_Function<void (*)(const Event &)> callback;
SemaphoredEventRouterObserver(My_Semaphore semaphore, mywrapper_Function<void (*)(const Event &)> o_callback) : m_semaphore(semaphore) {
callback = o_callback;
}
void operator()(const Event & event) {
callback(event);
semaphone.post();
}
}
The problem is I might have to create dozens of classes like this, because for other functors the arguments vary, so instead of receiving const Event & event
, I could receive string arg
, int c
, Myclass abc
or anything else.
Is it possible to create a template class for this? I am using just stl and I cannot use boost, although I would be curious to see answers related to boost as well.
Upvotes: 0
Views: 66
Reputation: 217293
With variadic template, you may do something like:
typename <typename... Ts>
class SemaphoredRouterObserver {
My_Semaphore m_semaphore;
mywrapper_Function<void (*)(Ts...)> callback;
public:
SemaphoredEventRouterObserver(My_Semaphore semaphore,
mywrapper_Function<void (*)(Ts...)> o_callback)
: m_semaphore(semaphore),
callback(o_callback)
{}
void operator()(Ts... args) {
callback(args...);
semaphone.post();
}
};
And then
using SemaphoredEventRouterObserver = SemaphoredRouterObserver<const Event&>;
Upvotes: 1