File: generics.h

package info (click to toggle)
drgn 0.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,852 kB
  • sloc: python: 74,992; ansic: 54,589; awk: 423; makefile: 351; sh: 99
file content (39 lines) | stat: -rw-r--r-- 1,104 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: LGPL-2.1-or-later

/**
 * @file
 *
 * Helpers for generic programming.
 */

#ifndef DRGN_GENERICS_H
#define DRGN_GENERICS_H

/**
 * Choose a type based on a condition.
 *
 * @param[in] condition Controlling integer constant expression.
 * @param[in] if_true Type if @p condition is non-zero.
 * @param[in] if_false Type if @p condition is zero.
 */
#define type_if(condition, if_true, if_false)			\
__typeof__(							\
       /* + 1 avoids a non-standard zero-length array. */	\
       *_Generic((int (*)[!(condition) + 1])0,			\
		 int (*)[1]: (__typeof__(if_true) *)0,		\
		 int (*)[2]: (__typeof__(if_false) *)0)		\
)

/**
 * Define a typedef based on a condition.
 *
 * @param[in] name Name of type.
 * @param[in] condition Controlling integer constant expression.
 * @param[in] if_true Type if @p condition is non-zero.
 * @param[in] if_false Type if @p condition is zero.
 */
#define typedef_if(name, condition, if_true, if_false)		\
	typedef type_if(condition, if_true, if_false) name

#endif /* DRGN_GENERICS_H */