Nested C Preprocessor Macros

The C preprocessor and macros are one of the most useful elements for writing portable C/C++ code . It even can  be used to completely different purposes, like text processing.  However, it suffers one major drawback: it does not support nested macros expansion (put differently, macros within macros).

Continue reading “Nested C Preprocessor Macros”

Some X-Macros Usages

Clever use of X-Macros (https://en.wikipedia.org/wiki/X_Macro ) allow  to decorrelate a set of data from the code which will handle them. For example,  I had recently to implement a parser, which would  analyse an incoming stream, isolate the embedded command and act upon.

So in this typical situation, you generally get the stream matched against some predefined-strings, arranged in an ordered way. Then, you get an ID, parameters and call a specific handler. Nothing fancy.

However, if you want this code to be maintainable (for example, you decide to change the handler name), and upgradable (adding new commands ID), you certainly would like some mechanism which would gather all the changeable bits (the commands, the ID’s, etc ..) from the pure parser structure. This is where X-Macros become handy.

Continue reading “Some X-Macros Usages”