From 01f312e212e8ad095800856a7247a0a2d85cc99d Mon Sep 17 00:00:00 2001 From: Dan Streetman Date: Thu, 2 Feb 2023 15:58:10 -0500 Subject: [PATCH] basic/macro: add macro to iterate variadic args (cherry picked from commit e179f2d89c9f0c951636d74de00136b4075cd1ac) Related: RHEL-16182 --- src/basic/macro.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/basic/macro.h b/src/basic/macro.h index 72a2c7267e..9c36683ef9 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -458,4 +458,13 @@ assert_cc(sizeof(dummy_t) == 0); _q && _q > (base) ? &_q[-1] : NULL; \ }) +/* Iterate through each variadic arg. All must be the same type as 'entry' or must be implicitly + * convertable. The iteration variable 'entry' must already be defined. */ +#define VA_ARGS_FOREACH(entry, ...) \ + _VA_ARGS_FOREACH(entry, UNIQ_T(_entries_, UNIQ), UNIQ_T(_current_, UNIQ), ##__VA_ARGS__) +#define _VA_ARGS_FOREACH(entry, _entries_, _current_, ...) \ + for (typeof(entry) _entries_[] = { __VA_ARGS__ }, *_current_ = _entries_; \ + ((long)(_current_ - _entries_) < (long)ELEMENTSOF(_entries_)) && ({ entry = *_current_; true; }); \ + _current_++) + #include "log.h"