x-macros

Stringifying an conditionally compiled enum in C

*爱你&永不变心* 提交于 2019-12-11 04:05:34
问题 Our system has a large number of enums denoting stuff such as events, errors etc. I'm trying to build infrastructure that would allow us to log each received event or error message as a string (instead of a plain integer), without having to build and maintain two lists for each enum. I found the X Macro technique quite suitable for my needs. Thus, instead of doing this: typedef enum { valA, valB, valC } some_enum; const char* some_enum_strings[] = { "valA", "valB", "valC" }; I'm doing this:

C - How to assign a value inside my macro?

ε祈祈猫儿з 提交于 2019-12-11 01:30:17
问题 I am trying to assign a value inside my x-macro, but I don't really understand why it is not working: #include <stdio.h> typedef struct { int a; int b; } struct_t; #define MY_LIST \ MY_ELEMENT(a) \ MY_ELEMENT(b) #define MY_ELEMENT(x) struct_t x; \ x.a=33; MY_LIST #undef MY_ELEMENT int main(void) { fprintf(stdout, "a: %d\n", a.a); return 0; } When compiling this I get the following error: test.c:14:2: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token x.a=33; Could

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

大城市里の小女人 提交于 2019-12-09 03:56:36
问题 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 struct with arrays - like this: typedef struct { uint8 Addr1[SIZEOF_ADDR]; uint8 Addr2[SIZEOF_ADDR]; uint8 Addr3[SIZEOF_ADDR]; } TEST; This is what would be to adapt: //--- first describe the structure, the fields, their types and how to print them

X-macro breaks doxygen callgraph

ε祈祈猫儿з 提交于 2019-12-04 06:02:01
问题 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(); level4(); } void level4(void) { } test.x X(1) X(2) X(3) I use doxygen to create callgraphs for these functions. Here's what I expected: level1: References level2(), level3(), and level4(). level2: References level3(), and level4(). Referenced by level1(). level3: References level4().

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

送分小仙女□ 提交于 2019-12-02 08:11:32
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 struct with arrays - like this: typedef struct { uint8 Addr1[SIZEOF_ADDR]; uint8 Addr2[SIZEOF_ADDR]; uint8 Addr3[SIZEOF_ADDR]; } TEST; This is what would be to adapt: //--- first describe the structure, the fields, their types and how to print them #define X_FIELDS \ X(int, field1, "%d") \ X(int, field2, "%d") \ X(char, field3, "%c") \ X(char *, field4

Is it legal to pass the macro name to an X-Macro list

时光毁灭记忆、已成空白 提交于 2019-12-01 20:33:19
问题 It occurred to me that the following would be a preferable style of X-macro trick: #define LIST_OF_COLOURS(X) \ X(RED) \ X(GREEN) \ X(BLUE) #define LIST_OF_FRUIT(X) \ X(APPLE) \ X(ORANGE) \ X(TOMATO) Specifically, passing the X macro to the list, rather than undefining and redefining it every time the list is instantiated. This allows: #define X_LIST(x) x, #define X_STRING_LIST(x) #x, #define COMPREHENSIVE_SETUP(n, l) \ enum n { l(X_LIST) }; \ char const* n##Names[] = { l(X_STRING_LIST) };