From ffb8d479c7bb8ac31bd02c8df8174bfe56863c17 Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Tue, 17 Dec 2019 09:27:24 +0100 Subject: [PATCH] sys: util: Add IF_ENABLED() macro Added macro for code inserting based on configuration flag. This macro is wrapper around COND_CODE_1(). Signed-off-by: Krzysztof Chruscinski --- include/sys/util.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/include/sys/util.h b/include/sys/util.h index 1977b1e3b2a..4a4e87f8db5 100644 --- a/include/sys/util.h +++ b/include/sys/util.h @@ -323,6 +323,25 @@ u8_t u8_to_dec(char *buf, u8_t buflen, u8_t value); #define Z_COND_CODE_1(_flag, _if_1_code, _else_code) \ __COND_CODE(_XXXX##_flag, _if_1_code, _else_code) +/** + * @brief Insert code if flag is defined and equals 1. + * + * Usage example: + * + * IF_ENABLED(CONFIG_FLAG, (u32_t foo;)) + * + * It can be considered as more compact alternative to: + * + * \#if defined(CONFIG_FLAG) && (CONFIG_FLAG == 1) + * u32_t foo; + * \#endif + * + * @param _flag Evaluated flag + * @param _code Code used if flag exists and equal 1. Argument must be + * in brackets. + */ +#define IF_ENABLED(_flag, _code) \ + COND_CODE_1(_flag, _code, ()) /** * @brief Insert code depending on result of flag evaluation. *