Mask all signals in GLib worker thread
[glib.git] / docs / reference / glib / tmpl / macros_misc.sgml
blob309bdedb9b79589ac3551a470421425b83cf3735
1 <!-- ##### SECTION Title ##### -->
2 Miscellaneous Macros
4 <!-- ##### SECTION Short_Description ##### -->
5 specialized macros which are not used often
7 <!-- ##### SECTION Long_Description ##### -->
8 <para>
9 These macros provide more specialized features which are not needed so often
10 by application programmers.
11 </para>
13 <!-- ##### SECTION See_Also ##### -->
14 <para>
16 </para>
18 <!-- ##### SECTION Stability_Level ##### -->
21 <!-- ##### SECTION Image ##### -->
24 <!-- ##### MACRO G_INLINE_FUNC ##### -->
25 <para>
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.
32 </para>
33 <para>
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.
37 </para>
41 <!-- ##### MACRO G_STMT_START ##### -->
42 <para>
43 Used within multi-statement macros so that they can be used in places where
44 only one statement is expected by the compiler.
45 </para>
49 <!-- ##### MACRO G_STMT_END ##### -->
50 <para>
51 Used within multi-statement macros so that they can be used in places where
52 only one statement is expected by the compiler.
53 </para>
57 <!-- ##### MACRO G_BEGIN_DECLS ##### -->
58 <para>
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>
61 around the header.
62 </para>
66 <!-- ##### MACRO G_END_DECLS ##### -->
67 <para>
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>
70 around the header.
71 </para>
75 <!-- ##### MACRO G_N_ELEMENTS ##### -->
76 <para>
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.
81 </para>
83 @arr: the array
86 <!-- ##### MACRO G_VA_COPY ##### -->
87 <para>
88 Portable way to copy <type>va_list</type> variables.
89 </para>
90 <para>
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.
94 </para>
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 ##### -->
101 <para>
102 Accepts a macro or a string and converts it into a string after
103 preprocessor argument expansion. For example, the following code:
104 </para>
106 <informalexample><programlisting>
107 #define AGE 27
108 const gchar *greeting = G_STRINGIFY (AGE) " today!";
109 </programlisting></informalexample>
111 <para>
112 is transformed by the preprocessor into (code equivalent to):
113 </para>
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 ##### -->
123 <para>
124 Yields a new preprocessor pasted identifier <code>identifier1identifier2</code>
125 from its expanded arguments @identifier1 and @identifier2. For example, the
126 following code:
127 </para>
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>
136 <para>
137 is transformed by the preprocessor into:
138 </para>
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
148 @Since: 2.20
151 <!-- ##### MACRO G_STATIC_ASSERT ##### -->
152 <para>
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.
156 </para>
157 <note><para>
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.
160 </para></note>
161 <para>
162 The macro should only be used once per source code line.
163 </para>
165 @expr: a constant expression.
166 @Since: 2.20
169 <!-- ##### MACRO G_STATIC_ASSERT_EXPR ##### -->
170 <para>
171 The G_STATIC_ASSERT_EXPR macro lets the programmer check a condition at
172 compile time. The condition needs to be compile time computable.
173 </para>
174 <para>
175 Unlike <literal>G_STATIC_ASSERT</literal>, this macro evaluates to an
176 expression and, as such, can be used in the middle of other expressions.
177 Its value should be ignored. This can be accomplished by placing it as
178 the first argument of a comma expression.
179 </para>
180 <informalexample><programlisting>
181 #define ADD_ONE_TO_INT(x) \
182 (G_STATIC_ASSERT_EXPR(sizeof (x) == sizeof (int)), ((x) + 1))
183 </programlisting></informalexample>
185 @expr: a constant expression.
186 @Since: 2.30
189 <!-- ##### MACRO G_GNUC_EXTENSION ##### -->
190 <para>
191 Expands to <literal>__extension__</literal> when <command>gcc</command> is
192 used as the compiler.
193 This simply tells <command>gcc</command> not to warn about the following non-standard code
194 when compiling with the <option>-pedantic</option> option.
195 </para>
199 <!-- ##### MACRO G_GNUC_CONST ##### -->
200 <para>
201 Expands to the GNU C <literal>const</literal> function attribute if the compiler is
202 <command>gcc</command>. Declaring a function as const enables better optimization of calls
203 to the function. A const function doesn't examine any values except its parameters, and has no
204 effects except its return value. See the GNU C documentation for details.
205 </para>
206 <note><para>
207 A function that has pointer arguments and examines the data pointed to
208 must <emphasis>not</emphasis> be declared const. Likewise, a function that
209 calls a non-const function usually must not be const. It doesn't make sense
210 for a const function to return void.
211 </para></note>
215 <!-- ##### MACRO G_GNUC_PURE ##### -->
216 <para>
217 Expands to the GNU C <literal>pure</literal> function attribute if the compiler is
218 <command>gcc</command>. Declaring a function as pure enables better optimization of
219 calls to the function. A pure function has no effects except its return value and the
220 return value depends only on the parameters and/or global variables.
221 See the GNU C documentation for details.
222 </para>
226 <!-- ##### MACRO G_GNUC_MALLOC ##### -->
227 <para>
228 Expands to the GNU C <literal>malloc</literal> function attribute if the
229 compiler is <command>gcc</command>. Declaring a function as malloc enables
230 better optimization of the function. A function can have the malloc attribute
231 if it returns a pointer which is guaranteed to not alias with any other pointer
232 when the function returns (in practice, this means newly allocated memory).
233 See the GNU C documentation for details.
234 </para>
236 @Since: 2.6
239 <!-- ##### MACRO G_GNUC_ALLOC_SIZE ##### -->
240 <para>
241 Expands to the GNU C <literal>alloc_size</literal> function attribute if the
242 compiler is a new enough <command>gcc</command>. This attribute tells the
243 compiler that the function returns a pointer to memory of a size that is
244 specified by the @x<!-- -->th function parameter.
245 See the GNU C documentation for details.
246 </para>
248 @x: the index of the argument specifying the allocation size
249 @Since: 2.18
252 <!-- ##### MACRO G_GNUC_ALLOC_SIZE2 ##### -->
253 <para>
254 Expands to the GNU C <literal>alloc_size</literal> function attribute if the
255 compiler is a new enough <command>gcc</command>. This attribute tells the
256 compiler that the function returns a pointer to memory of a size that is
257 specified by the product of two function parameters.
258 See the GNU C documentation for details.
259 </para>
261 @x: the index of the argument specifying one factor of the allocation size
262 @y: the index of the argument specifying the second factor of the allocation size
263 @Since: 2.18
266 <!-- ##### MACRO G_GNUC_DEPRECATED ##### -->
267 <para>
268 Expands to the GNU C <literal>deprecated</literal> attribute if the compiler
269 is <command>gcc</command>.
270 It can be used to mark typedefs, variables and functions as deprecated.
271 When called with the <option>-Wdeprecated-declarations</option> option, the compiler will
272 generate warnings when deprecated interfaces are used.
273 See the GNU C documentation for details.
274 </para>
276 @Since: 2.2
279 <!-- ##### MACRO G_GNUC_DEPRECATED_FOR ##### -->
280 <para>
281 Like %G_GNUC_DEPRECATED, but names the intended replacement for the
282 deprecated symbol if the version of <command>gcc</command> in use is
283 new enough to support custom deprecation messages.
284 See the GNU C documentation for details.
285 </para>
287 @f: the intended replacement for the deprecated symbol, such as the name of a
288 function
289 @Since: 2.26
292 <!-- ##### MACRO G_GNUC_NORETURN ##### -->
293 <para>
294 Expands to the GNU C <literal>noreturn</literal> function attribute if the
295 compiler is <command>gcc</command>. It is used for declaring functions which never return.
296 It enables optimization of the function, and avoids possible compiler
297 warnings. See the GNU C documentation for details.
298 </para>
302 <!-- ##### MACRO G_GNUC_UNUSED ##### -->
303 <para>
304 Expands to the GNU C <literal>unused</literal> function attribute if the compiler is
305 <command>gcc</command>. It is used for declaring functions which may never be used.
306 It avoids possible compiler warnings. See the GNU C documentation for details.
307 </para>
311 <!-- ##### MACRO G_GNUC_PRINTF ##### -->
312 <para>
313 Expands to the GNU C <literal>format</literal> function attribute if the compiler is
314 <command>gcc</command>. This is used for declaring functions which take a variable number of
315 arguments, with the same syntax as <function>printf()</function>.
316 It allows the compiler to type-check the arguments passed to the function.
317 See the GNU C documentation for details.
318 </para>
319 <informalexample><programlisting>
320 gint g_snprintf (gchar *string,
321 gulong n,
322 gchar const *format,
323 ...) G_GNUC_PRINTF (3, 4);
324 </programlisting></informalexample>
326 @format_idx: the index of the argument corresponding to the format string.
327 (The arguments are numbered from 1).
328 @arg_idx: the index of the first of the format arguments.
331 <!-- ##### MACRO G_GNUC_SCANF ##### -->
332 <para>
333 Expands to the GNU C <literal>format</literal> function attribute if the compiler is <command>gcc</command>.
334 This is used for declaring functions which take a variable number of
335 arguments, with the same syntax as <function>scanf()</function>.
336 It allows the compiler to type-check the arguments passed to the function.
337 See the GNU C documentation for details.
338 </para>
340 @format_idx: the index of the argument corresponding to the format string.
341 (The arguments are numbered from 1).
342 @arg_idx: the index of the first of the format arguments.
345 <!-- ##### MACRO G_GNUC_FORMAT ##### -->
346 <para>
347 Expands to the GNU C <literal>format_arg</literal> function attribute if the compiler is <command>gcc</command>.
348 This function attribute specifies that a function takes a format
349 string for a <function>printf()</function>, <function>scanf()</function>,
350 <function>strftime()</function> or <function>strfmon()</function> style
351 function and modifies it, so that the result can be passed to a
352 <function>printf()</function>, <function>scanf()</function>,
353 <function>strftime()</function> or <function>strfmon()</function> style
354 function (with the remaining arguments to the format function the same as
355 they would have been for the unmodified string).
356 See the GNU C documentation for details.
357 </para>
358 <informalexample><programlisting>
359 gchar *g_dgettext (gchar *domain_name, gchar *msgid) G_GNUC_FORMAT (2);
360 </programlisting></informalexample>
362 @arg_idx: the index of the argument.
365 <!-- ##### MACRO G_GNUC_NULL_TERMINATED ##### -->
366 <para>
367 Expands to the GNU C <literal>sentinel</literal> function attribute if the
368 compiler is <command>gcc</command>, or "" if it isn't. This function attribute
369 only applies to variadic functions and instructs the compiler to check that
370 the argument list is terminated with an explicit %NULL.
371 See the GNU C documentation for details.
372 </para>
374 Since: 2.8
378 <!-- ##### MACRO G_GNUC_WARN_UNUSED_RESULT ##### -->
379 <para>
380 Expands to the GNU C <literal>warn_unused_result</literal> function attribute
381 if the compiler is <command>gcc</command>, or "" if it isn't. This function
382 attribute makes the compiler emit a warning if the result of a function call
383 is ignored. See the GNU C documentation for details.
384 </para>
386 @Since: 2.10
389 <!-- ##### MACRO G_GNUC_FUNCTION ##### -->
390 <para>
391 Expands to "" on all modern compilers, and to <literal>__FUNCTION__</literal>
392 on <command>gcc</command> version 2.x. Don't use it.
393 </para>
395 @Deprecated: 2.16: Use #G_STRFUNC instead.
398 <!-- ##### MACRO G_GNUC_PRETTY_FUNCTION ##### -->
399 <para>
400 Expands to "" on all modern compilers, and to
401 <literal>__PRETTY_FUNCTION__</literal> on <command>gcc</command> version 2.x.
402 Don't use it.
403 </para>
405 @Deprecated: 2.16: Use #G_STRFUNC instead.
408 <!-- ##### MACRO G_GNUC_NO_INSTRUMENT ##### -->
409 <para>
410 Expands to the GNU C <literal>no_instrument_function</literal> function
411 attribute if the compiler is <command>gcc</command>. Functions with this
412 attribute will not be
413 instrumented for profiling, when the compiler is called with the
414 <option>-finstrument-functions</option> option.
415 See the GNU C documentation for details.
416 </para>
420 <!-- ##### MACRO G_HAVE_GNUC_VISIBILITY ##### -->
421 <para>
423 </para>
427 <!-- ##### MACRO G_GNUC_INTERNAL ##### -->
428 <para>
429 This attribute can be used for marking library functions as being used
430 internally to the library only, which may allow the compiler to handle
431 function calls more efficiently.
432 Note that static functions do not need to be marked as internal in this way.
433 See the GNU C documentation for details.
434 </para>
435 <para>
436 When using a compiler that supports the GNU C hidden visibility attribute,
437 this macro expands to <literal>__attribute__((visibility("hidden")))</literal>.
438 When using the Sun Studio compiler, it expands to <literal>__hidden</literal>.
439 </para>
440 <para>
441 Note that for portability, the attribute should be placed before the
442 function declaration. While GCC allows the macro after the declaration,
443 Sun Studio does not.
444 </para>
445 <informalexample><programlisting>
446 G_GNUC_INTERNAL
447 void _g_log_fallback_handler (const gchar *log_domain,
448 GLogLevelFlags log_level,
449 const gchar *message,
450 gpointer unused_data);
451 </programlisting></informalexample>
453 Since: 2.6
457 <!-- ##### MACRO G_GNUC_MAY_ALIAS ##### -->
458 <para>
459 Expands to the GNU C <literal>may_alias</literal> type attribute
460 if the compiler is <command>gcc</command>. Types with this attribute
461 will not be subjected to type-based alias analysis, but are assumed
462 to alias with any other type, just like char.
463 See the GNU C documentation for details.
464 </para>
466 Since: 2.14
470 <!-- ##### MACRO G_DEPRECATED ##### -->
471 <para>
473 </para>
477 <!-- ##### MACRO G_DEPRECATED_FOR ##### -->
478 <para>
480 </para>
482 @f:
485 <!-- ##### MACRO G_LIKELY ##### -->
486 <para>
487 Hints the compiler that the expression is likely to evaluate to a true
488 value. The compiler may use this information for optimizations.
489 </para>
490 <informalexample><programlisting>
491 if (G_LIKELY (random () != 1))
492 g_print ("not one");
493 </programlisting></informalexample>
495 @expr: the expression
496 @Returns: the value of @expr
497 @Since: 2.2
500 <!-- ##### MACRO G_UNLIKELY ##### -->
501 <para>
502 Hints the compiler that the expression is unlikely to evaluate to a true
503 value. The compiler may use this information for optimizations.
504 </para>
505 <informalexample><programlisting>
506 if (G_UNLIKELY (random () == 1))
507 g_print ("a random one");
508 </programlisting></informalexample>
510 @expr: the expression
511 @Returns: the value of @expr
512 @Since: 2.2
515 <!-- ##### MACRO G_STRLOC ##### -->
516 <para>
517 Expands to a string identifying the current code position.
518 </para>
522 <!-- ##### MACRO G_STRFUNC ##### -->
523 <para>
524 Expands to a string identifying the current function.
525 </para>
527 @Since: 2.4
530 <!-- ##### MACRO G_GINT16_MODIFIER ##### -->
531 <para>
532 The platform dependent length modifier for conversion specifiers for scanning
533 and printing values of type #gint16 or #guint16. It is a string literal,
534 but doesn't include the percent-sign, such that you can add precision and
535 length modifiers between percent-sign and conversion specifier and append a
536 conversion specifier.
537 </para>
539 <para>
540 The following example prints "0x7b";
541 <informalexample>
542 <programlisting>
543 gint16 value = 123;
544 g_print ("%#" G_GINT16_MODIFIER "x", value);
545 </programlisting>
546 </informalexample>
547 </para>
549 @Since: 2.4
552 <!-- ##### MACRO G_GINT16_FORMAT ##### -->
553 <para>
554 This is the platform dependent conversion specifier for scanning and
555 printing values of type #gint16. It is a string literal, but doesn't
556 include the percent-sign, such that you can add precision and length
557 modifiers between percent-sign and conversion specifier.
558 </para>
560 <para>
561 <informalexample>
562 <programlisting>
563 gint16 in;
564 gint32 out;
565 sscanf ("42", "%" G_GINT16_FORMAT, &amp;in)
566 out = in * 1000;
567 g_print ("%" G_GINT32_FORMAT, out);
568 </programlisting>
569 </informalexample>
570 </para>
574 <!-- ##### MACRO G_GUINT16_FORMAT ##### -->
575 <para>
576 This is the platform dependent conversion specifier for scanning and
577 printing values of type #guint16. See also #G_GINT16_FORMAT.
578 </para>
582 <!-- ##### MACRO G_GINT32_MODIFIER ##### -->
583 <para>
584 The platform dependent length modifier for conversion specifiers for scanning
585 and printing values of type #gint32 or #guint32. It is a string literal,
586 See also #G_GINT16_MODIFIER.
587 </para>
589 @Since: 2.4
592 <!-- ##### MACRO G_GINT32_FORMAT ##### -->
593 <para>
594 This is the platform dependent conversion specifier for scanning and
595 printing values of type #gint32. See also #G_GINT16_FORMAT.
596 </para>
600 <!-- ##### MACRO G_GUINT32_FORMAT ##### -->
601 <para>
602 This is the platform dependent conversion specifier for scanning and
603 printing values of type #guint32. See also #G_GINT16_FORMAT.
604 </para>
608 <!-- ##### MACRO G_GINT64_MODIFIER ##### -->
609 <para>
610 The platform dependent length modifier for conversion specifiers for scanning
611 and printing values of type #gint64 or #guint64. It is a string literal.
612 </para>
614 <note>
615 <para>
616 Some platforms do not support printing 64 bit integers,
617 even though the types are supported. On such platforms #G_GINT64_MODIFIER
618 is not defined.
619 </para>
620 </note>
622 @Since: 2.4
625 <!-- ##### MACRO G_GINT64_FORMAT ##### -->
626 <para>
627 This is the platform dependent conversion specifier for scanning and
628 printing values of type #gint64. See also #G_GINT16_FORMAT.
629 </para>
631 <note>
632 <para>
633 Some platforms do not support scanning and printing 64 bit integers,
634 even though the types are supported. On such platforms #G_GINT64_FORMAT
635 is not defined. Note that scanf() may not support 64 bit integers, even
636 if #G_GINT64_FORMAT is defined. Due to its weak error handling, scanf()
637 is not recommended for parsing anyway; consider using g_ascii_strtoull()
638 instead.
639 </para>
640 </note>
644 <!-- ##### MACRO G_GUINT64_FORMAT ##### -->
645 <para>
646 This is the platform dependent conversion specifier for scanning and
647 printing values of type #guint64. See also #G_GINT16_FORMAT.
648 </para>
650 <note>
651 <para>
652 Some platforms do not support scanning and printing 64 bit integers,
653 even though the types are supported. On such platforms #G_GUINT64_FORMAT
654 is not defined. Note that scanf() may not support 64 bit integers, even
655 if #G_GINT64_FORMAT is defined. Due to its weak error handling, scanf()
656 is not recommended for parsing anyway; consider using g_ascii_strtoull()
657 instead.
658 </para>
659 </note>
663 <!-- ##### MACRO G_GSIZE_MODIFIER ##### -->
664 <para>
665 The platform dependent length modifier for conversion specifiers for scanning
666 and printing values of type #gsize or #gssize. It is a string literal,
667 </para>
669 @Since: 2.6
672 <!-- ##### MACRO G_GSIZE_FORMAT ##### -->
673 <para>
674 This is the platform dependent conversion specifier for scanning and
675 printing values of type #gsize. See also #G_GINT16_FORMAT.
676 </para>
678 @Since: 2.6
681 <!-- ##### MACRO G_GSSIZE_FORMAT ##### -->
682 <para>
683 This is the platform dependent conversion specifier for scanning and
684 printing values of type #gssize. See also #G_GINT16_FORMAT.
685 </para>
687 @Since: 2.6
690 <!-- ##### MACRO G_GOFFSET_MODIFIER ##### -->
691 <para>
692 The platform dependent length modifier for conversion specifiers for scanning
693 and printing values of type #goffset. It is a string literal. See also
694 #G_GINT64_MODIFIER.
695 </para>
697 @Since: 2.20
700 <!-- ##### MACRO G_GOFFSET_FORMAT ##### -->
701 <para>
702 This is the platform dependent conversion specifier for scanning and
703 printing values of type #goffset. See also #G_GINT64_FORMAT.
704 </para>
706 Since: 2.20
710 <!-- ##### MACRO G_GINTPTR_MODIFIER ##### -->
711 <para>
712 The platform dependent length modifier for conversion specifiers for scanning
713 and printing values of type #gintptr or #guintptr. It is a string literal.
714 </para>
716 @Since: 2.22
719 <!-- ##### MACRO G_GINTPTR_FORMAT ##### -->
720 <para>
721 This is the platform dependent conversion specifier for scanning and
722 printing values of type #gintptr.
723 </para>
725 @Since: 2.22
728 <!-- ##### MACRO G_GUINTPTR_FORMAT ##### -->
729 <para>
730 This is the platform dependent conversion specifier for scanning and
731 printing values of type #guintptr.
732 </para>
734 @Since: 2.22