summaryrefslogtreecommitdiff
path: root/docs/reference/glib/tmpl/macros_misc.sgml
blob: c9e878cd96396587e917926e5280c3adaa245a7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
<!-- ##### SECTION Title ##### -->
Miscellaneous Macros

<!-- ##### SECTION Short_Description ##### -->
specialized macros which are not used often

<!-- ##### SECTION Long_Description ##### -->
<para>
These macros provide more specialized features which are not needed so often
by application programmers.
</para>

<!-- ##### SECTION See_Also ##### -->
<para>

</para>

<!-- ##### SECTION Stability_Level ##### -->


<!-- ##### MACRO G_INLINE_FUNC ##### -->
<para>
This macro is used to export function prototypes so they can be linked
with an external version when no inlining is performed. The file which
implements the functions should define %G_IMPLEMENTS_INLINES
before including the headers which contain %G_INLINE_FUNC declarations.
Since inlining is very compiler-dependent using these macros correctly
is very difficult. Their use is strongly discouraged.
</para>
<para>
This macro is often mistaken for a replacement for the inline keyword;
inline is already declared in a portable manner in the glib headers
and can be used normally.
</para>



<!-- ##### MACRO G_STMT_START ##### -->
<para>
Used within multi-statement macros so that they can be used in places where
only one statement is expected by the compiler.
</para>



<!-- ##### MACRO G_STMT_END ##### -->
<para>
Used within multi-statement macros so that they can be used in places where
only one statement is expected by the compiler.
</para>



