Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to conditionally determine which functions are called at compile time?

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.

like image 511
pbean Avatar asked Dec 03 '09 15:12

pbean


People also ask

What is determined at compile time?

Compile-time and Runtime are the two programming terms used in the software development. Compile-time is the time at which the source code is converted into an executable code while the run time is the time at which the executable code is started running.

In which the response to a function is determined at the compile time?

In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time. The linking of a function with an object during compile time is called early binding. It is also called static binding.

When function is selected for execution at the compilation time is called as?

In computing, compile-time function execution (or compile time function evaluation, or general constant expressions) is the ability of a compiler, that would normally compile a function to machine code and execute it at run time, to execute the function at compile time.

Is Auto determined at compile time?

Even when a variable is declared auto, its type is fixed at compile time.


2 Answers

You need pointers to functions, create an array of pointers to functions and index it dynamically.

Here link about function pointers.

like image 150
Arkaitz Jimenez Avatar answered Sep 23 '22 18:09

Arkaitz Jimenez


Compile-time solution: a pre-build step and include directive inside that loop, e.g.

while (1) {
#include "components.generated.c"
}

A basic script to generate that file might look like (Python):

components = ('input', 'networking', 'graphics')
# this could also be e.g. a glob on a directory or a config file

with open('components.generated.c', 'w') as fp:
    for component in components:
        print >>fp, '%s_process();' % component

Any decent build system will allow you to do that.

like image 32
Cat Plus Plus Avatar answered Sep 22 '22 18:09

Cat Plus Plus