I have a legacy piece of software that has a single manager which controls a run of the program. It calls methods of various callback classes throughout the execution of the program. These are the user hooks. The problem is that of these 1000 different hooks is that
1) they each obviously have a different interface
2) the run manager only has a slot for one of each.
I have noticed that only allowing for the user to register just one instance of each user hook-in class with the run manager results in a lot of spaghetti code from my group. I would like to write simple wrappers that contain a list of hookins, then loop over the list and call the callback of each instance. Example:
class SomeLegacyUserActionClass
{
public:
virtual void A();
virtual void B();
};
class MyWrapper : public SomeLegacyUserActionClass
{
std::vector< SomeLegacyUserActionClass* > actionList;
public:
void A()
{
// loop over each action in actionList
{
action->A();
}
}
void B()
{
// loop over each action in actionList
{
action->B();
}
}
void addAction( SomeLegacyUserActionClass* action ) { ... }
};
This is becoming very tedious and ugly to do with so many classes. Is there some way I can make a template class or something to do this in one fell blow? There is obviously a pattern here, I just don’t know if I can capitalize on it somehow in c++.
I guess I could get my group to implement some kind of decorator pattern for all their actions and do away with the vectors and loops.
Thanks
This is not possible with templates, because there is no way to retrieve the list of member-functions of a type for use in a template. If you really have a lot of classes, it might be sensible to use classic code-generation.