Don't use -w in shebang line
[glib.git] / gobject / gtype.h
blob24970bccda97341c5b29b0e8d2ea6138ee9df22c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
20 #error "Only <glib-object.h> can be included directly."
21 #endif
23 #ifndef __G_TYPE_H__
24 #define __G_TYPE_H__
26 #include <glib.h>
28 G_BEGIN_DECLS
30 /* Basic Type Macros
32 /**
33 * G_TYPE_FUNDAMENTAL:
34 * @type: A #GType value.
36 * The fundamental type which is the ancestor of @type.
37 * Fundamental types are types that serve as ultimate bases for the derived types,
38 * thus they are the roots of distinct inheritance hierarchies.
40 #define G_TYPE_FUNDAMENTAL(type) (g_type_fundamental (type))
41 /**
42 * G_TYPE_FUNDAMENTAL_MAX:
44 * An integer constant that represents the number of identifiers reserved
45 * for types that are assigned at compile-time.
47 #define G_TYPE_FUNDAMENTAL_MAX (255 << G_TYPE_FUNDAMENTAL_SHIFT)
49 /* Constant fundamental types,
50 * introduced by g_type_init().
52 /**
53 * G_TYPE_INVALID:
55 * An invalid #GType used as error return value in some functions which return
56 * a #GType.
58 #define G_TYPE_INVALID G_TYPE_MAKE_FUNDAMENTAL (0)
59 /**
60 * G_TYPE_NONE:
62 * A fundamental type which is used as a replacement for the C
63 * <literal>void</literal> return type.
65 #define G_TYPE_NONE G_TYPE_MAKE_FUNDAMENTAL (1)
66 /**
67 * G_TYPE_INTERFACE:
69 * The fundamental type from which all interfaces are derived.
71 #define G_TYPE_INTERFACE G_TYPE_MAKE_FUNDAMENTAL (2)
72 /**
73 * G_TYPE_CHAR:
75 * The fundamental type corresponding to #gchar.
76 * The type designated by G_TYPE_CHAR is unconditionally an 8-bit signed integer.
77 * This may or may not be the same type a the C type "gchar".
79 #define G_TYPE_CHAR G_TYPE_MAKE_FUNDAMENTAL (3)
80 /**
81 * G_TYPE_UCHAR:
83 * The fundamental type corresponding to #guchar.
85 #define G_TYPE_UCHAR G_TYPE_MAKE_FUNDAMENTAL (4)
86 /**
87 * G_TYPE_BOOLEAN:
89 * The fundamental type corresponding to #gboolean.
91 #define G_TYPE_BOOLEAN G_TYPE_MAKE_FUNDAMENTAL (5)
92 /**
93 * G_TYPE_INT:
95 * The fundamental type corresponding to #gint.
97 #define G_TYPE_INT G_TYPE_MAKE_FUNDAMENTAL (6)
98 /**
99 * G_TYPE_UINT:
101 * The fundamental type corresponding to #guint.
103 #define G_TYPE_UINT G_TYPE_MAKE_FUNDAMENTAL (7)
105 * G_TYPE_LONG:
107 * The fundamental type corresponding to #glong.
109 #define G_TYPE_LONG G_TYPE_MAKE_FUNDAMENTAL (8)
111 * G_TYPE_ULONG:
113 * The fundamental type corresponding to #gulong.
115 #define G_TYPE_ULONG G_TYPE_MAKE_FUNDAMENTAL (9)
117 * G_TYPE_INT64:
119 * The fundamental type corresponding to #gint64.
121 #define G_TYPE_INT64 G_TYPE_MAKE_FUNDAMENTAL (10)
123 * G_TYPE_UINT64:
125 * The fundamental type corresponding to #guint64.
127 #define G_TYPE_UINT64 G_TYPE_MAKE_FUNDAMENTAL (11)
129 * G_TYPE_ENUM:
131 * The fundamental type from which all enumeration types are derived.
133 #define G_TYPE_ENUM G_TYPE_MAKE_FUNDAMENTAL (12)
135 * G_TYPE_FLAGS:
137 * The fundamental type from which all flags types are derived.
139 #define G_TYPE_FLAGS G_TYPE_MAKE_FUNDAMENTAL (13)
141 * G_TYPE_FLOAT:
143 * The fundamental type corresponding to #gfloat.
145 #define G_TYPE_FLOAT G_TYPE_MAKE_FUNDAMENTAL (14)
147 * G_TYPE_DOUBLE:
149 * The fundamental type corresponding to #gdouble.
151 #define G_TYPE_DOUBLE G_TYPE_MAKE_FUNDAMENTAL (15)
153 * G_TYPE_STRING:
155 * The fundamental type corresponding to nul-terminated C strings.
157 #define G_TYPE_STRING G_TYPE_MAKE_FUNDAMENTAL (16)
159 * G_TYPE_POINTER:
161 * The fundamental type corresponding to #gpointer.
163 #define G_TYPE_POINTER G_TYPE_MAKE_FUNDAMENTAL (17)
165 * G_TYPE_BOXED:
167 * The fundamental type from which all boxed types are derived.
169 #define G_TYPE_BOXED G_TYPE_MAKE_FUNDAMENTAL (18)
171 * G_TYPE_PARAM:
173 * The fundamental type from which all #GParamSpec types are derived.
175 #define G_TYPE_PARAM G_TYPE_MAKE_FUNDAMENTAL (19)
177 * G_TYPE_OBJECT:
179 * The fundamental type for #GObject.
181 #define G_TYPE_OBJECT G_TYPE_MAKE_FUNDAMENTAL (20)
183 * G_TYPE_VARIANT:
185 * The fundamental type corresponding to #GVariant.
187 * All floating #GVariant instances passed through the #GType system are
188 * consumed.
190 * Note that callbacks in closures, and signal handlers
191 * for signals of return type %G_TYPE_VARIANT, must never return floating
192 * variants.
194 * Note: GLib 2.24 did include a boxed type with this name. It was replaced
195 * with this fundamental type in 2.26.
197 * Since: 2.26
199 #define G_TYPE_VARIANT G_TYPE_MAKE_FUNDAMENTAL (21)
202 /* Reserved fundamental type numbers to create new fundamental
203 * type IDs with G_TYPE_MAKE_FUNDAMENTAL().
204 * Send email to gtk-devel-list@gnome.org for reservations.
207 * G_TYPE_FUNDAMENTAL_SHIFT:
209 * Shift value used in converting numbers to type IDs.
211 #define G_TYPE_FUNDAMENTAL_SHIFT (2)
213 * G_TYPE_MAKE_FUNDAMENTAL:
214 * @x: the fundamental type number.
216 * Get the type ID for the fundamental type number @x.
217 * Use g_type_fundamental_next() instead of this macro to create new fundamental
218 * types.
220 * Returns: the GType
222 #define G_TYPE_MAKE_FUNDAMENTAL(x) ((GType) ((x) << G_TYPE_FUNDAMENTAL_SHIFT))
224 * G_TYPE_RESERVED_GLIB_FIRST:
226 * First fundamental type number to create a new fundamental type id with
227 * G_TYPE_MAKE_FUNDAMENTAL() reserved for GLib.
229 #define G_TYPE_RESERVED_GLIB_FIRST (22)
231 * G_TYPE_RESERVED_GLIB_LAST:
233 * Last fundamental type number reserved for GLib.
235 #define G_TYPE_RESERVED_GLIB_LAST (31)
237 * G_TYPE_RESERVED_BSE_FIRST:
239 * First fundamental type number to create a new fundamental type id with
240 * G_TYPE_MAKE_FUNDAMENTAL() reserved for BSE.
242 #define G_TYPE_RESERVED_BSE_FIRST (32)
244 * G_TYPE_RESERVED_BSE_LAST:
246 * Last fundamental type number reserved for BSE.
248 #define G_TYPE_RESERVED_BSE_LAST (48)
250 * G_TYPE_RESERVED_USER_FIRST:
252 * First available fundamental type number to create new fundamental
253 * type id with G_TYPE_MAKE_FUNDAMENTAL().
255 #define G_TYPE_RESERVED_USER_FIRST (49)
258 /* Type Checking Macros
261 * G_TYPE_IS_FUNDAMENTAL:
262 * @type: A #GType value.
264 * Checks if @type is a fundamental type.
266 * Returns: %TRUE on success.
268 #define G_TYPE_IS_FUNDAMENTAL(type) ((type) <= G_TYPE_FUNDAMENTAL_MAX)
270 * G_TYPE_IS_DERIVED:
271 * @type: A #GType value.
273 * Checks if @type is derived (or in object-oriented terminology:
274 * inherited) from another type (this holds true for all non-fundamental
275 * types).
277 * Returns: %TRUE on success.
279 #define G_TYPE_IS_DERIVED(type) ((type) > G_TYPE_FUNDAMENTAL_MAX)
281 * G_TYPE_IS_INTERFACE:
282 * @type: A #GType value.
284 * Checks if @type is an interface type.
285 * An interface type provides a pure API, the implementation
286 * of which is provided by another type (which is then said to conform
287 * to the interface). GLib interfaces are somewhat analogous to Java
288 * interfaces and C++ classes containing only pure virtual functions,
289 * with the difference that GType interfaces are not derivable (but see
290 * g_type_interface_add_prerequisite() for an alternative).
292 * Returns: %TRUE on success.
294 #define G_TYPE_IS_INTERFACE(type) (G_TYPE_FUNDAMENTAL (type) == G_TYPE_INTERFACE)
296 * G_TYPE_IS_CLASSED:
297 * @type: A #GType value.
299 * Checks if @type is a classed type.
301 * Returns: %TRUE on success.
303 #define G_TYPE_IS_CLASSED(type) (g_type_test_flags ((type), G_TYPE_FLAG_CLASSED))
305 * G_TYPE_IS_INSTANTIATABLE:
306 * @type: A #GType value.
308 * Checks if @type can be instantiated. Instantiation is the
309 * process of creating an instance (object) of this type.
311 * Returns: %TRUE on success.
313 #define G_TYPE_IS_INSTANTIATABLE(type) (g_type_test_flags ((type), G_TYPE_FLAG_INSTANTIATABLE))
315 * G_TYPE_IS_DERIVABLE:
316 * @type: A #GType value.
318 * Checks if @type is a derivable type. A derivable type can
319 * be used as the base class of a flat (single-level) class hierarchy.
321 * Returns: %TRUE on success.
323 #define G_TYPE_IS_DERIVABLE(type) (g_type_test_flags ((type), G_TYPE_FLAG_DERIVABLE))
325 * G_TYPE_IS_DEEP_DERIVABLE:
326 * @type: A #GType value.
328 * Checks if @type is a deep derivable type. A deep derivable type
329 * can be used as the base class of a deep (multi-level) class hierarchy.
331 * Returns: %TRUE on success.
333 #define G_TYPE_IS_DEEP_DERIVABLE(type) (g_type_test_flags ((type), G_TYPE_FLAG_DEEP_DERIVABLE))
335 * G_TYPE_IS_ABSTRACT:
336 * @type: A #GType value.
338 * Checks if @type is an abstract type. An abstract type can not be
339 * instantiated and is normally used as an abstract base class for
340 * derived classes.
342 * Returns: %TRUE on success.
344 #define G_TYPE_IS_ABSTRACT(type) (g_type_test_flags ((type), G_TYPE_FLAG_ABSTRACT))
346 * G_TYPE_IS_VALUE_ABSTRACT:
347 * @type: A #GType value.
349 * Checks if @type is an abstract value type. An abstract value type introduces
350 * a value table, but can't be used for g_value_init() and is normally used as
351 * an abstract base type for derived value types.
353 * Returns: %TRUE on success.
355 #define G_TYPE_IS_VALUE_ABSTRACT(type) (g_type_test_flags ((type), G_TYPE_FLAG_VALUE_ABSTRACT))
357 * G_TYPE_IS_VALUE_TYPE:
358 * @type: A #GType value.
360 * Checks if @type is a value type and can be used with g_value_init().
362 * Returns: %TRUE on success.
364 #define G_TYPE_IS_VALUE_TYPE(type) (g_type_check_is_value_type (type))
366 * G_TYPE_HAS_VALUE_TABLE:
367 * @type: A #GType value.
369 * Checks if @type has a #GTypeValueTable.
371 * Returns: %TRUE on success.
373 #define G_TYPE_HAS_VALUE_TABLE(type) (g_type_value_table_peek (type) != NULL)
376 /* Typedefs
379 * GType:
381 * A numerical value which represents the unique identifier of a registered
382 * type.
384 #if GLIB_SIZEOF_SIZE_T != GLIB_SIZEOF_LONG || !defined __cplusplus
385 typedef gsize GType;
386 #else /* for historic reasons, C++ links against gulong GTypes */
387 typedef gulong GType;
388 #endif
389 typedef struct _GValue GValue;
390 typedef union _GTypeCValue GTypeCValue;
391 typedef struct _GTypePlugin GTypePlugin;
392 typedef struct _GTypeClass GTypeClass;
393 typedef struct _GTypeInterface GTypeInterface;
394 typedef struct _GTypeInstance GTypeInstance;
395 typedef struct _GTypeInfo GTypeInfo;
396 typedef struct _GTypeFundamentalInfo GTypeFundamentalInfo;
397 typedef struct _GInterfaceInfo GInterfaceInfo;
398 typedef struct _GTypeValueTable GTypeValueTable;
399 typedef struct _GTypeQuery GTypeQuery;
402 /* Basic Type Structures
405 * GTypeClass:
407 * An opaque structure used as the base of all classes.
409 struct _GTypeClass
411 /*< private >*/
412 GType g_type;
415 * GTypeInstance:
417 * An opaque structure used as the base of all type instances.
419 struct _GTypeInstance
421 /*< private >*/
422 GTypeClass *g_class;
425 * GTypeInterface:
427 * An opaque structure used as the base of all interface types.
429 struct _GTypeInterface
431 /*< private >*/
432 GType g_type; /* iface type */
433 GType g_instance_type;
436 * GTypeQuery:
437 * @type: the #GType value of the type.
438 * @type_name: the name of the type.
439 * @class_size: the size of the class structure.
440 * @instance_size: the size of the instance structure.
442 * A structure holding information for a specific type. It is
443 * filled in by the g_type_query() function.
445 struct _GTypeQuery
447 GType type;
448 const gchar *type_name;
449 guint class_size;
450 guint instance_size;
454 /* Casts, checks and accessors for structured types
455 * usage of these macros is reserved to type implementations only
457 /*< protected >*/
459 * G_TYPE_CHECK_INSTANCE:
460 * @instance: Location of a #GTypeInstance structure.
462 * Checks if @instance is a valid #GTypeInstance structure,
463 * otherwise issues a warning and returns %FALSE.
465 * This macro should only be used in type implementations.
467 * Returns: %TRUE on success.
469 #define G_TYPE_CHECK_INSTANCE(instance) (_G_TYPE_CHI ((GTypeInstance*) (instance)))
471 * G_TYPE_CHECK_INSTANCE_CAST:
472 * @instance: Location of a #GTypeInstance structure.
473 * @g_type: The type to be returned.
474 * @c_type: The corresponding C type of @g_type.
476 * Checks that @instance is an instance of the type identified by @g_type
477 * and issues a warning if this is not the case. Returns @instance casted
478 * to a pointer to @c_type.
480 * This macro should only be used in type implementations.
482 #define G_TYPE_CHECK_INSTANCE_CAST(instance, g_type, c_type) (_G_TYPE_CIC ((instance), (g_type), c_type))
484 * G_TYPE_CHECK_INSTANCE_TYPE:
485 * @instance: Location of a #GTypeInstance structure.
486 * @g_type: The type to be checked
488 * Checks if @instance is an instance of the type identified by @g_type.
490 * This macro should only be used in type implementations.
492 * Returns: %TRUE on success.
494 #define G_TYPE_CHECK_INSTANCE_TYPE(instance, g_type) (_G_TYPE_CIT ((instance), (g_type)))
496 * G_TYPE_INSTANCE_GET_CLASS:
497 * @instance: Location of the #GTypeInstance structure.
498 * @g_type: The #GType of the class to be returned.
499 * @c_type: The C type of the class structure.
501 * Get the class structure of a given @instance, casted
502 * to a specified ancestor type @g_type of the instance.
504 * Note that while calling a GInstanceInitFunc(), the class pointer gets
505 * modified, so it might not always return the expected pointer.
507 * This macro should only be used in type implementations.
509 * Returns: a pointer to the class structure
511 #define G_TYPE_INSTANCE_GET_CLASS(instance, g_type, c_type) (_G_TYPE_IGC ((instance), (g_type), c_type))
513 * G_TYPE_INSTANCE_GET_INTERFACE:
514 * @instance: Location of the #GTypeInstance structure.
515 * @g_type: The #GType of the interface to be returned.
516 * @c_type: The C type of the interface structure.
518 * Get the interface structure for interface @g_type of a given @instance.
520 * This macro should only be used in type implementations.
522 * Returns: a pointer to the interface structure
524 #define G_TYPE_INSTANCE_GET_INTERFACE(instance, g_type, c_type) (_G_TYPE_IGI ((instance), (g_type), c_type))
526 * G_TYPE_CHECK_CLASS_CAST:
527 * @g_class: Location of a #GTypeClass structure.
528 * @g_type: The type to be returned.
529 * @c_type: The corresponding C type of class structure of @g_type.
531 * Checks that @g_class is a class structure of the type identified by @g_type
532 * and issues a warning if this is not the case. Returns @g_class casted
533 * to a pointer to @c_type.
535 * This macro should only be used in type implementations.
537 #define G_TYPE_CHECK_CLASS_CAST(g_class, g_type, c_type) (_G_TYPE_CCC ((g_class), (g_type), c_type))
539 * G_TYPE_CHECK_CLASS_TYPE:
540 * @g_class: Location of a #GTypeClass structure.
541 * @g_type: The type to be checked.
543 * Checks if @g_class is a class structure of the type identified by
544 * @g_type.
546 * This macro should only be used in type implementations.
548 * Returns: %TRUE on success.
550 #define G_TYPE_CHECK_CLASS_TYPE(g_class, g_type) (_G_TYPE_CCT ((g_class), (g_type)))
552 * G_TYPE_CHECK_VALUE:
553 * @value: a #GValue
555 * Checks if @value has been initialized to hold values
556 * of a value type.
558 * This macro should only be used in type implementations.
560 * Returns: %TRUE on success.
562 #define G_TYPE_CHECK_VALUE(value) (_G_TYPE_CHV ((value)))
564 * G_TYPE_CHECK_VALUE_TYPE:
565 * @value: a #GValue
566 * @g_type: The type to be checked.
568 * Checks if @value has been initialized to hold values
569 * of type @g_type.
571 * This macro should only be used in type implementations.
573 * Returns: %TRUE on success.
575 #define G_TYPE_CHECK_VALUE_TYPE(value, g_type) (_G_TYPE_CVH ((value), (g_type)))
577 * G_TYPE_FROM_INSTANCE:
578 * @instance: Location of a valid #GTypeInstance structure.
580 * Get the type identifier from a given @instance structure.
582 * This macro should only be used in type implementations.
584 * Returns: the #GType
586 #define G_TYPE_FROM_INSTANCE(instance) (G_TYPE_FROM_CLASS (((GTypeInstance*) (instance))->g_class))
588 * G_TYPE_FROM_CLASS:
589 * @g_class: Location of a valid #GTypeClass structure.
591 * Get the type identifier from a given @class structure.
593 * This macro should only be used in type implementations.
595 * Returns: the #GType
597 #define G_TYPE_FROM_CLASS(g_class) (((GTypeClass*) (g_class))->g_type)
599 * G_TYPE_FROM_INTERFACE:
600 * @g_iface: Location of a valid #GTypeInterface structure.
602 * Get the type identifier from a given @interface structure.
604 * This macro should only be used in type implementations.
606 * Returns: the #GType
608 #define G_TYPE_FROM_INTERFACE(g_iface) (((GTypeInterface*) (g_iface))->g_type)
611 * G_TYPE_INSTANCE_GET_PRIVATE:
612 * @instance: the instance of a type deriving from @private_type.
613 * @g_type: the type identifying which private data to retrieve.
614 * @c_type: The C type for the private structure.
616 * Gets the private structure for a particular type.
617 * The private structure must have been registered in the
618 * class_init function with g_type_class_add_private().
620 * This macro should only be used in type implementations.
622 * Since: 2.4
623 * Returns: a pointer to the private data structure.
625 #define G_TYPE_INSTANCE_GET_PRIVATE(instance, g_type, c_type) ((c_type*) g_type_instance_get_private ((GTypeInstance*) (instance), (g_type)))
628 * G_TYPE_CLASS_GET_PRIVATE:
629 * @klass: the class of a type deriving from @private_type.
630 * @g_type: the type identifying which private data to retrieve.
631 * @c_type: The C type for the private structure.
633 * Gets the private class structure for a particular type.
634 * The private structure must have been registered in the
635 * get_type() function with g_type_add_class_private().
637 * This macro should only be used in type implementations.
639 * Since: 2.24
640 * Returns: a pointer to the private data structure.
642 #define G_TYPE_CLASS_GET_PRIVATE(klass, g_type, c_type) ((c_type*) g_type_class_get_private ((GTypeClass*) (klass), (g_type)))
645 * GTypeDebugFlags:
646 * @G_TYPE_DEBUG_NONE: Print no messages.
647 * @G_TYPE_DEBUG_OBJECTS: Print messages about object bookkeeping.
648 * @G_TYPE_DEBUG_SIGNALS: Print messages about signal emissions.
649 * @G_TYPE_DEBUG_MASK: Mask covering all debug flags.
651 * The <type>GTypeDebugFlags</type> enumeration values can be passed to
652 * g_type_init_with_debug_flags() to trigger debugging messages during runtime.
653 * Note that the messages can also be triggered by setting the
654 * <envar>GOBJECT_DEBUG</envar> environment variable to a ':'-separated list of
655 * "objects" and "signals".
657 typedef enum /*< skip >*/
659 G_TYPE_DEBUG_NONE = 0,
660 G_TYPE_DEBUG_OBJECTS = 1 << 0,
661 G_TYPE_DEBUG_SIGNALS = 1 << 1,
662 G_TYPE_DEBUG_MASK = 0x03
663 } GTypeDebugFlags;
666 /* --- prototypes --- */
667 void g_type_init (void);
668 void g_type_init_with_debug_flags (GTypeDebugFlags debug_flags);
669 G_CONST_RETURN gchar* g_type_name (GType type);
670 GQuark g_type_qname (GType type);
671 GType g_type_from_name (const gchar *name);
672 GType g_type_parent (GType type);
673 guint g_type_depth (GType type);
674 GType g_type_next_base (GType leaf_type,
675 GType root_type);
676 gboolean g_type_is_a (GType type,
677 GType is_a_type);
678 gpointer g_type_class_ref (GType type);
679 gpointer g_type_class_peek (GType type);
680 gpointer g_type_class_peek_static (GType type);
681 void g_type_class_unref (gpointer g_class);
682 gpointer g_type_class_peek_parent (gpointer g_class);
683 gpointer g_type_interface_peek (gpointer instance_class,
684 GType iface_type);
685 gpointer g_type_interface_peek_parent (gpointer g_iface);
687 gpointer g_type_default_interface_ref (GType g_type);
688 gpointer g_type_default_interface_peek (GType g_type);
689 void g_type_default_interface_unref (gpointer g_iface);
691 /* g_free() the returned arrays */
692 GType* g_type_children (GType type,
693 guint *n_children);
694 GType* g_type_interfaces (GType type,
695 guint *n_interfaces);
697 /* per-type _static_ data */
698 void g_type_set_qdata (GType type,
699 GQuark quark,
700 gpointer data);
701 gpointer g_type_get_qdata (GType type,
702 GQuark quark);
703 void g_type_query (GType type,
704 GTypeQuery *query);
707 /* --- type registration --- */
709 * GBaseInitFunc:
710 * @g_class: The #GTypeClass structure to initialize.
712 * A callback function used by the type system to do base initialization
713 * of the class structures of derived types. It is called as part of the
714 * initialization process of all derived classes and should reallocate
715 * or reset all dynamic class members copied over from the parent class.
716 * For example, class members (such as strings) that are not sufficiently
717 * handled by a plain memory copy of the parent class into the derived class
718 * have to be altered. See GClassInitFunc() for a discussion of the class
719 * intialization process.
721 typedef void (*GBaseInitFunc) (gpointer g_class);
723 * GBaseFinalizeFunc:
724 * @g_class: The #GTypeClass structure to finalize.
726 * A callback function used by the type system to finalize those portions
727 * of a derived types class structure that were setup from the corresponding
728 * GBaseInitFunc() function. Class finalization basically works the inverse
729 * way in which class intialization is performed.
730 * See GClassInitFunc() for a discussion of the class intialization process.
732 typedef void (*GBaseFinalizeFunc) (gpointer g_class);
734 * GClassInitFunc:
735 * @g_class: The #GTypeClass structure to initialize.
736 * @class_data: The @class_data member supplied via the #GTypeInfo structure.
738 * A callback function used by the type system to initialize the class
739 * of a specific type. This function should initialize all static class
740 * members.
741 * The initialization process of a class involves:
742 * <itemizedlist>
743 * <listitem><para>
744 * 1 - Copying common members from the parent class over to the
745 * derived class structure.
746 * </para></listitem>
747 * <listitem><para>
748 * 2 - Zero initialization of the remaining members not copied
749 * over from the parent class.
750 * </para></listitem>
751 * <listitem><para>
752 * 3 - Invocation of the GBaseInitFunc() initializers of all parent
753 * types and the class' type.
754 * </para></listitem>
755 * <listitem><para>
756 * 4 - Invocation of the class' GClassInitFunc() initializer.
757 * </para></listitem>
758 * </itemizedlist>
759 * Since derived classes are partially initialized through a memory copy
760 * of the parent class, the general rule is that GBaseInitFunc() and
761 * GBaseFinalizeFunc() should take care of necessary reinitialization
762 * and release of those class members that were introduced by the type
763 * that specified these GBaseInitFunc()/GBaseFinalizeFunc().
764 * GClassInitFunc() should only care about initializing static
765 * class members, while dynamic class members (such as allocated strings
766 * or reference counted resources) are better handled by a GBaseInitFunc()
767 * for this type, so proper initialization of the dynamic class members
768 * is performed for class initialization of derived types as well.
769 * An example may help to correspond the intend of the different class
770 * initializers:
772 * |[
773 * typedef struct {
774 * GObjectClass parent_class;
775 * gint static_integer;
776 * gchar *dynamic_string;
777 * } TypeAClass;
778 * static void
779 * type_a_base_class_init (TypeAClass *class)
781 * class->dynamic_string = g_strdup ("some string");
783 * static void
784 * type_a_base_class_finalize (TypeAClass *class)
786 * g_free (class->dynamic_string);
788 * static void
789 * type_a_class_init (TypeAClass *class)
791 * class->static_integer = 42;
794 * typedef struct {
795 * TypeAClass parent_class;
796 * gfloat static_float;
797 * GString *dynamic_gstring;
798 * } TypeBClass;
799 * static void
800 * type_b_base_class_init (TypeBClass *class)
802 * class->dynamic_gstring = g_string_new ("some other string");
804 * static void
805 * type_b_base_class_finalize (TypeBClass *class)
807 * g_string_free (class->dynamic_gstring);
809 * static void
810 * type_b_class_init (TypeBClass *class)
812 * class->static_float = 3.14159265358979323846;
814 * ]|
815 * Initialization of TypeBClass will first cause initialization of
816 * TypeAClass (derived classes reference their parent classes, see
817 * g_type_class_ref() on this).
818 * Initialization of TypeAClass roughly involves zero-initializing its fields,
819 * then calling its GBaseInitFunc() type_a_base_class_init() to allocate
820 * its dynamic members (dynamic_string), and finally calling its GClassInitFunc()
821 * type_a_class_init() to initialize its static members (static_integer).
822 * The first step in the initialization process of TypeBClass is then
823 * a plain memory copy of the contents of TypeAClass into TypeBClass and
824 * zero-initialization of the remaining fields in TypeBClass.
825 * The dynamic members of TypeAClass within TypeBClass now need
826 * reinitialization which is performed by calling type_a_base_class_init()
827 * with an argument of TypeBClass.
828 * After that, the GBaseInitFunc() of TypeBClass, type_b_base_class_init()
829 * is called to allocate the dynamic members of TypeBClass (dynamic_gstring),
830 * and finally the GClassInitFunc() of TypeBClass, type_b_class_init(),
831 * is called to complete the initialization process with the static members
832 * (static_float).
833 * Corresponding finalization counter parts to the GBaseInitFunc() functions
834 * have to be provided to release allocated resources at class finalization
835 * time.
837 typedef void (*GClassInitFunc) (gpointer g_class,
838 gpointer class_data);
840 * GClassFinalizeFunc:
841 * @g_class: The #GTypeClass structure to finalize.
842 * @class_data: The @class_data member supplied via the #GTypeInfo structure.
844 * A callback function used by the type system to finalize a class.
845 * This function is rarely needed, as dynamically allocated class resources
846 * should be handled by GBaseInitFunc() and GBaseFinalizeFunc().
847 * Also, specification of a GClassFinalizeFunc() in the #GTypeInfo
848 * structure of a static type is invalid, because classes of static types
849 * will never be finalized (they are artificially kept alive when their
850 * reference count drops to zero).
852 typedef void (*GClassFinalizeFunc) (gpointer g_class,
853 gpointer class_data);
855 * GInstanceInitFunc:
856 * @instance: The instance to initialize.
857 * @g_class: The class of the type the instance is created for.
859 * A callback function used by the type system to initialize a new
860 * instance of a type. This function initializes all instance members and
861 * allocates any resources required by it.
862 * Initialization of a derived instance involves calling all its parent
863 * types instance initializers, so the class member of the instance
864 * is altered during its initialization to always point to the class that
865 * belongs to the type the current initializer was introduced for.
867 typedef void (*GInstanceInitFunc) (GTypeInstance *instance,
868 gpointer g_class);
870 * GInterfaceInitFunc:
871 * @g_iface: The interface structure to initialize.
872 * @iface_data: The @interface_data supplied via the #GInterfaceInfo structure.
874 * A callback function used by the type system to initialize a new
875 * interface. This function should initialize all internal data and
876 * allocate any resources required by the interface.
878 typedef void (*GInterfaceInitFunc) (gpointer g_iface,
879 gpointer iface_data);
881 * GInterfaceFinalizeFunc:
882 * @g_iface: The interface structure to finalize.
883 * @iface_data: The @interface_data supplied via the #GInterfaceInfo structure.
885 * A callback function used by the type system to finalize an interface.
886 * This function should destroy any internal data and release any resources
887 * allocated by the corresponding GInterfaceInitFunc() function.
889 typedef void (*GInterfaceFinalizeFunc) (gpointer g_iface,
890 gpointer iface_data);
892 * GTypeClassCacheFunc:
893 * @cache_data: data that was given to the g_type_add_class_cache_func() call
894 * @g_class: The #GTypeClass structure which is unreferenced
896 * A callback function which is called when the reference count of a class
897 * drops to zero. It may use g_type_class_ref() to prevent the class from
898 * being freed. You should not call g_type_class_unref() from a
899 * #GTypeClassCacheFunc function to prevent infinite recursion, use
900 * g_type_class_unref_uncached() instead.
902 * The functions have to check the class id passed in to figure
903 * whether they actually want to cache the class of this type, since all
904 * classes are routed through the same #GTypeClassCacheFunc chain.
906 * Returns: %TRUE to stop further #GTypeClassCacheFunc<!-- -->s from being
907 * called, %FALSE to continue.
909 typedef gboolean (*GTypeClassCacheFunc) (gpointer cache_data,
910 GTypeClass *g_class);
912 * GTypeInterfaceCheckFunc:
913 * @check_data: data passed to g_type_add_interface_check().
914 * @g_iface: the interface that has been initialized
916 * A callback called after an interface vtable is initialized.
917 * See g_type_add_interface_check().
919 * Since: 2.4
921 typedef void (*GTypeInterfaceCheckFunc) (gpointer check_data,
922 gpointer g_iface);
924 * GTypeFundamentalFlags:
925 * @G_TYPE_FLAG_CLASSED: Indicates a classed type.
926 * @G_TYPE_FLAG_INSTANTIATABLE: Indicates an instantiable type (implies classed).
927 * @G_TYPE_FLAG_DERIVABLE: Indicates a flat derivable type.
928 * @G_TYPE_FLAG_DEEP_DERIVABLE: Indicates a deep derivable type (implies derivable).
930 * Bit masks used to check or determine specific characteristics of a
931 * fundamental type.
933 typedef enum /*< skip >*/
935 G_TYPE_FLAG_CLASSED = (1 << 0),
936 G_TYPE_FLAG_INSTANTIATABLE = (1 << 1),
937 G_TYPE_FLAG_DERIVABLE = (1 << 2),
938 G_TYPE_FLAG_DEEP_DERIVABLE = (1 << 3)
939 } GTypeFundamentalFlags;
941 * GTypeFlags:
942 * @G_TYPE_FLAG_ABSTRACT: Indicates an abstract type. No instances can be
943 * created for an abstract type.
944 * @G_TYPE_FLAG_VALUE_ABSTRACT: Indicates an abstract value type, i.e. a type
945 * that introduces a value table, but can't be used for
946 * g_value_init().
948 * Bit masks used to check or determine characteristics of a type.
950 typedef enum /*< skip >*/
952 G_TYPE_FLAG_ABSTRACT = (1 << 4),
953 G_TYPE_FLAG_VALUE_ABSTRACT = (1 << 5)
954 } GTypeFlags;
956 * GTypeInfo:
957 * @class_size: Size of the class structure (required for interface, classed and instantiatable types).
958 * @base_init: Location of the base initialization function (optional).
959 * @base_finalize: Location of the base finalization function (optional).
960 * @class_init: Location of the class initialization function for
961 * classed and instantiatable types. Location of the default vtable
962 * inititalization function for interface types. (optional) This function
963 * is used both to fill in virtual functions in the class or default vtable,
964 * and to do type-specific setup such as registering signals and object
965 * properties.
966 * @class_finalize: Location of the class finalization function for
967 * classed and instantiatable types. Location fo the default vtable
968 * finalization function for interface types. (optional)
969 * @class_data: User-supplied data passed to the class init/finalize functions.
970 * @instance_size: Size of the instance (object) structure (required for instantiatable types only).
971 * @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, it is ignored, since instances are allocated with the <link linkend="glib-Memory-Slices">slice allocator</link> now.
972 * @instance_init: Location of the instance initialization function (optional, for instantiatable types only).
973 * @value_table: A #GTypeValueTable function table for generic handling of GValues of this type (usually only
974 * useful for fundamental types).
976 * This structure is used to provide the type system with the information
977 * required to initialize and destruct (finalize) a type's class and
978 * its instances.
979 * The initialized structure is passed to the g_type_register_static() function
980 * (or is copied into the provided #GTypeInfo structure in the
981 * g_type_plugin_complete_type_info()). The type system will perform a deep
982 * copy of this structure, so its memory does not need to be persistent
983 * across invocation of g_type_register_static().
985 struct _GTypeInfo
987 /* interface types, classed types, instantiated types */
988 guint16 class_size;
990 GBaseInitFunc base_init;
991 GBaseFinalizeFunc base_finalize;
993 /* interface types, classed types, instantiated types */
994 GClassInitFunc class_init;
995 GClassFinalizeFunc class_finalize;
996 gconstpointer class_data;
998 /* instantiated types */
999 guint16 instance_size;
1000 guint16 n_preallocs;
1001 GInstanceInitFunc instance_init;
1003 /* value handling */
1004 const GTypeValueTable *value_table;
1007 * GTypeFundamentalInfo:
1008 * @type_flags: #GTypeFundamentalFlags describing the characteristics of the fundamental type
1010 * A structure that provides information to the type system which is
1011 * used specifically for managing fundamental types.
1013 struct _GTypeFundamentalInfo
1015 GTypeFundamentalFlags type_flags;
1018 * GInterfaceInfo:
1019 * @interface_init: location of the interface initialization function
1020 * @interface_finalize: location of the interface finalization function
1021 * @interface_data: user-supplied data passed to the interface init/finalize functions
1023 * A structure that provides information to the type system which is
1024 * used specifically for managing interface types.
1026 struct _GInterfaceInfo
1028 GInterfaceInitFunc interface_init;
1029 GInterfaceFinalizeFunc interface_finalize;
1030 gpointer interface_data;
1033 * GTypeValueTable:
1034 * @value_init: Default initialize @values contents by poking values
1035 * directly into the value->data array. The data array of
1036 * the #GValue passed into this function was zero-filled
1037 * with <function>memset()</function>, so no care has to
1038 * be taken to free any
1039 * old contents. E.g. for the implementation of a string
1040 * value that may never be %NULL, the implementation might
1041 * look like:
1042 * |[
1043 * value->data[0].v_pointer = g_strdup ("");
1044 * ]|
1045 * @value_free: Free any old contents that might be left in the
1046 * data array of the passed in @value. No resources may
1047 * remain allocated through the #GValue contents after
1048 * this function returns. E.g. for our above string type:
1049 * |[
1050 * // only free strings without a specific flag for static storage
1051 * if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
1052 * g_free (value->data[0].v_pointer);
1053 * ]|
1054 * @value_copy: @dest_value is a #GValue with zero-filled data section
1055 * and @src_value is a properly setup #GValue of same or
1056 * derived type.
1057 * The purpose of this function is to copy the contents of
1058 * @src_value into @dest_value in a way, that even after
1059 * @src_value has been freed, the contents of @dest_value
1060 * remain valid. String type example:
1061 * |[
1062 * dest_value->data[0].v_pointer = g_strdup (src_value->data[0].v_pointer);
1063 * ]|
1064 * @value_peek_pointer: If the value contents fit into a pointer, such as objects
1065 * or strings, return this pointer, so the caller can peek at
1066 * the current contents. To extend on our above string example:
1067 * |[
1068 * return value->data[0].v_pointer;
1069 * ]|
1070 * @collect_format: A string format describing how to collect the contents of
1071 * this value bit-by-bit. Each character in the format represents
1072 * an argument to be collected, and the characters themselves indicate
1073 * the type of the argument. Currently supported arguments are:
1074 * <variablelist>
1075 * <varlistentry><term /><listitem><para>
1076 * 'i' - Integers. passed as collect_values[].v_int.
1077 * </para></listitem></varlistentry>
1078 * <varlistentry><term /><listitem><para>
1079 * 'l' - Longs. passed as collect_values[].v_long.
1080 * </para></listitem></varlistentry>
1081 * <varlistentry><term /><listitem><para>
1082 * 'd' - Doubles. passed as collect_values[].v_double.
1083 * </para></listitem></varlistentry>
1084 * <varlistentry><term /><listitem><para>
1085 * 'p' - Pointers. passed as collect_values[].v_pointer.
1086 * </para></listitem></varlistentry>
1087 * </variablelist>
1088 * It should be noted that for variable argument list construction,
1089 * ANSI C promotes every type smaller than an integer to an int, and
1090 * floats to doubles. So for collection of short int or char, 'i'
1091 * needs to be used, and for collection of floats 'd'.
1092 * @collect_value: The collect_value() function is responsible for converting the
1093 * values collected from a variable argument list into contents
1094 * suitable for storage in a GValue. This function should setup
1095 * @value similar to value_init(); e.g. for a string value that
1096 * does not allow %NULL pointers, it needs to either spew an error,
1097 * or do an implicit conversion by storing an empty string.
1098 * The @value passed in to this function has a zero-filled data
1099 * array, so just like for value_init() it is guaranteed to not
1100 * contain any old contents that might need freeing.
1101 * @n_collect_values is exactly the string length of @collect_format,
1102 * and @collect_values is an array of unions #GTypeCValue with
1103 * length @n_collect_values, containing the collected values
1104 * according to @collect_format.
1105 * @collect_flags is an argument provided as a hint by the caller.
1106 * It may contain the flag %G_VALUE_NOCOPY_CONTENTS indicating,
1107 * that the collected value contents may be considered "static"
1108 * for the duration of the @value lifetime.
1109 * Thus an extra copy of the contents stored in @collect_values is
1110 * not required for assignment to @value.
1111 * For our above string example, we continue with:
1112 * |[
1113 * if (!collect_values[0].v_pointer)
1114 * value->data[0].v_pointer = g_strdup ("");
1115 * else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
1117 * value->data[0].v_pointer = collect_values[0].v_pointer;
1118 * // keep a flag for the value_free() implementation to not free this string
1119 * value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
1121 * else
1122 * value->data[0].v_pointer = g_strdup (collect_values[0].v_pointer);
1123 * return NULL;
1124 * ]|
1125 * It should be noted, that it is generally a bad idea to follow the
1126 * #G_VALUE_NOCOPY_CONTENTS hint for reference counted types. Due to
1127 * reentrancy requirements and reference count assertions performed
1128 * by the #GSignal code, reference counts should always be incremented
1129 * for reference counted contents stored in the value->data array.
1130 * To deviate from our string example for a moment, and taking a look
1131 * at an exemplary implementation for collect_value() of #GObject:
1132 * |[
1133 * if (collect_values[0].v_pointer)
1135 * GObject *object = G_OBJECT (collect_values[0].v_pointer);
1136 * // never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types
1137 * value->data[0].v_pointer = g_object_ref (object);
1138 * return NULL;
1140 * else
1141 * return g_strdup_printf ("Object passed as invalid NULL pointer");
1143 * ]|
1144 * The reference count for valid objects is always incremented,
1145 * regardless of @collect_flags. For invalid objects, the example
1146 * returns a newly allocated string without altering @value.
1147 * Upon success, collect_value() needs to return %NULL. If, however,
1148 * an error condition occurred, collect_value() may spew an
1149 * error by returning a newly allocated non-%NULL string, giving
1150 * a suitable description of the error condition.
1151 * The calling code makes no assumptions about the @value
1152 * contents being valid upon error returns, @value
1153 * is simply thrown away without further freeing. As such, it is
1154 * a good idea to not allocate #GValue contents, prior to returning
1155 * an error, however, collect_values() is not obliged to return
1156 * a correctly setup @value for error returns, simply because
1157 * any non-%NULL return is considered a fatal condition so further
1158 * program behaviour is undefined.
1159 * @lcopy_format: Format description of the arguments to collect for @lcopy_value,
1160 * analogous to @collect_format. Usually, @lcopy_format string consists
1161 * only of 'p's to provide lcopy_value() with pointers to storage locations.
1162 * @lcopy_value: This function is responsible for storing the @value contents into
1163 * arguments passed through a variable argument list which got
1164 * collected into @collect_values according to @lcopy_format.
1165 * @n_collect_values equals the string length of @lcopy_format,
1166 * and @collect_flags may contain %G_VALUE_NOCOPY_CONTENTS.
1167 * In contrast to collect_value(), lcopy_value() is obliged to
1168 * always properly support %G_VALUE_NOCOPY_CONTENTS.
1169 * Similar to collect_value() the function may prematurely abort
1170 * by returning a newly allocated string describing an error condition.
1171 * To complete the string example:
1172 * |[
1173 * gchar **string_p = collect_values[0].v_pointer;
1174 * if (!string_p)
1175 * return g_strdup_printf ("string location passed as NULL");
1176 * if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
1177 * *string_p = value->data[0].v_pointer;
1178 * else
1179 * *string_p = g_strdup (value->data[0].v_pointer);
1180 * ]|
1181 * And an illustrative version of lcopy_value() for
1182 * reference-counted types:
1183 * |[
1184 * GObject **object_p = collect_values[0].v_pointer;
1185 * if (!object_p)
1186 * return g_strdup_printf ("object location passed as NULL");
1187 * if (!value->data[0].v_pointer)
1188 * *object_p = NULL;
1189 * else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) // always honour
1190 * *object_p = value->data[0].v_pointer;
1191 * else
1192 * *object_p = g_object_ref (value->data[0].v_pointer);
1193 * return NULL;
1194 * ]|
1196 * The #GTypeValueTable provides the functions required by the #GValue implementation,
1197 * to serve as a container for values of a type.
1200 struct _GTypeValueTable
1202 void (*value_init) (GValue *value);
1203 void (*value_free) (GValue *value);
1204 void (*value_copy) (const GValue *src_value,
1205 GValue *dest_value);
1206 /* varargs functionality (optional) */
1207 gpointer (*value_peek_pointer) (const GValue *value);
1208 gchar *collect_format;
1209 gchar* (*collect_value) (GValue *value,
1210 guint n_collect_values,
1211 GTypeCValue *collect_values,
1212 guint collect_flags);
1213 gchar *lcopy_format;
1214 gchar* (*lcopy_value) (const GValue *value,
1215 guint n_collect_values,
1216 GTypeCValue *collect_values,
1217 guint collect_flags);
1219 GType g_type_register_static (GType parent_type,
1220 const gchar *type_name,
1221 const GTypeInfo *info,
1222 GTypeFlags flags);
1223 GType g_type_register_static_simple (GType parent_type,
1224 const gchar *type_name,
1225 guint class_size,
1226 GClassInitFunc class_init,
1227 guint instance_size,
1228 GInstanceInitFunc instance_init,
1229 GTypeFlags flags);
1231 GType g_type_register_dynamic (GType parent_type,
1232 const gchar *type_name,
1233 GTypePlugin *plugin,
1234 GTypeFlags flags);
1235 GType g_type_register_fundamental (GType type_id,
1236 const gchar *type_name,
1237 const GTypeInfo *info,
1238 const GTypeFundamentalInfo *finfo,
1239 GTypeFlags flags);
1240 void g_type_add_interface_static (GType instance_type,
1241 GType interface_type,
1242 const GInterfaceInfo *info);
1243 void g_type_add_interface_dynamic (GType instance_type,
1244 GType interface_type,
1245 GTypePlugin *plugin);
1246 void g_type_interface_add_prerequisite (GType interface_type,
1247 GType prerequisite_type);
1248 GType*g_type_interface_prerequisites (GType interface_type,
1249 guint *n_prerequisites);
1250 void g_type_class_add_private (gpointer g_class,
1251 gsize private_size);
1252 gpointer g_type_instance_get_private (GTypeInstance *instance,
1253 GType private_type);
1255 void g_type_add_class_private (GType class_type,
1256 gsize private_size);
1257 gpointer g_type_class_get_private (GTypeClass *klass,
1258 GType private_type);
1261 /* --- GType boilerplate --- */
1263 * G_DEFINE_TYPE:
1264 * @TN: The name of the new type, in Camel case.
1265 * @t_n: The name of the new type, in lowercase, with words
1266 * separated by '_'.
1267 * @T_P: The #GType of the parent type.
1269 * A convenience macro for type implementations, which declares a
1270 * class initialization function, an instance initialization function (see #GTypeInfo for information about
1271 * these) and a static variable named @t_n<!-- -->_parent_class pointing to the parent class. Furthermore, it defines
1272 * a *_get_type() function. See G_DEFINE_TYPE_EXTENDED() for an example.
1274 * Since: 2.4
1276 #define G_DEFINE_TYPE(TN, t_n, T_P) G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, 0, {})
1278 * G_DEFINE_TYPE_WITH_CODE:
1279 * @TN: The name of the new type, in Camel case.
1280 * @t_n: The name of the new type in lowercase, with words separated by '_'.
1281 * @T_P: The #GType of the parent type.
1282 * @_C_: Custom code that gets inserted in the *_get_type() function.
1284 * A convenience macro for type implementations.
1285 * Similar to G_DEFINE_TYPE(), but allows you to insert custom code into the
1286 * *_get_type() function, e.g. interface implementations via G_IMPLEMENT_INTERFACE().
1287 * See G_DEFINE_TYPE_EXTENDED() for an example.
1289 * Since: 2.4
1291 #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()
1293 * G_DEFINE_ABSTRACT_TYPE:
1294 * @TN: The name of the new type, in Camel case.
1295 * @t_n: The name of the new type, in lowercase, with words
1296 * separated by '_'.
1297 * @T_P: The #GType of the parent type.
1299 * A convenience macro for type implementations.
1300 * Similar to G_DEFINE_TYPE(), but defines an abstract type.
1301 * See G_DEFINE_TYPE_EXTENDED() for an example.
1303 * Since: 2.4
1305 #define G_DEFINE_ABSTRACT_TYPE(TN, t_n, T_P) G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, G_TYPE_FLAG_ABSTRACT, {})
1307 * G_DEFINE_ABSTRACT_TYPE_WITH_CODE:
1308 * @TN: The name of the new type, in Camel case.
1309 * @t_n: The name of the new type, in lowercase, with words
1310 * separated by '_'.
1311 * @T_P: The #GType of the parent type.
1312 * @_C_: Custom code that gets inserted in the @type_name_get_type() function.
1314 * A convenience macro for type implementations.
1315 * Similar to G_DEFINE_TYPE_WITH_CODE(), but defines an abstract type and allows you to
1316 * insert custom code into the *_get_type() function, e.g. interface implementations
1317 * via G_IMPLEMENT_INTERFACE(). See G_DEFINE_TYPE_EXTENDED() for an example.
1319 * Since: 2.4
1321 #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()
1323 * G_DEFINE_TYPE_EXTENDED:
1324 * @TN: The name of the new type, in Camel case.
1325 * @t_n: The name of the new type, in lowercase, with words
1326 * separated by '_'.
1327 * @T_P: The #GType of the parent type.
1328 * @_f_: #GTypeFlags to pass to g_type_register_static()
1329 * @_C_: Custom code that gets inserted in the *_get_type() function.
1331 * The most general convenience macro for type implementations, on which
1332 * G_DEFINE_TYPE(), etc are based.
1334 * |[
1335 * G_DEFINE_TYPE_EXTENDED (GtkGadget,
1336 * gtk_gadget,
1337 * GTK_TYPE_WIDGET,
1338 * 0,
1339 * G_IMPLEMENT_INTERFACE (TYPE_GIZMO,
1340 * gtk_gadget_gizmo_init));
1341 * ]|
1342 * expands to
1343 * |[
1344 * static void gtk_gadget_init (GtkGadget *self);
1345 * static void gtk_gadget_class_init (GtkGadgetClass *klass);
1346 * static gpointer gtk_gadget_parent_class = NULL;
1347 * static void gtk_gadget_class_intern_init (gpointer klass)
1349 * gtk_gadget_parent_class = g_type_class_peek_parent (klass);
1350 * gtk_gadget_class_init ((GtkGadgetClass*) klass);
1353 * GType
1354 * gtk_gadget_get_type (void)
1356 * static volatile gsize g_define_type_id__volatile = 0;
1357 * if (g_once_init_enter (&g_define_type_id__volatile))
1359 * GType g_define_type_id =
1360 * g_type_register_static_simple (GTK_TYPE_WIDGET,
1361 * g_intern_static_string ("GtkGadget"),
1362 * sizeof (GtkGadgetClass),
1363 * (GClassInitFunc) gtk_gadget_class_intern_init,
1364 * sizeof (GtkGadget),
1365 * (GInstanceInitFunc) gtk_gadget_init,
1366 * (GTypeFlags) flags);
1368 * static const GInterfaceInfo g_implement_interface_info = {
1369 * (GInterfaceInitFunc) gtk_gadget_gizmo_init
1370 * };
1371 * g_type_add_interface_static (g_define_type_id, TYPE_GIZMO, &g_implement_interface_info);
1373 * g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
1375 * return g_define_type_id__volatile;
1377 * ]|
1378 * The only pieces which have to be manually provided are the definitions of
1379 * the instance and class structure and the definitions of the instance and
1380 * class init functions.
1382 * Since: 2.4
1384 #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()
1387 * G_DEFINE_INTERFACE:
1388 * @TN: The name of the new type, in Camel case.
1389 * @t_n: The name of the new type, in lowercase, with words separated by '_'.
1390 * @T_P: The #GType of the prerequisite type for the interface, or 0
1391 * (%G_TYPE_INVALID) for no prerequisite type.
1393 * A convenience macro for #GTypeInterface definitions, which declares
1394 * a default vtable initialization function and defines a *_get_type()
1395 * function.
1397 * The macro expects the interface initialization function to have the
1398 * name <literal>t_n ## _default_init</literal>, and the interface
1399 * structure to have the name <literal>TN ## Interface</literal>.
1401 * Since: 2.24
1403 #define G_DEFINE_INTERFACE(TN, t_n, T_P) G_DEFINE_INTERFACE_WITH_CODE(TN, t_n, T_P, ;)
1406 * G_DEFINE_INTERFACE_WITH_CODE:
1407 * @TN: The name of the new type, in Camel case.
1408 * @t_n: The name of the new type, in lowercase, with words separated by '_'.
1409 * @T_P: The #GType of the prerequisite type for the interface, or 0
1410 * (%G_TYPE_INVALID) for no prerequisite type.
1411 * @_C_: Custom code that gets inserted in the *_get_type() function.
1413 * A convenience macro for #GTypeInterface definitions. Similar to
1414 * G_DEFINE_INTERFACE(), but allows you to insert custom code into the
1415 * *_get_type() function, e.g. additional interface implementations
1416 * via G_IMPLEMENT_INTERFACE(), or additional prerequisite types. See
1417 * G_DEFINE_TYPE_EXTENDED() for a similar example using
1418 * G_DEFINE_TYPE_WITH_CODE().
1420 * Since: 2.24
1422 #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()
1425 * G_IMPLEMENT_INTERFACE:
1426 * @TYPE_IFACE: The #GType of the interface to add
1427 * @iface_init: The interface init function
1429 * A convenience macro to ease interface addition in the @_C_ section
1430 * of G_DEFINE_TYPE_WITH_CODE() or G_DEFINE_ABSTRACT_TYPE_WITH_CODE().
1431 * See G_DEFINE_TYPE_EXTENDED() for an example.
1433 * Note that this macro can only be used together with the G_DEFINE_TYPE_*
1434 * macros, since it depends on variable names from those macros.
1436 * Since: 2.4
1438 #define G_IMPLEMENT_INTERFACE(TYPE_IFACE, iface_init) { \
1439 const GInterfaceInfo g_implement_interface_info = { \
1440 (GInterfaceInitFunc) iface_init, NULL, NULL \
1441 }; \
1442 g_type_add_interface_static (g_define_type_id, TYPE_IFACE, &g_implement_interface_info); \
1445 #define _G_DEFINE_TYPE_EXTENDED_BEGIN(TypeName, type_name, TYPE_PARENT, flags) \
1447 static void type_name##_init (TypeName *self); \
1448 static void type_name##_class_init (TypeName##Class *klass); \
1449 static gpointer type_name##_parent_class = NULL; \
1450 static void type_name##_class_intern_init (gpointer klass) \
1452 type_name##_parent_class = g_type_class_peek_parent (klass); \
1453 type_name##_class_init ((TypeName##Class*) klass); \
1456 GType \
1457 type_name##_get_type (void) \
1459 static volatile gsize g_define_type_id__volatile = 0; \
1460 if (g_once_init_enter (&g_define_type_id__volatile)) \
1462 GType g_define_type_id = \
1463 g_type_register_static_simple (TYPE_PARENT, \
1464 g_intern_static_string (#TypeName), \
1465 sizeof (TypeName##Class), \
1466 (GClassInitFunc) type_name##_class_intern_init, \
1467 sizeof (TypeName), \
1468 (GInstanceInitFunc) type_name##_init, \
1469 (GTypeFlags) flags); \
1470 { /* custom code follows */
1471 #define _G_DEFINE_TYPE_EXTENDED_END() \
1472 /* following custom code */ \
1474 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); \
1476 return g_define_type_id__volatile; \
1477 } /* closes type_name##_get_type() */
1479 #define _G_DEFINE_INTERFACE_EXTENDED_BEGIN(TypeName, type_name, TYPE_PREREQ) \
1481 static void type_name##_default_init (TypeName##Interface *klass); \
1483 GType \
1484 type_name##_get_type (void) \
1486 static volatile gsize g_define_type_id__volatile = 0; \
1487 if (g_once_init_enter (&g_define_type_id__volatile)) \
1489 GType g_define_type_id = \
1490 g_type_register_static_simple (G_TYPE_INTERFACE, \
1491 g_intern_static_string (#TypeName), \
1492 sizeof (TypeName##Interface), \
1493 (GClassInitFunc)type_name##_default_init, \
1494 0, \
1495 (GInstanceInitFunc)NULL, \
1496 (GTypeFlags) 0); \
1497 if (TYPE_PREREQ) \
1498 g_type_interface_add_prerequisite (g_define_type_id, TYPE_PREREQ); \
1499 { /* custom code follows */
1500 #define _G_DEFINE_INTERFACE_EXTENDED_END() \
1501 /* following custom code */ \
1503 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); \
1505 return g_define_type_id__volatile; \
1506 } /* closes type_name##_get_type() */
1509 * G_DEFINE_BOXED_TYPE:
1510 * @TypeName: The name of the new type, in Camel case.
1511 * @type_name: The name of the new type, in lowercase, with words
1512 * separated by '_'.
1513 * @copy_func: the #GBoxedCopyFunc for the new type
1514 * @free_func: the #GBoxedFreeFunc for the new type
1516 * A convenience macro for boxed type implementations, which defines a
1517 * type_name_get_type() function registering the boxed type.
1519 * Since: 2.26
1521 #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, {})
1523 * G_DEFINE_BOXED_TYPE_WITH_CODE:
1524 * @TypeName: The name of the new type, in Camel case.
1525 * @type_name: The name of the new type, in lowercase, with words
1526 * separated by '_'.
1527 * @copy_func: the #GBoxedCopyFunc for the new type
1528 * @free_func: the #GBoxedFreeFunc for the new type
1529 * @_C_: Custom code that gets inserted in the *_get_type() function.
1531 * A convenience macro for boxed type implementations.
1532 * Similar to G_DEFINE_BOXED_TYPE(), but allows to insert custom code into the
1533 * type_name_get_type() function, e.g. to register value transformations with
1534 * g_value_register_transform_func().
1536 * Since: 2.26
1538 #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()
1540 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
1541 #define _G_DEFINE_BOXED_TYPE_BEGIN(TypeName, type_name, copy_func, free_func) \
1542 GType \
1543 type_name##_get_type (void) \
1545 static volatile gsize g_define_type_id__volatile = 0; \
1546 if (g_once_init_enter (&g_define_type_id__volatile)) \
1548 GType (* _g_register_boxed) \
1549 (const gchar *, \
1550 union \
1552 TypeName * (*do_copy_type) (TypeName *); \
1553 TypeName * (*do_const_copy_type) (const TypeName *); \
1554 GBoxedCopyFunc do_copy_boxed; \
1555 } __attribute__((__transparent_union__)), \
1556 union \
1558 void (* do_free_type) (TypeName *); \
1559 GBoxedFreeFunc do_free_boxed; \
1560 } __attribute__((__transparent_union__)) \
1561 ) = g_boxed_type_register_static; \
1562 GType g_define_type_id = \
1563 _g_register_boxed (g_intern_static_string (#TypeName), copy_func, free_func); \
1564 { /* custom code follows */
1565 #else
1566 #define _G_DEFINE_BOXED_TYPE_BEGIN(TypeName, type_name, copy_func, free_func) \
1567 GType \
1568 type_name##_get_type (void) \
1570 static volatile gsize g_define_type_id__volatile = 0; \
1571 if (g_once_init_enter (&g_define_type_id__volatile)) \
1573 GType g_define_type_id = \
1574 g_boxed_type_register_static (g_intern_static_string (#TypeName), \
1575 (GBoxedCopyFunc) copy_func, \
1576 (GBoxedFreeFunc) free_func); \
1577 { /* custom code follows */
1578 #endif /* __GNUC__ */
1581 * G_DEFINE_POINTER_TYPE:
1582 * @TypeName: The name of the new type, in Camel case.
1583 * @type_name: The name of the new type, in lowercase, with words
1584 * separated by '_'.
1586 * A convenience macro for pointer type implementations, which defines a
1587 * type_name_get_type() function registering the pointer type.
1589 * Since: 2.26
1591 #define G_DEFINE_POINTER_TYPE(TypeName, type_name) G_DEFINE_POINTER_TYPE_WITH_CODE (TypeName, type_name, {})
1593 * G_DEFINE_POINTER_TYPE_WITH_CODE:
1594 * @TypeName: The name of the new type, in Camel case.
1595 * @type_name: The name of the new type, in lowercase, with words
1596 * separated by '_'.
1597 * @_C_: Custom code that gets inserted in the *_get_type() function.
1599 * A convenience macro for pointer type implementations.
1600 * Similar to G_DEFINE_POINTER_TYPE(), but allows to insert custom code into the
1601 * type_name_get_type() function.
1603 * Since: 2.26
1605 #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()
1607 #define _G_DEFINE_POINTER_TYPE_BEGIN(TypeName, type_name) \
1608 GType \
1609 type_name##_get_type (void) \
1611 static volatile gsize g_define_type_id__volatile = 0; \
1612 if (g_once_init_enter (&g_define_type_id__volatile)) \
1614 GType g_define_type_id = \
1615 g_pointer_type_register_static (g_intern_static_string (#TypeName)); \
1616 { /* custom code follows */
1618 /* --- protected (for fundamental type implementations) --- */
1619 GTypePlugin* g_type_get_plugin (GType type);
1620 GTypePlugin* g_type_interface_get_plugin (GType instance_type,
1621 GType interface_type);
1622 GType g_type_fundamental_next (void);
1623 GType g_type_fundamental (GType type_id);
1624 GTypeInstance* g_type_create_instance (GType type);
1625 void g_type_free_instance (GTypeInstance *instance);
1627 void g_type_add_class_cache_func (gpointer cache_data,
1628 GTypeClassCacheFunc cache_func);
1629 void g_type_remove_class_cache_func (gpointer cache_data,
1630 GTypeClassCacheFunc cache_func);
1631 void g_type_class_unref_uncached (gpointer g_class);
1633 void g_type_add_interface_check (gpointer check_data,
1634 GTypeInterfaceCheckFunc check_func);
1635 void g_type_remove_interface_check (gpointer check_data,
1636 GTypeInterfaceCheckFunc check_func);
1638 GTypeValueTable* g_type_value_table_peek (GType type);
1641 /*< private >*/
1642 gboolean g_type_check_instance (GTypeInstance *instance) G_GNUC_PURE;
1643 GTypeInstance* g_type_check_instance_cast (GTypeInstance *instance,
1644 GType iface_type);
1645 gboolean g_type_check_instance_is_a (GTypeInstance *instance,
1646 GType iface_type) G_GNUC_PURE;
1647 GTypeClass* g_type_check_class_cast (GTypeClass *g_class,
1648 GType is_a_type);
1649 gboolean g_type_check_class_is_a (GTypeClass *g_class,
1650 GType is_a_type) G_GNUC_PURE;
1651 gboolean g_type_check_is_value_type (GType type) G_GNUC_CONST;
1652 gboolean g_type_check_value (GValue *value) G_GNUC_PURE;
1653 gboolean g_type_check_value_holds (GValue *value,
1654 GType type) G_GNUC_PURE;
1655 gboolean g_type_test_flags (GType type,
1656 guint flags) G_GNUC_CONST;
1659 /* --- debugging functions --- */
1660 G_CONST_RETURN gchar* g_type_name_from_instance (GTypeInstance *instance);
1661 G_CONST_RETURN gchar* g_type_name_from_class (GTypeClass *g_class);
1664 /* --- internal functions --- */
1665 G_GNUC_INTERNAL void g_value_c_init (void); /* sync with gvalue.c */
1666 G_GNUC_INTERNAL void g_value_types_init (void); /* sync with gvaluetypes.c */
1667 G_GNUC_INTERNAL void g_enum_types_init (void); /* sync with genums.c */
1668 G_GNUC_INTERNAL void g_param_type_init (void); /* sync with gparam.c */
1669 G_GNUC_INTERNAL void g_boxed_type_init (void); /* sync with gboxed.c */
1670 G_GNUC_INTERNAL void g_object_type_init (void); /* sync with gobject.c */
1671 G_GNUC_INTERNAL void g_param_spec_types_init (void); /* sync with gparamspecs.c */
1672 G_GNUC_INTERNAL void g_value_transforms_init (void); /* sync with gvaluetransform.c */
1673 G_GNUC_INTERNAL void g_signal_init (void); /* sync with gsignal.c */
1676 /* --- implementation bits --- */
1677 #ifndef G_DISABLE_CAST_CHECKS
1678 # define _G_TYPE_CIC(ip, gt, ct) \
1679 ((ct*) g_type_check_instance_cast ((GTypeInstance*) ip, gt))
1680 # define _G_TYPE_CCC(cp, gt, ct) \
1681 ((ct*) g_type_check_class_cast ((GTypeClass*) cp, gt))
1682 #else /* G_DISABLE_CAST_CHECKS */
1683 # define _G_TYPE_CIC(ip, gt, ct) ((ct*) ip)
1684 # define _G_TYPE_CCC(cp, gt, ct) ((ct*) cp)
1685 #endif /* G_DISABLE_CAST_CHECKS */
1686 #define _G_TYPE_CHI(ip) (g_type_check_instance ((GTypeInstance*) ip))
1687 #define _G_TYPE_CHV(vl) (g_type_check_value ((GValue*) vl))
1688 #define _G_TYPE_IGC(ip, gt, ct) ((ct*) (((GTypeInstance*) ip)->g_class))
1689 #define _G_TYPE_IGI(ip, gt, ct) ((ct*) g_type_interface_peek (((GTypeInstance*) ip)->g_class, gt))
1690 #ifdef __GNUC__
1691 # define _G_TYPE_CIT(ip, gt) (G_GNUC_EXTENSION ({ \
1692 GTypeInstance *__inst = (GTypeInstance*) ip; GType __t = gt; gboolean __r; \
1693 if (!__inst) \
1694 __r = FALSE; \
1695 else if (__inst->g_class && __inst->g_class->g_type == __t) \
1696 __r = TRUE; \
1697 else \
1698 __r = g_type_check_instance_is_a (__inst, __t); \
1699 __r; \
1701 # define _G_TYPE_CCT(cp, gt) (G_GNUC_EXTENSION ({ \
1702 GTypeClass *__class = (GTypeClass*) cp; GType __t = gt; gboolean __r; \
1703 if (!__class) \
1704 __r = FALSE; \
1705 else if (__class->g_type == __t) \
1706 __r = TRUE; \
1707 else \
1708 __r = g_type_check_class_is_a (__class, __t); \
1709 __r; \
1711 # define _G_TYPE_CVH(vl, gt) (G_GNUC_EXTENSION ({ \
1712 GValue *__val = (GValue*) vl; GType __t = gt; gboolean __r; \
1713 if (!__val) \
1714 __r = FALSE; \
1715 else if (__val->g_type == __t) \
1716 __r = TRUE; \
1717 else \
1718 __r = g_type_check_value_holds (__val, __t); \
1719 __r; \
1721 #else /* !__GNUC__ */
1722 # define _G_TYPE_CIT(ip, gt) (g_type_check_instance_is_a ((GTypeInstance*) ip, gt))
1723 # define _G_TYPE_CCT(cp, gt) (g_type_check_class_is_a ((GTypeClass*) cp, gt))
1724 # define _G_TYPE_CVH(vl, gt) (g_type_check_value_holds ((GValue*) vl, gt))
1725 #endif /* !__GNUC__ */
1727 * G_TYPE_FLAG_RESERVED_ID_BIT:
1729 * A bit in the type number that's supposed to be left untouched.
1731 #define G_TYPE_FLAG_RESERVED_ID_BIT ((GType) (1 << 0))
1732 extern GTypeDebugFlags _g_type_debug_flags;
1734 G_END_DECLS
1736 #endif /* __G_TYPE_H__ */