d0ef1849f1fdbc82f3786deebc060525493c337a
[platform/upstream/glib.git] / docs / reference / glib / tmpl / macros_misc.sgml
1 <!-- ##### SECTION Title ##### -->
2 Miscellaneous Macros
3
4 <!-- ##### SECTION Short_Description ##### -->
5 specialized macros which are not used often
6
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>
12
13 <!-- ##### SECTION See_Also ##### -->
14 <para>
15
16 </para>
17
18 <!-- ##### SECTION Stability_Level ##### -->
19
20
21 <!-- ##### SECTION Image ##### -->
22
23
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>
38
39
40
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>
46
47
48
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>
54
55
56
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>
63
64
65
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>
72
73
74
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>
82
83 @arr: the array
84
85
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>
95
96 @ap1: the <type>va_list</type> variable to place a copy of @ap2 in.
97 @ap2: a <type>va_list</type>.
98
99
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>
105
106 <informalexample><programlisting>
107 #define AGE 27
108 const gchar *greeting = G_STRINGIFY (AGE) " today!";
109 </programlisting></informalexample>
110
111 <para>
112 is transformed by the preprocessor into (code equivalent to):
113 </para>
114
115 <informalexample><programlisting>
116 const gchar *greeting = "27 today!";
117 </programlisting></informalexample>
118
119 @macro_or_string: a macro or a string.
120
121
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>
128
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>
135
136 <para>
137 is transformed by the preprocessor into:
138 </para>
139
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>
145
146 @identifier1: an identifier
147 @identifier2: an identifier
148 @Since: 2.20
149
150
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>
164
165 @expr: a constant expression.
166 @Since: 2.20
167
168 <!-- ##### MACRO G_STATIC_ASSERT_EXPR ##### -->
169 <para>
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.
172 </para>
173 <para>
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.
178 </para>
179 <informalexample><programlisting>
180 #define ADD_ONE_TO_INT(x) \
181   (G_STATIC_ASSERT_EXPR(sizeof (x) == sizeof (int)), ((x) + 1))
182 </programlisting></informalexample>
183
184 @expr: a constant expression.
185 @Since: 2.30
186
187 <!-- ##### MACRO G_GNUC_EXTENSION ##### -->
188 <para>
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.
193 </para>
194
195
196
197 <!-- ##### MACRO G_GNUC_CONST ##### -->
198 <para>
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. 
203 </para>
204 <note><para>
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.
209 </para></note>
210
211
212
213 <!-- ##### MACRO G_GNUC_PURE ##### -->
214 <para>
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. 
220 </para>
221
222
223
224 <!-- ##### MACRO G_GNUC_MALLOC ##### -->
225 <para>
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. 
232 </para>
233
234 @Since: 2.6
235
236
237 <!-- ##### MACRO G_GNUC_ALLOC_SIZE ##### -->
238 <para>
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. 
244 </para>
245
246 @x: the index of the argument specifying the allocation size
247 @Since: 2.18
248
249
250 <!-- ##### MACRO G_GNUC_ALLOC_SIZE2 ##### -->
251 <para>
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. 
257 </para>
258
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
261 @Since: 2.18
262
263
264 <!-- ##### MACRO G_GNUC_DEPRECATED ##### -->
265 <para>
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. 
272 </para>
273
274 @Since: 2.2
275
276
277 <!-- ##### MACRO G_GNUC_DEPRECATED_FOR ##### -->
278 <para>
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.
283 </para>
284
285 @f: the intended replacement for the deprecated symbol, such as the name of a
286     function
287 @Since: 2.25.3
288
289
290 <!-- ##### MACRO G_GNUC_NORETURN ##### -->
291 <para>
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. 
296 </para>
297
298
299
300 <!-- ##### MACRO G_GNUC_UNUSED ##### -->
301 <para>
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. 
305 </para>
306
307
308
309 <!-- ##### MACRO G_GNUC_PRINTF ##### -->
310 <para>
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. 
316 </para>
317 <informalexample><programlisting>
318 gint g_snprintf (gchar  *string,
319                  gulong       n,
320                  gchar const *format,
321                  ...) G_GNUC_PRINTF (3, 4);
322 </programlisting></informalexample>
323
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.
327
328
329 <!-- ##### MACRO G_GNUC_SCANF ##### -->
330 <para>
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. 
336 </para>
337
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.
341
342
343 <!-- ##### MACRO G_GNUC_FORMAT ##### -->
344 <para>
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. 
355 </para>
356 <informalexample><programlisting>
357 gchar *g_dgettext (gchar *domain_name, gchar *msgid) G_GNUC_FORMAT (2);
358 </programlisting></informalexample>
359
360 @arg_idx: the index of the argument.
361
362
363 <!-- ##### MACRO G_GNUC_NULL_TERMINATED ##### -->
364 <para>
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. 
370 </para>
371
372 Since: 2.8
373
374
375
376 <!-- ##### MACRO G_GNUC_WARN_UNUSED_RESULT ##### -->
377 <para>
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. 
382 </para>
383
384 @Since: 2.10
385
386
387 <!-- ##### MACRO G_GNUC_FUNCTION ##### -->
388 <para>
389 Expands to "" on all modern compilers, and to <literal>__FUNCTION__</literal>
390 on <command>gcc</command> version 2.x. Don't use it.
391 </para>
392
393 @Deprecated: 2.16: Use #G_STRFUNC instead.
394
395
396 <!-- ##### MACRO G_GNUC_PRETTY_FUNCTION ##### -->
397 <para>
398 Expands to "" on all modern compilers, and to 
399 <literal>__PRETTY_FUNCTION__</literal> on <command>gcc</command> version 2.x. 
400 Don't use it.
401 </para>
402
403 @Deprecated: 2.16: Use #G_STRFUNC instead.
404
405
406 <!-- ##### MACRO G_GNUC_NO_INSTRUMENT ##### -->
407 <para>
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. 
414 </para>
415
416
417
418 <!-- ##### MACRO G_HAVE_GNUC_VISIBILITY ##### -->
419 <para>
420
421 </para>
422
423
424
425 <!-- ##### MACRO G_GNUC_INTERNAL ##### -->
426 <para>
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. 
432 </para>
433 <para>
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>.
437 </para>
438 <para>
439 Note that for portability, the attribute should be placed before the
440 function declaration. While GCC allows the macro after the declaration, 
441 Sun Studio does not.
442 </para>
443 <informalexample><programlisting>
444 G_GNUC_INTERNAL
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>
450
451 Since: 2.6
452
453
454
455 <!-- ##### MACRO G_GNUC_MAY_ALIAS ##### -->
456 <para>
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. 
462 </para>
463
464 Since: 2.14
465
466
467
468 <!-- ##### MACRO G_LIKELY ##### -->
469 <para>
470 Hints the compiler that the expression is likely to evaluate to a true
471 value. The compiler may use this information for optimizations.
472 </para>
473 <informalexample><programlisting>
474 if (G_LIKELY (random () != 1))
475   g_print ("not one");
476 </programlisting></informalexample>
477
478 @expr: the expression
479 @Returns: the value of @expr
480 @Since: 2.2
481
482
483 <!-- ##### MACRO G_UNLIKELY ##### -->
484 <para>
485 Hints the compiler that the expression is unlikely to evaluate to a true
486 value. The compiler may use this information for optimizations.
487 </para>
488 <informalexample><programlisting>
489 if (G_UNLIKELY (random () == 1))
490   g_print ("a random one");
491 </programlisting></informalexample>
492
493 @expr: the expression
494 @Returns: the value of @expr
495 @Since: 2.2
496
497
498 <!-- ##### MACRO G_STRLOC ##### -->
499 <para>
500 Expands to a string identifying the current code position. 
501 </para>
502
503
504
505 <!-- ##### MACRO G_STRFUNC ##### -->
506 <para>
507 Expands to a string identifying the current function. 
508 </para>
509
510 @Since: 2.4
511
512
513 <!-- ##### MACRO G_GINT16_MODIFIER ##### -->
514 <para>
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.
520 </para>
521
522 <para>
523 The following example prints "0x7b";
524 <informalexample>
525 <programlisting>
526 gint16 value = 123;
527 g_print ("%#" G_GINT16_MODIFIER "x", value);
528 </programlisting>
529 </informalexample>
530 </para>
531
532 @Since: 2.4
533
534
535 <!-- ##### MACRO G_GINT16_FORMAT ##### -->
536 <para>
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.
541 </para>
542
543 <para>
544 <informalexample>
545 <programlisting>
546 gint16 in;
547 gint32 out;
548 sscanf ("42", "%" G_GINT16_FORMAT, &amp;in)
549 out = in * 1000;
550 g_print ("%" G_GINT32_FORMAT, out);
551 </programlisting>
552 </informalexample>
553 </para>
554
555
556
557 <!-- ##### MACRO G_GUINT16_FORMAT ##### -->
558 <para>
559 This is the platform dependent conversion specifier for scanning and
560 printing values of type #guint16. See also #G_GINT16_FORMAT.
561 </para>
562
563
564
565 <!-- ##### MACRO G_GINT32_MODIFIER ##### -->
566 <para>
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.
570 </para>
571
572 @Since: 2.4
573
574
575 <!-- ##### MACRO G_GINT32_FORMAT ##### -->
576 <para>
577 This is the platform dependent conversion specifier for scanning and
578 printing values of type #gint32. See also #G_GINT16_FORMAT.
579 </para>
580
581
582
583 <!-- ##### MACRO G_GUINT32_FORMAT ##### -->
584 <para>
585 This is the platform dependent conversion specifier for scanning and
586 printing values of type #guint32. See also #G_GINT16_FORMAT.
587 </para>
588
589
590
591 <!-- ##### MACRO G_GINT64_MODIFIER ##### -->
592 <para>
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.
595 </para>
596
597 <note>
598 <para>
599 Some platforms do not support printing 64 bit integers,
600 even though the types are supported. On such platforms #G_GINT64_MODIFIER
601 is not defined.
602 </para>
603 </note>
604
605 @Since: 2.4
606
607
608 <!-- ##### MACRO G_GINT64_FORMAT ##### -->
609 <para>
610 This is the platform dependent conversion specifier for scanning and
611 printing values of type #gint64. See also #G_GINT16_FORMAT.
612 </para>
613
614 <note>
615 <para>
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()
621 instead.
622 </para>
623 </note>
624
625
626
627 <!-- ##### MACRO G_GUINT64_FORMAT ##### -->
628 <para>
629 This is the platform dependent conversion specifier for scanning and
630 printing values of type #guint64. See also #G_GINT16_FORMAT.
631 </para>
632
633 <note>
634 <para>
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.
640 </para>
641 </note>
642
643
644
645 <!-- ##### MACRO G_GSIZE_MODIFIER ##### -->
646 <para>
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, 
649 </para>
650
651 @Since: 2.6
652
653
654 <!-- ##### MACRO G_GSIZE_FORMAT ##### -->
655 <para>
656 This is the platform dependent conversion specifier for scanning and
657 printing values of type #gsize. See also #G_GINT16_FORMAT.
658 </para>
659
660 @Since: 2.6
661
662
663 <!-- ##### MACRO G_GSSIZE_FORMAT ##### -->
664 <para>
665 This is the platform dependent conversion specifier for scanning and
666 printing values of type #gssize. See also #G_GINT16_FORMAT.
667 </para>
668
669 @Since: 2.6
670
671
672 <!-- ##### MACRO G_GOFFSET_MODIFIER ##### -->
673 <para>
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
676 #G_GINT64_MODIFIER.
677 </para>
678
679 @Since: 2.20
680
681
682 <!-- ##### MACRO G_GOFFSET_FORMAT ##### -->
683 <para>
684 This is the platform dependent conversion specifier for scanning and
685 printing values of type #goffset. See also #G_GINT64_FORMAT.
686 </para>
687
688 Since: 2.20
689
690
691
692 <!-- ##### MACRO G_GINTPTR_MODIFIER ##### -->
693 <para>
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.
696 </para>
697
698 @Since: 2.22
699
700
701 <!-- ##### MACRO G_GINTPTR_FORMAT ##### -->
702 <para>
703 This is the platform dependent conversion specifier for scanning and
704 printing values of type #gintptr. 
705 </para>
706
707 @Since: 2.22
708
709
710 <!-- ##### MACRO G_GUINTPTR_FORMAT ##### -->
711 <para>
712 This is the platform dependent conversion specifier for scanning and
713 printing values of type #guintptr.
714 </para>
715
716 @Since: 2.22
717
718