|
CX Framework
Cross-platform C utility framework
|
Macros | |
| #define | count_macro_args(...) |
| #define | opt_arg(...) |
| #define | tokconcat(x, y) _tokconcat_actual(x, y) |
| #define | tokcat2(x, y) _tokconcat_actual(x, y) |
| Concatenate two tokens (alias for tokconcat) | |
| #define | tokcat3(x1, x2, x3) tokcat2(tokcat2(x1, x2), x3) |
| Concatenate three tokens. | |
| #define | tokcat4(x1, x2, x3, x4) tokcat2(tokcat3(x1, x2, x3), x4) |
| Concatenate four tokens. | |
| #define | tokcat5(x1, x2, x3, x4, x5) tokcat2(tokcat4(x1, x2, x3, x4), x5) |
| Concatenate five tokens. | |
| #define | tokconcatU(x, y) _tokconcatU_actual(x, y) |
| #define | tokcatU2(x, y) _tokconcatU_actual(x, y) |
| Concatenate two tokens with underscore (alias for tokconcatU) | |
| #define | tokcatU3(x1, x2, x3) tokcatU2(tokcatU2(x1, x2), x3) |
| Concatenate three tokens with underscores. | |
| #define | tokcatU4(x1, x2, x3, x4) tokcatU2(tokcatU3(x1, x2, x3), x4) |
| Concatenate four tokens with underscores. | |
| #define | tokcatU5(x1, x2, x3, x4, x5) tokcatU2(tokcatU4(x1, x2, x3, x4), x5) |
| Concatenate five tokens with underscores. | |
| #define | tokstring(x) _tokstring_actual(x) |
| #define | tokeval(...) __VA_ARGS__ |
| #define | unused_noeval(x) (true ? (void)0 : (void)(x)) |
| #define | nop_stmt ((void)0) |
Commonly-used utility macros
| #define count_macro_args | ( | ... | ) |
Count the number of macro arguments (up to 50)
Returns the number of arguments passed to the macro. This is commonly used to implement variadic macros that need to know how many arguments were provided. When zero arguments are passed, returns 1 (a limitation of the preprocessor).
This implementation uses an extra indirection macro to work around MSVC preprocessor issues.
Example:
| #define nop_stmt ((void)0) |
No-operation statement
A statement that does nothing, useful for satisfying syntax requirements or as a placeholder. Commonly used in macro definitions where a statement is required but no action is needed.
Example:
| #define opt_arg | ( | ... | ) |
Extract a single optional argument, defaulting to 0 if not present
This macro is used to implement functions with optional arguments by providing a default value of 0 when the argument is omitted. It's commonly used for optional flags parameters in function wrappers.
The implementation differs between compilers due to preprocessor differences:
__VA_OPT__ for clean optional argument handlingExample:
| #define tokconcat | ( | x, | |
| y | |||
| ) | _tokconcat_actual(x, y) |
Concatenate two tokens after macro expansion
Uses an extra level of indirection to ensure both tokens are expanded before concatenation. Without this indirection, the ## operator would paste tokens before they are expanded.
Example:
| #define tokconcatU | ( | x, | |
| y | |||
| ) | _tokconcatU_actual(x, y) |
Concatenate two tokens with underscore separator after macro expansion
Similar to tokconcat but inserts an underscore between the tokens.
Example:
| #define tokeval | ( | ... | ) | __VA_ARGS__ |
Force macro expansion of arguments
This utility macro forces the preprocessor to fully expand its arguments. It's primarily used to work around MSVC preprocessor quirks and to separate tokens in macro processing.
Example:
| #define tokstring | ( | x | ) | _tokstring_actual(x) |
Convert a token to a string literal after macro expansion
Uses an extra level of indirection to ensure the token is expanded before stringification. The # operator would stringify the token name without expansion otherwise.
Example:
| #define unused_noeval | ( | x | ) | (true ? (void)0 : (void)(x)) |
Mark a variable as intentionally unused without evaluating it
This macro silences "unused variable" warnings without actually evaluating the expression. The expression is used in an unevaluated context (the false branch of a ternary operator) so it has no runtime cost and doesn't trigger side effects.
The GCC implementation uses a helper function to work around compiler-specific issues.
Example: