1/* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef __G_TYPE_H__
20#define __G_TYPE_H__
21
22#if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
23#error "Only <glib-object.h> can be included directly."
24#endif
25
26#include <glib.h>
27#include <gobject/gobject-visibility.h>
28
29G_BEGIN_DECLS
30
31/* Basic Type Macros
32 */
33/**
34 * G_TYPE_FUNDAMENTAL:
35 * @type: A #GType value.
36 *
37 * The fundamental type which is the ancestor of @type.
38 *
39 * Fundamental types are types that serve as ultimate bases for the derived types,
40 * thus they are the roots of distinct inheritance hierarchies.
41 */
42#define G_TYPE_FUNDAMENTAL(type) (g_type_fundamental (type))
43/**
44 * G_TYPE_FUNDAMENTAL_SHIFT:
45 *
46 * Shift value used in converting numbers to type IDs.
47 */
48#define G_TYPE_FUNDAMENTAL_SHIFT (2)
49/**
50 * G_TYPE_FUNDAMENTAL_MAX: (value 1020)
51 *
52 * An integer constant that represents the number of identifiers reserved
53 * for types that are assigned at compile-time.
54 */
55#define G_TYPE_FUNDAMENTAL_MAX (255 << G_TYPE_FUNDAMENTAL_SHIFT)
56
57/* Constant fundamental types,
58 */
59/**
60 * G_TYPE_INVALID:
61 *
62 * An invalid #GType used as error return value in some functions which return
63 * a #GType.
64 */
65#define G_TYPE_INVALID G_TYPE_MAKE_FUNDAMENTAL (0)
66/**
67 * G_TYPE_NONE:
68 *
69 * A fundamental type which is used as a replacement for the C
70 * void return type.
71 */
72#define G_TYPE_NONE G_TYPE_MAKE_FUNDAMENTAL (1)
73/**
74 * G_TYPE_INTERFACE:
75 *
76 * The fundamental type from which all interfaces are derived.
77 */
78#define G_TYPE_INTERFACE G_TYPE_MAKE_FUNDAMENTAL (2)
79/**
80 * G_TYPE_CHAR:
81 *
82 * The fundamental type corresponding to #gchar.
83 *
84 * The type designated by %G_TYPE_CHAR is unconditionally an 8-bit signed integer.
85 * This may or may not be the same type a the C type "gchar".
86 */
87#define G_TYPE_CHAR G_TYPE_MAKE_FUNDAMENTAL (3)
88/**
89 * G_TYPE_UCHAR:
90 *
91 * The fundamental type corresponding to #guchar.
92 */
93#define G_TYPE_UCHAR G_TYPE_MAKE_FUNDAMENTAL (4)
94/**
95 * G_TYPE_BOOLEAN:
96 *
97 * The fundamental type corresponding to #gboolean.
98 */
99#define G_TYPE_BOOLEAN G_TYPE_MAKE_FUNDAMENTAL (5)
100/**
101 * G_TYPE_INT:
102 *
103 * The fundamental type corresponding to #gint.
104 */
105#define G_TYPE_INT G_TYPE_MAKE_FUNDAMENTAL (6)
106/**
107 * G_TYPE_UINT:
108 *
109 * The fundamental type corresponding to #guint.
110 */
111#define G_TYPE_UINT G_TYPE_MAKE_FUNDAMENTAL (7)
112/**
113 * G_TYPE_LONG:
114 *
115 * The fundamental type corresponding to #glong.
116 */
117#define G_TYPE_LONG G_TYPE_MAKE_FUNDAMENTAL (8)
118/**
119 * G_TYPE_ULONG:
120 *
121 * The fundamental type corresponding to #gulong.
122 */
123#define G_TYPE_ULONG G_TYPE_MAKE_FUNDAMENTAL (9)
124/**
125 * G_TYPE_INT64:
126 *
127 * The fundamental type corresponding to #gint64.
128 */
129#define G_TYPE_INT64 G_TYPE_MAKE_FUNDAMENTAL (10)
130/**
131 * G_TYPE_UINT64:
132 *
133 * The fundamental type corresponding to #guint64.
134 */
135#define G_TYPE_UINT64 G_TYPE_MAKE_FUNDAMENTAL (11)
136/**
137 * G_TYPE_ENUM:
138 *
139 * The fundamental type from which all enumeration types are derived.
140 */
141#define G_TYPE_ENUM G_TYPE_MAKE_FUNDAMENTAL (12)
142/**
143 * G_TYPE_FLAGS:
144 *
145 * The fundamental type from which all flags types are derived.
146 */
147#define G_TYPE_FLAGS G_TYPE_MAKE_FUNDAMENTAL (13)
148/**
149 * G_TYPE_FLOAT:
150 *
151 * The fundamental type corresponding to #gfloat.
152 */
153#define G_TYPE_FLOAT G_TYPE_MAKE_FUNDAMENTAL (14)
154/**
155 * G_TYPE_DOUBLE:
156 *
157 * The fundamental type corresponding to #gdouble.
158 */
159#define G_TYPE_DOUBLE G_TYPE_MAKE_FUNDAMENTAL (15)
160/**
161 * G_TYPE_STRING:
162 *
163 * The fundamental type corresponding to nul-terminated C strings.
164 */
165#define G_TYPE_STRING G_TYPE_MAKE_FUNDAMENTAL (16)
166/**
167 * G_TYPE_POINTER:
168 *
169 * The fundamental type corresponding to #gpointer.
170 */
171#define G_TYPE_POINTER G_TYPE_MAKE_FUNDAMENTAL (17)
172/**
173 * G_TYPE_BOXED:
174 *
175 * The fundamental type from which all boxed types are derived.
176 */
177#define G_TYPE_BOXED G_TYPE_MAKE_FUNDAMENTAL (18)
178/**
179 * G_TYPE_PARAM:
180 *
181 * The fundamental type from which all #GParamSpec types are derived.
182 */
183#define G_TYPE_PARAM G_TYPE_MAKE_FUNDAMENTAL (19)
184/**
185 * G_TYPE_OBJECT:
186 *
187 * The fundamental type for #GObject.
188 */
189#define G_TYPE_OBJECT G_TYPE_MAKE_FUNDAMENTAL (20)
190/**
191 * G_TYPE_VARIANT:
192 *
193 * The fundamental type corresponding to #GVariant.
194 *
195 * All floating #GVariant instances passed through the #GType system are
196 * consumed.
197 *
198 * Note that callbacks in closures, and signal handlers
199 * for signals of return type %G_TYPE_VARIANT, must never return floating
200 * variants.
201 *
202 * Note: GLib 2.24 did include a boxed type with this name. It was replaced
203 * with this fundamental type in 2.26.
204 *
205 * Since: 2.26
206 */
207#define G_TYPE_VARIANT G_TYPE_MAKE_FUNDAMENTAL (21)
208
209
210/* Reserved fundamental type numbers to create new fundamental
211 * type IDs with G_TYPE_MAKE_FUNDAMENTAL().
212 *
213 * Open an issue on https://gitlab.gnome.org/GNOME/glib/issues/new for
214 * reservations.
215 */
216/**
217 * G_TYPE_MAKE_FUNDAMENTAL:
218 * @x: the fundamental type number.
219 *
220 * Get the type ID for the fundamental type number @x.
221 *
222 * Use g_type_fundamental_next() instead of this macro to create new fundamental
223 * types.
224 *
225 * Returns: the GType
226 */
227#define G_TYPE_MAKE_FUNDAMENTAL(x) ((GType) ((x) << G_TYPE_FUNDAMENTAL_SHIFT))
228/**
229 * G_TYPE_RESERVED_GLIB_FIRST:
230 *
231 * First fundamental type number to create a new fundamental type id with
232 * G_TYPE_MAKE_FUNDAMENTAL() reserved for GLib.
233 */
234#define G_TYPE_RESERVED_GLIB_FIRST (22)
235/**
236 * G_TYPE_RESERVED_GLIB_LAST:
237 *
238 * Last fundamental type number reserved for GLib.
239 */
240#define G_TYPE_RESERVED_GLIB_LAST (31)
241/**
242 * G_TYPE_RESERVED_BSE_FIRST:
243 *
244 * First fundamental type number to create a new fundamental type id with
245 * G_TYPE_MAKE_FUNDAMENTAL() reserved for BSE.
246 */
247#define G_TYPE_RESERVED_BSE_FIRST (32)
248/**
249 * G_TYPE_RESERVED_BSE_LAST:
250 *
251 * Last fundamental type number reserved for BSE.
252 */
253#define G_TYPE_RESERVED_BSE_LAST (48)
254/**
255 * G_TYPE_RESERVED_USER_FIRST:
256 *
257 * First available fundamental type number to create new fundamental
258 * type id with G_TYPE_MAKE_FUNDAMENTAL().
259 */
260#define G_TYPE_RESERVED_USER_FIRST (49)
261
262
263/* Type Checking Macros
264 */
265/**
266 * G_TYPE_IS_FUNDAMENTAL:
267 * @type: A #GType value
268 *
269 * Checks if @type is a fundamental type.
270 *
271 * Returns: %TRUE is @type is fundamental
272 */
273#define G_TYPE_IS_FUNDAMENTAL(type) ((type) <= G_TYPE_FUNDAMENTAL_MAX)
274/**
275 * G_TYPE_IS_DERIVED:
276 * @type: A #GType value
277 *
278 * Checks if @type is derived (or in object-oriented terminology:
279 * inherited) from another type (this holds true for all non-fundamental
280 * types).
281 *
282 * Returns: %TRUE if @type is derived
283 */
284#define G_TYPE_IS_DERIVED(type) ((type) > G_TYPE_FUNDAMENTAL_MAX)
285/**
286 * G_TYPE_IS_INTERFACE:
287 * @type: A #GType value
288 *
289 * Checks if @type is an interface type.
290 *
291 * An interface type provides a pure API, the implementation
292 * of which is provided by another type (which is then said to conform
293 * to the interface). GLib interfaces are somewhat analogous to Java
294 * interfaces and C++ classes containing only pure virtual functions,
295 * with the difference that GType interfaces are not derivable (but see
296 * g_type_interface_add_prerequisite() for an alternative).
297 *
298 * Returns: %TRUE if @type is an interface
299 */
300#define G_TYPE_IS_INTERFACE(type) (G_TYPE_FUNDAMENTAL (type) == G_TYPE_INTERFACE)
301/**
302 * G_TYPE_IS_CLASSED:
303 * @type: A #GType value
304 *
305 * Checks if @type is a classed type.
306 *
307 * A classed type has an associated #GTypeClass which can be derived to store
308 * class-wide virtual function pointers and data for all instances of the type.
309 * This allows for subclassing. All #GObjects are classed; none of the scalar
310 * fundamental types built into GLib are classed.
311 *
312 * Interfaces are not classed: while their #GTypeInterface struct could be
313 * considered similar to #GTypeClass, and classes can derive interfaces,
314 * #GTypeInterface doesn’t allow for subclassing.
315 *
316 * Returns: %TRUE if @type is classed
317 */
318#define G_TYPE_IS_CLASSED(type) (g_type_test_flags ((type), G_TYPE_FLAG_CLASSED))
319/**
320 * G_TYPE_IS_INSTANTIATABLE:
321 * @type: A #GType value
322 *
323 * Checks if @type can be instantiated. Instantiation is the
324 * process of creating an instance (object) of this type.
325 *
326 * Returns: %TRUE if @type is instantiatable
327 */
328#define G_TYPE_IS_INSTANTIATABLE(type) (g_type_test_flags ((type), G_TYPE_FLAG_INSTANTIATABLE))
329/**
330 * G_TYPE_IS_DERIVABLE:
331 * @type: A #GType value
332 *
333 * Checks if @type is a derivable type. A derivable type can
334 * be used as the base class of a flat (single-level) class hierarchy.
335 *
336 * Returns: %TRUE if @type is derivable
337 */
338#define G_TYPE_IS_DERIVABLE(type) (g_type_test_flags ((type), G_TYPE_FLAG_DERIVABLE))
339/**
340 * G_TYPE_IS_DEEP_DERIVABLE:
341 * @type: A #GType value
342 *
343 * Checks if @type is a deep derivable type. A deep derivable type
344 * can be used as the base class of a deep (multi-level) class hierarchy.
345 *
346 * Returns: %TRUE if @type is deep derivable
347 */
348#define G_TYPE_IS_DEEP_DERIVABLE(type) (g_type_test_flags ((type), G_TYPE_FLAG_DEEP_DERIVABLE))
349/**
350 * G_TYPE_IS_ABSTRACT:
351 * @type: A #GType value
352 *
353 * Checks if @type is an abstract type. An abstract type cannot be
354 * instantiated and is normally used as an abstract base class for
355 * derived classes.
356 *
357 * Returns: %TRUE if @type is abstract
358 */
359#define G_TYPE_IS_ABSTRACT(type) (g_type_test_flags ((type), G_TYPE_FLAG_ABSTRACT))
360/**
361 * G_TYPE_IS_VALUE_ABSTRACT:
362 * @type: A #GType value
363 *
364 * Checks if @type is an abstract value type. An abstract value type introduces
365 * a value table, but can't be used for g_value_init() and is normally used as
366 * an abstract base type for derived value types.
367 *
368 * Returns: %TRUE if @type is an abstract value type
369 */
370#define G_TYPE_IS_VALUE_ABSTRACT(type) (g_type_test_flags ((type), G_TYPE_FLAG_VALUE_ABSTRACT))
371/**
372 * G_TYPE_IS_VALUE_TYPE:
373 * @type: A #GType value
374 *
375 * Checks if @type is a value type and can be used with g_value_init().
376 *
377 * Returns: %TRUE if @type is a value type
378 */
379#define G_TYPE_IS_VALUE_TYPE(type) (g_type_check_is_value_type (type))
380/**
381 * G_TYPE_HAS_VALUE_TABLE:
382 * @type: A #GType value
383 *
384 * Checks if @type has a #GTypeValueTable.
385 *
386 * Returns: %TRUE if @type has a value table
387 */
388#define G_TYPE_HAS_VALUE_TABLE(type) (g_type_value_table_peek (type) != NULL)
389/**
390 * G_TYPE_IS_FINAL:
391 * @type: a #GType value
392 *
393 * Checks if @type is a final type. A final type cannot be derived any
394 * further.
395 *
396 * Returns: %TRUE if @type is final
397 *
398 * Since: 2.70
399 */
400#define G_TYPE_IS_FINAL(type) (g_type_test_flags ((type), G_TYPE_FLAG_FINAL)) GOBJECT_AVAILABLE_MACRO_IN_2_70
401
402/**
403 * G_TYPE_IS_DEPRECATED:
404 * @type: a #GType value
405 *
406 * Checks if @type is deprecated. Instantiating a deprecated type will
407 * trigger a warning if running with `G_ENABLE_DIAGNOSTIC=1`.
408 *
409 * Returns: %TRUE if the type is deprecated
410 *
411 * Since: 2.76
412 */
413#define G_TYPE_IS_DEPRECATED(type) (g_type_test_flags ((type), G_TYPE_FLAG_DEPRECATED)) GOBJECT_AVAILABLE_MACRO_IN_2_76
414
415
416/* Typedefs
417 */
418/**
419 * GType:
420 *
421 * A numerical value which represents the unique identifier of a registered
422 * type.
423 */
424#if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_SIZE_T
425typedef guintptr GType;
426#elif GLIB_SIZEOF_SIZE_T != GLIB_SIZEOF_LONG || !defined (G_CXX_STD_VERSION)
427typedef gsize GType;
428#else /* for historic reasons, C++ on non-Morello/CHERI systems links against gulong GTypes */
429typedef gulong GType;
430#endif
431typedef struct _GValue GValue;
432typedef union _GTypeCValue GTypeCValue;
433typedef struct _GTypePlugin GTypePlugin;
434typedef struct _GTypeClass GTypeClass;
435typedef struct _GTypeInterface GTypeInterface;
436typedef struct _GTypeInstance GTypeInstance;
437typedef struct _GTypeInfo GTypeInfo;
438typedef struct _GTypeFundamentalInfo GTypeFundamentalInfo;
439typedef struct _GInterfaceInfo GInterfaceInfo;
440typedef struct _GTypeValueTable GTypeValueTable;
441typedef struct _GTypeQuery GTypeQuery;
442
443
444/* Basic Type Structures
445 */
446/**
447 * GTypeClass:
448 *
449 * An opaque structure used as the base of all classes.
450 */
451struct _GTypeClass
452{
453 /*< private >*/
454 GType g_type;
455};
456/**
457 * GTypeInstance:
458 *
459 * An opaque structure used as the base of all type instances.
460 */
461struct _GTypeInstance
462{
463 /*< private >*/
464 GTypeClass *g_class;
465};
466/**
467 * GTypeInterface:
468 *
469 * An opaque structure used as the base of all interface types.
470 */
471struct _GTypeInterface
472{
473 /*< private >*/
474 GType g_type; /* iface type */
475 GType g_instance_type;
476};
477/**
478 * GTypeQuery:
479 * @type: the #GType value of the type
480 * @type_name: the name of the type
481 * @class_size: the size of the class structure
482 * @instance_size: the size of the instance structure
483 *
484 * A structure holding information for a specific type.
485 *
486 * See also: g_type_query()
487 */
488struct _GTypeQuery
489{
490 GType type;
491 const gchar *type_name;
492 guint class_size;
493 guint instance_size;
494};
495
496
497/* Casts, checks and accessors for structured types
498 * usage of these macros is reserved to type implementations only
499 */
500/*< protected >*/
501/**
502 * G_TYPE_CHECK_INSTANCE:
503 * @instance: Location of a #GTypeInstance structure
504 *
505 * Checks if @instance is a valid #GTypeInstance structure,
506 * otherwise issues a warning and returns %FALSE. %NULL is not a valid
507 * #GTypeInstance.
508 *
509 * This macro should only be used in type implementations.
510 *
511 * Returns: %TRUE if @instance is valid
512 */
513#define G_TYPE_CHECK_INSTANCE(instance) (_G_TYPE_CHI ((GTypeInstance*) (instance)))
514/**
515 * G_TYPE_CHECK_INSTANCE_CAST:
516 * @instance: (nullable): Location of a #GTypeInstance structure
517 * @g_type: The type to be returned
518 * @c_type: The corresponding C type of @g_type
519 *
520 * Checks that @instance is an instance of the type identified by @g_type
521 * and issues a warning if this is not the case. Returns @instance casted
522 * to a pointer to @c_type.
523 *
524 * No warning will be issued if @instance is %NULL, and %NULL will be returned.
525 *
526 * This macro should only be used in type implementations.
527 */
528#define G_TYPE_CHECK_INSTANCE_CAST(instance, g_type, c_type) (_G_TYPE_CIC ((instance), (g_type), c_type))
529/**
530 * G_TYPE_CHECK_INSTANCE_TYPE:
531 * @instance: (nullable): Location of a #GTypeInstance structure.
532 * @g_type: The type to be checked
533 *
534 * Checks if @instance is an instance of the type identified by @g_type. If
535 * @instance is %NULL, %FALSE will be returned.
536 *
537 * This macro should only be used in type implementations.
538 *
539 * Returns: %TRUE if @instance is an instance of @g_type
540 */
541#define G_TYPE_CHECK_INSTANCE_TYPE(instance, g_type) (_G_TYPE_CIT ((instance), (g_type)))
542/**
543 * G_TYPE_CHECK_INSTANCE_FUNDAMENTAL_TYPE:
544 * @instance: (nullable): Location of a #GTypeInstance structure.
545 * @g_type: The fundamental type to be checked
546 *
547 * Checks if @instance is an instance of the fundamental type identified by @g_type.
548 * If @instance is %NULL, %FALSE will be returned.
549 *
550 * This macro should only be used in type implementations.
551 *
552 * Returns: %TRUE if @instance is an instance of @g_type
553 */
554#define G_TYPE_CHECK_INSTANCE_FUNDAMENTAL_TYPE(instance, g_type) (_G_TYPE_CIFT ((instance), (g_type)))
555/**
556 * G_TYPE_INSTANCE_GET_CLASS:
557 * @instance: Location of the #GTypeInstance structure
558 * @g_type: The #GType of the class to be returned
559 * @c_type: The C type of the class structure
560 *
561 * Get the class structure of a given @instance, casted
562 * to a specified ancestor type @g_type of the instance.
563 *
564 * Note that while calling a GInstanceInitFunc(), the class pointer
565 * gets modified, so it might not always return the expected pointer.
566 *
567 * This macro should only be used in type implementations.
568 *
569 * Returns: a pointer to the class structure
570 */
571#define G_TYPE_INSTANCE_GET_CLASS(instance, g_type, c_type) (_G_TYPE_IGC ((instance), (g_type), c_type))
572/**
573 * G_TYPE_INSTANCE_GET_INTERFACE:
574 * @instance: Location of the #GTypeInstance structure
575 * @g_type: The #GType of the interface to be returned
576 * @c_type: The C type of the interface structure
577 *
578 * Get the interface structure for interface @g_type of a given @instance.
579 *
580 * This macro should only be used in type implementations.
581 *
582 * Returns: a pointer to the interface structure
583 */
584#define G_TYPE_INSTANCE_GET_INTERFACE(instance, g_type, c_type) (_G_TYPE_IGI ((instance), (g_type), c_type))
585/**
586 * G_TYPE_CHECK_CLASS_CAST:
587 * @g_class: Location of a #GTypeClass structure
588 * @g_type: The type to be returned
589 * @c_type: The corresponding C type of class structure of @g_type
590 *
591 * Checks that @g_class is a class structure of the type identified by @g_type
592 * and issues a warning if this is not the case. Returns @g_class casted
593 * to a pointer to @c_type. %NULL is not a valid class structure.
594 *
595 * This macro should only be used in type implementations.
596 */
597#define G_TYPE_CHECK_CLASS_CAST(g_class, g_type, c_type) (_G_TYPE_CCC ((g_class), (g_type), c_type))
598/**
599 * G_TYPE_CHECK_CLASS_TYPE:
600 * @g_class: (nullable): Location of a #GTypeClass structure
601 * @g_type: The type to be checked
602 *
603 * Checks if @g_class is a class structure of the type identified by
604 * @g_type. If @g_class is %NULL, %FALSE will be returned.
605 *
606 * This macro should only be used in type implementations.
607 *
608 * Returns: %TRUE if @g_class is a class structure of @g_type
609 */
610#define G_TYPE_CHECK_CLASS_TYPE(g_class, g_type) (_G_TYPE_CCT ((g_class), (g_type)))
611/**
612 * G_TYPE_CHECK_VALUE:
613 * @value: a #GValue
614 *
615 * Checks if @value has been initialized to hold values
616 * of a value type.
617 *
618 * This macro should only be used in type implementations.
619 *
620 * Returns: %TRUE if @value is initialized
621 */
622#define G_TYPE_CHECK_VALUE(value) (_G_TYPE_CHV ((value)))
623/**
624 * G_TYPE_CHECK_VALUE_TYPE:
625 * @value: a #GValue
626 * @g_type: The type to be checked
627 *
628 * Checks if @value has been initialized to hold values
629 * of type @g_type.
630 *
631 * This macro should only be used in type implementations.
632 *
633 * Returns: %TRUE if @value has been initialized to hold values of type @g_type
634 */
635#define G_TYPE_CHECK_VALUE_TYPE(value, g_type) (_G_TYPE_CVH ((value), (g_type)))
636/**
637 * G_TYPE_FROM_INSTANCE:
638 * @instance: Location of a valid #GTypeInstance structure
639 *
640 * Get the type identifier from a given @instance structure.
641 *
642 * This macro should only be used in type implementations.
643 *
644 * Returns: the #GType
645 */
646#define G_TYPE_FROM_INSTANCE(instance) (G_TYPE_FROM_CLASS (((GTypeInstance*) (instance))->g_class))
647/**
648 * G_TYPE_FROM_CLASS:
649 * @g_class: Location of a valid #GTypeClass structure
650 *
651 * Get the type identifier from a given @class structure.
652 *
653 * This macro should only be used in type implementations.
654 *
655 * Returns: the #GType
656 */
657#define G_TYPE_FROM_CLASS(g_class) (((GTypeClass*) (g_class))->g_type)
658/**
659 * G_TYPE_FROM_INTERFACE:
660 * @g_iface: Location of a valid #GTypeInterface structure
661 *
662 * Get the type identifier from a given @interface structure.
663 *
664 * This macro should only be used in type implementations.
665 *
666 * Returns: the #GType
667 */
668#define G_TYPE_FROM_INTERFACE(g_iface) (((GTypeInterface*) (g_iface))->g_type)
669
670/**
671 * G_TYPE_INSTANCE_GET_PRIVATE:
672 * @instance: the instance of a type deriving from @private_type
673 * @g_type: the type identifying which private data to retrieve
674 * @c_type: The C type for the private structure
675 *
676 * Gets the private structure for a particular type.
677 *
678 * The private structure must have been registered in the
679 * class_init function with g_type_class_add_private().
680 *
681 * This macro should only be used in type implementations.
682 *
683 * Since: 2.4
684 * Deprecated: 2.58: Use G_ADD_PRIVATE() and the generated
685 * `your_type_get_instance_private()` function instead
686 * Returns: (not nullable): a pointer to the private data structure
687 */
688#define G_TYPE_INSTANCE_GET_PRIVATE(instance, g_type, c_type) ((c_type*) g_type_instance_get_private ((GTypeInstance*) (instance), (g_type))) GOBJECT_DEPRECATED_MACRO_IN_2_58_FOR(G_ADD_PRIVATE)
689
690/**
691 * G_TYPE_CLASS_GET_PRIVATE:
692 * @klass: the class of a type deriving from @private_type
693 * @g_type: the type identifying which private data to retrieve
694 * @c_type: The C type for the private structure
695 *
696 * Gets the private class structure for a particular type.
697 *
698 * The private structure must have been registered in the
699 * get_type() function with g_type_add_class_private().
700 *
701 * This macro should only be used in type implementations.
702 *
703 * Since: 2.24
704 * Returns: (not nullable): a pointer to the private data structure
705 */
706#define G_TYPE_CLASS_GET_PRIVATE(klass, g_type, c_type) ((c_type*) g_type_class_get_private ((GTypeClass*) (klass), (g_type)))
707
708/**
709 * GTypeDebugFlags:
710 * @G_TYPE_DEBUG_NONE: Print no messages
711 * @G_TYPE_DEBUG_OBJECTS: Print messages about object bookkeeping
712 * @G_TYPE_DEBUG_SIGNALS: Print messages about signal emissions
713 * @G_TYPE_DEBUG_MASK: Mask covering all debug flags
714 * @G_TYPE_DEBUG_INSTANCE_COUNT: Keep a count of instances of each type
715 *
716 * These flags used to be passed to g_type_init_with_debug_flags() which
717 * is now deprecated.
718 *
719 * If you need to enable debugging features, use the `GOBJECT_DEBUG`
720 * environment variable.
721 *
722 * Deprecated: 2.36: g_type_init() is now done automatically
723 */
724typedef enum /*< skip >*/
725{
726 G_TYPE_DEBUG_NONE = 0,
727 G_TYPE_DEBUG_OBJECTS = 1 << 0,
728 G_TYPE_DEBUG_SIGNALS = 1 << 1,
729 G_TYPE_DEBUG_INSTANCE_COUNT = 1 << 2,
730 G_TYPE_DEBUG_MASK = 0x07
731} G_GNUC_FLAG_ENUM GTypeDebugFlags GOBJECT_DEPRECATED_TYPE_IN_2_36;
732
733
734/* --- prototypes --- */
735G_GNUC_BEGIN_IGNORE_DEPRECATIONS
736GOBJECT_DEPRECATED_IN_2_36
737void g_type_init (void);
738GOBJECT_DEPRECATED_IN_2_36
739void g_type_init_with_debug_flags (GTypeDebugFlags debug_flags);
740G_GNUC_END_IGNORE_DEPRECATIONS
741
742GOBJECT_AVAILABLE_IN_ALL
743const gchar * g_type_name (GType type);
744GOBJECT_AVAILABLE_IN_ALL
745GQuark g_type_qname (GType type);
746GOBJECT_AVAILABLE_IN_ALL
747GType g_type_from_name (const gchar *name);
748GOBJECT_AVAILABLE_IN_ALL
749GType g_type_parent (GType type);
750GOBJECT_AVAILABLE_IN_ALL
751guint g_type_depth (GType type);
752GOBJECT_AVAILABLE_IN_ALL
753GType g_type_next_base (GType leaf_type,
754 GType root_type);
755GOBJECT_AVAILABLE_IN_ALL
756gboolean g_type_is_a (GType type,
757 GType is_a_type);
758
759/* Hoist exact GType comparisons into the caller */
760#define g_type_is_a(a,b) ((a) == (b) || (g_type_is_a) ((a), (b)))
761
762GOBJECT_AVAILABLE_IN_2_84
763gpointer g_type_class_get (GType type);
764GOBJECT_AVAILABLE_IN_ALL
765gpointer g_type_class_ref (GType type);
766GOBJECT_AVAILABLE_IN_ALL
767gpointer g_type_class_peek (GType type);
768GOBJECT_AVAILABLE_IN_ALL
769gpointer g_type_class_peek_static (GType type);
770GOBJECT_AVAILABLE_IN_ALL
771void g_type_class_unref (gpointer g_class);
772GOBJECT_AVAILABLE_IN_ALL
773gpointer g_type_class_peek_parent (gpointer g_class);
774GOBJECT_AVAILABLE_IN_ALL
775gpointer g_type_interface_peek (gpointer instance_class,
776 GType iface_type);
777GOBJECT_AVAILABLE_IN_ALL
778gpointer g_type_interface_peek_parent (gpointer g_iface);
779
780GOBJECT_AVAILABLE_IN_2_84
781gpointer g_type_default_interface_get (GType g_type);
782GOBJECT_AVAILABLE_IN_ALL
783gpointer g_type_default_interface_ref (GType g_type);
784GOBJECT_AVAILABLE_IN_ALL
785gpointer g_type_default_interface_peek (GType g_type);
786GOBJECT_AVAILABLE_IN_ALL
787void g_type_default_interface_unref (gpointer g_iface);
788
789/* g_free() the returned arrays */
790GOBJECT_AVAILABLE_IN_ALL
791GType* g_type_children (GType type,
792 guint *n_children);
793GOBJECT_AVAILABLE_IN_ALL
794GType* g_type_interfaces (GType type,
795 guint *n_interfaces);
796
797/* per-type _static_ data */
798GOBJECT_AVAILABLE_IN_ALL
799void g_type_set_qdata (GType type,
800 GQuark quark,
801 gpointer data);
802GOBJECT_AVAILABLE_IN_ALL
803gpointer g_type_get_qdata (GType type,
804 GQuark quark);
805GOBJECT_AVAILABLE_IN_ALL
806void g_type_query (GType type,
807 GTypeQuery *query);
808
809GOBJECT_AVAILABLE_IN_2_44
810int g_type_get_instance_count (GType type);
811
812/* --- type registration --- */
813/**
814 * GBaseInitFunc:
815 * @g_class: (type GObject.TypeClass): The #GTypeClass structure to initialize
816 *
817 * A callback function used by the type system to do base initialization
818 * of the class structures of derived types.
819 *
820 * This function is called as part of the initialization process of all derived
821 * classes and should reallocate or reset all dynamic class members copied over
822 * from the parent class.
823 *
824 * For example, class members (such as strings) that are not sufficiently
825 * handled by a plain memory copy of the parent class into the derived class
826 * have to be altered. See GClassInitFunc() for a discussion of the class
827 * initialization process.
828 */
829typedef void (*GBaseInitFunc) (gpointer g_class);
830/**
831 * GBaseFinalizeFunc:
832 * @g_class: (type GObject.TypeClass): The #GTypeClass structure to finalize
833 *
834 * A callback function used by the type system to finalize those portions
835 * of a derived types class structure that were setup from the corresponding
836 * GBaseInitFunc() function.
837 *
838 * Class finalization basically works the inverse way in which class
839 * initialization is performed.
840 *
841 * See GClassInitFunc() for a discussion of the class initialization process.
842 */
843typedef void (*GBaseFinalizeFunc) (gpointer g_class);
844/**
845 * GClassInitFunc:
846 * @g_class: (type GObject.TypeClass): The #GTypeClass structure to initialize.
847 * @class_data: The @class_data member supplied via the #GTypeInfo structure.
848 *
849 * A callback function used by the type system to initialize the class
850 * of a specific type.
851 *
852 * This function should initialize all static class members.
853 *
854 * The initialization process of a class involves:
855 *
856 * - Copying common members from the parent class over to the
857 * derived class structure.
858 * - Zero initialization of the remaining members not copied
859 * over from the parent class.
860 * - Invocation of the GBaseInitFunc() initializers of all parent
861 * types and the class' type.
862 * - Invocation of the class' GClassInitFunc() initializer.
863 *
864 * Since derived classes are partially initialized through a memory copy
865 * of the parent class, the general rule is that GBaseInitFunc() and
866 * GBaseFinalizeFunc() should take care of necessary reinitialization
867 * and release of those class members that were introduced by the type
868 * that specified these GBaseInitFunc()/GBaseFinalizeFunc().
869 * GClassInitFunc() should only care about initializing static
870 * class members, while dynamic class members (such as allocated strings
871 * or reference counted resources) are better handled by a GBaseInitFunc()
872 * for this type, so proper initialization of the dynamic class members
873 * is performed for class initialization of derived types as well.
874 *
875 * An example may help to correspond the intend of the different class
876 * initializers:
877 *
878 * |[<!-- language="C" -->
879 * typedef struct {
880 * GObjectClass parent_class;
881 * gint static_integer;
882 * gchar *dynamic_string;
883 * } TypeAClass;
884 * static void
885 * type_a_base_class_init (TypeAClass *class)
886 * {
887 * class->dynamic_string = g_strdup ("some string");
888 * }
889 * static void
890 * type_a_base_class_finalize (TypeAClass *class)
891 * {
892 * g_free (class->dynamic_string);
893 * }
894 * static void
895 * type_a_class_init (TypeAClass *class)
896 * {
897 * class->static_integer = 42;
898 * }
899 *
900 * typedef struct {
901 * TypeAClass parent_class;
902 * gfloat static_float;
903 * GString *dynamic_gstring;
904 * } TypeBClass;
905 * static void
906 * type_b_base_class_init (TypeBClass *class)
907 * {
908 * class->dynamic_gstring = g_string_new ("some other string");
909 * }
910 * static void
911 * type_b_base_class_finalize (TypeBClass *class)
912 * {
913 * g_string_free (class->dynamic_gstring);
914 * }
915 * static void
916 * type_b_class_init (TypeBClass *class)
917 * {
918 * class->static_float = 3.14159265358979323846;
919 * }
920 * ]|
921 *
922 * Initialization of TypeBClass will first cause initialization of
923 * TypeAClass (derived classes reference their parent classes, see
924 * g_type_class_ref() on this).
925 *
926 * Initialization of TypeAClass roughly involves zero-initializing its fields,
927 * then calling its GBaseInitFunc() type_a_base_class_init() to allocate
928 * its dynamic members (dynamic_string), and finally calling its GClassInitFunc()
929 * type_a_class_init() to initialize its static members (static_integer).
930 * The first step in the initialization process of TypeBClass is then
931 * a plain memory copy of the contents of TypeAClass into TypeBClass and
932 * zero-initialization of the remaining fields in TypeBClass.
933 * The dynamic members of TypeAClass within TypeBClass now need
934 * reinitialization which is performed by calling type_a_base_class_init()
935 * with an argument of TypeBClass.
936 *
937 * After that, the GBaseInitFunc() of TypeBClass, type_b_base_class_init()
938 * is called to allocate the dynamic members of TypeBClass (dynamic_gstring),
939 * and finally the GClassInitFunc() of TypeBClass, type_b_class_init(),
940 * is called to complete the initialization process with the static members
941 * (static_float).
942 *
943 * Corresponding finalization counter parts to the GBaseInitFunc() functions
944 * have to be provided to release allocated resources at class finalization
945 * time.
946 */
947typedef void (*GClassInitFunc) (gpointer g_class,
948 gpointer class_data);
949/**
950 * GClassFinalizeFunc:
951 * @g_class: (type GObject.TypeClass): The #GTypeClass structure to finalize
952 * @class_data: The @class_data member supplied via the #GTypeInfo structure
953 *
954 * A callback function used by the type system to finalize a class.
955 *
956 * This function is rarely needed, as dynamically allocated class resources
957 * should be handled by GBaseInitFunc() and GBaseFinalizeFunc().
958 *
959 * Also, specification of a GClassFinalizeFunc() in the #GTypeInfo
960 * structure of a static type is invalid, because classes of static types
961 * will never be finalized (they are artificially kept alive when their
962 * reference count drops to zero).
963 */
964typedef void (*GClassFinalizeFunc) (gpointer g_class,
965 gpointer class_data);
966/**
967 * GInstanceInitFunc:
968 * @instance: The instance to initialize
969 * @g_class: (type GObject.TypeClass): The class of the type the instance is
970 * created for
971 *
972 * A callback function used by the type system to initialize a new
973 * instance of a type.
974 *
975 * This function initializes all instance members and allocates any resources
976 * required by it.
977 *
978 * Initialization of a derived instance involves calling all its parent
979 * types instance initializers, so the class member of the instance
980 * is altered during its initialization to always point to the class that
981 * belongs to the type the current initializer was introduced for.
982 *
983 * The extended members of @instance are guaranteed to have been filled with
984 * zeros before this function is called.
985 */
986typedef void (*GInstanceInitFunc) (GTypeInstance *instance,
987 gpointer g_class);
988/**
989 * GInterfaceInitFunc:
990 * @g_iface: (type GObject.TypeInterface): The interface structure to initialize
991 * @iface_data: The @interface_data supplied via the #GInterfaceInfo structure
992 *
993 * A callback function used by the type system to initialize a new
994 * interface.
995 *
996 * This function should initialize all internal data and* allocate any
997 * resources required by the interface.
998 *
999 * The members of @iface_data are guaranteed to have been filled with
1000 * zeros before this function is called.
1001 */
1002typedef void (*GInterfaceInitFunc) (gpointer g_iface,
1003 gpointer iface_data);
1004/**
1005 * GInterfaceFinalizeFunc:
1006 * @g_iface: (type GObject.TypeInterface): The interface structure to finalize
1007 * @iface_data: The @interface_data supplied via the #GInterfaceInfo structure
1008 *
1009 * A callback function used by the type system to finalize an interface.
1010 *
1011 * This function should destroy any internal data and release any resources
1012 * allocated by the corresponding GInterfaceInitFunc() function.
1013 */
1014typedef void (*GInterfaceFinalizeFunc) (gpointer g_iface,
1015 gpointer iface_data);
1016/**
1017 * GTypeClassCacheFunc:
1018 * @cache_data: data that was given to the g_type_add_class_cache_func() call
1019 * @g_class: (type GObject.TypeClass): The #GTypeClass structure which is
1020 * unreferenced
1021 *
1022 * A callback function which is called when the reference count of a class
1023 * drops to zero.
1024 *
1025 * It may use g_type_class_ref() to prevent the class from being freed. You
1026 * should not call g_type_class_unref() from a #GTypeClassCacheFunc function
1027 * to prevent infinite recursion, use g_type_class_unref_uncached() instead.
1028 *
1029 * The functions have to check the class id passed in to figure
1030 * whether they actually want to cache the class of this type, since all
1031 * classes are routed through the same #GTypeClassCacheFunc chain.
1032 *
1033 * Returns: %TRUE to stop further #GTypeClassCacheFuncs from being
1034 * called, %FALSE to continue
1035 */
1036typedef gboolean (*GTypeClassCacheFunc) (gpointer cache_data,
1037 GTypeClass *g_class);
1038/**
1039 * GTypeInterfaceCheckFunc:
1040 * @check_data: data passed to g_type_add_interface_check()
1041 * @g_iface: (type GObject.TypeInterface): the interface that has been
1042 * initialized
1043 *
1044 * A callback called after an interface vtable is initialized.
1045 *
1046 * See g_type_add_interface_check().
1047 *
1048 * Since: 2.4
1049 */
1050typedef void (*GTypeInterfaceCheckFunc) (gpointer check_data,
1051 gpointer g_iface);
1052/**
1053 * GTypeFundamentalFlags:
1054 * @G_TYPE_FLAG_CLASSED: Indicates a classed type
1055 * @G_TYPE_FLAG_INSTANTIATABLE: Indicates an instantiatable type (implies classed)
1056 * @G_TYPE_FLAG_DERIVABLE: Indicates a flat derivable type
1057 * @G_TYPE_FLAG_DEEP_DERIVABLE: Indicates a deep derivable type (implies derivable)
1058 *
1059 * Bit masks used to check or determine specific characteristics of a
1060 * fundamental type.
1061 */
1062typedef enum /*< skip >*/
1063{
1064 /* There is no G_TYPE_FUNDAMENTAL_FLAGS_NONE: this is implemented to use
1065 * the same bits as GTypeFlags */
1066 G_TYPE_FLAG_CLASSED = (1 << 0),
1067 G_TYPE_FLAG_INSTANTIATABLE = (1 << 1),
1068 G_TYPE_FLAG_DERIVABLE = (1 << 2),
1069 G_TYPE_FLAG_DEEP_DERIVABLE = (1 << 3)
1070} G_GNUC_FLAG_ENUM GTypeFundamentalFlags;
1071/**
1072 * GTypeFlags:
1073 * @G_TYPE_FLAG_ABSTRACT: Indicates an abstract type. No instances can be
1074 * created for an abstract type
1075 * @G_TYPE_FLAG_VALUE_ABSTRACT: Indicates an abstract value type, i.e. a type
1076 * that introduces a value table, but can't be used for
1077 * g_value_init()
1078 * @G_TYPE_FLAG_FINAL: Indicates a final type. A final type is a non-derivable
1079 * leaf node in a deep derivable type hierarchy tree. Since: 2.70
1080 * @G_TYPE_FLAG_DEPRECATED: The type is deprecated and may be removed in a
1081 * future version. A warning will be emitted if it is instantiated while
1082 * running with `G_ENABLE_DIAGNOSTIC=1`. Since 2.76
1083 *
1084 * Bit masks used to check or determine characteristics of a type.
1085 */
1086typedef enum /*< skip >*/
1087{
1088 /**
1089 * G_TYPE_FLAG_NONE:
1090 *
1091 * No special flags.
1092 *
1093 * Since: 2.74
1094 */
1095 G_TYPE_FLAG_NONE GOBJECT_AVAILABLE_ENUMERATOR_IN_2_74 = 0,
1096 G_TYPE_FLAG_ABSTRACT = (1 << 4),
1097 G_TYPE_FLAG_VALUE_ABSTRACT = (1 << 5),
1098 G_TYPE_FLAG_FINAL GOBJECT_AVAILABLE_ENUMERATOR_IN_2_70 = (1 << 6),
1099 G_TYPE_FLAG_DEPRECATED GOBJECT_AVAILABLE_ENUMERATOR_IN_2_76 = (1 << 7)
1100} G_GNUC_FLAG_ENUM GTypeFlags;
1101/**
1102 * GTypeInfo:
1103 * @class_size: Size of the class structure (required for interface, classed and instantiatable types)
1104 * @base_init: Location of the base initialization function (optional)
1105 * @base_finalize: Location of the base finalization function (optional)
1106 * @class_init: Location of the class initialization function for
1107 * classed and instantiatable types. Location of the default vtable
1108 * initialization function for interface types. (optional) This function
1109 * is used both to fill in virtual functions in the class or default vtable,
1110 * and to do type-specific setup such as registering signals and object
1111 * properties.
1112 * @class_finalize: Location of the class finalization function for
1113 * classed and instantiatable types. Location of the default vtable
1114 * finalization function for interface types. (optional)
1115 * @class_data: User-supplied data passed to the class init/finalize functions
1116 * @instance_size: Size of the instance (object) structure (required for instantiatable types only)
1117 * @n_preallocs: Prior to GLib 2.10, it specified the number of pre-allocated (cached) instances to reserve memory for (0 indicates no caching). Since GLib 2.10 this field is ignored.
1118 * @instance_init: Location of the instance initialization function (optional, for instantiatable types only)
1119 * @value_table: A #GTypeValueTable function table for generic handling of GValues
1120 * of this type (usually only useful for fundamental types)
1121 *
1122 * This structure is used to provide the type system with the information
1123 * required to initialize and destruct (finalize) a type's class and
1124 * its instances.
1125 *
1126 * The initialized structure is passed to the g_type_register_static() function
1127 * (or is copied into the provided #GTypeInfo structure in the
1128 * g_type_plugin_complete_type_info()). The type system will perform a deep
1129 * copy of this structure, so its memory does not need to be persistent
1130 * across invocation of g_type_register_static().
1131 */
1132struct _GTypeInfo
1133{
1134 /* interface types, classed types, instantiated types */
1135 guint16 class_size;
1136
1137 GBaseInitFunc base_init;
1138 GBaseFinalizeFunc base_finalize;
1139
1140 /* interface types, classed types, instantiated types */
1141 GClassInitFunc class_init;
1142 GClassFinalizeFunc class_finalize;
1143 gconstpointer class_data;
1144
1145 /* instantiated types */
1146 guint16 instance_size;
1147 guint16 n_preallocs;
1148 GInstanceInitFunc instance_init;
1149
1150 /* value handling */
1151 const GTypeValueTable *value_table;
1152};
1153/**
1154 * GTypeFundamentalInfo:
1155 * @type_flags: #GTypeFundamentalFlags describing the characteristics of the fundamental type
1156 *
1157 * A structure that provides information to the type system which is
1158 * used specifically for managing fundamental types.
1159 */
1160struct _GTypeFundamentalInfo
1161{
1162 GTypeFundamentalFlags type_flags;
1163};
1164/**
1165 * GInterfaceInfo:
1166 * @interface_init: location of the interface initialization function
1167 * @interface_finalize: location of the interface finalization function
1168 * @interface_data: user-supplied data passed to the interface init/finalize functions
1169 *
1170 * A structure that provides information to the type system which is
1171 * used specifically for managing interface types.
1172 */
1173struct _GInterfaceInfo
1174{
1175 GInterfaceInitFunc interface_init;
1176 GInterfaceFinalizeFunc interface_finalize;
1177 gpointer interface_data;
1178};
1179
1180/**
1181 * GTypeValueInitFunc:
1182 * @value: the value to initialize
1183 *
1184 * Initializes the value contents by setting the fields of the `value->data`
1185 * array.
1186 *
1187 * The data array of the #GValue passed into this function was zero-filled
1188 * with `memset()`, so no care has to be taken to free any old contents.
1189 * For example, in the case of a string value that may never be %NULL, the
1190 * implementation might look like:
1191 *
1192 * |[<!-- language="C" -->
1193 * value->data[0].v_pointer = g_strdup ("");
1194 * ]|
1195 *
1196 * Since: 2.78
1197 */
1198GOBJECT_AVAILABLE_TYPE_IN_2_78
1199typedef void (* GTypeValueInitFunc) (GValue *value);
1200
1201/**
1202 * GTypeValueFreeFunc:
1203 * @value: the value to free
1204 *
1205 * Frees any old contents that might be left in the `value->data` array of
1206 * the given value.
1207 *
1208 * No resources may remain allocated through the #GValue contents after this
1209 * function returns. E.g. for our above string type:
1210 *
1211 * |[<!-- language="C" -->
1212 * // only free strings without a specific flag for static storage
1213 * if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
1214 * g_free (value->data[0].v_pointer);
1215 * ]|
1216 *
1217 * Since: 2.78
1218 */
1219GOBJECT_AVAILABLE_TYPE_IN_2_78
1220typedef void (* GTypeValueFreeFunc) (GValue *value);
1221
1222/**
1223 * GTypeValueCopyFunc:
1224 * @src_value: the value to copy
1225 * @dest_value: (out): the location of the copy
1226 *
1227 * Copies the content of a #GValue into another.
1228 *
1229 * The @dest_value is a #GValue with zero-filled data section and @src_value
1230 * is a properly initialized #GValue of same type, or derived type.
1231 *
1232 * The purpose of this function is to copy the contents of @src_value
1233 * into @dest_value in a way, that even after @src_value has been freed, the
1234 * contents of @dest_value remain valid. String type example:
1235 *
1236 * |[<!-- language="C" -->
1237 * dest_value->data[0].v_pointer = g_strdup (src_value->data[0].v_pointer);
1238 * ]|
1239 *
1240 * Since: 2.78
1241 */
1242GOBJECT_AVAILABLE_TYPE_IN_2_78
1243typedef void (* GTypeValueCopyFunc) (const GValue *src_value,
1244 GValue *dest_value);
1245
1246/**
1247 * GTypeValuePeekPointerFunc:
1248 * @value: the value to peek
1249 *
1250 * If the value contents fit into a pointer, such as objects or strings,
1251 * return this pointer, so the caller can peek at the current contents.
1252 *
1253 * To extend on our above string example:
1254 *
1255 * |[<!-- language="C" -->
1256 * return value->data[0].v_pointer;
1257 * ]|
1258 *
1259 * Returns: (transfer none): a pointer to the value contents
1260 *
1261 * Since: 2.78
1262 */
1263GOBJECT_AVAILABLE_TYPE_IN_2_78
1264typedef gpointer (* GTypeValuePeekPointerFunc) (const GValue *value);
1265
1266/**
1267 * GTypeValueCollectFunc:
1268 * @value: the value to initialize
1269 * @n_collect_values: the number of collected values
1270 * @collect_values: (array length=n_collect_values): the collected values
1271 * @collect_flags: optional flags
1272 *
1273 * This function is responsible for converting the values collected from
1274 * a variadic argument list into contents suitable for storage in a #GValue.
1275 *
1276 * This function should setup @value similar to #GTypeValueInitFunc; e.g.
1277 * for a string value that does not allow `NULL` pointers, it needs to either
1278 * emit an error, or do an implicit conversion by storing an empty string.
1279 *
1280 * The @value passed in to this function has a zero-filled data array, so
1281 * just like for #GTypeValueInitFunc it is guaranteed to not contain any old
1282 * contents that might need freeing.
1283 *
1284 * The @n_collect_values argument is the string length of the `collect_format`
1285 * field of #GTypeValueTable, and `collect_values` is an array of #GTypeCValue
1286 * with length of @n_collect_values, containing the collected values according
1287 * to `collect_format`.
1288 *
1289 * The @collect_flags argument provided as a hint by the caller. It may
1290 * contain the flag %G_VALUE_NOCOPY_CONTENTS indicating that the collected
1291 * value contents may be considered ‘static’ for the duration of the @value
1292 * lifetime. Thus an extra copy of the contents stored in @collect_values is
1293 * not required for assignment to @value.
1294 *
1295 * For our above string example, we continue with:
1296 *
1297 * |[<!-- language="C" -->
1298 * if (!collect_values[0].v_pointer)
1299 * value->data[0].v_pointer = g_strdup ("");
1300 * else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
1301 * {
1302 * value->data[0].v_pointer = collect_values[0].v_pointer;
1303 * // keep a flag for the value_free() implementation to not free this string
1304 * value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
1305 * }
1306 * else
1307 * value->data[0].v_pointer = g_strdup (collect_values[0].v_pointer);
1308 * return NULL;
1309 * ]|
1310 *
1311 * It should be noted, that it is generally a bad idea to follow the
1312 * %G_VALUE_NOCOPY_CONTENTS hint for reference counted types. Due to
1313 * reentrancy requirements and reference count assertions performed
1314 * by the signal emission code, reference counts should always be
1315 * incremented for reference counted contents stored in the `value->data`
1316 * array. To deviate from our string example for a moment, and taking
1317 * a look at an exemplary implementation for `GTypeValueTable.collect_value()`
1318 * of `GObject`:
1319 *
1320 * |[<!-- language="C" -->
1321 * GObject *object = G_OBJECT (collect_values[0].v_pointer);
1322 * g_return_val_if_fail (object != NULL,
1323 * g_strdup_printf ("Object %p passed as invalid NULL pointer", object));
1324 * // never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types
1325 * value->data[0].v_pointer = g_object_ref (object);
1326 * return NULL;
1327 * ]|
1328 *
1329 * The reference count for valid objects is always incremented, regardless
1330 * of `collect_flags`. For invalid objects, the example returns a newly
1331 * allocated string without altering `value`.
1332 *
1333 * Upon success, `collect_value()` needs to return `NULL`. If, however,
1334 * an error condition occurred, `collect_value()` should return a newly
1335 * allocated string containing an error diagnostic.
1336 *
1337 * The calling code makes no assumptions about the `value` contents being
1338 * valid upon error returns, `value` is simply thrown away without further
1339 * freeing. As such, it is a good idea to not allocate `GValue` contents
1340 * prior to returning an error; however, `collect_values()` is not obliged
1341 * to return a correctly setup @value for error returns, simply because
1342 * any non-`NULL` return is considered a fatal programming error, and
1343 * further program behaviour is undefined.
1344 *
1345 * Returns: (transfer full) (nullable): `NULL` on success, otherwise a
1346 * newly allocated error string on failure
1347 *
1348 * Since: 2.78
1349 */
1350GOBJECT_AVAILABLE_TYPE_IN_2_78
1351typedef gchar * (* GTypeValueCollectFunc) (GValue *value,
1352 guint n_collect_values,
1353 GTypeCValue *collect_values,
1354 guint collect_flags);
1355
1356/**
1357 * GTypeValueLCopyFunc:
1358 * @value: the value to lcopy
1359 * @n_collect_values: the number of collected values
1360 * @collect_values: (array length=n_collect_values): the collected
1361 * locations for storage
1362 * @collect_flags: optional flags
1363 *
1364 * This function is responsible for storing the `value`
1365 * contents into arguments passed through a variadic argument list which
1366 * got collected into `collect_values` according to `lcopy_format`.
1367 *
1368 * The `n_collect_values` argument equals the string length of
1369 * `lcopy_format`, and `collect_flags` may contain %G_VALUE_NOCOPY_CONTENTS.
1370 *
1371 * In contrast to #GTypeValueCollectFunc, this function is obliged to always
1372 * properly support %G_VALUE_NOCOPY_CONTENTS.
1373 *
1374 * Similar to #GTypeValueCollectFunc the function may prematurely abort by
1375 * returning a newly allocated string describing an error condition. To
1376 * complete the string example:
1377 *
1378 * |[<!-- language="C" -->
1379 * gchar **string_p = collect_values[0].v_pointer;
1380 * g_return_val_if_fail (string_p != NULL,
1381 * g_strdup ("string location passed as NULL"));
1382 *
1383 * if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
1384 * *string_p = value->data[0].v_pointer;
1385 * else
1386 * *string_p = g_strdup (value->data[0].v_pointer);
1387 * ]|
1388 *
1389 * And an illustrative version of this function for reference-counted
1390 * types:
1391 *
1392 * |[<!-- language="C" -->
1393 * GObject **object_p = collect_values[0].v_pointer;
1394 * g_return_val_if_fail (object_p != NULL,
1395 * g_strdup ("object location passed as NULL"));
1396 *
1397 * if (value->data[0].v_pointer == NULL)
1398 * *object_p = NULL;
1399 * else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) // always honour
1400 * *object_p = value->data[0].v_pointer;
1401 * else
1402 * *object_p = g_object_ref (value->data[0].v_pointer);
1403 *
1404 * return NULL;
1405 * ]|
1406 *
1407 * Returns: (transfer full) (nullable): `NULL` on success, otherwise
1408 * a newly allocated error string on failure
1409 *
1410 * Since: 2.78
1411 */
1412GOBJECT_AVAILABLE_TYPE_IN_2_78
1413typedef gchar * (* GTypeValueLCopyFunc) (const GValue *value,
1414 guint n_collect_values,
1415 GTypeCValue *collect_values,
1416 guint collect_flags);
1417
1418/**
1419 * GTypeValueTable:
1420 * @value_init: Function to initialize a GValue
1421 * @value_free: Function to free a GValue
1422 * @value_copy: Function to copy a GValue
1423 * @value_peek_pointer: Function to peek the contents of a GValue if they fit
1424 * into a pointer
1425 * @collect_format: A string format describing how to collect the contents of
1426 * this value bit-by-bit. Each character in the format represents
1427 * an argument to be collected, and the characters themselves indicate
1428 * the type of the argument. Currently supported arguments are:
1429 *
1430 * - `'i'`: Integers, passed as `collect_values[].v_int`
1431 * - `'l'`: Longs, passed as `collect_values[].v_long`
1432 * - `'d'`: Doubles, passed as `collect_values[].v_double`
1433 * - `'p'`: Pointers, passed as `collect_values[].v_pointer`
1434 *
1435 * It should be noted that for variable argument list construction,
1436 * ANSI C promotes every type smaller than an integer to an int, and
1437 * floats to doubles. So for collection of short int or char, `'i'`
1438 * needs to be used, and for collection of floats `'d'`.
1439 * @collect_value: Function to initialize a GValue from the values
1440 * collected from variadic arguments
1441 * @lcopy_format: Format description of the arguments to collect for @lcopy_value,
1442 * analogous to @collect_format. Usually, @lcopy_format string consists
1443 * only of `'p'`s to provide lcopy_value() with pointers to storage locations.
1444 * @lcopy_value: Function to store the contents of a value into the
1445 * locations collected from variadic arguments
1446 *
1447 * The #GTypeValueTable provides the functions required by the #GValue
1448 * implementation, to serve as a container for values of a type.
1449 */
1450G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1451struct _GTypeValueTable
1452{
1453 GTypeValueInitFunc value_init;
1454 GTypeValueFreeFunc value_free;
1455 GTypeValueCopyFunc value_copy;
1456 GTypeValuePeekPointerFunc value_peek_pointer;
1457
1458 const gchar *collect_format;
1459 GTypeValueCollectFunc collect_value;
1460
1461 const gchar *lcopy_format;
1462 GTypeValueLCopyFunc lcopy_value;
1463};
1464G_GNUC_END_IGNORE_DEPRECATIONS
1465
1466GOBJECT_AVAILABLE_IN_ALL
1467GType g_type_register_static (GType parent_type,
1468 const gchar *type_name,
1469 const GTypeInfo *info,
1470 GTypeFlags flags);
1471GOBJECT_AVAILABLE_IN_ALL
1472GType g_type_register_static_simple (GType parent_type,
1473 const gchar *type_name,
1474 guint class_size,
1475 GClassInitFunc class_init,
1476 guint instance_size,
1477 GInstanceInitFunc instance_init,
1478 GTypeFlags flags);
1479
1480GOBJECT_AVAILABLE_IN_ALL
1481GType g_type_register_dynamic (GType parent_type,
1482 const gchar *type_name,
1483 GTypePlugin *plugin,
1484 GTypeFlags flags);
1485GOBJECT_AVAILABLE_IN_ALL
1486GType g_type_register_fundamental (GType type_id,
1487 const gchar *type_name,
1488 const GTypeInfo *info,
1489 const GTypeFundamentalInfo *finfo,
1490 GTypeFlags flags);
1491GOBJECT_AVAILABLE_IN_ALL
1492void g_type_add_interface_static (GType instance_type,
1493 GType interface_type,
1494 const GInterfaceInfo *info);
1495GOBJECT_AVAILABLE_IN_ALL
1496void g_type_add_interface_dynamic (GType instance_type,
1497 GType interface_type,
1498 GTypePlugin *plugin);
1499GOBJECT_AVAILABLE_IN_ALL
1500void g_type_interface_add_prerequisite (GType interface_type,
1501 GType prerequisite_type);
1502GOBJECT_AVAILABLE_IN_ALL
1503GType*g_type_interface_prerequisites (GType interface_type,
1504 guint *n_prerequisites);
1505GOBJECT_AVAILABLE_IN_2_68
1506GType g_type_interface_instantiatable_prerequisite
1507 (GType interface_type);
1508GOBJECT_DEPRECATED_IN_2_58
1509void g_type_class_add_private (gpointer g_class,
1510 gsize private_size);
1511GOBJECT_AVAILABLE_IN_2_38
1512gint g_type_add_instance_private (GType class_type,
1513 gsize private_size);
1514GOBJECT_AVAILABLE_IN_ALL
1515gpointer g_type_instance_get_private (GTypeInstance *instance,
1516 GType private_type);
1517GOBJECT_AVAILABLE_IN_2_38
1518void g_type_class_adjust_private_offset (gpointer g_class,
1519 gint *private_size_or_offset);
1520
1521GOBJECT_AVAILABLE_IN_ALL
1522void g_type_add_class_private (GType class_type,
1523 gsize private_size);
1524GOBJECT_AVAILABLE_IN_ALL
1525gpointer g_type_class_get_private (GTypeClass *klass,
1526 GType private_type);
1527GOBJECT_AVAILABLE_IN_2_38
1528gint g_type_class_get_instance_private_offset (gpointer g_class);
1529
1530GOBJECT_AVAILABLE_IN_2_34
1531void g_type_ensure (GType type);
1532GOBJECT_AVAILABLE_IN_2_36
1533guint g_type_get_type_registration_serial (void);
1534
1535
1536/* --- GType boilerplate --- */
1537/**
1538 * G_DECLARE_FINAL_TYPE:
1539 * @ModuleObjName: The name of the new type, in camel case (like `GtkWidget`)
1540 * @module_obj_name: The name of the new type in lowercase, with words
1541 * separated by `_` (like `gtk_widget`)
1542 * @MODULE: The name of the module, in all caps (like `GTK`)
1543 * @OBJ_NAME: The bare name of the type, in all caps (like `WIDGET`)
1544 * @ParentName: the name of the parent type, in camel case (like `GtkWidget`)
1545 *
1546 * A convenience macro for emitting the usual declarations in the header file
1547 * for a type which is not (at the present time) intended to be subclassed.
1548 *
1549 * You might use it in a header as follows:
1550 *
1551 * |[<!-- language="C" -->
1552 * #ifndef _myapp_window_h_
1553 * #define _myapp_window_h_
1554 *
1555 * #include <gtk/gtk.h>
1556 *
1557 * #define MY_APP_TYPE_WINDOW my_app_window_get_type ()
1558 * G_DECLARE_FINAL_TYPE (MyAppWindow, my_app_window, MY_APP, WINDOW, GtkWindow)
1559 *
1560 * MyAppWindow * my_app_window_new (void);
1561 *
1562 * ...
1563 *
1564 * #endif
1565 * ]|
1566 *
1567 * And use it as follow in your C file:
1568 *
1569 * |[<!-- language="C" -->
1570 * struct _MyAppWindow
1571 * {
1572 * GtkWindow parent;
1573 * ...
1574 * };
1575 * G_DEFINE_TYPE (MyAppWindow, my_app_window, GTK_TYPE_WINDOW)
1576 * ]|
1577 *
1578 * This results in the following things happening:
1579 *
1580 * - the usual `my_app_window_get_type()` function is declared with a return type of #GType
1581 *
1582 * - the `MyAppWindow` type is defined as a `typedef` of `struct _MyAppWindow`. The struct itself is not
1583 * defined and should be defined from the `.c` file before `G_DEFINE_TYPE()` is used.
1584 *
1585 * - the `MY_APP_WINDOW()` cast is emitted as `static inline` function along with the `MY_APP_IS_WINDOW()` type
1586 * checking function
1587 *
1588 * - the `MyAppWindowClass` type is defined as a struct containing `GtkWindowClass`. This is done for the
1589 * convenience of the person defining the type and should not be considered to be part of the ABI. In
1590 * particular, without a firm declaration of the instance structure, it is not possible to subclass the type
1591 * and therefore the fact that the size of the class structure is exposed is not a concern and it can be
1592 * freely changed at any point in the future.
1593 *
1594 * - `g_autoptr()` support being added for your type, based on the type of your parent class
1595 *
1596 * You can only use this function if your parent type also supports `g_autoptr()`.
1597 *
1598 * Because the type macro (`MY_APP_TYPE_WINDOW` in the above example) is not a callable, you must continue to
1599 * manually define this as a macro for yourself.
1600 *
1601 * The declaration of the `_get_type()` function is the first thing emitted by the macro. This allows this macro
1602 * to be used in the usual way with export control and API versioning macros.
1603 *
1604 * If you want to declare your own class structure, use [func@GObject.DECLARE_DERIVABLE_TYPE].
1605 *
1606 * If you are writing a library, it is important to note that it is possible to convert a type from using
1607 * `G_DECLARE_FINAL_TYPE()` to `G_DECLARE_DERIVABLE_TYPE()` without breaking API or ABI. As a precaution, you
1608 * should therefore use `G_DECLARE_FINAL_TYPE()` until you are sure that it makes sense for your class to be
1609 * subclassed. Once a class structure has been exposed it is not possible to change its size or remove or
1610 * reorder items without breaking the API and/or ABI.
1611 *
1612 * Since: 2.44
1613 **/
1614#define G_DECLARE_FINAL_TYPE(ModuleObjName, module_obj_name, MODULE, OBJ_NAME, ParentName) \
1615 GType module_obj_name##_get_type (void); \
1616 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
1617 typedef struct _##ModuleObjName ModuleObjName; \
1618 typedef struct { ParentName##Class parent_class; } ModuleObjName##Class; \
1619 \
1620 _GLIB_DEFINE_AUTOPTR_CHAINUP (ModuleObjName, ParentName) \
1621 G_DEFINE_AUTOPTR_CLEANUP_FUNC (ModuleObjName##Class, g_type_class_unref) \
1622 \
1623 G_GNUC_UNUSED static inline ModuleObjName * MODULE##_##OBJ_NAME (gpointer ptr) { \
1624 return G_TYPE_CHECK_INSTANCE_CAST (ptr, module_obj_name##_get_type (), ModuleObjName); } \
1625 G_GNUC_UNUSED static inline gboolean MODULE##_IS_##OBJ_NAME (gpointer ptr) { \
1626 return G_TYPE_CHECK_INSTANCE_TYPE (ptr, module_obj_name##_get_type ()); } \
1627 G_GNUC_END_IGNORE_DEPRECATIONS
1628
1629/**
1630 * G_DECLARE_DERIVABLE_TYPE:
1631 * @ModuleObjName: The name of the new type, in camel case (like `GtkWidget`)
1632 * @module_obj_name: The name of the new type in lowercase, with words
1633 * separated by `_` (like `gtk_widget`)
1634 * @MODULE: The name of the module, in all caps (like `GTK`)
1635 * @OBJ_NAME: The bare name of the type, in all caps (like `WIDGET`)
1636 * @ParentName: the name of the parent type, in camel case (like `GtkWidget`)
1637 *
1638 * A convenience macro for emitting the usual declarations in the
1639 * header file for a type which is intended to be subclassed.
1640 *
1641 * You might use it in a header as follows:
1642 *
1643 * |[<!-- language="C" -->
1644 * #ifndef _gtk_frobber_h_
1645 * #define _gtk_frobber_h_
1646 *
1647 * #define GTK_TYPE_FROBBER gtk_frobber_get_type ()
1648 * GDK_AVAILABLE_IN_3_12
1649 * G_DECLARE_DERIVABLE_TYPE (GtkFrobber, gtk_frobber, GTK, FROBBER, GtkWidget)
1650 *
1651 * struct _GtkFrobberClass
1652 * {
1653 * GtkWidgetClass parent_class;
1654 *
1655 * void (* handle_frob) (GtkFrobber *frobber,
1656 * guint n_frobs);
1657 *
1658 * gpointer padding[12];
1659 * };
1660 *
1661 * GtkWidget * gtk_frobber_new (void);
1662 *
1663 * ...
1664 *
1665 * #endif
1666 * ]|
1667 *
1668 * Since the instance structure is public it is often needed to declare a
1669 * private struct as follow in your C file:
1670 *
1671 * |[<!-- language="C" -->
1672 * typedef struct _GtkFrobberPrivate GtkFrobberPrivate;
1673 * struct _GtkFrobberPrivate
1674 * {
1675 * ...
1676 * };
1677 * G_DEFINE_TYPE_WITH_PRIVATE (GtkFrobber, gtk_frobber, GTK_TYPE_WIDGET)
1678 * ]|
1679 *
1680 * This results in the following things happening:
1681 *
1682 * - the usual `gtk_frobber_get_type()` function is declared with a return type of #GType
1683 *
1684 * - the `GtkFrobber` struct is created with `GtkWidget` as the first and only item. You are expected to use
1685 * a private structure from your .c file to store your instance variables.
1686 *
1687 * - the `GtkFrobberClass` type is defined as a typedef to `struct _GtkFrobberClass`, which is left undefined.
1688 * You should do this from the header file directly after you use the macro.
1689 *
1690 * - the `GTK_FROBBER()` and `GTK_FROBBER_CLASS()` casts are emitted as `static inline` functions along with
1691 * the `GTK_IS_FROBBER()` and `GTK_IS_FROBBER_CLASS()` type checking functions and `GTK_FROBBER_GET_CLASS()`
1692 * function.
1693 *
1694 * - g_autoptr() support being added for your type, based on the type of your parent class
1695 *
1696 * You can only use this function if your parent type also supports `g_autoptr()`.
1697 *
1698 * Because the type macro (`GTK_TYPE_FROBBER` in the above example) is not a callable, you must continue to
1699 * manually define this as a macro for yourself.
1700 *
1701 * The declaration of the `_get_type()` function is the first thing emitted by the macro. This allows this macro
1702 * to be used in the usual way with export control and API versioning macros.
1703 *
1704 * If you are writing a library, it is important to note that it is possible to convert a type from using
1705 * [func@GObject.DECLARE_FINAL_TYPE] to `G_DECLARE_DERIVABLE_TYPE()` without breaking API or ABI. As a precaution, you
1706 * should therefore use `G_DECLARE_FINAL_TYPE()` until you are sure that it makes sense for your class to be
1707 * subclassed. Once a class structure has been exposed it is not possible to change its size or remove or
1708 * reorder items without breaking the API and/or ABI. If you want to declare your own class structure, use
1709 * `G_DECLARE_DERIVABLE_TYPE()`. If you want to declare a class without exposing the class or instance
1710 * structures, use `G_DECLARE_FINAL_TYPE()`.
1711 *
1712 * If you must use `G_DECLARE_DERIVABLE_TYPE()` you should be sure to include some padding at the bottom of your
1713 * class structure to leave space for the addition of future virtual functions.
1714 *
1715 * Since: 2.44
1716 **/
1717#define G_DECLARE_DERIVABLE_TYPE(ModuleObjName, module_obj_name, MODULE, OBJ_NAME, ParentName) \
1718 GType module_obj_name##_get_type (void); \
1719 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
1720 typedef struct _##ModuleObjName ModuleObjName; \
1721 typedef struct _##ModuleObjName##Class ModuleObjName##Class; \
1722 struct _##ModuleObjName { ParentName parent_instance; }; \
1723 \
1724 _GLIB_DEFINE_AUTOPTR_CHAINUP (ModuleObjName, ParentName) \
1725 G_DEFINE_AUTOPTR_CLEANUP_FUNC (ModuleObjName##Class, g_type_class_unref) \
1726 \
1727 G_GNUC_UNUSED static inline ModuleObjName * MODULE##_##OBJ_NAME (gpointer ptr) { \
1728 return G_TYPE_CHECK_INSTANCE_CAST (ptr, module_obj_name##_get_type (), ModuleObjName); } \
1729 G_GNUC_UNUSED static inline ModuleObjName##Class * MODULE##_##OBJ_NAME##_CLASS (gpointer ptr) { \
1730 return G_TYPE_CHECK_CLASS_CAST (ptr, module_obj_name##_get_type (), ModuleObjName##Class); } \
1731 G_GNUC_UNUSED static inline gboolean MODULE##_IS_##OBJ_NAME (gpointer ptr) { \
1732 return G_TYPE_CHECK_INSTANCE_TYPE (ptr, module_obj_name##_get_type ()); } \
1733 G_GNUC_UNUSED static inline gboolean MODULE##_IS_##OBJ_NAME##_CLASS (gpointer ptr) { \
1734 return G_TYPE_CHECK_CLASS_TYPE (ptr, module_obj_name##_get_type ()); } \
1735 G_GNUC_UNUSED static inline ModuleObjName##Class * MODULE##_##OBJ_NAME##_GET_CLASS (gpointer ptr) { \
1736 return G_TYPE_INSTANCE_GET_CLASS (ptr, module_obj_name##_get_type (), ModuleObjName##Class); } \
1737 G_GNUC_END_IGNORE_DEPRECATIONS
1738
1739/**
1740 * G_DECLARE_INTERFACE:
1741 * @ModuleObjName: The name of the new type, in camel case (like `GtkWidget`)
1742 * @module_obj_name: The name of the new type in lowercase, with words
1743 * separated by `_` (like `gtk_widget`)
1744 * @MODULE: The name of the module, in all caps (like `GTK`)
1745 * @OBJ_NAME: The bare name of the type, in all caps (like `WIDGET`)
1746 * @PrerequisiteName: the name of the prerequisite type, in camel case (like `GtkWidget`)
1747 *
1748 * A convenience macro for emitting the usual declarations in the header file for a #GInterface type.
1749 *
1750 * You might use it in a header as follows:
1751 *
1752 * |[<!-- language="C" -->
1753 * #ifndef _my_model_h_
1754 * #define _my_model_h_
1755 *
1756 * #define MY_TYPE_MODEL my_model_get_type ()
1757 * GDK_AVAILABLE_IN_3_12
1758 * G_DECLARE_INTERFACE (MyModel, my_model, MY, MODEL, GObject)
1759 *
1760 * struct _MyModelInterface
1761 * {
1762 * GTypeInterface g_iface;
1763 *
1764 * gpointer (* get_item) (MyModel *model);
1765 * };
1766 *
1767 * gpointer my_model_get_item (MyModel *model);
1768 *
1769 * ...
1770 *
1771 * #endif
1772 * ]|
1773 *
1774 * And use it as follow in your C file:
1775 *
1776 * |[<!-- language="C" -->
1777 * G_DEFINE_INTERFACE (MyModel, my_model, G_TYPE_OBJECT);
1778 *
1779 * static void
1780 * my_model_default_init (MyModelInterface *iface)
1781 * {
1782 * ...
1783 * }
1784 * ]|
1785 *
1786 * This results in the following things happening:
1787 *
1788 * - the usual `my_model_get_type()` function is declared with a return type of #GType
1789 *
1790 * - the `MyModelInterface` type is defined as a typedef to `struct _MyModelInterface`,
1791 * which is left undefined. You should do this from the header file directly after
1792 * you use the macro.
1793 *
1794 * - the `MY_MODEL()` cast is emitted as `static inline` functions along with
1795 * the `MY_IS_MODEL()` type checking function and `MY_MODEL_GET_IFACE()` function.
1796 *
1797 * - g_autoptr() support being added for your type, based on your prerequisite type.
1798 *
1799 * You can only use this function if your prerequisite type also supports g_autoptr().
1800 *
1801 * Because the type macro (`MY_TYPE_MODEL` in the above example) is not a callable, you must continue to
1802 * manually define this as a macro for yourself.
1803 *
1804 * The declaration of the `_get_type()` function is the first thing emitted by the macro. This allows this macro
1805 * to be used in the usual way with export control and API versioning macros.
1806 *
1807 * Since: 2.44
1808 **/
1809#define G_DECLARE_INTERFACE(ModuleObjName, module_obj_name, MODULE, OBJ_NAME, PrerequisiteName) \
1810 GType module_obj_name##_get_type (void); \
1811 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
1812 typedef struct _##ModuleObjName ModuleObjName; \
1813 typedef struct _##ModuleObjName##Interface ModuleObjName##Interface; \
1814 \
1815 _GLIB_DEFINE_AUTOPTR_CHAINUP (ModuleObjName, PrerequisiteName) \
1816 \
1817 G_GNUC_UNUSED static inline ModuleObjName * MODULE##_##OBJ_NAME (gpointer ptr) { \
1818 return G_TYPE_CHECK_INSTANCE_CAST (ptr, module_obj_name##_get_type (), ModuleObjName); } \
1819 G_GNUC_UNUSED static inline gboolean MODULE##_IS_##OBJ_NAME (gpointer ptr) { \
1820 return G_TYPE_CHECK_INSTANCE_TYPE (ptr, module_obj_name##_get_type ()); } \
1821 G_GNUC_UNUSED static inline ModuleObjName##Interface * MODULE##_##OBJ_NAME##_GET_IFACE (gpointer ptr) { \
1822 return G_TYPE_INSTANCE_GET_INTERFACE (ptr, module_obj_name##_get_type (), ModuleObjName##Interface); } \
1823 G_GNUC_END_IGNORE_DEPRECATIONS
1824
1825/**
1826 * G_DEFINE_TYPE:
1827 * @TN: The name of the new type, in Camel case.
1828 * @t_n: The name of the new type, in lowercase, with words
1829 * separated by `_`.
1830 * @T_P: The #GType of the parent type.
1831 *
1832 * A convenience macro for type implementations, which declares a class
1833 * initialization function, an instance initialization function (see #GTypeInfo
1834 * for information about these) and a static variable named `t_n_parent_class`
1835 * pointing to the parent class. Furthermore, it defines a `*_get_type()` function.
1836 * See G_DEFINE_TYPE_EXTENDED() for an example.
1837 *
1838 * Since: 2.4
1839 */
1840#define G_DEFINE_TYPE(TN, t_n, T_P) G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, 0, {})
1841/**
1842 * G_DEFINE_TYPE_WITH_CODE:
1843 * @TN: The name of the new type, in Camel case.
1844 * @t_n: The name of the new type in lowercase, with words separated by `_`.
1845 * @T_P: The #GType of the parent type.
1846 * @_C_: Custom code that gets inserted in the `*_get_type()` function.
1847 *
1848 * A convenience macro for type implementations.
1849 *
1850 * Similar to G_DEFINE_TYPE(), but allows you to insert custom code into the
1851 * `*_get_type()` function, e.g. interface implementations via G_IMPLEMENT_INTERFACE().
1852 * See G_DEFINE_TYPE_EXTENDED() for an example.
1853 *
1854 * Since: 2.4
1855 */
1856#define G_DEFINE_TYPE_WITH_CODE(TN, t_n, T_P, _C_) _G_DEFINE_TYPE_EXTENDED_BEGIN (TN, t_n, T_P, 0) {_C_;} _G_DEFINE_TYPE_EXTENDED_END()
1857/**
1858 * G_DEFINE_TYPE_WITH_PRIVATE:
1859 * @TN: The name of the new type, in Camel case.
1860 * @t_n: The name of the new type, in lowercase, with words
1861 * separated by `_`.
1862 * @T_P: The #GType of the parent type.
1863 *
1864 * A convenience macro for type implementations, which declares a class
1865 * initialization function, an instance initialization function (see #GTypeInfo
1866 * for information about these), a static variable named `t_n_parent_class`
1867 * pointing to the parent class, and adds private instance data to the type.
1868 *
1869 * Furthermore, it defines a `*_get_type()` function. See G_DEFINE_TYPE_EXTENDED()
1870 * for an example.
1871 *
1872 * Note that private structs added with this macros must have a struct
1873 * name of the form `TN ## Private`.
1874 *
1875 * The private instance data can be retrieved using the automatically generated
1876 * getter function `t_n_get_instance_private()`.
1877 *
1878 * See also: G_ADD_PRIVATE()
1879 *
1880 * Since: 2.38
1881 */
1882#define G_DEFINE_TYPE_WITH_PRIVATE(TN, t_n, T_P) G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, 0, G_ADD_PRIVATE (TN))
1883/**
1884 * G_DEFINE_ABSTRACT_TYPE:
1885 * @TN: The name of the new type, in Camel case.
1886 * @t_n: The name of the new type, in lowercase, with words
1887 * separated by `_`.
1888 * @T_P: The #GType of the parent type.
1889 *
1890 * A convenience macro for type implementations.
1891 *
1892 * Similar to G_DEFINE_TYPE(), but defines an abstract type.
1893 * See G_DEFINE_TYPE_EXTENDED() for an example.
1894 *
1895 * Since: 2.4
1896 */
1897#define G_DEFINE_ABSTRACT_TYPE(TN, t_n, T_P) G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, G_TYPE_FLAG_ABSTRACT, {})
1898/**
1899 * G_DEFINE_ABSTRACT_TYPE_WITH_CODE:
1900 * @TN: The name of the new type, in Camel case.
1901 * @t_n: The name of the new type, in lowercase, with words
1902 * separated by `_`.
1903 * @T_P: The #GType of the parent type.
1904 * @_C_: Custom code that gets inserted in the `type_name_get_type()` function.
1905 *
1906 * A convenience macro for type implementations.
1907 *
1908 * Similar to G_DEFINE_TYPE_WITH_CODE(), but defines an abstract type and
1909 * allows you to insert custom code into the `*_get_type()` function, e.g.
1910 * interface implementations via G_IMPLEMENT_INTERFACE().
1911 *
1912 * See G_DEFINE_TYPE_EXTENDED() for an example.
1913 *
1914 * Since: 2.4
1915 */
1916#define G_DEFINE_ABSTRACT_TYPE_WITH_CODE(TN, t_n, T_P, _C_) _G_DEFINE_TYPE_EXTENDED_BEGIN (TN, t_n, T_P, G_TYPE_FLAG_ABSTRACT) {_C_;} _G_DEFINE_TYPE_EXTENDED_END()
1917/**
1918 * G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE:
1919 * @TN: The name of the new type, in Camel case.
1920 * @t_n: The name of the new type, in lowercase, with words
1921 * separated by `_`.
1922 * @T_P: The #GType of the parent type.
1923 *
1924 * Similar to G_DEFINE_TYPE_WITH_PRIVATE(), but defines an abstract type.
1925 *
1926 * See G_DEFINE_TYPE_EXTENDED() for an example.
1927 *
1928 * Since: 2.38
1929 */
1930#define G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(TN, t_n, T_P) G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, G_TYPE_FLAG_ABSTRACT, G_ADD_PRIVATE (TN))
1931/**
1932 * G_DEFINE_FINAL_TYPE:
1933 * @TN: the name of the new type, in Camel case
1934 * @t_n: the name of the new type, in lower case, with words
1935 * separated by `_` (snake case)
1936 * @T_P: the #GType of the parent type
1937 *
1938 * A convenience macro for type implementations.
1939 *
1940 * Similar to [func@GObject.DEFINE_TYPE], but defines a final type.
1941 *
1942 * See [func@GObject.DEFINE_TYPE_EXTENDED] for an example.
1943 *
1944 * Since: 2.70
1945 */
1946#define G_DEFINE_FINAL_TYPE(TN, t_n, T_P) G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, G_TYPE_FLAG_FINAL, {}) GOBJECT_AVAILABLE_MACRO_IN_2_70
1947/**
1948 * G_DEFINE_FINAL_TYPE_WITH_CODE:
1949 * @TN: the name of the new type, in Camel case
1950 * @t_n: the name of the new type, in lower case, with words
1951 * separated by `_` (snake case)
1952 * @T_P: the #GType of the parent type
1953 * @_C_: Custom code that gets inserted in the `type_name_get_type()` function.
1954 *
1955 * A convenience macro for type implementations.
1956 *
1957 * Similar to G_DEFINE_TYPE_WITH_CODE(), but defines a final type and
1958 * allows you to insert custom code into the `*_get_type()` function, e.g.
1959 * interface implementations via G_IMPLEMENT_INTERFACE().
1960 *
1961 * See G_DEFINE_TYPE_EXTENDED() for an example.
1962 *
1963 * Since: 2.70
1964 */
1965#define G_DEFINE_FINAL_TYPE_WITH_CODE(TN, t_n, T_P, _C_) _G_DEFINE_TYPE_EXTENDED_BEGIN (TN, t_n, T_P, G_TYPE_FLAG_FINAL) {_C_;} _G_DEFINE_TYPE_EXTENDED_END() GOBJECT_AVAILABLE_MACRO_IN_2_70
1966/**
1967 * G_DEFINE_FINAL_TYPE_WITH_PRIVATE:
1968 * @TN: the name of the new type, in Camel case
1969 * @t_n: the name of the new type, in lower case, with words
1970 * separated by `_` (snake case)
1971 * @T_P: the #GType of the parent type
1972 *
1973 * A convenience macro for type implementations.
1974 *
1975 * Similar to G_DEFINE_TYPE_WITH_PRIVATE(), but defines a final type.
1976 *
1977 * See G_DEFINE_TYPE_EXTENDED() for an example.
1978 *
1979 * Since: 2.70
1980 */
1981#define G_DEFINE_FINAL_TYPE_WITH_PRIVATE(TN, t_n, T_P) G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, G_TYPE_FLAG_FINAL, G_ADD_PRIVATE (TN)) GOBJECT_AVAILABLE_MACRO_IN_2_70
1982/**
1983 * G_DEFINE_TYPE_EXTENDED:
1984 * @TN: The name of the new type, in Camel case.
1985 * @t_n: The name of the new type, in lowercase, with words
1986 * separated by `_`.
1987 * @T_P: The #GType of the parent type.
1988 * @_f_: #GTypeFlags to pass to g_type_register_static()
1989 * @_C_: Custom code that gets inserted in the `*_get_type()` function.
1990 *
1991 * The most general convenience macro for type implementations, on which
1992 * G_DEFINE_TYPE(), etc are based.
1993 *
1994 * |[<!-- language="C" -->
1995 * G_DEFINE_TYPE_EXTENDED (GtkGadget,
1996 * gtk_gadget,
1997 * GTK_TYPE_WIDGET,
1998 * 0,
1999 * G_ADD_PRIVATE (GtkGadget)
2000 * G_IMPLEMENT_INTERFACE (TYPE_GIZMO,
2001 * gtk_gadget_gizmo_init));
2002 * ]|
2003 *
2004 * expands to
2005 *
2006 * |[<!-- language="C" -->
2007 * static void gtk_gadget_init (GtkGadget *self);
2008 * static void gtk_gadget_class_init (GtkGadgetClass *klass);
2009 * static gpointer gtk_gadget_parent_class = NULL;
2010 * static gint GtkGadget_private_offset;
2011 * static void gtk_gadget_class_intern_init (gpointer klass)
2012 * {
2013 * gtk_gadget_parent_class = g_type_class_peek_parent (klass);
2014 * if (GtkGadget_private_offset != 0)
2015 * g_type_class_adjust_private_offset (klass, &GtkGadget_private_offset);
2016 * gtk_gadget_class_init ((GtkGadgetClass*) klass);
2017 * }
2018 * static inline gpointer gtk_gadget_get_instance_private (GtkGadget *self)
2019 * {
2020 * return (G_STRUCT_MEMBER_P (self, GtkGadget_private_offset));
2021 * }
2022 *
2023 * GType
2024 * gtk_gadget_get_type (void)
2025 * {
2026 * static GType static_g_define_type_id = 0;
2027 * if (g_once_init_enter_pointer (&static_g_define_type_id))
2028 * {
2029 * GType g_define_type_id =
2030 * g_type_register_static_simple (GTK_TYPE_WIDGET,
2031 * g_intern_static_string ("GtkGadget"),
2032 * sizeof (GtkGadgetClass),
2033 * (GClassInitFunc) gtk_gadget_class_intern_init,
2034 * sizeof (GtkGadget),
2035 * (GInstanceInitFunc) gtk_gadget_init,
2036 * 0);
2037 * {
2038 * GtkGadget_private_offset =
2039 * g_type_add_instance_private (g_define_type_id, sizeof (GtkGadgetPrivate));
2040 * }
2041 * {
2042 * const GInterfaceInfo g_implement_interface_info = {
2043 * (GInterfaceInitFunc) gtk_gadget_gizmo_init
2044 * };
2045 * g_type_add_interface_static (g_define_type_id, TYPE_GIZMO, &g_implement_interface_info);
2046 * }
2047 * g_once_init_leave_pointer (&static_g_define_type_id, g_define_type_id);
2048 * }
2049 * return static_g_define_type_id;
2050 * }
2051 * ]|
2052 *
2053 * The only pieces which have to be manually provided are the definitions of
2054 * the instance and class structure and the definitions of the instance and
2055 * class init functions.
2056 *
2057 * Since: 2.4
2058 */
2059#define G_DEFINE_TYPE_EXTENDED(TN, t_n, T_P, _f_, _C_) _G_DEFINE_TYPE_EXTENDED_BEGIN (TN, t_n, T_P, _f_) {_C_;} _G_DEFINE_TYPE_EXTENDED_END()
2060
2061/**
2062 * G_DEFINE_INTERFACE:
2063 * @TN: The name of the new type, in Camel case.
2064 * @t_n: The name of the new type, in lowercase, with words separated by `_`.
2065 * @T_P: The #GType of the prerequisite type for the interface, or %G_TYPE_INVALID
2066 * for no prerequisite type.
2067 *
2068 * A convenience macro for #GTypeInterface definitions, which declares
2069 * a default vtable initialization function and defines a `*_get_type()`
2070 * function.
2071 *
2072 * The macro expects the interface initialization function to have the
2073 * name `t_n ## _default_init`, and the interface structure to have the
2074 * name `TN ## Interface`.
2075 *
2076 * The initialization function has signature
2077 * `static void t_n ## _default_init (TypeName##Interface *klass);`, rather than
2078 * the full #GInterfaceInitFunc signature, for brevity and convenience. If you
2079 * need to use an initialization function with an `iface_data` argument, you
2080 * must write the #GTypeInterface definitions manually.
2081 *
2082 * Since: 2.24
2083 */
2084#define G_DEFINE_INTERFACE(TN, t_n, T_P) G_DEFINE_INTERFACE_WITH_CODE(TN, t_n, T_P, ;)
2085
2086/**
2087 * G_DEFINE_INTERFACE_WITH_CODE:
2088 * @TN: The name of the new type, in Camel case.
2089 * @t_n: The name of the new type, in lowercase, with words separated by `_`.
2090 * @T_P: The #GType of the prerequisite type for the interface, or %G_TYPE_INVALID
2091 * for no prerequisite type.
2092 * @_C_: Custom code that gets inserted in the `*_get_type()` function.
2093 *
2094 * A convenience macro for #GTypeInterface definitions.
2095 *
2096 * Similar to G_DEFINE_INTERFACE(), but allows you to insert custom code
2097 * into the `*_get_type()` function, e.g. additional interface implementations
2098 * via G_IMPLEMENT_INTERFACE(), or additional prerequisite types.
2099 *
2100 * See G_DEFINE_TYPE_EXTENDED() for a similar example using
2101 * G_DEFINE_TYPE_WITH_CODE().
2102 *
2103 * Since: 2.24
2104 */
2105#define G_DEFINE_INTERFACE_WITH_CODE(TN, t_n, T_P, _C_) _G_DEFINE_INTERFACE_EXTENDED_BEGIN(TN, t_n, T_P) {_C_;} _G_DEFINE_INTERFACE_EXTENDED_END()
2106
2107/**
2108 * G_IMPLEMENT_INTERFACE:
2109 * @TYPE_IFACE: The #GType of the interface to add
2110 * @iface_init: (type GInterfaceInitFunc): The interface init function, of type #GInterfaceInitFunc
2111 *
2112 * A convenience macro to ease interface addition in the `_C_` section
2113 * of G_DEFINE_TYPE_WITH_CODE() or G_DEFINE_ABSTRACT_TYPE_WITH_CODE().
2114 * See G_DEFINE_TYPE_EXTENDED() for an example.
2115 *
2116 * Note that this macro can only be used together with the `G_DEFINE_TYPE_*`
2117 * macros, since it depends on variable names from those macros.
2118 *
2119 * Since: 2.4
2120 */
2121#define G_IMPLEMENT_INTERFACE(TYPE_IFACE, iface_init) { \
2122 const GInterfaceInfo g_implement_interface_info = { \
2123 (GInterfaceInitFunc)(void (*)(void)) iface_init, NULL, NULL \
2124 }; \
2125 g_type_add_interface_static (g_define_type_id, TYPE_IFACE, &g_implement_interface_info); \
2126}
2127
2128/**
2129 * G_ADD_PRIVATE:
2130 * @TypeName: the name of the type in CamelCase
2131 *
2132 * A convenience macro to ease adding private data to instances of a new type
2133 * in the @_C_ section of G_DEFINE_TYPE_WITH_CODE() or
2134 * G_DEFINE_ABSTRACT_TYPE_WITH_CODE().
2135 *
2136 * For instance:
2137 *
2138 * |[<!-- language="C" -->
2139 * typedef struct _MyObject MyObject;
2140 * typedef struct _MyObjectClass MyObjectClass;
2141 *
2142 * typedef struct {
2143 * gint foo;
2144 * gint bar;
2145 * } MyObjectPrivate;
2146 *
2147 * G_DEFINE_TYPE_WITH_CODE (MyObject, my_object, G_TYPE_OBJECT,
2148 * G_ADD_PRIVATE (MyObject))
2149 * ]|
2150 *
2151 * Will add `MyObjectPrivate` as the private data to any instance of the
2152 * `MyObject` type.
2153 *
2154 * `G_DEFINE_TYPE_*` macros will automatically create a private function
2155 * based on the arguments to this macro, which can be used to safely
2156 * retrieve the private data from an instance of the type; for instance:
2157 *
2158 * |[<!-- language="C" -->
2159 * gint
2160 * my_object_get_foo (MyObject *obj)
2161 * {
2162 * MyObjectPrivate *priv = my_object_get_instance_private (obj);
2163 *
2164 * g_return_val_if_fail (MY_IS_OBJECT (obj), 0);
2165 *
2166 * return priv->foo;
2167 * }
2168 *
2169 * void
2170 * my_object_set_bar (MyObject *obj,
2171 * gint bar)
2172 * {
2173 * MyObjectPrivate *priv = my_object_get_instance_private (obj);
2174 *
2175 * g_return_if_fail (MY_IS_OBJECT (obj));
2176 *
2177 * if (priv->bar != bar)
2178 * priv->bar = bar;
2179 * }
2180 * ]|
2181 *
2182 * Since GLib 2.72, the returned `MyObjectPrivate` pointer is guaranteed to be
2183 * aligned to at least the alignment of the largest basic GLib type (typically
2184 * this is #guint64 or #gdouble). If you need larger alignment for an element in
2185 * the struct, you should allocate it on the heap (aligned), or arrange for your
2186 * `MyObjectPrivate` struct to be appropriately padded.
2187 *
2188 * Note that this macro can only be used together with the `G_DEFINE_TYPE_*`
2189 * macros, since it depends on variable names from those macros.
2190 *
2191 * Also note that private structs added with these macros must have a struct
2192 * name of the form `TypeNamePrivate`.
2193 *
2194 * It is safe to call the `_get_instance_private` function on %NULL or invalid
2195 * objects since it's only adding an offset to the instance pointer. In that
2196 * case the returned pointer must not be dereferenced.
2197 *
2198 * Since: 2.38
2199 */
2200#define G_ADD_PRIVATE(TypeName) { \
2201 TypeName##_private_offset = \
2202 g_type_add_instance_private (g_define_type_id, sizeof (TypeName##Private)); \
2203}
2204
2205/**
2206 * G_PRIVATE_OFFSET:
2207 * @TypeName: the name of the type in CamelCase
2208 * @field: the name of the field in the private data structure
2209 *
2210 * Evaluates to the offset of the @field inside the instance private data
2211 * structure for @TypeName.
2212 *
2213 * Note that this macro can only be used together with the `G_DEFINE_TYPE_*`
2214 * and G_ADD_PRIVATE() macros, since it depends on variable names from
2215 * those macros.
2216 *
2217 * Since: 2.38
2218 */
2219#define G_PRIVATE_OFFSET(TypeName, field) \
2220 (TypeName##_private_offset + (G_STRUCT_OFFSET (TypeName##Private, field)))
2221
2222/**
2223 * G_PRIVATE_FIELD_P:
2224 * @TypeName: the name of the type in CamelCase
2225 * @inst: the instance of @TypeName you wish to access
2226 * @field_name: the name of the field in the private data structure
2227 *
2228 * Evaluates to a pointer to the @field_name inside the @inst private data
2229 * structure for @TypeName.
2230 *
2231 * Note that this macro can only be used together with the `G_DEFINE_TYPE_*`
2232 * and G_ADD_PRIVATE() macros, since it depends on variable names from
2233 * those macros.
2234 *
2235 * Since: 2.38
2236 */
2237#define G_PRIVATE_FIELD_P(TypeName, inst, field_name) \
2238 G_STRUCT_MEMBER_P (inst, G_PRIVATE_OFFSET (TypeName, field_name))
2239
2240/**
2241 * G_PRIVATE_FIELD:
2242 * @TypeName: the name of the type in CamelCase
2243 * @inst: the instance of @TypeName you wish to access
2244 * @field_type: the type of the field in the private data structure
2245 * @field_name: the name of the field in the private data structure
2246 *
2247 * Evaluates to the @field_name inside the @inst private data
2248 * structure for @TypeName.
2249 *
2250 * Note that this macro can only be used together with the `G_DEFINE_TYPE_*`
2251 * and G_ADD_PRIVATE() macros, since it depends on variable names from
2252 * those macros.
2253 *
2254 * Since: 2.38
2255 */
2256#define G_PRIVATE_FIELD(TypeName, inst, field_type, field_name) \
2257 G_STRUCT_MEMBER (field_type, inst, G_PRIVATE_OFFSET (TypeName, field_name))
2258
2259/* we need to have this macro under conditional expansion, as it references
2260 * a function that has been added in 2.38. see bug:
2261 * https://bugzilla.gnome.org/show_bug.cgi?id=703191
2262 */
2263#if GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_38
2264#define _G_DEFINE_TYPE_EXTENDED_CLASS_INIT(TypeName, type_name) \
2265static void type_name##_class_intern_init (gpointer klass) \
2266{ \
2267 type_name##_parent_class = g_type_class_peek_parent (klass); \
2268 if (TypeName##_private_offset != 0) \
2269 g_type_class_adjust_private_offset (klass, &TypeName##_private_offset); \
2270 type_name##_class_init ((TypeName##Class*) klass); \
2271}
2272
2273#else
2274#define _G_DEFINE_TYPE_EXTENDED_CLASS_INIT(TypeName, type_name) \
2275static void type_name##_class_intern_init (gpointer klass) \
2276{ \
2277 type_name##_parent_class = g_type_class_peek_parent (klass); \
2278 type_name##_class_init ((TypeName##Class*) klass); \
2279}
2280#endif /* GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_38 */
2281
2282#if GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_80
2283#define _g_type_once_init_type GType
2284#define _g_type_once_init_enter g_once_init_enter_pointer
2285#define _g_type_once_init_leave g_once_init_leave_pointer
2286#else /* if GLIB_VERSION_MAX_ALLOWED < GLIB_VERSION_2_80 */
2287#define _g_type_once_init_type gsize
2288#define _g_type_once_init_enter g_once_init_enter
2289#define _g_type_once_init_leave g_once_init_leave
2290#endif /* GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_80 */
2291
2292/* Added for _G_DEFINE_TYPE_EXTENDED_WITH_PRELUDE */
2293#define _G_DEFINE_TYPE_EXTENDED_BEGIN_PRE(TypeName, type_name) \
2294\
2295static void type_name##_init (TypeName *self); \
2296static void type_name##_class_init (TypeName##Class *klass); \
2297static GType type_name##_get_type_once (void); \
2298G_GNUC_UNUSED static gpointer type_name##_parent_class = NULL; \
2299static gint TypeName##_private_offset; \
2300\
2301_G_DEFINE_TYPE_EXTENDED_CLASS_INIT(TypeName, type_name) \
2302\
2303G_GNUC_UNUSED \
2304static inline gpointer \
2305type_name##_get_instance_private (TypeName *self) \
2306{ \
2307 return (G_STRUCT_MEMBER_P (self, TypeName##_private_offset)); \
2308} \
2309\
2310GType \
2311type_name##_get_type (void) \
2312{ \
2313 static _g_type_once_init_type static_g_define_type_id = 0;
2314 /* Prelude goes here */
2315
2316/* Added for _G_DEFINE_TYPE_EXTENDED_WITH_PRELUDE */
2317#define _G_DEFINE_TYPE_EXTENDED_BEGIN_REGISTER(TypeName, type_name, TYPE_PARENT, flags) \
2318 if (_g_type_once_init_enter (&static_g_define_type_id)) \
2319 { \
2320 GType g_define_type_id = type_name##_get_type_once (); \
2321 _g_type_once_init_leave (&static_g_define_type_id, g_define_type_id); \
2322 } \
2323 return static_g_define_type_id; \
2324} /* closes type_name##_get_type() */ \
2325\
2326G_NO_INLINE \
2327static GType \
2328type_name##_get_type_once (void) \
2329{ \
2330 GType g_define_type_id = \
2331 g_type_register_static_simple (TYPE_PARENT, \
2332 g_intern_static_string (#TypeName), \
2333 sizeof (TypeName##Class), \
2334 (GClassInitFunc)(void (*)(void)) type_name##_class_intern_init, \
2335 sizeof (TypeName), \
2336 (GInstanceInitFunc)(void (*)(void)) type_name##_init, \
2337 (GTypeFlags) flags); \
2338 { /* custom code follows */
2339#define _G_DEFINE_TYPE_EXTENDED_END() \
2340 /* following custom code */ \
2341 } \
2342 return g_define_type_id; \
2343} /* closes type_name##_get_type_once() */
2344
2345/* This was defined before we had _G_DEFINE_TYPE_EXTENDED_WITH_PRELUDE in
2346 * gtype-private.h, it's simplest to keep it.
2347 */
2348#define _G_DEFINE_TYPE_EXTENDED_BEGIN(TypeName, type_name, TYPE_PARENT, flags) \
2349 _G_DEFINE_TYPE_EXTENDED_BEGIN_PRE (TypeName, type_name) \
2350 _G_DEFINE_TYPE_EXTENDED_BEGIN_REGISTER (TypeName, type_name, TYPE_PARENT, flags)
2351
2352/* Intentionally using (GTypeFlags) 0 instead of G_TYPE_FLAG_NONE here,
2353 * to avoid deprecation warnings with older GLIB_VERSION_MAX_ALLOWED */
2354#define _G_DEFINE_INTERFACE_EXTENDED_BEGIN(TypeName, type_name, TYPE_PREREQ) \
2355\
2356static void type_name##_default_init (TypeName##Interface *klass); \
2357\
2358GType \
2359type_name##_get_type (void) \
2360{ \
2361 static _g_type_once_init_type static_g_define_type_id = 0; \
2362 if (_g_type_once_init_enter (&static_g_define_type_id)) \
2363 { \
2364 GType g_define_type_id = \
2365 g_type_register_static_simple (G_TYPE_INTERFACE, \
2366 g_intern_static_string (#TypeName), \
2367 sizeof (TypeName##Interface), \
2368 (GClassInitFunc)(void (*)(void)) type_name##_default_init, \
2369 0, \
2370 (GInstanceInitFunc)NULL, \
2371 (GTypeFlags) 0); \
2372 if (TYPE_PREREQ != G_TYPE_INVALID) \
2373 g_type_interface_add_prerequisite (g_define_type_id, TYPE_PREREQ); \
2374 { /* custom code follows */
2375#define _G_DEFINE_INTERFACE_EXTENDED_END() \
2376 /* following custom code */ \
2377 } \
2378 _g_type_once_init_leave (&static_g_define_type_id, g_define_type_id); \
2379 } \
2380 return static_g_define_type_id; \
2381} /* closes type_name##_get_type() */
2382
2383/**
2384 * G_DEFINE_BOXED_TYPE:
2385 * @TypeName: The name of the new type, in Camel case
2386 * @type_name: The name of the new type, in lowercase, with words
2387 * separated by `_`
2388 * @copy_func: the #GBoxedCopyFunc for the new type
2389 * @free_func: the #GBoxedFreeFunc for the new type
2390 *
2391 * A convenience macro for defining a new custom boxed type.
2392 *
2393 * Using this macro is the recommended way of defining new custom boxed
2394 * types, over calling g_boxed_type_register_static() directly. It defines
2395 * a `type_name_get_type()` function which will return the newly defined
2396 * #GType, enabling lazy instantiation.
2397 *
2398 * You might start by putting declarations in a header as follows:
2399 *
2400 * |[<!-- language="C" -->
2401 * #define MY_TYPE_STRUCT my_struct_get_type ()
2402 * GType my_struct_get_type (void);
2403 *
2404 * MyStruct * my_struct_new (void);
2405 * void my_struct_free (MyStruct *self);
2406 * MyStruct * my_struct_copy (MyStruct *self);
2407 * ]|
2408 *
2409 * And then use this macro and define your implementation in the source file as
2410 * follows:
2411 *
2412 * |[<!-- language="C" -->
2413 * MyStruct *
2414 * my_struct_new (void)
2415 * {
2416 * // ... your code to allocate a new MyStruct ...
2417 * }
2418 *
2419 * void
2420 * my_struct_free (MyStruct *self)
2421 * {
2422 * // ... your code to free a MyStruct ...
2423 * }
2424 *
2425 * MyStruct *
2426 * my_struct_copy (MyStruct *self)
2427 * {
2428 * // ... your code return a newly allocated copy of a MyStruct ...
2429 * }
2430 *
2431 * G_DEFINE_BOXED_TYPE (MyStruct, my_struct, my_struct_copy, my_struct_free)
2432 *
2433 * void
2434 * foo ()
2435 * {
2436 * MyStruct *ms;
2437 *
2438 * ms = my_struct_new ();
2439 * // ... your code ...
2440 * my_struct_free (ms);
2441 * }
2442 * ]|
2443 *
2444 * Since: 2.26
2445 */
2446#define G_DEFINE_BOXED_TYPE(TypeName, type_name, copy_func, free_func) G_DEFINE_BOXED_TYPE_WITH_CODE (TypeName, type_name, copy_func, free_func, {})
2447/**
2448 * G_DEFINE_BOXED_TYPE_WITH_CODE:
2449 * @TypeName: The name of the new type, in Camel case
2450 * @type_name: The name of the new type, in lowercase, with words
2451 * separated by `_`
2452 * @copy_func: the #GBoxedCopyFunc for the new type
2453 * @free_func: the #GBoxedFreeFunc for the new type
2454 * @_C_: Custom code that gets inserted in the `*_get_type()` function
2455 *
2456 * A convenience macro for boxed type implementations.
2457 *
2458 * Similar to G_DEFINE_BOXED_TYPE(), but allows to insert custom code into the
2459 * `type_name_get_type()` function, e.g. to register value transformations with
2460 * g_value_register_transform_func(), for instance:
2461 *
2462 * |[<!-- language="C" -->
2463 * G_DEFINE_BOXED_TYPE_WITH_CODE (GdkRectangle, gdk_rectangle,
2464 * gdk_rectangle_copy,
2465 * gdk_rectangle_free,
2466 * register_rectangle_transform_funcs (g_define_type_id))
2467 * ]|
2468 *
2469 * Similarly to the `G_DEFINE_TYPE_*` family of macros, the #GType of the newly
2470 * defined boxed type is exposed in the `g_define_type_id` variable.
2471 *
2472 * Since: 2.26
2473 */
2474#define G_DEFINE_BOXED_TYPE_WITH_CODE(TypeName, type_name, copy_func, free_func, _C_) _G_DEFINE_BOXED_TYPE_BEGIN (TypeName, type_name, copy_func, free_func) {_C_;} _G_DEFINE_TYPE_EXTENDED_END()
2475
2476/* Only use this in non-C++ on GCC >= 2.7, except for Darwin/ppc64.
2477 * See https://bugzilla.gnome.org/show_bug.cgi?id=647145
2478 */
2479#if !defined (G_CXX_STD_VERSION) && (G_GNUC_CHECK_VERSION(2, 7)) && \
2480 !(defined (__APPLE__) && defined (__ppc64__))
2481#define _G_DEFINE_BOXED_TYPE_BEGIN(TypeName, type_name, copy_func, free_func) \
2482static GType type_name##_get_type_once (void); \
2483\
2484GType \
2485type_name##_get_type (void) \
2486{ \
2487 static _g_type_once_init_type static_g_define_type_id = 0; \
2488 if (_g_type_once_init_enter (&static_g_define_type_id)) \
2489 { \
2490 GType g_define_type_id = type_name##_get_type_once (); \
2491 _g_type_once_init_leave (&static_g_define_type_id, g_define_type_id); \
2492 } \
2493 return static_g_define_type_id; \
2494} \
2495\
2496G_NO_INLINE \
2497static GType \
2498type_name##_get_type_once (void) \
2499{ \
2500 GType (* _g_register_boxed) \
2501 (const gchar *, \
2502 union \
2503 { \
2504 TypeName * (*do_copy_type) (TypeName *); \
2505 TypeName * (*do_const_copy_type) (const TypeName *); \
2506 GBoxedCopyFunc do_copy_boxed; \
2507 } __attribute__((__transparent_union__)), \
2508 union \
2509 { \
2510 void (* do_free_type) (TypeName *); \
2511 GBoxedFreeFunc do_free_boxed; \
2512 } __attribute__((__transparent_union__)) \
2513 ) = g_boxed_type_register_static; \
2514 GType g_define_type_id = \
2515 _g_register_boxed (g_intern_static_string (#TypeName), copy_func, free_func); \
2516 { /* custom code follows */
2517#else
2518#define _G_DEFINE_BOXED_TYPE_BEGIN(TypeName, type_name, copy_func, free_func) \
2519static GType type_name##_get_type_once (void); \
2520\
2521GType \
2522type_name##_get_type (void) \
2523{ \
2524 static _g_type_once_init_type static_g_define_type_id = 0; \
2525 if (_g_type_once_init_enter (&static_g_define_type_id)) \
2526 { \
2527 GType g_define_type_id = type_name##_get_type_once (); \
2528 _g_type_once_init_leave (&static_g_define_type_id, g_define_type_id); \
2529 } \
2530 return static_g_define_type_id; \
2531} \
2532\
2533G_NO_INLINE \
2534static GType \
2535type_name##_get_type_once (void) \
2536{ \
2537 GType g_define_type_id = \
2538 g_boxed_type_register_static (g_intern_static_string (#TypeName), \
2539 (GBoxedCopyFunc) copy_func, \
2540 (GBoxedFreeFunc) free_func); \
2541 { /* custom code follows */
2542#endif /* __GNUC__ */
2543
2544/**
2545 * G_DEFINE_POINTER_TYPE:
2546 * @TypeName: The name of the new type, in Camel case
2547 * @type_name: The name of the new type, in lowercase, with words
2548 * separated by `_`
2549 *
2550 * A convenience macro for pointer type implementations, which defines a
2551 * `type_name_get_type()` function registering the pointer type.
2552 *
2553 * Since: 2.26
2554 */
2555#define G_DEFINE_POINTER_TYPE(TypeName, type_name) G_DEFINE_POINTER_TYPE_WITH_CODE (TypeName, type_name, {})
2556/**
2557 * G_DEFINE_POINTER_TYPE_WITH_CODE:
2558 * @TypeName: The name of the new type, in Camel case
2559 * @type_name: The name of the new type, in lowercase, with words
2560 * separated by `_`
2561 * @_C_: Custom code that gets inserted in the `*_get_type()` function
2562 *
2563 * A convenience macro for pointer type implementations.
2564 * Similar to G_DEFINE_POINTER_TYPE(), but allows to insert
2565 * custom code into the `type_name_get_type()` function.
2566 *
2567 * Since: 2.26
2568 */
2569#define G_DEFINE_POINTER_TYPE_WITH_CODE(TypeName, type_name, _C_) _G_DEFINE_POINTER_TYPE_BEGIN (TypeName, type_name) {_C_;} _G_DEFINE_TYPE_EXTENDED_END()
2570
2571#define _G_DEFINE_POINTER_TYPE_BEGIN(TypeName, type_name) \
2572static GType type_name##_get_type_once (void); \
2573\
2574GType \
2575type_name##_get_type (void) \
2576{ \
2577 static _g_type_once_init_type static_g_define_type_id = 0; \
2578 if (_g_type_once_init_enter (&static_g_define_type_id)) \
2579 { \
2580 GType g_define_type_id = type_name##_get_type_once (); \
2581 _g_type_once_init_leave (&static_g_define_type_id, g_define_type_id); \
2582 } \
2583 return static_g_define_type_id; \
2584} \
2585\
2586G_NO_INLINE \
2587static GType \
2588type_name##_get_type_once (void) \
2589{ \
2590 GType g_define_type_id = \
2591 g_pointer_type_register_static (g_intern_static_string (#TypeName)); \
2592 { /* custom code follows */
2593
2594/* --- protected (for fundamental type implementations) --- */
2595GOBJECT_AVAILABLE_IN_ALL
2596GTypePlugin* g_type_get_plugin (GType type);
2597GOBJECT_AVAILABLE_IN_ALL
2598GTypePlugin* g_type_interface_get_plugin (GType instance_type,
2599 GType interface_type);
2600GOBJECT_AVAILABLE_IN_ALL
2601GType g_type_fundamental_next (void);
2602GOBJECT_AVAILABLE_IN_ALL
2603GType g_type_fundamental (GType type_id);
2604GOBJECT_AVAILABLE_IN_ALL
2605GTypeInstance* g_type_create_instance (GType type);
2606GOBJECT_AVAILABLE_IN_ALL
2607void g_type_free_instance (GTypeInstance *instance);
2608
2609GOBJECT_AVAILABLE_IN_ALL
2610void g_type_add_class_cache_func (gpointer cache_data,
2611 GTypeClassCacheFunc cache_func);
2612GOBJECT_AVAILABLE_IN_ALL
2613void g_type_remove_class_cache_func (gpointer cache_data,
2614 GTypeClassCacheFunc cache_func);
2615GOBJECT_AVAILABLE_IN_ALL
2616void g_type_class_unref_uncached (gpointer g_class);
2617
2618GOBJECT_AVAILABLE_IN_ALL
2619void g_type_add_interface_check (gpointer check_data,
2620 GTypeInterfaceCheckFunc check_func);
2621GOBJECT_AVAILABLE_IN_ALL
2622void g_type_remove_interface_check (gpointer check_data,
2623 GTypeInterfaceCheckFunc check_func);
2624
2625GOBJECT_AVAILABLE_IN_ALL
2626GTypeValueTable* g_type_value_table_peek (GType type);
2627
2628
2629/*< private >*/
2630GOBJECT_AVAILABLE_IN_ALL
2631gboolean g_type_check_instance (GTypeInstance *instance) G_GNUC_PURE;
2632GOBJECT_AVAILABLE_IN_ALL
2633GTypeInstance* g_type_check_instance_cast (GTypeInstance *instance,
2634 GType iface_type);
2635GOBJECT_AVAILABLE_IN_ALL
2636gboolean g_type_check_instance_is_a (GTypeInstance *instance,
2637 GType iface_type) G_GNUC_PURE;
2638GOBJECT_AVAILABLE_IN_2_42
2639gboolean g_type_check_instance_is_fundamentally_a (GTypeInstance *instance,
2640 GType fundamental_type) G_GNUC_PURE;
2641GOBJECT_AVAILABLE_IN_ALL
2642GTypeClass* g_type_check_class_cast (GTypeClass *g_class,
2643 GType is_a_type);
2644GOBJECT_AVAILABLE_IN_ALL
2645gboolean g_type_check_class_is_a (GTypeClass *g_class,
2646 GType is_a_type) G_GNUC_PURE;
2647GOBJECT_AVAILABLE_IN_ALL
2648gboolean g_type_check_is_value_type (GType type) G_GNUC_PURE;
2649GOBJECT_AVAILABLE_IN_ALL
2650gboolean g_type_check_value (const GValue *value) G_GNUC_PURE;
2651GOBJECT_AVAILABLE_IN_ALL
2652gboolean g_type_check_value_holds (const GValue *value,
2653 GType type) G_GNUC_PURE;
2654GOBJECT_AVAILABLE_IN_ALL
2655gboolean g_type_test_flags (GType type,
2656 guint flags) G_GNUC_PURE;
2657
2658
2659/* --- debugging functions --- */
2660GOBJECT_AVAILABLE_IN_ALL
2661const gchar * g_type_name_from_instance (GTypeInstance *instance);
2662GOBJECT_AVAILABLE_IN_ALL
2663const gchar * g_type_name_from_class (GTypeClass *g_class);
2664
2665
2666/* --- implementation bits --- */
2667#if defined(G_DISABLE_CAST_CHECKS) || defined(__OPTIMIZE__)
2668# define _G_TYPE_CIC(ip, gt, ct) ((ct*) (void *) ip)
2669# define _G_TYPE_CCC(cp, gt, ct) ((ct*) (void *) cp)
2670#else
2671# define _G_TYPE_CIC(ip, gt, ct) \
2672 ((ct*) (void *) g_type_check_instance_cast ((GTypeInstance*) ip, gt))
2673# define _G_TYPE_CCC(cp, gt, ct) \
2674 ((ct*) (void *) g_type_check_class_cast ((GTypeClass*) cp, gt))
2675#endif
2676
2677#define _G_TYPE_CHI(ip) (g_type_check_instance ((GTypeInstance*) ip))
2678#define _G_TYPE_CHV(vl) (g_type_check_value ((GValue*) vl))
2679#define _G_TYPE_IGC(ip, gt, ct) ((ct*) (((GTypeInstance*) ip)->g_class))
2680#define _G_TYPE_IGI(ip, gt, ct) ((ct*) g_type_interface_peek (((GTypeInstance*) ip)->g_class, gt))
2681#define _G_TYPE_CIFT(ip, ft) (g_type_check_instance_is_fundamentally_a ((GTypeInstance*) ip, ft))
2682#ifdef __GNUC__
2683# define _G_TYPE_CIT(ip, gt) (G_GNUC_EXTENSION ({ \
2684 GTypeInstance *_inst = (GTypeInstance*) ip; GType _t = gt; gboolean _r; \
2685 if (!_inst) \
2686 _r = FALSE; \
2687 else if (_inst->g_class && _inst->g_class->g_type == _t) \
2688 _r = TRUE; \
2689 else \
2690 _r = g_type_check_instance_is_a (_inst, _t); \
2691 _r; \
2692}))
2693# define _G_TYPE_CCT(cp, gt) (G_GNUC_EXTENSION ({ \
2694 GTypeClass *_class = (GTypeClass*) cp; GType _t = gt; gboolean _r; \
2695 if (!_class) \
2696 _r = FALSE; \
2697 else if (_class->g_type == _t) \
2698 _r = TRUE; \
2699 else \
2700 _r = g_type_check_class_is_a (_class, _t); \
2701 _r; \
2702}))
2703# define _G_TYPE_CVH(vl, gt) (G_GNUC_EXTENSION ({ \
2704 const GValue *_val = (const GValue*) vl; GType _t = gt; gboolean _r; \
2705 if (!_val) \
2706 _r = FALSE; \
2707 else if (_val->g_type == _t) \
2708 _r = TRUE; \
2709 else \
2710 _r = g_type_check_value_holds (_val, _t); \
2711 _r; \
2712}))
2713#else /* !__GNUC__ */
2714# define _G_TYPE_CIT(ip, gt) (g_type_check_instance_is_a ((GTypeInstance*) ip, gt))
2715# define _G_TYPE_CCT(cp, gt) (g_type_check_class_is_a ((GTypeClass*) cp, gt))
2716# define _G_TYPE_CVH(vl, gt) (g_type_check_value_holds ((const GValue*) vl, gt))
2717#endif /* !__GNUC__ */
2718/**
2719 * G_TYPE_FLAG_RESERVED_ID_BIT:
2720 *
2721 * A bit in the type number that's supposed to be left untouched.
2722 */
2723#define G_TYPE_FLAG_RESERVED_ID_BIT ((GType) (1 << 0))
2724
2725/**
2726 * GPOINTER_TO_TYPE:
2727 * @p: The pointer to convert to a #GType
2728 *
2729 * This macro should be used instead of GPOINTER_TO_SIZE() to ensure
2730 * portability since #GType is not guaranteed to be the same as #gsize.
2731 *
2732 * Since: 2.80
2733 */
2734#define GPOINTER_TO_TYPE(p) ((GType) (guintptr) (p)) GOBJECT_AVAILABLE_MACRO_IN_2_80
2735/**
2736 * GTYPE_TO_POINTER:
2737 * @t: The #GType to convert to a pointer
2738 *
2739 * This macro should be used instead of GSIZE_TO_POINTER() to ensure
2740 * portability since #GType is not guaranteed to be the same as #gsize.
2741 *
2742 * Since: 2.80
2743 */
2744#define GTYPE_TO_POINTER(t) ((gpointer) (guintptr) (t)) GOBJECT_AVAILABLE_MACRO_IN_2_80
2745
2746G_END_DECLS
2747
2748#endif /* __G_TYPE_H__ */
2749