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