Debian Codegraph @

libdrgn/generics.h

raw
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 generic programming.
8 */
9
10 #ifndef DRGN_GENERICS_H
11 #define DRGN_GENERICS_H
12
13 /**
14 * Choose a type based on a condition.
15 *
16 * @param[in] condition Controlling integer constant expression.
17 * @param[in] if_true Type if @p condition is non-zero.
18 * @param[in] if_false Type if @p condition is zero.
19 */
20 #define type_if(condition, if_true, if_false) \
21 __typeof__( \
22 /* + 1 avoids a non-standard zero-length array. */ \
23 *_Generic((int (*)[!(condition) + 1])0, \
24 int (*)[1]: (__typeof__(if_true) *)0, \
25 int (*)[2]: (__typeof__(if_false) *)0) \
26 )
27
28 /**
29 * Define a typedef based on a condition.
30 *
31 * @param[in] name Name of type.
32 * @param[in] condition Controlling integer constant expression.
33 * @param[in] if_true Type if @p condition is non-zero.
34 * @param[in] if_false Type if @p condition is zero.
35 */
36 #define typedef_if(name, condition, if_true, if_false) \
37 typedef type_if(condition, if_true, if_false) name
38
39 #endif /* DRGN_GENERICS_H */