C Standard Library Extensions 1.2.4
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 __FILE__ ":" CX_STRINGIFY(__LINE__) ":" __PRETTY_FUNCTION__ "()"
100#else
101# define CX_CODE_POS __FILE__ ":" CX_STRINGIFY(__LINE__)
102#endif
103
104
105/*
106 * Current function identifier
107 */
108#if defined (__GNUC__)
109# define CX_FUNC_NAME ((const char*) (__PRETTY_FUNCTION__))
110#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 19901L
111# define CX_FUNC_NAME ((const char*) (__func__))
112#else
113# define CX_FUNC_NAME ((const char*) ("???"))
114#endif
115
116
117/*
118 * C code guard
119 */
120
121#undef CX_BEGIN_DECLS
122#undef CX_END_DECLS
123
124#ifdef __cplusplus
125# define CX_BEGIN_DECLS extern "C" {
126# define CX_END_DECLS }
127#else
128# define CX_BEGIN_DECLS /* empty */
129# define CX_END_DECLS /* empty */
130#endif
131
132
133/*
134 * Some popular macros. If the system provides already a definition for some
135 * of them this definition is used, assuming the definition is correct.
136 */
137
138#ifndef NULL
139# ifdef __cplusplus
140# define NULL (0L)
141# else /* !__cplusplus */
142# define NULL ((void *) 0)
143# endif /* !__cplusplus */
144#endif
145
146#ifndef FALSE
147# define FALSE (0)
148#endif
149
150#ifndef TRUE
151# define TRUE (!FALSE)
152#endif
153
154#ifndef CX_MIN
155# define CX_MIN(a, b) ((a) < (b) ? (a) : (b))
156#endif
157
158#ifndef CX_MAX
159# define CX_MAX(a, b) ((a) > (b) ? (a) : (b))
160#endif
161
162#ifndef CX_ABS
163# define CX_ABS(a) ((a) < (0) ? -(a) : (a))
164#endif
165
166#ifndef CX_CLAMP
167# define CX_CLAMP(a, low, high) (((a) > (high)) ? (high) : (((a) < (low)) ? (low) : (a)))
168#endif
169
170
171/*
172 * Number of elements in an array
173 */
174
175#define CX_N_ELEMENTS(array) (sizeof (array) / sizeof ((array)[0]))
176
177#endif /* CX_MACROS_H */