1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 /* This file must not include any other glib header file and must thus
28 * not refer to variables from glibconfig.h
31 #ifndef __G_MACROS_H__
32 #define __G_MACROS_H__
34 /* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
35 * where this is valid. This allows for warningless compilation of
36 * "long long" types even in the presence of '-ansi -pedantic'.
38 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
39 # define G_GNUC_EXTENSION __extension__
41 # define G_GNUC_EXTENSION
44 /* Provide macros to feature the GCC function attribute.
46 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
53 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
54 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
55 __attribute__((format (printf, format_idx, arg_idx)))
56 #define G_GNUC_SCANF( format_idx, arg_idx ) \
57 __attribute__((format (scanf, format_idx, arg_idx)))
58 #define G_GNUC_FORMAT( arg_idx ) \
59 __attribute__((format_arg (arg_idx)))
60 #define G_GNUC_NORETURN \
61 __attribute__((noreturn))
62 #define G_GNUC_CONST \
63 __attribute__((const))
64 #define G_GNUC_UNUSED \
65 __attribute__((unused))
67 #define G_GNUC_PRINTF( format_idx, arg_idx )
68 #define G_GNUC_SCANF( format_idx, arg_idx )
69 #define G_GNUC_FORMAT( arg_idx )
70 #define G_GNUC_NORETURN
73 #endif /* !__GNUC__ */
75 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
76 * macros, so we can refer to them as strings unconditionally.
79 #define G_GNUC_FUNCTION __FUNCTION__
80 #define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
82 #define G_GNUC_FUNCTION ""
83 #define G_GNUC_PRETTY_FUNCTION ""
84 #endif /* !__GNUC__ */
86 #define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string)
87 #define G_STRINGIFY_ARG(contents) #contents
89 /* Provide a string identifying the current code position */
91 # define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
93 # define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__)
96 /* Guard C code in headers, while including them from C++ */
98 # define G_BEGIN_DECLS extern "C" {
99 # define G_END_DECLS }
101 # define G_BEGIN_DECLS
105 /* Provide definitions for some commonly used macros.
106 * Some of them are only provided if they haven't already
107 * been defined. It is assumed that if they are already
108 * defined then the current definition is correct.
113 # else /* !__cplusplus */
114 # define NULL ((void*) 0)
115 # endif /* !__cplusplus */
123 #define TRUE (!FALSE)
127 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
130 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
133 #define ABS(a) (((a) < 0) ? -(a) : (a))
136 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
138 /* Count the number of elements in an array. The array must be defined
139 * as such; using this with a dynamically allocated array will give
142 #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
144 /* Provide convenience macros for handling structure
145 * fields through their offsets.
147 #define G_STRUCT_OFFSET(struct_type, member) \
148 ((glong) ((guint8*) &((struct_type*) 0)->member))
149 #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
150 ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
151 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
152 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
154 /* Provide simple macro statement wrappers (adapted from Perl):
155 * G_STMT_START { statements; } G_STMT_END;
156 * can be used as a single statement, as in
157 * if (x) G_STMT_START { ... } G_STMT_END; else ...
159 * For gcc we will wrap the statements within `({' and `})' braces.
160 * For SunOS they will be wrapped within `if (1)' and `else (void) 0',
161 * and otherwise within `do' and `while (0)'.
163 #if !(defined (G_STMT_START) && defined (G_STMT_END))
164 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
165 # define G_STMT_START (void)(
166 # define G_STMT_END )
168 # if (defined (sun) || defined (__sun__))
169 # define G_STMT_START if (1)
170 # define G_STMT_END else (void)0
172 # define G_STMT_START do
173 # define G_STMT_END while (0)
178 /* Allow the app programmer to select whether or not return values
179 * (usually char*) are const or not. Don't try using this feature for
180 * functions with C++ linkage.
182 #ifdef G_DISABLE_CONST_RETURNS
183 #define G_CONST_RETURN
185 #define G_CONST_RETURN const
188 #endif /* __G_MACROS_H__ */