Wild-card style C preprocessor trick

Viewed 326

I have a lot of compilation flags that configure my program for different customers. For instance: MODE_A_1, MODE_A_2, MODE_B_1, ... MODE_AB_54, ...

I would really like some preprocessor trick that would allow me to to such pseudo code:

#ifdef MODE_A_.*

Instead of having to type every MODE_A_.. occurrences:

#if defined(MODE_A_1) && defined(MODE_A_2) && ...

It would be even cooler if regexes could be applied.

2 Answers

Instead of going by the macro name, why not make the macro name a bit pattern denoting features. For example

#define FEAT_VCORNERS        0x01
#define FEAT_VPANEL          0x02
#define FEAT_DOOR_FULL_PANEL 0x04

OK so it limits you to 32 individual features - that is not many but it may be enough

Then for each customer you define what features they have. It doesn't matter if the pattern is repeated

#define CUST_A (FEAT_VCORNERS)
#define CUST_B (FEAT_VCORNERS)
#define CUST_C (FEAT_VPANEL | FEAT_DOOR_FULL_PANEL)
#define CUST_D (FEAT_VCORNERS | FEAT_VPANEL)

In the build input you could define -DCUST=CUST_D. In the code, you just need to test

#if CUST & FEAT_VCORNERS
...
#endif

I have a lot of compilation flags that configure my program for different customers. For instance: MODE_A_1, MODE_A_2, MODE_B_1, ... MODE_AB_54, ...

But "MODE_A_1" is not a customer. You're confusing two completely different things at a very basic level.

If you want a list of features you can enable/disable independently for different customers, use something like

#if defined(CUSTOMER_GUILLAUME)

#define ENABLE_FEATURE_A
#define ENABLE_FEATURE_1

#elif defined(CUSTOMER_USELESS)

#define ENABLE_FEATURE_B
#define ENABLE_FEATURE_2

// ...

#endif

Then instead of

#if defined(MODE_A_1) && defined(MODE_A_2) && ...

you just use

#if defined(ENABLE_FEATURE_A)

Note that I'm just using on/off feature flags. You can set them to say integer version numbers if you need finer control such as #if (FEATURE_C_VERSION > 123) ...

Related