Questions tagged [x-macros]

Using macros to use the same tokens in multiple ways.

X-Macros are a use of the to use the same tokens in different patterns.
They either use #includes or additional macros for the input to transform:

#define MAPPING X(0, 55 do_a) X(1, 42, do_b) X(23, 7, do_c)

// Sometime later:
    switch(expr) {
        #define X(a, b, c) case a: return b + c(a, b);
        MAPPING
        #undef X
    default:
        abort();
    }

Or maybe:

    switch(expr) {
        #define X(a, b, c) case a: return b + c(a, b);
        #include "mapping.inc"
        #undef X
    default:
        abort();
    }

Why use X?
It's extremely short, and unlikely to already be #define-d.

46 questions
1
vote
2 answers

Can any one explain about X-macros with example code to use them?

I am trying to understand the X-macros topic in detail. But didn't get the full clarity on this. It would be better if any one of the expert will explain this topic with some example "how to use, how to call." I have found several articles, but…
0
votes
1 answer

How to relate X-macro to array of function pointer

I applied X-macro mechanism to get enumeration-to-string relation. #define CMD_TABLE \ X(cmd_A)\ X(cmd_B)\ //////////////////////////////////// typedef enum { EMPTY, #define X(x) x, CMD_TABLE #undef…
Andy Lin
  • 381
  • 2
  • 14
0
votes
0 answers

Generating multiple lists from one C X-Macro

I have a list of command ENUMs that can either be a request type, or a response type. typedef enum { ENUM1, ENUM2, ... } command_t; And, I was planning on using X Macros to generate its ENUM, as well as name-string mapping table as…
0
votes
2 answers

Conditional X-MACROs to align ENUMs and String

I have a list of enums: typedef enum { ENUM1, ENUM2, #if FLAG ENUM3, #endif } enum_var_t; And a corresponding list of strings to align: typedef struct { char[50] name; int val; } name_val_map_t name_val_map_t…
0
votes
1 answer

force unused parameter for macro

Simple idea: I'm using X-macros to define command list structure and declare command callbacks. #include #include #include #include #include #define COMMAND_LIST(X) \ X(toto_all) \ …
Guillaume D
  • 1,958
  • 2
  • 6
  • 30
0
votes
1 answer

Compilation error when printing array elements in a X-Macro

I have the following code where I am using x-macro: #define X_FIELDS \ X(int, var1) \ X(uint8_t, var3) \ X(uint16_t, var4) \ XA(uint8_t, arr1, 4) \ XB(char, arr2, 2) typedef struct { #define X(type,…
Akay
  • 946
  • 8
  • 23
0
votes
1 answer

Making x macro work with do while 0

I am trying to make x macro work with do while 0 as checkpatch.pl is unhappy about it. However, it breaks the logic. Wondering if anyone have any suggestions to make it work? #define X_TYPES do { \ X(BABA, "baba") \ X(INVALID,…
user3053970
  • 85
  • 1
  • 10
0
votes
1 answer

How to turn a macro's name into literal string, when the macro is inside an struct array?

say I have the following #define STR(x) #x #define ONE 1 #define TWO 2 typedef struct { int item; char * name; }bag_t; bag_t my_bag[] = { {ONE, ""}; {TWO, ""}; } I want to add the name of the macro to the name variable so…
essie
  • 1
0
votes
1 answer

Use enum to determine type of return result ( a hack using Macro )

I have many types of game-object that are related together is some ways. All relations is implemented by Map. #include using namespace std; template class Map{ //N:N relation public: std::vector
javaLover
  • 6,039
  • 2
  • 14
  • 57
0
votes
1 answer

X-Macro with concatenation parameter

I am using X-Macro to create enumerations using the following code #define WIDGET_OFFSETS \ X_WIDGET_OFFSET(OFFSET_HEIGHT_WA, 1, OFFSET_HEIGHT_WB, 2, OFFSET_HEIGHT_WC, 3) \ …
lordhog
  • 2,673
  • 5
  • 26
  • 35
0
votes
1 answer

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,…
0
votes
1 answer

Preprocessor #include directive and macro expansion

I have the code below. When using the #define LIST directive, doxygen generates documentation for the Child_* classes. When using the #include directive, documentation of the Child_* classes are ignored. Config file is exposed below as well.…
Heyji
  • 1,051
  • 8
  • 26
0
votes
1 answer

X-macro breaks doxygen callgraph

I have 3 files: test.c int table[] = { #define X(val) val, #include "test.x" #undef X }; void level2(void) { level3(); level4(); } void level3(void) { level4(); } test2.c void level1(void) { level2(); level3(); …
user694733
  • 13,861
  • 1
  • 40
  • 62
0
votes
1 answer

Is it possible to modify this X-Macro to build a struct, which includes arrays? How?

Found this very helpful Q/A on SO: Is there any way to loop through a struct with elements of different types in C? but since I am quite new to the whole X-Macro stuff, I was wondering, if and how would it be possible to adapt this example for a…
Jook
  • 4,356
  • 3
  • 21
  • 48
-3
votes
1 answer

Force strings and pointers into flash when using X-macro expansion

I thought I understood X-macros pretty well but this is really stumping me. I defined the following code: #define FOR_DESCR_STRINGS(apply) \ apply(LANGUAGE_ID_STRING, "\0x04\0x03\0x09\0x04") \ apply(MANUFACTURER_STRING, "Quest Engineering &…