Skip to content

Commit c288a3a

Browse files
ddstreetbluca
authored andcommitted
basic/macro: add macro to iterate variadic args
(cherry picked from commit e179f2d) (cherry picked from commit cd4f43b)
1 parent 4d06d37 commit c288a3a

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/basic/macro.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,13 @@ assert_cc(sizeof(dummy_t) == 0);
439439
_q && _q > (base) ? &_q[-1] : NULL; \
440440
})
441441

442+
/* Iterate through each variadic arg. All must be the same type as 'entry' or must be implicitly
443+
* convertable. The iteration variable 'entry' must already be defined. */
444+
#define VA_ARGS_FOREACH(entry, ...) \
445+
_VA_ARGS_FOREACH(entry, UNIQ_T(_entries_, UNIQ), UNIQ_T(_current_, UNIQ), ##__VA_ARGS__)
446+
#define _VA_ARGS_FOREACH(entry, _entries_, _current_, ...) \
447+
for (typeof(entry) _entries_[] = { __VA_ARGS__ }, *_current_ = _entries_; \
448+
((long)(_current_ - _entries_) < (long)ELEMENTSOF(_entries_)) && ({ entry = *_current_; true; }); \
449+
_current_++)
450+
442451
#include "log.h"

0 commit comments

Comments
 (0)