Skip to content

Commit bbb77ba

Browse files
committed
[nrf fromlist] sys: util_macro: Add UTIL_CONCAT_OR and UTIL_CONCAT_AND
-Adding macro UTIL_CONCAT_OR that does primitive concatenation of variable number of arguments with "||" between them into a single token. -Adding macro UTIL_CONCAT_AND that does primitive concatenation of variable number of arguments with "&&" between them into a single token. The reason for adding these macro is that FOR_EACH or similar existing macros doesn't support end-termination in such a way that the separator tokens ("||" or "&&") is skipped after the last processed parameter Signed-off-by: Frank Audun Kvamtrø <[email protected]>
1 parent 64855d3 commit bbb77ba

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

include/zephyr/sys/util_internal.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
#define IS_ENABLED_ALL_13(a, ...) COND_CODE_1(a, (IS_ENABLED_ALL_12(__VA_ARGS__,)), (0))
9191
#define IS_ENABLED_ALL_14(a, ...) COND_CODE_1(a, (IS_ENABLED_ALL_13(__VA_ARGS__,)), (0))
9292
#define IS_ENABLED_ALL_15(a, ...) COND_CODE_1(a, (IS_ENABLED_ALL_14(__VA_ARGS__,)), (0))
93+
9394
/* Implementation of IS_EQ(). Returns 1 if _0 and _1 are the same integer from
9495
* 0 to 4096, 0 otherwise.
9596
*/
@@ -157,6 +158,48 @@
157158
#define UTIL_EXPAND(...) __VA_ARGS__
158159
#define UTIL_REPEAT(...) UTIL_LISTIFY(__VA_ARGS__)
159160

