Commit 42335036 authored by Christopher Friedt's avatar Christopher Friedt Committed by Stephanos Ioannidis
Browse files

include: util: add array for-each macros



AFAIK, we do not have array for-each loops. Seemed like an
obvious gap to fill, so introduce two variants:

* ARRAY_FOR_EACH(array, idx_var)
* ARRAY_FOR_EACH_PTR(array, ptr_var)

Signed-off-by: default avatarChristopher Friedt <cfriedt@meta.com>
parent 11613e08
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -202,6 +202,24 @@ extern "C" {
		(POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) / sizeof((array)[0]);              \
	})

/**
 * @brief Iterate over members of an array using an index variable
 *
 * @param array the array in question
 * @param idx name of array index variable
 */
#define ARRAY_FOR_EACH(array, idx) for (size_t idx = 0; (idx) < ARRAY_SIZE(array); ++(idx))

/**
 * @brief Iterate over members of an array using a pointer
 *
 * @param array the array in question
 * @param ptr pointer to an element of @p array
 */
#define ARRAY_FOR_EACH_PTR(array, ptr)                                                             \
	for (__typeof__(*(array)) *ptr = (array); (size_t)((ptr) - (array)) < ARRAY_SIZE(array);   \
	     ++(ptr))

/**
 * @brief Validate if two entities have a compatible type
 *