lundi 4 janvier 2021

Choose certain MACRO depending on if a define is set

I have two different macros that can get run depending on if the a define is set or not:

// Will do stuff later with the macroString
#define CASE_true(DEFINE, params) \
    auto macroString = std::to_string(DEFINE); \
    return true;

#define CASE_false(DEFINE, params) \
    return false;

What I want to be able to do is run CHOOSE_CASE macro with a define variable name and expand the respective function.

#define IS_DEFINED(x) IS_DEFINED2(x)
#define IS_DEFINED2(x) (#x[0] == 0 || (#x[0] >= '1' && #x[0] <= '9'))
    
#define CHOOSE_CASE(DEFINE, params) \
        CASE_##IS_DEFINED(DEFINE)##(DEFINE, params)

So if in my application somebody writes: #define MY_VAR 1, then CASE_ will expand the true case and get the value of MY_VAR, if MY_VAR is not set/defined, then CASE_ will expand the false case and not do anything with the define. Is this possible? The IS_DEFINED MACRO is probably not set up correctly but I don't know of any way to get it to work.

Aucun commentaire:

Enregistrer un commentaire