Questions tagged [x-macros]
Using macros to use the same tokens in multiple ways.
x-macros
64
questions
92
votes
8
answers
36k
views
Real-world use of X-Macros
I just learned of X-Macros. What real-world uses of X-Macros have you seen? When are they the right tool for the job?
14
votes
3
answers
10k
views
Can I append to a preprocessor macro?
Is there any way in standard C—or with GNU extensions—to append stuff to a macro definition? E.g., given a macro defined as
#define List foo bar
can I append bas so that it List expands as if I’d ...
7
votes
2
answers
1k
views
Is Misra-C compatible with X-macros?
It could be argued that in many cases X-macros increase safety, because it makes easier to make sure that generated arrays are the same length for example.
However, Misra C (from 2004 reference) ...
6
votes
2
answers
587
views
How to eliminate a redundant macro parameter
A while ago, I wrote a set of X-macros for a largish project. I needed to maintain coherent lists of both strings and enumerated references/hash values/callback functions etc. Here is what the ...
5
votes
1
answer
240
views
Is it legal to pass the macro name to an X-Macro list
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) \
...
5
votes
2
answers
1k
views
Reducing code repetition in C++ (or x-treme x-macros)
I am using x-macros to reduce the amount of repetition and code duplication while implementing a Lua interface for the game Bitfighter. The following code works fine:
// ...
4
votes
2
answers
1k
views
How to get reflection-like functionality in C, without x-macros
Related to this question on Software Engineering about easily serializing various struct contents on demand, I found an article which uses x-macros to create struct metadata needed for "out of the box"...
3
votes
4
answers
677
views
Convert endianness of integer fields in struct using macros
Consider the following struct and functions
typedef struct __attribute__((__packed__)) req_file {
uint32_t start_pos;
uint32_t byte_count;
uint16_t name_len;
} req_file;
void req_file_hton(...
3
votes
1
answer
223
views
Cartesian product of X-macro list with itself
Suppose that, in C, I have an list of things specified using X-macros. For example,
#define TYPES(X, ...) \
X(__VA_ARGS__, i, int) \
X(__VA_ARGS__, j, unsigned int) \
X(__VA_ARGS__, l, long) \
...
3
votes
2
answers
803
views
Using a macro as an argument in an x-macro definition
Consider the following user-style x-macro:
#define PRIMES_X(func) \
func(2) \
func(3) \
func(5)
We can use this to call a passed-in macro func repeatedly with first three primes. For example:
...
3
votes
1
answer
206
views
Where do my commas disappear in variadic macro expansion?
I am writing a x-macro based register file layout description system for a project of mine. Most of the time, the macros expand to a hierarchy of template classes. However, I also want an enumeration ...
3
votes
2
answers
427
views
Use specific entry of an X macro
I am using X macros to generate functions setting GPIOs to 0 or 1 (I generate around 60 functions to set around 30 GPIOs). Here is an example (I have just written this example, so the syntax may be ...
3
votes
1
answer
7k
views
Stringifying an conditionally compiled enum in C
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 (...
3
votes
2
answers
742
views
X-macro enum based on previous value
I want to generate a enum with a X-Macro. The enum has to increace based on prev size.
I have this
#define LIST
VAR(one, 0x02)
VAR(two, 0x02)
VAR(tree, 0x03)
and want to generate this
enum{
one = ...
3
votes
5
answers
573
views
Using C X macros in combination with #ifdef
Assuming my code looks like in the following snippet:
#ifdef COND1
extern int func1(void);
#endif
...
#ifdef CONDN
extern int funcn(void);
#endif
my_struct funcs[] = {
#ifdef COND1
{"...
2
votes
2
answers
781
views
Can any one explain about X-macros with example code to use them? [closed]
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 ...
2
votes
2
answers
185
views
Is it possible to use X-Macro with std::variant (or with template in general)?
I hope to do the following using X-macro with c++17, but since template parameter does not support trailing comma, it does not work for the std::variant part. Is there someway around it?
#define ...
2
votes
4
answers
185
views
Creating a related x-macro from an existing one
Consider the following user-style x-macro:
#define PRIMES_X(func) \
func(2) \
func(3) \
func(5) \
func(7)
We can use this to expand a passed-in macro func repeatedly with the first four ...
2
votes
1
answer
747
views
#include inside an x-macro
I want to write an x-macro that generates some code. The code depends on several headers and is intended to be generated inside namespaces.
The problem is that the xmacro's includes are being ...
2
votes
1
answer
1k
views
X-Macros with Boost.Preprocessor?
Splitting this off from my question regarding appending to CPP macros:
Has anyone here used the Boost.Preprocessor library’s data types to implement something like the X-macro?
2
votes
2
answers
150
views
C - X Macro Self Iteration / Expansion
I am curious if any of you can think of a way upon macro expansion to repeat the macro itself. Here is an incredibly small scale version of an overall bigger problem:
#include<stdio.h>
#define ...
2
votes
1
answer
354
views
Is it possible to (recursively) "introspect" nested C structs using x-macros?
I was reading this article (Struct iteration through (ab)use of the preprocessor), where the author uses x-macros and offsetof to add metadata to structs which would allow their members to be easily ...
2
votes
1
answer
360
views
Printing XMacro struct to the console with C++
I'm playing around with structs and classes and I saw a really cool bit of coding I wanted to try out: the x-macro.
My code is broken up into 3 bits, the header, the x-macro, and the main cpp file. ...
2
votes
2
answers
163
views
How to convert macro argument value to string using XMACROs instead of converting the passed argument itself
I am posting this question here after I have done enough research within our community. However, I couldn't find a proper solution for my problem yet. So, I am posting my question here.
#define ...
1
vote
3
answers
4k
views
C - How to assign a value inside my macro?
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 \
...
1
vote
2
answers
707
views
Functional-Programming Style in C++ Macros: Is this documented anywhere?
Reading some C++ code I came across what I'll call a "functional" use of function Macros roughly as follows (this is a totally stylized example to make the point):
#define TOP_LEVEL(ARG1) \
ARG1("...
1
vote
2
answers
963
views
X-Macros Redefinition
When i include the "xmacro.h" in header file which is used by multiple header files i get linking error:
Error LNK2005: "char const * * iD_Strings" (?iD_Strings@@3PAPBDA)
already defined in ...
1
vote
1
answer
61
views
Nested switch statment using X-macros
#define LIST_D1(val, mask, _lc, _UC, comment, parentDictId) \
__BTN(val 1, mask ~0, _lc##cred_amazon_mws_token, _UC##CRED_AMAZON_MWS_TOKEN, comment N_("Amazon MWS Auth Token"), ...
1
vote
2
answers
92
views
x-macro conditional error - number comparison
I would like to generate compile time error for X-macro for all X(a, b) where a > b
/* X(a, b) */
#define LIST \
X(10, 20) \
X(5, 20) \
X(30, 20) \
X(1, 20)
So, generate error ...
1
vote
2
answers
918
views
Expand X macro inside another macro
I have a function that prints an output using fprintf(), and it uses a macro both for the format string and the parameters. Since there are several places printing this info, this allows expanding the ...
1
vote
1
answer
986
views
Concatenate multiple tokens for X macro
I'm trying to use X macros and preprocessor concatenation, both for the first time, together.
I've read a lot of the other questions on SO related to preprocessor concatenation but not yet been able ...
1
vote
1
answer
115
views
defining same elements in X macro C
I have been exploring X macro and I created this table
#define FAULT_TABLE(FAULT) \
FAULT(INVALID, FAULT_CATEGORY_NONE, action_none) \
FAULT(COMMS_FAILURE, ...
1
vote
1
answer
117
views
X-macro driven C++ template class instantiation
I am trying to instantiate a templatized class based on an X-macro. However, this is giving me syntax errors error: wrong number of template arguments (0, should be 1). What is the correct way to ...
1
vote
2
answers
244
views
Check existence of an entry in C pre-processor list
Is it possible to check for the existence of an entry in a list defined by X-macro? Given the example code below, I'd like the #if defined(GEORGE) condition to be true.
EDIT: without doing an ...
1
vote
1
answer
241
views
create Macro to collect token (parameter) into a list, one by one
I am trying to create a macro that generate pointer to instance of another class to denote directional relation.
//#define BIND(A,B) ?
//can be modified a little, top header
BIND(CAT,DOG)
BIND(CAT,...
1
vote
0
answers
270
views
clang-format indents X macros
I'm using c-style X macros to create two way enums like this:
namespace ns
{
#define CFG_INT_TABLE \
X(CFG_1, "cfg_1", ...
1
vote
1
answer
680
views
Understanding the text replacement in x macro
The code below explain how the x macros works in a simple way in c programming langauge
#include <stdio.h>
// Defines four variables.
#define VARIABLES \
X(value1, 1) \
X(value2, ...
1
vote
0
answers
65
views
hierarchical non-rectangular list of names and enums in c++98
I'm trying to assemble a hierarchical list of enums (indices) and their corresponding names so that I can access specific ones by name or loop through all of them, depending on the section of my code ...
1
vote
0
answers
510
views
one #macro add a field and a case (in switch-case)
How to define macro (ALL_SWITCH) to recognize another macro (SINGLE_CASE) occurrences as list, and insert codes into 2 different places?
Is it possible?
Example
I want to create macro ...
1
vote
1
answer
305
views
Struct in a XMacro
I am trying to assign a value to a struct from the XMacro table.
Is there any other way to assign the "offset" to this value of the struct?
typedef enum Time_Unit_Tag
{
NO_UNIT,
HOUR,
...
1
vote
1
answer
65
views
Preprocessing find max between #define
I have two header files making the same define with different values:
file1.h:
#define NUM_OF_TREES 10
file2.h:
#define NUM_OF_TREES 20
In another file i want to define
...
0
votes
2
answers
108
views
Combine different arity X-macros
I am defining a lexer for a programming language. Part of this involves having a table of keywords and tokens:
#define FOREACH_KEYWORD(V) \
V(And, ...
0
votes
2
answers
94
views
manage similar structs, one with arrays, the other with pointers
I have some structs containing arrays
struct mystruct_withArrays {
int foo;
int bar[MaxN];
int baz[MaxM];
}
and their equivalent with pointers
struct mystruct_withPointers {
int foo;
int *...
0
votes
1
answer
304
views
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
{
...
0
votes
1
answer
68
views
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, ...
0
votes
1
answer
81
views
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 ...
0
votes
1
answer
622
views
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
...
0
votes
2
answers
258
views
Generic type conversion in C
I have a list of data types (STANDARD_TYPES). I would like to automatically (using X macros) create functions that would convert from one of those type to another.
I have the following code:
#define ...
0
votes
2
answers
108
views
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 ...
0
votes
1
answer
157
views
force unused parameter for macro
Simple idea:
I'm using X-macros to define command list structure and declare command callbacks.
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>...