C Standard Library Extensions 1.2.7
cxmacros.h
1/*
2 * This file is part of the ESO C Extension Library
3 * Copyright (C) 2001-2017 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20
21/*
22 * This file MUST not include any other cext header file.
23 */
24
25#ifndef CX_MACROS_H
26#define CX_MACROS_H
27
28
29/*
30 * Get the system's definition of NULL from stddef.h
31 */
32
33#include <stddef.h>
34
35
36/*
37 * An alias for __extension__
38 */
39
40#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
41# define CX_GNUC_EXTENSION __extension__
42#else
43# define CX_GNUC_EXTENSION
44#endif
45
46
47/*
48 * Macros for the GNU compiler function attributes
49 */
50
51#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
52# define CX_GNUC_PURE __attribute__((__pure__))
53# define CX_GNUC_MALLOC __attribute__((__malloc__))
54#else
55# define G_GNUC_PURE
56# define G_GNUC_MALLOC
57#endif
58
59#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
60# define CX_GNUC_PRINTF(fmt_idx, arg_idx) \
61 __attribute__((__format__(__printf__, fmt_idx, arg_idx)))
62# define CX_GNUC_SCANF(fmt_idx, arg_idx) \
63 __attribute__((__format__(__scanf__, fmt_idx, arg_idx)))
64# define CX_GNUC_FORMAT(arg_idx) __attribute__((__format_arg__(arg_idx)))
65# define CX_GNUC_NORETURN __attribute__((__noreturn__))
66# define CX_GNUC_CONST __attribute__((__const__))
67# define CX_GNUC_UNUSED __attribute__((__unused__))
68#else
69# define CX_GNUC_PRINTF(fmt_idx, arg_idx)
70# define CX_GNUC_SCANF(fmt_idx, arg_idx)
71# define CX_GNUC_FORMAT(arg_idx)
72# define CX_GNUC_NORETURN
73# define CX_GNUC_CONST
74# define CX_GNUC_UNUSED
75#endif
76
77
78/*
79 * Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with macros.
80 */
81
82#if defined(__GNUC__) && (__GNUC__ < 3)
83# define CX_GNUC_FUNCTION __FUNCTION__
84# define CX_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
85#else /* !__GNUC__ */
86# define CX_GNUC_FUNCTION ""
87# define CX_GNUC_PRETTY_FUNCTION ""
88#endif /* !__GNUC__ */
89
90#define CX_STRINGIFY(macro) CX_STRINGIFY_ARG(macro)
91#define CX_STRINGIFY_ARG(contents) #contents
92
93
94/*
95 * String identifier for the current code position
96 */
97
98#if defined(__GNUC__) && (__GNUC__ < 3)
99# define CX_CODE_POS \
100 __FILE__ ":" CX_STRINGIFY(__LINE__) ":" __PRETTY_FUNCTION__ "()"
101#else
102# define CX_CODE_POS __FILE__ ":" CX_STRINGIFY(__LINE__)
103#endif
104
105
106/*
107 * Current function identifier
108 */
109#if defined(__GNUC__)
110# define CX_FUNC_NAME ((const char *)(__PRETTY_FUNCTION__))
111#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 19901L
112# define CX_FUNC_NAME ((const char *)(__func__))
113#else
114# define CX_FUNC_NAME ((const char *)("???"))
115#endif
116
117
118/*
119 * C code guard
120 */
121
122#undef CX_BEGIN_DECLS
123#undef CX_END_DECLS
124
125#ifdef __cplusplus
126# define CX_BEGIN_DECLS extern "C" {
127# define CX_END_DECLS }
128#else
129# define CX_BEGIN_DECLS /* empty */
130# define CX_END_DECLS /* empty */
131#endif
132
133
134/*
135 * Some popular macros. If the system provides already a definition for some
136 * of them this definition is used, assuming the definition is correct.
137 */
138
139#ifndef NULL
140# ifdef __cplusplus
141# define NULL (0L)
142# else /* !__cplusplus */
143# define NULL ((void *)0)
144# endif /* !__cplusplus */
145#endif
146
147#ifndef FALSE
148# define FALSE (0)
149#endif
150
151#ifndef TRUE
152# define TRUE (!FALSE)
153#endif
154
155#ifndef CX_MIN
156# define CX_MIN(a, b) ((a) < (b) ? (a) : (b))
157#endif
158
159#ifndef CX_MAX
160# define CX_MAX(a, b) ((a) > (b) ? (a) : (b))
161#endif
162
163#ifndef CX_ABS
164# define CX_ABS(a) ((a) < (0) ? -(a) : (a))
165#endif
166
167#ifndef CX_CLAMP
168# define CX_CLAMP(a, low, high) \
169 (((a) > (high)) ? (high) : (((a) < (low)) ? (low) : (a)))
170#endif
171
172
173/*
174 * Number of elements in an array
175 */
176
177#define CX_N_ELEMENTS(array) (sizeof(array) / sizeof((array)[0]))
178
179#endif /* CX_MACROS_H */