Define empty for gcc 2.95.
[platform/upstream/glib.git] / glib / gmacros.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 /* This file must not include any other glib header file and must thus
28  * not refer to variables from glibconfig.h 
29  */
30
31 #ifndef __G_MACROS_H__
32 #define __G_MACROS_H__
33
34 /* We include stddef.h to get the system's definition of NULL
35  */
36 #include <stddef.h>
37
38 /* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
39  * where this is valid. This allows for warningless compilation of
40  * "long long" types even in the presence of '-ansi -pedantic'. 
41  */
42 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
43 #  define G_GNUC_EXTENSION __extension__
44 #else
45 #  define G_GNUC_EXTENSION
46 #endif
47
48 /* Provide macros to feature the GCC function attribute.
49  */
50 #if    __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
51 #define G_GNUC_PURE                            \
52   __attribute__((__pure__))
53 #define G_GNUC_MALLOC                           \
54   __attribute__((__malloc__))
55 #else
56 #define G_GNUC_PURE
57 #define G_GNUC_MALLOC
58 #endif
59
60 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
61 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
62   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
63 #define G_GNUC_SCANF( format_idx, arg_idx )     \
64   __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
65 #define G_GNUC_FORMAT( arg_idx )                \
66   __attribute__((__format_arg__ (arg_idx)))
67 #define G_GNUC_NORETURN                         \
68   __attribute__((__noreturn__))
69 #define G_GNUC_CONST                            \
70   __attribute__((__const__))
71 #define G_GNUC_UNUSED                           \
72   __attribute__((__unused__))
73 #define G_GNUC_NO_INSTRUMENT                    \
74   __attribute__((__no_instrument_function__))
75 #else   /* !__GNUC__ */
76 #define G_GNUC_PRINTF( format_idx, arg_idx )
77 #define G_GNUC_SCANF( format_idx, arg_idx )
78 #define G_GNUC_FORMAT( arg_idx )
79 #define G_GNUC_NORETURN
80 #define G_GNUC_CONST
81 #define G_GNUC_UNUSED
82 #define G_GNUC_NO_INSTRUMENT
83 #endif  /* !__GNUC__ */
84
85 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
86 #define G_GNUC_DEPRECATED                            \
87   __attribute__((__deprecated__))
88 #else
89 #define G_GNUC_DEPRECATED
90 #endif /* __GNUC__ */
91
92 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
93  * macros, so we can refer to them as strings unconditionally.
94  * usage not-recommended since gcc-3.0
95  */
96 #if defined (__GNUC__) && (__GNUC__ < 3)
97 #define G_GNUC_FUNCTION         __FUNCTION__
98 #define G_GNUC_PRETTY_FUNCTION  __PRETTY_FUNCTION__
99 #else   /* !__GNUC__ */
100 #define G_GNUC_FUNCTION         ""
101 #define G_GNUC_PRETTY_FUNCTION  ""
102 #endif  /* !__GNUC__ */
103
104 #define G_STRINGIFY(macro_or_string)    G_STRINGIFY_ARG (macro_or_string)
105 #define G_STRINGIFY_ARG(contents)       #contents
106
107 /* Provide a string identifying the current code position */
108 #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
109 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
110 #else
111 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__)
112 #endif
113
114 /* Provide a string identifying the current function, non-concatenatable */
115 #if defined (__GNUC__)
116 #  define G_STRFUNC     ((const char*) (__PRETTY_FUNCTION__))
117 #elif defined (G_HAVE_ISO_VARARGS)
118 #  define G_STRFUNC     ((const char*) (__func__))
119 #else
120 #  define G_STRFUNC     ((const char*) ("???"))
121 #endif
122
123 /* Guard C code in headers, while including them from C++ */
124 #ifdef  __cplusplus
125 # define G_BEGIN_DECLS  extern "C" {
126 # define G_END_DECLS    }
127 #else
128 # define G_BEGIN_DECLS
129 # define G_END_DECLS
130 #endif
131
132 /* Provide definitions for some commonly used macros.
133  *  Some of them are only provided if they haven't already
134  *  been defined. It is assumed that if they are already
135  *  defined then the current definition is correct.
136  */
137 #ifndef NULL
138 #  ifdef __cplusplus
139 #    define NULL        (0L)
140 #  else /* !__cplusplus */
141 #    define NULL        ((void*) 0)
142 #  endif /* !__cplusplus */
143 #endif
144
145 #ifndef FALSE
146 #define FALSE   (0)
147 #endif
148
149 #ifndef TRUE
150 #define TRUE    (!FALSE)
151 #endif
152
153 #undef  MAX
154 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
155
156 #undef  MIN
157 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
158
159 #undef  ABS
160 #define ABS(a)     (((a) < 0) ? -(a) : (a))
161
162 #undef  CLAMP
163 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
164
165 /* Count the number of elements in an array. The array must be defined
166  * as such; using this with a dynamically allocated array will give
167  * incorrect results.
168  */
169 #define G_N_ELEMENTS(arr)               (sizeof (arr) / sizeof ((arr)[0]))
170
171 /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
172  */
173 #define GPOINTER_TO_SIZE(p)     ((gsize) (p))
174 #define GSIZE_TO_POINTER(s)     ((gpointer) (gsize) (s))
175
176 /* Provide convenience macros for handling structure
177  * fields through their offsets.
178  */
179 #define G_STRUCT_OFFSET(struct_type, member)    \
180     ((glong) ((guint8*) &((struct_type*) 0)->member))
181 #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
182     ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
183 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
184     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
185
186 /* Provide simple macro statement wrappers (adapted from Perl):
187  *  G_STMT_START { statements; } G_STMT_END;
188  *  can be used as a single statement, as in
189  *  if (x) G_STMT_START { ... } G_STMT_END; else ...
190  *
191  *  For gcc we will wrap the statements within `({' and `})' braces.
192  *  For SunOS they will be wrapped within `if (1)' and `else (void) 0',
193  *  and otherwise within `do' and `while (0)'.
194  */
195 #if !(defined (G_STMT_START) && defined (G_STMT_END))
196 #  if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
197 #    define G_STMT_START        (void) __extension__ (
198 #    define G_STMT_END          )
199 #  else
200 #    if (defined (sun) || defined (__sun__))
201 #      define G_STMT_START      if (1)
202 #      define G_STMT_END        else (void)0
203 #    else
204 #      define G_STMT_START      do
205 #      define G_STMT_END        while (0)
206 #    endif
207 #  endif
208 #endif
209
210 /* Allow the app programmer to select whether or not return values
211  * (usually char*) are const or not.  Don't try using this feature for
212  * functions with C++ linkage.
213  */
214 #ifdef G_DISABLE_CONST_RETURNS
215 #define G_CONST_RETURN
216 #else
217 #define G_CONST_RETURN const
218 #endif
219
220 /*
221  * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to 
222  * the compiler about the expected result of an expression. Some compilers
223  * can use this information for optimizations.
224  *
225  * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
226  * putting assignments in g_return_if_fail ().  
227  */
228 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
229 #define _G_BOOLEAN_EXPR(expr)                   \
230  __extension__ ({                               \
231    int _g_boolean_var_;                         \
232    if (expr)                                    \
233       _g_boolean_var_ = 1;                      \
234    else                                         \
235       _g_boolean_var_ = 0;                      \
236    _g_boolean_var_;                             \
237 })
238 #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))
239 #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))
240 #else
241 #define G_LIKELY(expr) (expr)
242 #define G_UNLIKELY(expr) (expr)
243 #endif
244
245 #endif /* __G_MACROS_H__ */