Search code examples
c-preprocessorx-macros

How to reduce number arguments in xmacro table expansion


I have been working with tables based on xmacros like this:

#define TABLE_MACRO(MAN_TYPE, WOMAN_TYPE) \
   MAN_TYPE(   John, Doe, "Addr1", arg_a, arg_b, arg_c)   \
   WOMAN_TYPE( Jane, Joe, "Addr2", arg_a, arg_b, arg_c)   \
   MAN_TYPE(   Bill, Tom, "Addr3", arg_a, arg_b, arg_c)   \

I have tables with many more arguments than what I am showing, however, in many cases I am expanding the table for only 1 or 2. I use them to generate variables and enums. For example:

#define NAME_LIST(name,last,addr, arg1, arg2, arg3) name,\

enum {
   TABLE_MACRO(NAME_LIST,NAME_LIST)
}Name_List;

Is there a way to take TABLE_MACRO and redefine or change the expansion order of it to have expand to just this?

TABLE_MACRO_NAMES_ONLY(MAN_TYPE, WOMAN_TYPE) \
   MAN_TYPE(   John, )                       \
   WOMAN_TYPE( Jane, )                       \
   MAN_TYPE(   Bill, )                       \

My objetive is to have simplified tables, to be used like:

#define NEW_NAME(name)  New_##name,

TABLE_MACRO_NAMES_ONLY(NEW_NAME, NEW_NAME)

Solution

  • Hopefully the waiting payed off:
    I came up with two solutions. One in which you can keep your original table as it is, but the usage is a bit more inconvenient.

    Solution 1

    // your original table as is
    #define TABLE_MACRO(MAN_TYPE, WOMAN_TYPE) \
       MAN_TYPE(   John, Doe, "Addr1", arg_a, arg_b, arg_c)   \
       WOMAN_TYPE( Jane, Joe, "Addr2", arg_a, arg_b, arg_c)   \
       MAN_TYPE(   Bill, Tom, "Addr3", arg_a, arg_b, arg_c)   \
    
    // create Names only table
    #define MTYPE(a,b,c,d,e,f) MAN(a)
    #define WTYPE(a,b,c,d,e,f) WOMAN(a)
    #define TABLE_NAMES_ONLY \
       TABLE_MACRO(MTYPE,WTYPE)
    
    // usage (here, the defines MUST be named "MAN" and "WOMAN".
    // At least they have to align with the definition of MTYPE and WTYPE)
    #define MAN(name) man_##name
    #define WOMAN(name) woman_##name
    TABLE_NAMES_ONLY
    

    The other solution involves extending your original table by two arguments, but in the end, the usage is easier.

    Solution 2

    // your extended original table (see extra arguments)
    #define TABLE_MACRO(MAN_TYPE, WOMAN_TYPE, m_extra, w_extra) \
       MAN_TYPE(   John, Doe, "Addr1", arg_a, arg_b, arg_c, m_extra, w_extra)   \
       WOMAN_TYPE( Jane, Joe, "Addr2", arg_a, arg_b, arg_c, m_extra, w_extra)   \
       MAN_TYPE(   Bill, Tom, "Addr3", arg_a, arg_b, arg_c, m_extra, w_extra)   \
    
    // create Names only table
    #define MTYPE(a,b,c,d,e,f, m_extra, w_extra) m_extra(a)
    #define WTYPE(a,b,c,d,e,f, m_extra, w_extra) w_extra(a)
    #define TABLE_NAMES_ONLY(male_func, female_func) \
       TABLE_MACRO(MTYPE, WTYPE, male_func, female_func)
    
    // usage (here, the naming of the 2 following defines is arbitrary)
    #define MAN(a) man_##a
    #define WOMAN(a) woman_##a
    TABLE_NAMES_ONLY(MAN, WOMAN)
    
    // different usage:
    #define NAME(a) name_##a
    TABLE_NAMES_ONLY(NAME, NAME)
    

    Both solutions have been tested with gcc 5.3.0 using gcc -E yourTestFile.c. It's possible that there are even better solutions, but these are the ones that came to my mind.