| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
/** |
| 5 |
* @file |
| 6 |
* |
| 7 |
* Helpers for C arrays. |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef DRGN_ARRAY_H |
| 11 |
#define DRGN_ARRAY_H |
| 12 |
|
| 13 |
#include "pp.h" |
| 14 |
#include "util.h" |
| 15 |
|
| 16 |
/** @cond */ |
| 17 |
#define array_for_each_impl(var, arr, unique_end) \ |
| 18 |
for (typeof((arr)[0]) *var = (arr), \ |
| 19 |
*unique_end = var + array_size(arr); \ |
| 20 |
var < unique_end; var++) |
| 21 |
/** @endcond */ |
| 22 |
|
| 23 |
/** |
| 24 |
* Return the number of elements in an array. |
| 25 |
* |
| 26 |
* @hideinitializer |
| 27 |
*/ |
| 28 |
#define array_size(arr) \ |
| 29 |
static_assert_expression(is_array(arr), \ |
| 30 |
"not an array", \ |
| 31 |
sizeof(arr) / sizeof((arr)[0])) |
| 32 |
|
| 33 |
/** |
| 34 |
* Iterate over every element in an array. |
| 35 |
* |
| 36 |
* The element is declared as `element_type *var` in the scope of the loop. |
| 37 |
* |
| 38 |
* @hideinitializer |
| 39 |
*/ |
| 40 |
#define array_for_each(var, arr) \ |
| 41 |
array_for_each_impl(var, arr, PP_UNIQUE(end)) |
| 42 |
|
| 43 |
#endif /* DRGN_ARRAY_H */ |