161+
#define UTIL_OR_CAT(a, b) a || b
162+
#define UTIL_CONCAT_OR_(...) \
163+
UTIL_CONCAT_OR_N_(NUM_VA_ARGS(__VA_ARGS__), __VA_ARGS__)
164+
#define UTIL_CONCAT_OR_N_(N, ...) UTIL_CAT(UTIL_CONCAT_OR_, N)(__VA_ARGS__)
165+
#define UTIL_CONCAT_OR_0
166+
#define UTIL_CONCAT_OR_1(a, ...) a
167+
#define UTIL_CONCAT_OR_2(a, ...) UTIL_OR_CAT(a, UTIL_CONCAT_OR_1(__VA_ARGS__))
168+
#define UTIL_CONCAT_OR_3(a, ...) UTIL_OR_CAT(a, UTIL_CONCAT_OR_2(__VA_ARGS__))
169+
#define UTIL_CONCAT_OR_4(a, ...) UTIL_OR_CAT(a, UTIL_CONCAT_OR_3(__VA_ARGS__))
170+
#define UTIL_CONCAT_OR_5(a, ...) UTIL_OR_CAT(a, UTIL_CONCAT_OR_4(__VA_ARGS__))
171+
#define UTIL_CONCAT_OR_6(a, ...) UTIL_OR_CAT(a, UTIL_CONCAT_OR_5(__VA_ARGS__))
172+
#define UTIL_CONCAT_OR_7(a, ...) UTIL_OR_CAT(a, UTIL_CONCAT_OR_6(__VA_ARGS__))
173+
#define UTIL_CONCAT_OR_8(a, ...) UTIL_OR_CAT(a, UTIL_CONCAT_OR_7(__VA_ARGS__))
174+
#define UTIL_CONCAT_OR_9(a, ...) UTIL_OR_CAT(a, UTIL_CONCAT_OR_8(__VA_ARGS__))
175+
#define UTIL_CONCAT_OR_10(a, ...) UTIL_OR_CAT(a, UTIL_CONCAT_OR_9(__VA_ARGS__))
176+
#define UTIL_CONCAT_OR_11(a, ...) UTIL_OR_CAT(a, UTIL_CONCAT_OR_10(__VA_ARGS__))
177+
#define UTIL_CONCAT_OR_12(a, ...) UTIL_OR_CAT(a, UTIL_CONCAT_OR_11(__VA_ARGS__))
178+
#define UTIL_CONCAT_OR_13(a, ...) UTIL_OR_CAT(a, UTIL_CONCAT_OR_12(__VA_ARGS__))
179+
#define UTIL_CONCAT_OR_14(a, ...) UTIL_OR_CAT(a, UTIL_CONCAT_OR_13(__VA_ARGS__))
180+
#define UTIL_CONCAT_OR_15(a, ...) UTIL_OR_CAT(a, UTIL_CONCAT_OR_14(__VA_ARGS__))
181+
182+
#define UTIL_AND_CAT(a, b) a && b
183+
#define UTIL_CONCAT_AND_(...) \
184+
UTIL_CONCAT_AND_N_(NUM_VA_ARGS(__VA_ARGS__), __VA_ARGS__)
185+
#define UTIL_CONCAT_AND_N_(N, ...) UTIL_CAT(UTIL_CONCAT_AND_, N)(__VA_ARGS__)
186+
#define UTIL_CONCAT_AND_0
187+
#define UTIL_CONCAT_AND_1(a, ...) a
188+
#define UTIL_CONCAT_AND_2(a, ...) UTIL_AND_CAT(a, UTIL_CONCAT_AND_1(__VA_ARGS__))
189+
#define UTIL_CONCAT_AND_3(a, ...) UTIL_AND_CAT(a, UTIL_CONCAT_AND_2(__VA_ARGS__))
190+
#define UTIL_CONCAT_AND_4(a, ...) UTIL_AND_CAT(a, UTIL_CONCAT_AND_3(__VA_ARGS__))
191+
#define UTIL_CONCAT_AND_5(a, ...) UTIL_AND_CAT(a, UTIL_CONCAT_AND_4(__VA_ARGS__))
192+
#define UTIL_CONCAT_AND_6(a, ...) UTIL_AND_CAT(a, UTIL_CONCAT_AND_5(__VA_ARGS__))
193+
#define UTIL_CONCAT_AND_7(a, ...) UTIL_AND_CAT(a, UTIL_CONCAT_AND_6(__VA_ARGS__))
194+
#define UTIL_CONCAT_AND_8(a, ...) UTIL_AND_CAT(a, UTIL_CONCAT_AND_7(__VA_ARGS__))
195+
#define UTIL_CONCAT_AND_9(a, ...) UTIL_AND_CAT(a, UTIL_CONCAT_AND_8(__VA_ARGS__))
196+
#define UTIL_CONCAT_AND_10(a, ...) UTIL_AND_CAT(a, UTIL_CONCAT_AND_9(__VA_ARGS__))
197+
#define UTIL_CONCAT_AND_11(a, ...) UTIL_AND_CAT(a, UTIL_CONCAT_AND_10(__VA_ARGS__))
198+
#define UTIL_CONCAT_AND_12(a, ...) UTIL_AND_CAT(a, UTIL_CONCAT_AND_11(__VA_ARGS__))
199+
#define UTIL_CONCAT_AND_13(a, ...) UTIL_AND_CAT(a, UTIL_CONCAT_AND_12(__VA_ARGS__))
200+
#define UTIL_CONCAT_AND_14(a, ...) UTIL_AND_CAT(a, UTIL_CONCAT_AND_13(__VA_ARGS__))
201+
#define UTIL_CONCAT_AND_15(a, ...) UTIL_AND_CAT(a, UTIL_CONCAT_AND_14(__VA_ARGS__))
202+
160203
#define _CONCAT_0(arg, ...) arg
161204
#define _CONCAT_1(arg, ...) UTIL_CAT(arg, _CONCAT_0(__VA_ARGS__))
162205
#define _CONCAT_2(arg, ...) UTIL_CAT(arg, _CONCAT_1(__VA_ARGS__))

include/zephyr/sys/util_macro.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,27 @@ extern "C" {
485485
*/
486486
#define UTIL_X2(y) UTIL_PRIMITIVE_CAT(Z_UTIL_X2_, y)
487487

488+
/**
489+
* @brief Concatenates all arguments with "||" between them into a single token
490+
*
491+
* This macro does primitive concatenation and doesn't try to process the
492+
* parameters in preprocessor.
493+
*
494+
* @param ... Arguments to concatenate with @p || between them
495+
* @return A concatenation of all the arguments with @p || between them
496+
*/
497+
#define UTIL_CONCAT_OR(...) UTIL_CONCAT_OR_(__VA_ARGS__)
498+
499+
/**
500+
* @brief Concatenates all arguments with "&&" between them into a single token
501+
*
502+
* This macro does primitive concatenation and doens't try to process the
503+
* parameters in preprocessor.
504+
*
505+
* @param ... Arguments to concatenate with @p && between them
506+
* @return A concatenation of all the arguments with @p || between them
507+
*/
508+
#define UTIL_CONCAT_AND(...) UTIL_CONCAT_AND_(__VA_ARGS__)
488509

489510
/**
490511
* @brief Generates a sequence of code with configurable separator.

0 commit comments

Comments
 (0)