Change naming convention for enum class enumerators
[babeltrace.git] / src / common / macros.h
CommitLineData
eb31c5e6 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
eb31c5e6 3 *
0235b0db 4 * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
eb31c5e6 5 */
fd3c708f 6
0235b0db
MJ
7#ifndef _BABELTRACE_INTERNAL_H
8#define _BABELTRACE_INTERNAL_H
70bd0a12 9
826b4673
PP
10#ifdef __cplusplus
11extern "C" {
12#endif
13
087cd0f5
SM
14#ifdef __cplusplus
15#define BT_EXTERN_C extern "C"
16#else
17#define BT_EXTERN_C
18#endif
19
91d81473 20#define bt_max_t(type, a, b) \
f3e4505b 21 ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
f3e4505b 22
5d6c80a5 23/*
1353b066 24 * BT_EXPORT: set the visibility for exported functions.
5d6c80a5 25 */
675860f4 26#if defined(_WIN32) || defined(__CYGWIN__)
1353b066 27#define BT_EXPORT
675860f4 28#else
1353b066 29#define BT_EXPORT __attribute__((visibility("default")))
675860f4 30#endif
5d6c80a5 31
e0f8968a
SM
32/*
33 * BT_NOEXCEPT: defined to `noexcept` if compiling as C++, else empty.
34 */
35#if defined(__cplusplus)
36#define BT_NOEXCEPT noexcept
37#else
38#define BT_NOEXCEPT
39#endif
40
9340eff9
SM
41/* Enable `txt` if developer mode is enabled. */
42#ifdef BT_DEV_MODE
43#define BT_IF_DEV_MODE(txt) txt
44#else
45#define BT_IF_DEV_MODE(txt)
46#endif
47
55b71f05
SM
48/*
49 * Yield `ref`'s value while setting `ref` to NULL.
50 *
51 * This can be used to give a strong reference to a callee:
52 *
53 * add_foo_to_list(list, BT_MOVE_REF(foo));
54 *
55 * or in a simple assignment:
56 *
57 * my_struct->foo = BT_MOVE_REF(foo);
58 *
59 * When moving a reference in a function call, the reference is given to the
60 * callee even if that function call fails, so make sure the called function
61 * is written accordingly.
62 */
63
64#define BT_MOVE_REF(ref) \
65 ({ \
0ec1ae8b 66 __typeof__(ref) _ref = ref; \
55b71f05
SM
67 ref = NULL; \
68 _ref; \
69 })
70
d50d46f3
SM
71/* Wrapper for g_array_index that adds bound checking. */
72#define bt_g_array_index(a, t, i) \
73 g_array_index((a), t, ({ BT_ASSERT_DBG((i) < (a)->len); (i); }))
74
1778c2a4
PP
75/*
76 * Copied from:
77 * <https://stackoverflow.com/questions/37411809/how-to-elegantly-fix-this-unused-variable-warning/37412551#37412551>:
78 *
79 * * sizeof() ensures that the expression is not evaluated at all, so
80 * its side-effects don't happen. That is to be consistent with the
81 * usual behaviour of debug-only constructs, such as assert().
82 *
83 * * `((_expr), 0)` uses the comma operator to swallow the actual type
84 * of `(_expr)`. This is to prevent VLAs from triggering evaluation.
85 *
86 * * `(void)` explicitly ignores the result of `(_expr)` and sizeof() so
87 * no "unused value" warning appears.
88 */
89
90#define BT_USE_EXPR(_expr) ((void) sizeof((void) (_expr), 0))
91#define BT_USE_EXPR2(_expr1, _expr2) \
92 ((void) sizeof((void) (_expr1), (void) (_expr2), 0))
93#define BT_USE_EXPR3(_expr1, _expr2, _expr3) \
94 ((void) sizeof((void) (_expr1), (void) (_expr2), (void) (_expr3), 0))
95#define BT_USE_EXPR4(_expr1, _expr2, _expr3, _expr4) \
96 ((void) sizeof((void) (_expr1), (void) (_expr2), \
97 (void) (_expr3), (void) (_expr4), 0))
98#define BT_USE_EXPR5(_expr1, _expr2, _expr3, _expr4, _expr5) \
99 ((void) sizeof((void) (_expr1), (void) (_expr2), \
100 (void) (_expr3), (void) (_expr4), (void) (_expr5), 0))
101
1c5ea5eb
SM
102#define BT_DIAG_PUSH _Pragma ("GCC diagnostic push")
103#define BT_DIAG_POP _Pragma ("GCC diagnostic push")
104
105#define BT_DIAG_IGNORE_SHADOW _Pragma("GCC diagnostic ignored \"-Wshadow\"")
106
2ea09241
SM
107#if defined __clang__
108# if __has_warning("-Wunused-but-set-variable")
109# define BT_DIAG_IGNORE_UNUSED_BUT_SET_VARIABLE \
110 _Pragma("GCC diagnostic ignored \"-Wunused-but-set-variable\"")
111# endif
112#endif
113
114#if !defined BT_DIAG_IGNORE_UNUSED_BUT_SET_VARIABLE
115# define BT_DIAG_IGNORE_UNUSED_BUT_SET_VARIABLE
116#endif
117
826b4673
PP
118#ifdef __cplusplus
119}
120#endif
121
70bd0a12 122#endif
This page took 0.122151 seconds and 4 git commands to generate.