Preprocessor utility macros. More...
Macros | |
#define | NRFX_COND_CODE_1(_flag, _if_1_code, _else_code) _NRFX_COND_CODE_1(_flag, _if_1_code, _else_code) |
Macro for inserting code depending on whether _flag exists and expands to 1 or not. | |
#define | NRFX_COND_CODE_0(_flag, _if_0_code, _else_code) _NRFX_COND_CODE_0(_flag, _if_0_code, _else_code) |
Macro for inserting code depending on whether _flag exists and expands to 0 or not. | |
#define | NRFX_IS_ENABLED(config_macro) _NRFX_IS_ENABLED1(config_macro) |
Macro for checking for macro definition in compiler-visible expressions. | |
#define | NRFX_LISTIFY(LEN, F, sep, ...) NRFX_CONCAT_2(_NRFX_LISTIFY_, LEN)(F, sep, __VA_ARGS__) |
Macro for generating a sequence of code with configurable separator. | |
#define | NRFX_IS_EMPTY(arg) _NRFX_IS_EMPTY(arg) |
Macro for checking if input argument is empty. | |
#define | NRFX_NUM_VA_ARGS_LESS_1(...) |
Macro for calculating number of arguments in the variable arguments list minus one. | |
#define | NRFX_CONCAT(...) NRFX_CONCAT_2(_NRFX_CONCAT_, NRFX_NUM_VA_ARGS_LESS_1(__VA_ARGS__))(__VA_ARGS__) |
Macro for concatenating multiple arguments. | |
#define | NRFX_ARG_HAS_PARENTHESIS(x) _NRFX_GET_ARG3(_NRFX_EVAL(_NRFX_ARG_HAS_PARENTHESIS x, 1, 0)) |
Macro for checking if argument starts with opening round bracket and contains matching closing bracket (parenthesis). | |
#define | NRFX_FOR_EACH(F, sep, ...) _NRFX_FOR_EACH(F, sep, NRFX_REVERSE_ARGS(__VA_ARGS__)) |
Macro for calling a macro F on each provided argument with a given separator between each call. | |
#define | NRFX_FOR_EACH_IDX(F, sep, ...) _NRFX_FOR_EACH_IDX(F, sep, NRFX_REVERSE_ARGS(__VA_ARGS__)) |
Call macro F on each provided argument, with the argument's index as an additional parameter. | |
#define | NRFX_FOR_EACH_FIXED_ARG(F, sep, fixed_arg, ...) _NRFX_FOR_EACH_FIXED_ARG(F, sep, fixed_arg, NRFX_REVERSE_ARGS(__VA_ARGS__)) |
Macro for calling macro F on each provided argument, with an additional fixed argument as a parameter. | |
#define | NRFX_FOR_EACH_IDX_FIXED_ARG(F, sep, fixed_arg, ...) _NRFX_FOR_EACH_IDX_FIXED_ARG(F, sep, fixed_arg, NRFX_REVERSE_ARGS(__VA_ARGS__)) |
Macro from calling macro F for each variable argument with an index and fixed argument. | |
#define | NRFX_REVERSE_ARGS(...) _NRFX_FOR_EACH_ENGINE(_NRFX_FOR_EACH_EXEC, (,), NRFX_EVAL, _, __VA_ARGS__) |
Macro for reversing arguments order. | |
#define | NRFX_MAX_N(...) |
Macro for getting the highest value from input arguments. | |
Preprocessor utility macros.
#define NRFX_ARG_HAS_PARENTHESIS | ( | x | ) | _NRFX_GET_ARG3(_NRFX_EVAL(_NRFX_ARG_HAS_PARENTHESIS x, 1, 0)) |
Macro for checking if argument starts with opening round bracket and contains matching closing bracket (parenthesis).
[in] | x | Input argument. |
1 | If input argument starts with opening bracket and contains closing bracket. |
0 | If input argument does not match above mentioned condition. |
#define NRFX_CONCAT | ( | ... | ) | NRFX_CONCAT_2(_NRFX_CONCAT_, NRFX_NUM_VA_ARGS_LESS_1(__VA_ARGS__))(__VA_ARGS__) |
Macro for concatenating multiple arguments.
Support up to 8 arguments.
[in] | ... | Arguments to concatenate. |
#define NRFX_COND_CODE_0 | ( | _flag, | |
_if_0_code, | |||
_else_code | |||
) | _NRFX_COND_CODE_0(_flag, _if_0_code, _else_code) |
Macro for inserting code depending on whether _flag
exists and expands to 0 or not.
This is like NRFX_COND_CODE_1(), except that it tests whether _flag
expands to the integer literal 0. It expands to _if_0_code
if so, and _else_code
otherwise; both of these must be enclosed in parentheses.
[in] | _flag | Evaluated flag |
[in] | _if_0_code | Result if _flag expands to 0; must be in parentheses |
[in] | _else_code | Result otherwise; must be in parentheses |
#define NRFX_COND_CODE_1 | ( | _flag, | |
_if_1_code, | |||
_else_code | |||
) | _NRFX_COND_CODE_1(_flag, _if_1_code, _else_code) |
Macro for inserting code depending on whether _flag
exists and expands to 1 or not.
To prevent the preprocessor from treating commas as argument separators, the _if_1_code
and _else_code
expressions must be inside brackets/parentheses: ()
. These are stripped away during macro expansion.
Example:
NRFX_COND_CODE_1(CONFIG_FLAG, (uint32_t x;), (there_is_no_flag();))
If CONFIG_FLAG
is defined to 1, this expands to:
uint32_t x;
It expands to there_is_no_flag();
otherwise.
This could be used as an alternative to:
#if defined(CONFIG_FLAG) && (CONFIG_FLAG == 1) #define MAYBE_DECLARE(x) uint32_t x #else #define MAYBE_DECLARE(x) there_is_no_flag() #endif MAYBE_DECLARE(x);
However, the advantage of COND_CODE_1() is that code is resolved in place where it is used, while the #if
method defines MAYBE_DECLARE
on two lines and requires it to be invoked again on a separate line. This makes COND_CODE_1() more concise and also sometimes more useful when used within another macro's expansion.
_flag
can be the result of preprocessor expansion, however _if_1_code
is only expanded if _flag
expands to the integer literal 1. Integer expressions that evaluate to 1, e.g. after doing some arithmetic, will not work.[in] | _flag | Evaluated flag |
[in] | _if_1_code | Result if _flag expands to 1; must be in parentheses |
[in] | _else_code | Result otherwise; must be in parentheses |
#define NRFX_FOR_EACH | ( | F, | |
sep, | |||
... | |||
) | _NRFX_FOR_EACH(F, sep, NRFX_REVERSE_ARGS(__VA_ARGS__)) |
Macro for calling a macro F
on each provided argument with a given separator between each call.
Example:
#define F(x) int a##x NRFX_FOR_EACH(F, (;), 4, 5, 6);
This expands to:
int a4; int a5; int a6;
F | Macro to invoke |
sep | Separator (e.g. comma or semicolon). Must be in parentheses; this is required to enable providing a comma as a separator. |
... | Variable argument list. The macro F is invoked as F(element) for each element in the list. |
#define NRFX_FOR_EACH_FIXED_ARG | ( | F, | |
sep, | |||
fixed_arg, | |||
... | |||
) | _NRFX_FOR_EACH_FIXED_ARG(F, sep, fixed_arg, NRFX_REVERSE_ARGS(__VA_ARGS__)) |
Macro for calling macro F
on each provided argument, with an additional fixed argument as a parameter.
This is like NRFX_FOR_EACH(), except F
should be a macro which takes two arguments: F(variable_arg, fixed_arg)
.
Example:
static void func(int val, void *dev); NRFX_FOR_EACH_FIXED_ARG(func, (;), dev, 4, 5, 6);
This expands to:
func(4, dev); func(5, dev); func(6, dev);
F | Macro to invoke |
sep | Separator (e.g. comma or semicolon). Must be in parentheses; this is required to enable providing a comma as a separator. |
fixed_arg | Fixed argument passed to F as the second macro parameter. |
... | Variable argument list. The macro F is invoked as F(element, fixed_arg) for each element in the list. |
#define NRFX_FOR_EACH_IDX | ( | F, | |
sep, | |||
... | |||
) | _NRFX_FOR_EACH_IDX(F, sep, NRFX_REVERSE_ARGS(__VA_ARGS__)) |
Call macro F
on each provided argument, with the argument's index as an additional parameter.
This is like NRFX_FOR_EACH(), except F
should be a macro which takes two arguments: F(index, variable_arg)
.
Example:
#define F(idx, x) int a##idx = x NRFX_FOR_EACH_IDX(F, (;), 4, 5, 6);
This expands to:
int a0 = 4; int a1 = 5; int a2 = 6;
F | Macro to invoke |
sep | Separator (e.g. comma or semicolon). Must be in parentheses; this is required to enable providing a comma as a separator. |
... | Variable argument list. The macro F is invoked as F(index, element) for each element in the list. |
#define NRFX_FOR_EACH_IDX_FIXED_ARG | ( | F, | |
sep, | |||
fixed_arg, | |||
... | |||
) | _NRFX_FOR_EACH_IDX_FIXED_ARG(F, sep, fixed_arg, NRFX_REVERSE_ARGS(__VA_ARGS__)) |
Macro from calling macro F
for each variable argument with an index and fixed argument.
This is like the combination of NRFX_FOR_EACH_IDX() with NRFX_FOR_EACH_FIXED_ARG().
Example:
#define F(idx, x, fixed_arg) int fixed_arg##idx = x NRFX_FOR_EACH_IDX_FIXED_ARG(F, (;), a, 4, 5, 6);
This expands to:
int a0 = 4; int a1 = 5; int a2 = 6;
F | Macro to invoke |
sep | Separator (e.g. comma or semicolon). Must be in parentheses; This is required to enable providing a comma as a separator. |
fixed_arg | Fixed argument passed to F as the third macro parameter. |
... | Variable list of arguments. The macro F is invoked as F(index, element, fixed_arg) for each element in the list. |
#define NRFX_IS_EMPTY | ( | arg | ) | _NRFX_IS_EMPTY(arg) |
Macro for checking if input argument is empty.
Empty means that nothing is provided or provided value is resolved to nothing (e.g. empty define).
Macro idea is taken from P99 which is under Apache 2.0 license and described by Jens Gustedt https://gustedt.wordpress.com/2010/06/08/detect-empty-macro-arguments/
arg | Argument. |
1 | if argument is empty. |
0 | if argument is not empty. |
#define NRFX_IS_ENABLED | ( | config_macro | ) | _NRFX_IS_ENABLED1(config_macro) |
Macro for checking for macro definition in compiler-visible expressions.
It has the effect of taking a macro value that may be defined to "1" or may not be defined at all and turning it into a literal expression that can be handled by the C compiler instead of just the preprocessor.
That is, it works similarly to #if defined(CONFIG_FOO)
except that its expansion is a C expression. Thus, much #ifdef
usage can be replaced with equivalents like:
if (IS_ENABLED(CONFIG_FOO)) { do_something_with_foo }
This is cleaner since the compiler can generate errors and warnings for do_something_with_foo
even when CONFIG_FOO
is undefined.
[in] | config_macro | Macro to check |
config_macro
is defined to 1, 0 otherwise (including if config_macro
is not defined) #define NRFX_LISTIFY | ( | LEN, | |
F, | |||
sep, | |||
... | |||
) | NRFX_CONCAT_2(_NRFX_LISTIFY_, LEN)(F, sep, __VA_ARGS__) |
Macro for generating a sequence of code with configurable separator.
Example:
#define FOO(i, _) MY_PWM ## i { NRFX_LISTIFY(PWM_COUNT, FOO, (,)) }
The above two lines expand to:
{ MY_PWM0 , MY_PWM1 }
[in] | LEN | The length of the sequence. Must be an integer literal less than 255. |
[in] | F | A macro function that accepts at least two arguments: F(i, ...) . F is called repeatedly in the expansion. Its first argument i is the index in the sequence, and the variable list of arguments passed to LISTIFY are passed through to F . |
[in] | sep | Separator (e.g. comma or semicolon). Must be in parentheses; this is required to enable providing a comma as separator. |
#define NRFX_MAX_N | ( | ... | ) |
Macro for getting the highest value from input arguments.
It is similar to NRFX_MAX but accepts a variable number of arguments.
... | Variable argument list. |
#define NRFX_NUM_VA_ARGS_LESS_1 | ( | ... | ) |
Macro for calculating number of arguments in the variable arguments list minus one.
[in] | ... | List of arguments |
#define NRFX_REVERSE_ARGS | ( | ... | ) | _NRFX_FOR_EACH_ENGINE(_NRFX_FOR_EACH_EXEC, (,), NRFX_EVAL, _, __VA_ARGS__) |
Macro for reversing arguments order.
... | Variable argument list. |