1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 specialized macros which are not used often
7 <!-- ##### SECTION Long_Description ##### -->
9 These macros provide more specialized features which are not needed so often
10 by application programmers.
13 <!-- ##### SECTION See_Also ##### -->
18 <!-- ##### SECTION Stability_Level ##### -->
21 <!-- ##### SECTION Image ##### -->
24 <!-- ##### MACRO G_INLINE_FUNC ##### -->
26 This macro is used to export function prototypes so they can be linked
27 with an external version when no inlining is performed. The file which
28 implements the functions should define %G_IMPLEMENTS_INLINES
29 before including the headers which contain %G_INLINE_FUNC declarations.
30 Since inlining is very compiler-dependent using these macros correctly
31 is very difficult. Their use is strongly discouraged.
34 This macro is often mistaken for a replacement for the inline keyword;
35 inline is already declared in a portable manner in the glib headers
36 and can be used normally.
41 <!-- ##### MACRO G_STMT_START ##### -->
43 Used within multi-statement macros so that they can be used in places where
44 only one statement is expected by the compiler.
49 <!-- ##### MACRO G_STMT_END ##### -->
51 Used within multi-statement macros so that they can be used in places where
52 only one statement is expected by the compiler.
57 <!-- ##### MACRO G_BEGIN_DECLS ##### -->
59 Used (along with #G_END_DECLS) to bracket header files. If the
60 compiler in use is a C++ compiler, adds <literal>extern "C"</literal>
66 <!-- ##### MACRO G_END_DECLS ##### -->
68 Used (along with #G_BEGIN_DECLS) to bracket header files. If the
69 compiler in use is a C++ compiler, adds <literal>extern "C"</literal>
75 <!-- ##### MACRO G_N_ELEMENTS ##### -->
77 Determines the number of elements in an array. The array must be
78 declared so the compiler knows its size at compile-time; this
79 macro will not work on an array allocated on the heap, only static
80 arrays or arrays on the stack.
86 <!-- ##### MACRO G_VA_COPY ##### -->
88 Portable way to copy <type>va_list</type> variables.
91 In order to use this function, you must include <filename>string.h</filename>
92 yourself, because this macro may use <function>memmove()</function> and GLib
93 does not include <function>string.h</function> for you.
96 @ap1: the <type>va_list</type> variable to place a copy of @ap2 in.
97 @ap2: a <type>va_list</type>.
100 <!-- ##### MACRO G_STRINGIFY ##### -->
102 Accepts a macro or a string and converts it into a string after
103 preprocessor argument expansion. For example, the following code:
106 <informalexample><programlisting>
108 const gchar *greeting = G_STRINGIFY (AGE) " today!";
109 </programlisting></informalexample>
112 is transformed by the preprocessor into (code equivalent to):
115 <informalexample><programlisting>
116 const gchar *greeting = "27 today!";
117 </programlisting></informalexample>
119 @macro_or_string: a macro or a string.
122 <!-- ##### MACRO G_PASTE ##### -->
124 Yields a new preprocessor pasted identifier <code>identifier1identifier2</code>
125 from its expanded arguments @identifier1 and @identifier2. For example, the
129 <informalexample><programlisting>
130 #define GET(traveller,method) G_PASTE(traveller_get_, method) (traveller)
131 const gchar *name = GET (traveller, name);
132 const gchar *quest = GET (traveller, quest);
133 GdkColor *favourite = GET (traveller, favourite_colour);
134 </programlisting></informalexample>
137 is transformed by the preprocessor into:
140 <informalexample><programlisting>
141 const gchar *name = traveller_get_name (traveller);
142 const gchar *quest = traveller_get_quest (traveller);
143 GdkColor *favourite = traveller_get_favourite_colour (traveller);
144 </programlisting></informalexample>
146 @identifier1: an identifier
147 @identifier2: an identifier
151 <!-- ##### MACRO G_STATIC_ASSERT ##### -->
153 The G_STATIC_ASSERT macro lets the programmer check a condition at compile time,
154 the condition needs to be compile time computable.
155 The macro can be used in any place where a <literal>typedef</literal> is valid.
158 A <literal>typedef</literal> is generally allowed in exactly the same
159 places that a variable declaration is allowed. For this reason, you should not use <literal>G_STATIC_ASSERT</literal> in the middle of blocks of code.
162 The macro should only be used once per source code line.
165 @expr: a constant expression.
168 <!-- ##### MACRO G_STATIC_ASSERT_EXPR ##### -->
170 The G_STATIC_ASSERT_EXPR macro lets the programmer check a condition at
171 compile time. The condition needs to be compile time computable.
174 Unlike <literal>G_STATIC_ASSERT</literal>, this macro evaluates to an
175 expression and, as such, can be used in the middle of other expressions.
176 Its value should be ignored. This can be accomplished by placing it as
177 the first argument of a comma expression.
179 <informalexample><programlisting>
180 #define ADD_ONE_TO_INT(x) \
181 (G_STATIC_ASSERT_EXPR(sizeof (x) == sizeof (int)), ((x) + 1))
182 </programlisting></informalexample>
184 @expr: a constant expression.
187 <!-- ##### MACRO G_GNUC_EXTENSION ##### -->
189 Expands to <literal>__extension__</literal> when <command>gcc</command> is
190 used as the compiler.
191 This simply tells <command>gcc</command> not to warn about the following non-standard code
192 when compiling with the <option>-pedantic</option> option.
197 <!-- ##### MACRO G_GNUC_CONST ##### -->
199 Expands to the GNU C <literal>const</literal> function attribute if the compiler is
200 <command>gcc</command>. Declaring a function as const enables better optimization of calls
201 to the function. A const function doesn't examine any values except its parameters, and has no
202 effects except its return value. See the GNU C documentation for details.
205 A function that has pointer arguments and examines the data pointed to
206 must <emphasis>not</emphasis> be declared const. Likewise, a function that
207 calls a non-const function usually must not be const. It doesn't make sense
208 for a const function to return void.
213 <!-- ##### MACRO G_GNUC_PURE ##### -->
215 Expands to the GNU C <literal>pure</literal> function attribute if the compiler is
216 <command>gcc</command>. Declaring a function as pure enables better optimization of
217 calls to the function. A pure function has no effects except its return value and the
218 return value depends only on the parameters and/or global variables.
219 See the GNU C documentation for details.
224 <!-- ##### MACRO G_GNUC_MALLOC ##### -->
226 Expands to the GNU C <literal>malloc</literal> function attribute if the
227 compiler is <command>gcc</command>. Declaring a function as malloc enables
228 better optimization of the function. A function can have the malloc attribute
229 if it returns a pointer which is guaranteed to not alias with any other pointer
230 when the function returns (in practice, this means newly allocated memory).
231 See the GNU C documentation for details.
237 <!-- ##### MACRO G_GNUC_ALLOC_SIZE ##### -->
239 Expands to the GNU C <literal>alloc_size</literal> function attribute if the
240 compiler is a new enough <command>gcc</command>. This attribute tells the
241 compiler that the function returns a pointer to memory of a size that is
242 specified by the @x<!-- -->th function parameter.
243 See the GNU C documentation for details.
246 @x: the index of the argument specifying the allocation size
250 <!-- ##### MACRO G_GNUC_ALLOC_SIZE2 ##### -->
252 Expands to the GNU C <literal>alloc_size</literal> function attribute if the
253 compiler is a new enough <command>gcc</command>. This attribute tells the
254 compiler that the function returns a pointer to memory of a size that is
255 specified by the product of two function parameters.
256 See the GNU C documentation for details.
259 @x: the index of the argument specifying one factor of the allocation size
260 @y: the index of the argument specifying the second factor of the allocation size
264 <!-- ##### MACRO G_GNUC_DEPRECATED ##### -->
266 Expands to the GNU C <literal>deprecated</literal> attribute if the compiler
267 is <command>gcc</command>.
268 It can be used to mark typedefs, variables and functions as deprecated.
269 When called with the <option>-Wdeprecated-declarations</option> option, the compiler will
270 generate warnings when deprecated interfaces are used.
271 See the GNU C documentation for details.
277 <!-- ##### MACRO G_GNUC_DEPRECATED_FOR ##### -->
279 Like %G_GNUC_DEPRECATED, but names the intended replacement for the
280 deprecated symbol if the version of <command>gcc</command> in use is
281 new enough to support custom deprecation messages.
282 See the GNU C documentation for details.
285 @f: the intended replacement for the deprecated symbol, such as the name of a
290 <!-- ##### MACRO G_GNUC_NORETURN ##### -->
292 Expands to the GNU C <literal>noreturn</literal> function attribute if the
293 compiler is <command>gcc</command>. It is used for declaring functions which never return.
294 It enables optimization of the function, and avoids possible compiler
295 warnings. See the GNU C documentation for details.
300 <!-- ##### MACRO G_GNUC_UNUSED ##### -->
302 Expands to the GNU C <literal>unused</literal> function attribute if the compiler is
303 <command>gcc</command>. It is used for declaring functions which may never be used.
304 It avoids possible compiler warnings. See the GNU C documentation for details.
309 <!-- ##### MACRO G_GNUC_PRINTF ##### -->
311 Expands to the GNU C <literal>format</literal> function attribute if the compiler is
312 <command>gcc</command>. This is used for declaring functions which take a variable number of
313 arguments, with the same syntax as <function>printf()</function>.
314 It allows the compiler to type-check the arguments passed to the function.
315 See the GNU C documentation for details.
317 <informalexample><programlisting>
318 gint g_snprintf (gchar *string,
321 ...) G_GNUC_PRINTF (3, 4);
322 </programlisting></informalexample>
324 @format_idx: the index of the argument corresponding to the format string.
325 (The arguments are numbered from 1).
326 @arg_idx: the index of the first of the format arguments.
329 <!-- ##### MACRO G_GNUC_SCANF ##### -->
331 Expands to the GNU C <literal>format</literal> function attribute if the compiler is <command>gcc</command>.
332 This is used for declaring functions which take a variable number of
333 arguments, with the same syntax as <function>scanf()</function>.
334 It allows the compiler to type-check the arguments passed to the function.
335 See the GNU C documentation for details.
338 @format_idx: the index of the argument corresponding to the format string.
339 (The arguments are numbered from 1).
340 @arg_idx: the index of the first of the format arguments.
343 <!-- ##### MACRO G_GNUC_FORMAT ##### -->
345 Expands to the GNU C <literal>format_arg</literal> function attribute if the compiler is <command>gcc</command>.
346 This function attribute specifies that a function takes a format
347 string for a <function>printf()</function>, <function>scanf()</function>,
348 <function>strftime()</function> or <function>strfmon()</function> style
349 function and modifies it, so that the result can be passed to a
350 <function>printf()</function>, <function>scanf()</function>,
351 <function>strftime()</function> or <function>strfmon()</function> style
352 function (with the remaining arguments to the format function the same as
353 they would have been for the unmodified string).
354 See the GNU C documentation for details.
356 <informalexample><programlisting>
357 gchar *g_dgettext (gchar *domain_name, gchar *msgid) G_GNUC_FORMAT (2);
358 </programlisting></informalexample>
360 @arg_idx: the index of the argument.
363 <!-- ##### MACRO G_GNUC_NULL_TERMINATED ##### -->
365 Expands to the GNU C <literal>sentinel</literal> function attribute if the
366 compiler is <command>gcc</command>, or "" if it isn't. This function attribute
367 only applies to variadic functions and instructs the compiler to check that
368 the argument list is terminated with an explicit %NULL.
369 See the GNU C documentation for details.
376 <!-- ##### MACRO G_GNUC_WARN_UNUSED_RESULT ##### -->
378 Expands to the GNU C <literal>warn_unused_result</literal> function attribute
379 if the compiler is <command>gcc</command>, or "" if it isn't. This function
380 attribute makes the compiler emit a warning if the result of a function call
381 is ignored. See the GNU C documentation for details.
387 <!-- ##### MACRO G_GNUC_FUNCTION ##### -->
389 Expands to "" on all modern compilers, and to <literal>__FUNCTION__</literal>
390 on <command>gcc</command> version 2.x. Don't use it.
393 @Deprecated: 2.16: Use #G_STRFUNC instead.
396 <!-- ##### MACRO G_GNUC_PRETTY_FUNCTION ##### -->
398 Expands to "" on all modern compilers, and to
399 <literal>__PRETTY_FUNCTION__</literal> on <command>gcc</command> version 2.x.
403 @Deprecated: 2.16: Use #G_STRFUNC instead.
406 <!-- ##### MACRO G_GNUC_NO_INSTRUMENT ##### -->
408 Expands to the GNU C <literal>no_instrument_function</literal> function
409 attribute if the compiler is <command>gcc</command>. Functions with this
410 attribute will not be
411 instrumented for profiling, when the compiler is called with the
412 <option>-finstrument-functions</option> option.
413 See the GNU C documentation for details.
418 <!-- ##### MACRO G_HAVE_GNUC_VISIBILITY ##### -->
425 <!-- ##### MACRO G_GNUC_INTERNAL ##### -->
427 This attribute can be used for marking library functions as being used
428 internally to the library only, which may allow the compiler to handle
429 function calls more efficiently.
430 Note that static functions do not need to be marked as internal in this way.
431 See the GNU C documentation for details.
434 When using a compiler that supports the GNU C hidden visibility attribute,
435 this macro expands to <literal>__attribute__((visibility("hidden")))</literal>.
436 When using the Sun Studio compiler, it expands to <literal>__hidden</literal>.
439 Note that for portability, the attribute should be placed before the
440 function declaration. While GCC allows the macro after the declaration,
443 <informalexample><programlisting>
445 void _g_log_fallback_handler (const gchar *log_domain,
446 GLogLevelFlags log_level,
447 const gchar *message,
448 gpointer unused_data);
449 </programlisting></informalexample>
455 <!-- ##### MACRO G_GNUC_MAY_ALIAS ##### -->
457 Expands to the GNU C <literal>may_alias</literal> type attribute
458 if the compiler is <command>gcc</command>. Types with this attribute
459 will not be subjected to type-based alias analysis, but are assumed
460 to alias with any other type, just like char.
461 See the GNU C documentation for details.
468 <!-- ##### MACRO G_LIKELY ##### -->
470 Hints the compiler that the expression is likely to evaluate to a true
471 value. The compiler may use this information for optimizations.
473 <informalexample><programlisting>
474 if (G_LIKELY (random () != 1))
476 </programlisting></informalexample>
478 @expr: the expression
479 @Returns: the value of @expr
483 <!-- ##### MACRO G_UNLIKELY ##### -->
485 Hints the compiler that the expression is unlikely to evaluate to a true
486 value. The compiler may use this information for optimizations.
488 <informalexample><programlisting>
489 if (G_UNLIKELY (random () == 1))
490 g_print ("a random one");
491 </programlisting></informalexample>
493 @expr: the expression
494 @Returns: the value of @expr
498 <!-- ##### MACRO G_STRLOC ##### -->
500 Expands to a string identifying the current code position.
505 <!-- ##### MACRO G_STRFUNC ##### -->
507 Expands to a string identifying the current function.
513 <!-- ##### MACRO G_GINT16_MODIFIER ##### -->
515 The platform dependent length modifier for conversion specifiers for scanning
516 and printing values of type #gint16 or #guint16. It is a string literal,
517 but doesn't include the percent-sign, such that you can add precision and
518 length modifiers between percent-sign and conversion specifier and append a
519 conversion specifier.
523 The following example prints "0x7b";
527 g_print ("%#" G_GINT16_MODIFIER "x", value);
535 <!-- ##### MACRO G_GINT16_FORMAT ##### -->
537 This is the platform dependent conversion specifier for scanning and
538 printing values of type #gint16. It is a string literal, but doesn't
539 include the percent-sign, such that you can add precision and length
540 modifiers between percent-sign and conversion specifier.
548 sscanf ("42", "%" G_GINT16_FORMAT, &in)
550 g_print ("%" G_GINT32_FORMAT, out);
557 <!-- ##### MACRO G_GUINT16_FORMAT ##### -->
559 This is the platform dependent conversion specifier for scanning and
560 printing values of type #guint16. See also #G_GINT16_FORMAT.
565 <!-- ##### MACRO G_GINT32_MODIFIER ##### -->
567 The platform dependent length modifier for conversion specifiers for scanning
568 and printing values of type #gint32 or #guint32. It is a string literal,
569 See also #G_GINT16_MODIFIER.
575 <!-- ##### MACRO G_GINT32_FORMAT ##### -->
577 This is the platform dependent conversion specifier for scanning and
578 printing values of type #gint32. See also #G_GINT16_FORMAT.
583 <!-- ##### MACRO G_GUINT32_FORMAT ##### -->
585 This is the platform dependent conversion specifier for scanning and
586 printing values of type #guint32. See also #G_GINT16_FORMAT.
591 <!-- ##### MACRO G_GINT64_MODIFIER ##### -->
593 The platform dependent length modifier for conversion specifiers for scanning
594 and printing values of type #gint64 or #guint64. It is a string literal.
599 Some platforms do not support printing 64 bit integers,
600 even though the types are supported. On such platforms #G_GINT64_MODIFIER
608 <!-- ##### MACRO G_GINT64_FORMAT ##### -->
610 This is the platform dependent conversion specifier for scanning and
611 printing values of type #gint64. See also #G_GINT16_FORMAT.
616 Some platforms do not support scanning and printing 64 bit integers,
617 even though the types are supported. On such platforms #G_GINT64_FORMAT
618 is not defined. Note that scanf() may not support 64 bit integers, even
619 if #G_GINT64_FORMAT is defined. Due to its weak error handling, scanf()
620 is not recommended for parsing anyway; consider using g_ascii_strtoull()
627 <!-- ##### MACRO G_GUINT64_FORMAT ##### -->
629 This is the platform dependent conversion specifier for scanning and
630 printing values of type #guint64. See also #G_GINT16_FORMAT.
635 Some platforms do not support scanning and printing 64 bit integers,
636 even though the types are supported. On such platforms #G_GUINT64_FORMAT
637 is not defined. Note that scanf() may not support 64 bit integers, even
638 if #G_GINT64_FORMAT is defined. Due to its weak error handling, scanf() is not
639 recommended for parsing anyway; consider using g_strtoull() instead.
645 <!-- ##### MACRO G_GSIZE_MODIFIER ##### -->
647 The platform dependent length modifier for conversion specifiers for scanning
648 and printing values of type #gsize or #gssize. It is a string literal,
654 <!-- ##### MACRO G_GSIZE_FORMAT ##### -->
656 This is the platform dependent conversion specifier for scanning and
657 printing values of type #gsize. See also #G_GINT16_FORMAT.
663 <!-- ##### MACRO G_GSSIZE_FORMAT ##### -->
665 This is the platform dependent conversion specifier for scanning and
666 printing values of type #gssize. See also #G_GINT16_FORMAT.
672 <!-- ##### MACRO G_GOFFSET_MODIFIER ##### -->
674 The platform dependent length modifier for conversion specifiers for scanning
675 and printing values of type #goffset. It is a string literal. See also
682 <!-- ##### MACRO G_GOFFSET_FORMAT ##### -->
684 This is the platform dependent conversion specifier for scanning and
685 printing values of type #goffset. See also #G_GINT64_FORMAT.
692 <!-- ##### MACRO G_GINTPTR_MODIFIER ##### -->
694 The platform dependent length modifier for conversion specifiers for scanning
695 and printing values of type #gintptr or #guintptr. It is a string literal.
701 <!-- ##### MACRO G_GINTPTR_FORMAT ##### -->
703 This is the platform dependent conversion specifier for scanning and
704 printing values of type #gintptr.
710 <!-- ##### MACRO G_GUINTPTR_FORMAT ##### -->
712 This is the platform dependent conversion specifier for scanning and
713 printing values of type #guintptr.