I'm working on implementing a very, very basic component system in C, but now I am at a point where I want to 'dynamically' call some functions. The set-up is very easy: the main program is simply an endless while loop, in which some conditions are checked and in which a "process" function is called for each enabled component.
For example, now it works like this:
while (1) {
input_process();
network_process();
graphics_process();
}
But I want to separate it into separate components, and somehow define in a central place which parts are used. This could be done with simple defines, like so:
#define HAS_NETWORK
...
while (1) {
input_process();
#ifdef HAS_NETWORK
network_process();
#endif
graphics_process();
}
As you can see this is alright for 1 or maybe only a few components, but if I want to do this for all of these (input, network and graphics) and additional components in the future, I would have to put separate #ifdefs in there for each of them, and that's quite tedious.
In pseudo code, what I'm trying to accomplish is the following:
components = {'input', 'network', 'graphics'}
...
foreach component in components
execute component_process()
This way components could easily be added in the future. I don't really mind if the checking is done compile time or run time (although I obviously prefer compile time, but I can imagine run time is easier to implement). I have no idea how to even start.