Add G_GNUC_DEPRECATED. (Tom Tromey, #87969)
[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 #else
54 #define G_GNUC_PURE
55 #endif
56
57 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
58 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
59   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
60 #define G_GNUC_SCANF( format_idx, arg_idx )     \
61   __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
62 #define G_GNUC_FORMAT( arg_idx )                \
63   __attribute__((__format_arg__ (arg_idx)))
64 #define G_GNUC_NORETURN                         \
65   __attribute__((__noreturn__))
66 #define G_GNUC_CONST                            \
67   __attribute__((__const__))
68 #define G_GNUC_UNUSED                           \
69   __attribute__((__unused__))
70 #define G_GNUC_NO_INSTRUMENT                    \
71   __attribute__((__no_instrument_function__))
72 #else   /* !__GNUC__ */
73 #define G_GNUC_PRINTF( format_idx, arg_idx )
74 #define G_GNUC_SCANF( format_idx, arg_idx )
75 #define G_GNUC_FORMAT( arg_idx )
76 #define G_GNUC_NORETURN
77 #define G_GNUC_CONST
78 #define G_GNUC_UNUSED
79 #define G_GNUC_NO_INSTRUMENT
80 #endif  /* !__GNUC__ */
81
82 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
83 #define G_GNUC_DEPRECATED                            \
84   __attribute__((__deprecated__))
85 #else
86 #define G_GNUC_DEPRECATED
87 #endif /* __GNUC__ */
88
89 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
90 #define G_HIDDEN_SYMBOL
91   __attribute__((__visibility__("hidden")))
92 #else
93 #define G_HIDDEN_SYMBOL
94 #endif /* __GNUC__ */
95
96 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
97  * macros, so we can refer to them as strings unconditionally.
98  */
99 #if defined (__GNUC__) && (__GNUC__ < 3)
100 #define G_GNUC_FUNCTION         __FUNCTION__
101 #define G_GNUC_PRETTY_FUNCTION  __PRETTY_FUNCTION__
102 #else   /* !__GNUC__ */
103 #define G_GNUC_FUNCTION         ""
104 #define G_GNUC_PRETTY_FUNCTION  ""
105 #endif  /* !__GNUC__ */
106
107 #define G_STRINGIFY(macro_or_string)    G_STRINGIFY_ARG (macro_or_string)
108 #define G_STRINGIFY_ARG(contents)       #contents
109
110 /* Provide a string identifying the current code position */
111 #if defined(__GNUC__) && (__GNUC__ < 3)
112 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
113 #else
114 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__)
115 #endif
116
117 /* Guard C code in headers, while including them from C++ */
118 #ifdef  __cplusplus
119 # define G_BEGIN_DECLS  extern "C" {
120 # define G_END_DECLS    }
121 #else
122 # define G_BEGIN_DECLS
123 # define G_END_DECLS
124 #endif
125
126 /* Provide definitions for some commonly used macros.
127  *  Some of them are only provided if they haven't already
128  *  been defined. It is assumed that if they are already
129  *  defined then the current definition is correct.
130  */
131 #ifndef NULL
132 #  ifdef __cplusplus
133 #    define NULL        (0L)
134 #  else /* !__cplusplus */
135 #    define NULL        ((void*) 0)
136 #  endif /* !__cplusplus */
137 #endif
138
139 #ifndef FALSE
140 #define FALSE   (0)
141 #endif
142
143 #ifndef TRUE
144 #define TRUE    (!FALSE)
145 #endif
146
147 #undef  MAX
148 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
149
150 #undef  MIN
151 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
152
153 #undef  ABS
154 #define ABS(a)     (((a) < 0) ? -(a) : (a))
155
156 #undef  CLAMP
157 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
158
159 /* Count the number of elements in an array. The array must be defined
160  * as such; using this with a dynamically allocated array will give
161  * incorrect results.
162  */
163 #define G_N_ELEMENTS(arr)               (sizeof (arr) / sizeof ((arr)[0]))
164
165 /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
166  */
167 #define GPOINTER_TO_SIZE(p)     ((gsize) (p))
168 #define GSIZE_TO_POINTER(s)     ((gpointer) (gsize) (s))
169
170 /* Provide convenience macros for handling structure
171  * fields through their offsets.
172  */
173 #define G_STRUCT_OFFSET(struct_type, member)    \
174     ((glong) ((guint8*) &((struct_type*) 0)->member))
175 #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
176     ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
177 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
178     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
179
180 /* Provide simple macro statement wrappers (adapted from Perl):
181  *  G_STMT_START { statements; } G_STMT_END;
182  *  can be used as a single statement, as in
183  *  if (x) G_STMT_START { ... } G_STMT_END; else ...
184  *
185  *  For gcc we will wrap the statements within `({' and `})' braces.
186  *  For SunOS they will be wrapped within `if (1)' and `else (void) 0',
187  *  and otherwise within `do' and `while (0)'.
188  */
189 #if !(defined (G_STMT_START) && defined (G_STMT_END))
190 #  if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
191 #    define G_STMT_START        (void)(
192 #    define G_STMT_END          )
193 #  else
194 #    if (defined (sun) || defined (__sun__))
195 #      define G_STMT_START      if (1)
196 #      define G_STMT_END        else (void)0
197 #    else
198 #      define G_STMT_START      do
199 #      define G_STMT_END        while (0)
200 #    endif
201 #  endif
202 #endif
203
204 /* Allow the app programmer to select whether or not return values
205  * (usually char*) are const or not.  Don't try using this feature for
206  * functions with C++ linkage.
207  */
208 #ifdef G_DISABLE_CONST_RETURNS
209 #define G_CONST_RETURN
210 #else
211 #define G_CONST_RETURN const
212 #endif
213
214 /*
215  * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to 
216  * the compiler about the expected result of an expression. Some compilers
217  * can use this information for optimizations.
218  */
219 #if defined(__GNUC__) && (__GNUC__ > 2)
220 #define G_LIKELY(expr) __builtin_expect (!!(expr), 1)
221 #define G_UNLIKELY(expr) __builtin_expect (!!(expr), 0)
222 #else
223 #define G_LIKELY(expr) expr
224 #define G_UNLIKELY(expr) expr
225 #endif
226
227 #endif /* __G_MACROS_H__ */
228
229