<!-- ##### MACRO G_BEGIN_DECLS ##### -->
<para>
Used (along with #G_END_DECLS) to bracket header files. If the
compiler in use is a C++ compiler, adds <literal>extern "C"</literal> 
around the header.
</para>



<!-- ##### MACRO G_END_DECLS ##### -->
<para>
Used (along with #G_BEGIN_DECLS) to bracket header files. If the
compiler in use is a C++ compiler, adds <literal>extern "C"</literal> 
around the header.
</para>



<!-- ##### MACRO G_N_ELEMENTS ##### -->
<para>
Determines the number of elements in an array. The array must be
declared so the compiler knows its size at compile-time; this 
macro will not work on an array allocated on the heap, only static
arrays or arrays on the stack.
</para>

@arr: the array


<!-- ##### MACRO G_VA_COPY ##### -->
<para>
Portable way to copy <type>va_list</type> variables.
</para>
<para>
In order to use this function, you must include <filename>string.h</filename> 
yourself, because this macro may use <function>memmove()</function> and GLib 
does not include <function>string.h</function> for you.
</para>

@ap1: the <type>va_list</type> variable to place a copy of @ap2 in.
@ap2: a <type>va_list</type>.


<!-- ##### MACRO G_STRINGIFY ##### -->
<para>
Accepts a macro or a string and converts it into a string after
preprocessor argument expansion.
</para>

@macro_or_string: a macro or a string.


<!-- ##### MACRO G_PASTE ##### -->
<para>
Yields a new preprocessor pasted identifier 'identifier1identifier2'
from its expanded arguments 'identifier1' and 'identifier2'.
</para>

@identifier1: an identifier
@identifier2: an identifier
@Since: 2.20


<!-- ##### MACRO G_PASTE_ARGS ##### -->
<para>

</para>

@identifier1: 
@identifier2: 


<!-- ##### MACRO G_STATIC_ASSERT ##### -->
<para>
The G_STATIC_ASSERT macro lets the programmer check a condition at compile time,
the condition needs to be compile time computable.
The macro can be used in any place where a <literal>typedef</literal> is valid.
The macro should only be used once per source code line.
</para>

@expr: a constant expression.
@Since: 2.20


<!-- ##### MACRO G_GNUC_EXTENSION ##### -->
<para>
Expands to <literal>__extension__</literal> when <command>gcc</command> is 
used as the compiler.
This simply tells <command>gcc</command> not to warn about the following non-standard code
when compiling with the <option>-pedantic</option> option.
</para>



<!-- ##### MACRO G_GNUC_CONST ##### -->
<para>
Expands to the GNU C <literal>const</literal> function attribute if the compiler is 
<command>gcc</command>. Declaring a function as const enables better optimization of calls 
to the function. A const function doesn't examine any values except its parameters, and has no 
effects except its return value. See the GNU C documentation for details. 
</para>
<note><para>
A function that has pointer arguments and examines the data pointed to 
must <emphasis>not</emphasis> be declared const. Likewise, a function that 
calls a non-const function usually must not be const. It doesn't make sense 
for a const function to return void.
</para></note>



<!-- ##### MACRO G_GNUC_PURE ##### -->
<para>
Expands to the GNU C <literal>pure</literal> function attribute if the compiler is 
<command>gcc</command>. Declaring a function as pure enables better optimization of 
calls to the function. A pure function has no effects except its return value and the 
return value depends only on the parameters and/or global variables.
See the GNU C documentation for details. 
</para>



<!-- ##### MACRO G_GNUC_MALLOC ##### -->
<para>
Expands to the GNU C <literal>malloc</literal> function attribute if the 
compiler is <command>gcc</command>. Declaring a function as malloc enables 
better optimization of the function. A function can have the malloc attribute 
if it returns a pointer which is guaranteed to not alias with any other pointer
when the function returns (in practice, this means newly allocated memory).  
See the GNU C documentation for details. 
</para>

@Since: 2.6


<!-- ##### MACRO G_GNUC_ALLOC_SIZE ##### -->
<para>
Expands to the GNU C <literal>alloc_size</literal> function attribute if the 
compiler is a new enough <command>gcc</command>. This attribute tells the
compiler that the function returns a pointer to memory of a size that is
specified by the @x<!-- -->th function parameter.
See the GNU C documentation for details. 
</para>

@x: the index of the argument specifying the allocation size
@Since: 2.18


<!-- ##### MACRO G_GNUC_ALLOC_SIZE2 ##### -->
<para>
Expands to the GNU C <literal>alloc_size</literal> function attribute if the 
compiler is a new enough <command>gcc</command>. This attribute tells the
compiler that the function returns a pointer to memory of a size that is
specified by the product of two function parameters.
See the GNU C documentation for details. 
</para>

@x: the index of the argument specifying one factor of the allocation size
@y: the index of the argument specifying the second factor of the allocation size
@Since: 2.18


<!-- ##### MACRO G_GNUC_DEPRECATED ##### -->
<para>
Expands to the GNU C <literal>deprecated</literal> attribute if the compiler 
is <command>gcc</command>.
It can be used to mark typedefs, variables and functions as deprecated. 
When called with the <option>-Wdeprecated</option> option, the compiler will 
generate warnings when deprecated interfaces are used.
See the GNU C documentation for details. 
</para>

@Since: 2.2


<!-- ##### MACRO G_GNUC_NORETURN ##### -->
<para>
Expands to the GNU C <literal>noreturn</literal> function attribute if the 
compiler is <command>gcc</command>. It is used for declaring functions which never return.
It enables optimization of the function, and avoids possible compiler
warnings. See the GNU C documentation for details. 
</para>



<!-- ##### MACRO G_GNUC_UNUSED ##### -->
<para>
Expands to the GNU C <literal>unused</literal> function attribute if the compiler is 
<command>gcc</command>. It is used for declaring functions which may never be used.
It avoids possible compiler warnings. See the GNU C documentation for details. 
</para>



<!-- ##### MACRO G_GNUC_PRINTF ##### -->
<para>
Expands to the GNU C <literal>format</literal> function attribute if the compiler is 
<command>gcc</command>. This is used for declaring functions which take a variable number of
arguments, with the same syntax as <function>printf()</function>.
It allows the compiler to type-check the arguments passed to the function.
See the GNU C documentation for details. 
</para>
<informalexample><programlisting>
gint g_snprintf (gchar  *string,
                 gulong       n,
                 gchar const *format,
                 ...) G_GNUC_PRINTF (3, 4);
</programlisting></informalexample>

@format_idx: the index of the argument corresponding to the format string.
(The arguments are numbered from 1).
@arg_idx: the index of the first of the format arguments.


<!-- ##### MACRO G_GNUC_SCANF ##### -->
<para>
Expands to the GNU C <literal>format</literal> function attribute if the compiler is <command>gcc</command>.
This is used for declaring functions which take a variable number of
arguments, with the same syntax as <function>scanf()</function>.
It allows the compiler to type-check the arguments passed to the function.
See the GNU C documentation for details. 
</para>

@format_idx: the index of the argument corresponding to the format string.
(The arguments are numbered from 1).
@arg_idx: the index of the first of the format arguments.


<!-- ##### MACRO G_GNUC_FORMAT ##### -->
<para>
Expands to the GNU C <literal>format_arg</literal> function attribute if the compiler is <command>gcc</command>.
This function attribute specifies that a function takes a format
string for a <function>printf()</function>, <function>scanf()</function>, 
<function>strftime()</function> or <function>strfmon()</function> style
function and modifies it, so that the result can be passed to a 
<function>printf()</function>, <function>scanf()</function>, 
<function>strftime()</function> or <function>strfmon()</function> style 
function (with the remaining arguments to the format function the same as 
they would have been for the unmodified string). 
See the GNU C documentation for details. 
</para>
<informalexample><programlisting>
gchar *g_dgettext (gchar *domain_name, gchar *msgid) G_GNUC_FORMAT (2);
</programlisting></informalexample>

@arg_idx: the index of the argument.


<!-- ##### MACRO G_GNUC_NULL_TERMINATED ##### -->
<para>
Expands to the GNU C <literal>sentinel</literal> function attribute if the 
compiler is <command>gcc</command>, or "" if it isn't. This function attribute
only applies to variadic functions and instructs the compiler to check that 
the argument list is terminated with an explicit %NULL.
See the GNU C documentation for details. 
</para>

Since: 2.8



<!-- ##### MACRO G_GNUC_WARN_UNUSED_RESULT ##### -->
<para>
Expands to the GNU C <literal>warn_unused_result</literal> function attribute 
if the compiler is <command>gcc</command>, or "" if it isn't. This function 
attribute makes the compiler emit a warning if the result of a function call
is ignored. See the GNU C documentation for details. 
</para>

@Since: 2.10


<!-- ##### MACRO G_GNUC_FUNCTION ##### -->
<para>
Expands to "" on all modern compilers, and to <literal>__FUNCTION__</literal>
on <command>gcc</command> version 2.x. Don't use it.
</para>

@Deprecated: 2.16


<!-- ##### MACRO G_GNUC_PRETTY_FUNCTION ##### -->
<para>
Expands to "" on all modern compilers, and to 
<literal>__PRETTY_FUNCTION__</literal> on <command>gcc</command> version 2.x. 
Don't use it.
</para>

@Deprecated: 2.16


<!-- ##### MACRO G_GNUC_NO_INSTRUMENT ##### -->
<para>
Expands to the GNU C <literal>no_instrument_function</literal> function 
attribute if the compiler is <command>gcc</command>. Functions with this 
attribute will not be 
instrumented for profiling, when the compiler is called with the
<option>-finstrument-functions</option> option.
See the GNU C documentation for details. 
</para>



<!-- ##### MACRO G_HAVE_GNUC_VISIBILITY ##### -->
<para>
This macro is defined as 1 if the compiler supports ELF visibility 
attributes (currently only <command>gcc</command>).
</para>

Since: 2.6



<!-- ##### MACRO G_GNUC_INTERNAL ##### -->
<para>
This attribute can be used for marking library functions as being used 
internally to the library only, which may allow the compiler to handle
function calls more efficiently. 
Note that static functions do not need to be marked as internal in this way. 
See the GNU C documentation for details. 
</para>
<para>
When using a compiler that supports the GNU C hidden visibility attribute, 
this macro expands to <literal>__attribute__((visibility("hidden")))</literal>.
When using the Sun Studio compiler, it expands to <literal>__hidden</literal>.
</para>
<para>
Note that for portability, the attribute should be placed before the
function declaration. While GCC allows the macro after the declaration, 
Sun Studio does not.
</para>
<informalexample><programlisting>
G_GNUC_INTERNAL
void _g_log_fallback_handler (const gchar    *log_domain,
                              GLogLevelFlags  log_level,
                              const gchar    *message,
                              gpointer        unused_data);
</programlisting></informalexample>

Since: 2.6



<!-- ##### MACRO G_GNUC_MAY_ALIAS ##### -->
<para>
Expands to the GNU C <literal>may_alias</literal> type attribute 
if the compiler is <command>gcc</command>. Types with this attribute 
will not be subjected to type-based alias analysis, but are assumed
to alias with any other type, just like char.
See the GNU C documentation for details. 
</para>

Since: 2.14



<!-- ##### FUNCTION G_LIKELY ##### -->
<para>
Hints the compiler that the expression is likely to evaluate to a true
value. The compiler may use this information for optimizations.
</para>
<informalexample><programlisting>
if (G_LIKELY (random () != 1))
  g_print ("not one");
</programlisting></informalexample>

@Returns: the value of @expr
@Since: 2.2
<!-- # Unused Parameters # -->
@expr: the expression


<!-- ##### MACRO G_UNLIKELY ##### -->
<para>
Hints the compiler that the expression is unlikely to evaluate to a true
value. The compiler may use this information for optimizations.
</para>
<informalexample><programlisting>
if (G_UNLIKELY (random () == 1))
  g_print ("a random one");
</programlisting></informalexample>

@expr: the expression
@Returns: the value of @expr
@Since: 2.2


<!-- ##### MACRO G_STRLOC ##### -->
<para>
Expands to a string identifying the current code position. 
</para>



<!-- ##### MACRO G_STRFUNC ##### -->
<para>
Expands to a string identifying the current function. 
</para>

@Since: 2.4


<!-- ##### MACRO G_GINT16_MODIFIER ##### -->
<para>
The platform dependent length modifier for constructing printf() conversion
specifiers for values of type #gint16 or #guint16. It is a string literal, 
but doesn't include the percent-sign, such that you can add precision and 
length modifiers between percent-sign and conversion specifier and append a 
conversion specifier.
</para>

<para>
The following example prints "0x7b";
<informalexample>
<programlisting>
gint16 value = 123;
g_print ("%#" G_GINT16_MODIFIER "x", value);
</programlisting>
</informalexample>
</para>

@Since: 2.4


<!-- ##### MACRO G_GINT16_FORMAT ##### -->
<para>
This is the platform dependent conversion specifier for scanning and
printing values of type #gint16. It is a string literal, but doesn't
include the percent-sign, such that you can add precision and length
modifiers between percent-sign and conversion specifier.
</para>

<para>
<informalexample>
<programlisting>
gint16 in;
gint32 out;
sscanf ("42", "%" G_GINT16_FORMAT, &amp;in)
out = in * 1000;
g_print ("%" G_GINT32_FORMAT, out);
</programlisting>
</informalexample>
</para>



<!-- ##### MACRO G_GUINT16_FORMAT ##### -->
<para>
This is the platform dependent conversion specifier for scanning and
printing values of type #guint16. See also #G_GINT16_FORMAT.
</para>



<!-- ##### MACRO G_GINT32_MODIFIER ##### -->
<para>
The platform dependent length modifier for constructing printf() conversion
specifiers for values of type #gint32 or #guint32. See also #G_GINT16_MODIFIER.
</para>

@Since: 2.4


<!-- ##### MACRO G_GINT32_FORMAT ##### -->
<para>
This is the platform dependent conversion specifier for scanning and
printing values of type #gint32. See also #G_GINT16_FORMAT.
</para>



<!-- ##### MACRO G_GUINT32_FORMAT ##### -->
<para>
This is the platform dependent conversion specifier for scanning and
printing values of type #guint32. See also #G_GINT16_FORMAT.
</para>



<!-- ##### MACRO G_GINT64_MODIFIER ##### -->
<para>
The platform dependent length modifier for constructing printf() conversion
specifiers for values of type #gint64 or #guint64. See also #G_GINT16_MODIFIER.
</para>

<note>
<para>
Some platforms do not support printing 64 bit integers,
even though the types are supported. On such platforms #G_GINT64_MODIFIER
is not defined.
</para>
</note>

@Since: 2.4


<!-- ##### MACRO G_GINT64_FORMAT ##### -->
<para>
This is the platform dependent conversion specifier for scanning and
printing values of type #gint64. See also #G_GINT16_FORMAT.
</para>

<note>
<para>
Some platforms do not support scanning and printing 64 bit integers,
even though the types are supported. On such platforms #G_GINT64_FORMAT
is not defined. Note that scanf() may not support 64 bit integers, even
if #G_GINT64_FORMAT is defined. Due to its weak error handling, scanf() is not 
recommended for parsing anyway; consider using g_strtoull() instead.
</para>
</note>



<!-- ##### MACRO G_GUINT64_FORMAT ##### -->
<para>
This is the platform dependent conversion specifier for scanning and
printing values of type #guint64. See also #G_GINT16_FORMAT.
</para>

<note>
<para>
Some platforms do not support scanning and printing 64 bit integers,
even though the types are supported. On such platforms #G_GUINT64_FORMAT
is not defined.  Note that scanf() may not support 64 bit integers, even
if #G_GINT64_FORMAT is defined. Due to its weak error handling, scanf() is not 
recommended for parsing anyway; consider using g_strtoull() instead.
</para>
</note>



<!-- ##### MACRO G_GSIZE_MODIFIER ##### -->
<para>
The platform dependent length modifier for constructing printf() conversion
specifiers for values of type #gsize or #gssize. See also #G_GINT16_MODIFIER.
</para>

@Since: 2.6


<!-- ##### MACRO G_GSIZE_FORMAT ##### -->
<para>
This is the platform dependent conversion specifier for scanning and
printing values of type #gsize. See also #G_GINT16_FORMAT.
</para>

@Since: 2.6


<!-- ##### MACRO G_GSSIZE_FORMAT ##### -->
<para>
This is the platform dependent conversion specifier for scanning and
printing values of type #gssize. See also #G_GINT16_FORMAT.
</para>

@Since: 2.6