Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Let a macro count its invocations

I've a huge C project with a module reading and managing configuration data. If I have to add a new configuration parameter, I'll have to edit several functions, e.g. as pseudo-code:

void read_configuration(config *c) {
    read_param("p1", c->p1);
    read_param("p2", c->p2);
    read_param("p3", c->p3);
    /* ... */
}

void dump_configuration(config *c) {
    dump_param("p1", c->p1);
    dump_param("p2", c->p2);
    dump_param("p3", c->p3);
    /* ... */
}

Is there a way to ensure by macro at compile time, that each location has at least the same count of parameters? I thought of making dump_param some kind of macro counting the invocations and then add something like

#if nr_read != nr_dump
    #error "You forgot something, idiot!"
#endif

at the end of the module. I can't find a method to make the macro count its invocations, though...

like image 797
urzeit Avatar asked Dec 03 '25 09:12

urzeit


2 Answers

Since the list of parameters is the same in both functions, how about factoring that out and avoid any possible mismatch ?

Using X-macros

#define X_CONFIG_PARAMS(config) \
    X("p1", (config).p1)        \
    X("p2", (config).p2)        \
    X("p3", (config).p3)

void read_configuration(config *c) {
#define X(name, param) read_param(name, &param);
    X_CONFIG_PARAMS(*c)
#undef X
}

void dump_configuration(config *c) {
#define X(name, param) dump_param(name, &param);
    X_CONFIG_PARAMS(*c)
#undef X
}

Using function pointers

void alter_config(config *c, void(*func)(char const *name, Param *param)) {
    func("p1", &c->p1);
    func("p2", &c->p2);
    func("p3", &c->p3);
}

void read_configuration(config *c) {
    alter_config(c, read_param);
}

void dump_configuration(config *c) {
    alter_config(c, dump_param);
}

Using an array and offsetof

struct param_info {
    char const *name;
    size_t config_offs;
};

param_info allParams[] = {
    {"p1", offsetof(config, p1)},
    {"p2", offsetof(config, p2)},
    {"p3", offsetof(config, p3)}
};

void read_configuration(config *c) {
    size_t paramCount = sizeof allParams / sizeof *allParams;
    for(size_t i = 0; i < paramCount; ++i) {
        Param *p = (Param*)((char*)config + allParams[i].config_offs);
        read_param(allParams[i].name, p);
    }
}

void dump_configuration(config *c) {
    size_t paramCount = sizeof allParams / sizeof *allParams;
    for(size_t i = 0; i < paramCount; ++i) {
        Param *p = (Param*)((char*)config + allParams[i].config_offs);
        dump_param(allParams[i].name, p);
    }
}
like image 196
Quentin Avatar answered Dec 05 '25 22:12

Quentin


I would rather let the preprocessor write the code in the first place.

It could look something like this:

Define the list of parameters in a separate file, say parameters.inc:

PARAM (p1)
PARAM (p2)
...

Then in the source code locally define the macro PARAM as required and let the preprocessor include and expand the contents of parameters.inc:

void read_configuration(config *c) {
#define PARAM(NAME) read_param(#NAME, c->NAME);
#include "parameters.inc"
#undef PARAM
}

void dump_configuration(config *c) {
#define PARAM(NAME) dump_param(#NAME, c->NAME);
#include "parameters.inc"
#undef PARAM
}
like image 38
mkrieger1 Avatar answered Dec 05 '25 23:12

mkrieger1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!