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