gtype.h 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  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 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. #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
  20. #error "Only <glib-object.h> can be included directly."
  21. #endif
  22. #include <glib.h>
  23. G_BEGIN_DECLS
  24. /* Basic Type Macros
  25. */
  26. /**
  27. * G_TYPE_FUNDAMENTAL:
  28. * @type: A #GType value.
  29. *
  30. * The fundamental type which is the ancestor of @type.
  31. * Fundamental types are types that serve as ultimate bases for the derived types,
  32. * thus they are the roots of distinct inheritance hierarchies.
  33. */
  34. #define G_TYPE_FUNDAMENTAL(type) (g_type_fundamental (type))
  35. /**
  36. * G_TYPE_FUNDAMENTAL_MAX:
  37. *
  38. * An integer constant that represents the number of identifiers reserved
  39. * for types that are assigned at compile-time.
  40. */
  41. #define G_TYPE_FUNDAMENTAL_MAX (255 << G_TYPE_FUNDAMENTAL_SHIFT)
  42. /* Constant fundamental types,
  43. */
  44. /**
  45. * G_TYPE_INVALID:
  46. *
  47. * An invalid #GType used as error return value in some functions which return
  48. * a #GType.
  49. */
  50. #define G_TYPE_INVALID G_TYPE_MAKE_FUNDAMENTAL (0)
  51. /**
  52. * G_TYPE_NONE:
  53. *
  54. * A fundamental type which is used as a replacement for the C
  55. * void return type.
  56. */
  57. #define G_TYPE_NONE G_TYPE_MAKE_FUNDAMENTAL (1)
  58. /**
  59. * G_TYPE_INTERFACE:
  60. *
  61. * The fundamental type from which all interfaces are derived.
  62. */
  63. #define G_TYPE_INTERFACE G_TYPE_MAKE_FUNDAMENTAL (2)
  64. /**
  65. * G_TYPE_CHAR:
  66. *
  67. * The fundamental type corresponding to #gchar.
  68. * The type designated by G_TYPE_CHAR is unconditionally an 8-bit signed integer.
  69. * This may or may not be the same type a the C type "gchar".
  70. */
  71. #define G_TYPE_CHAR G_TYPE_MAKE_FUNDAMENTAL (3)
  72. /**
  73. * G_TYPE_UCHAR:
  74. *
  75. * The fundamental type corresponding to #guchar.
  76. */
  77. #define G_TYPE_UCHAR G_TYPE_MAKE_FUNDAMENTAL (4)
  78. /**
  79. * G_TYPE_BOOLEAN:
  80. *
  81. * The fundamental type corresponding to #gboolean.
  82. */
  83. #define G_TYPE_BOOLEAN G_TYPE_MAKE_FUNDAMENTAL (5)
  84. /**
  85. * G_TYPE_INT:
  86. *
  87. * The fundamental type corresponding to #gint.
  88. */
  89. #define G_TYPE_INT G_TYPE_MAKE_FUNDAMENTAL (6)
  90. /**
  91. * G_TYPE_UINT:
  92. *
  93. * The fundamental type corresponding to #guint.
  94. */
  95. #define G_TYPE_UINT G_TYPE_MAKE_FUNDAMENTAL (7)
  96. /**
  97. * G_TYPE_LONG:
  98. *
  99. * The fundamental type corresponding to #glong.
  100. */
  101. #define G_TYPE_LONG G_TYPE_MAKE_FUNDAMENTAL (8)
  102. /**
  103. * G_TYPE_ULONG:
  104. *
  105. * The fundamental type corresponding to #gulong.
  106. */
  107. #define G_TYPE_ULONG G_TYPE_MAKE_FUNDAMENTAL (9)
  108. /**
  109. * G_TYPE_INT64:
  110. *
  111. * The fundamental type corresponding to #gint64.
  112. */
  113. #define G_TYPE_INT64 G_TYPE_MAKE_FUNDAMENTAL (10)
  114. /**
  115. * G_TYPE_UINT64:
  116. *
  117. * The fundamental type corresponding to #guint64.
  118. */
  119. #define G_TYPE_UINT64 G_TYPE_MAKE_FUNDAMENTAL (11)
  120. /**
  121. * G_TYPE_ENUM:
  122. *
  123. * The fundamental type from which all enumeration types are derived.
  124. */
  125. #define G_TYPE_ENUM G_TYPE_MAKE_FUNDAMENTAL (12)
  126. /**
  127. * G_TYPE_FLAGS:
  128. *
  129. * The fundamental type from which all flags types are derived.
  130. */
  131. #define G_TYPE_FLAGS G_TYPE_MAKE_FUNDAMENTAL (13)
  132. /**
  133. * G_TYPE_FLOAT:
  134. *
  135. * The fundamental type corresponding to #gfloat.
  136. */
  137. #define G_TYPE_FLOAT G_TYPE_MAKE_FUNDAMENTAL (14)
  138. /**
  139. * G_TYPE_DOUBLE:
  140. *
  141. * The fundamental type corresponding to #gdouble.
  142. */
  143. #define G_TYPE_DOUBLE G_TYPE_MAKE_FUNDAMENTAL (15)
  144. /**
  145. * G_TYPE_STRING:
  146. *
  147. * The fundamental type corresponding to nul-terminated C strings.
  148. */
  149. #define G_TYPE_STRING G_TYPE_MAKE_FUNDAMENTAL (16)
  150. /**
  151. * G_TYPE_POINTER:
  152. *
  153. * The fundamental type corresponding to #gpointer.
  154. */
  155. #define G_TYPE_POINTER G_TYPE_MAKE_FUNDAMENTAL (17)
  156. /**
  157. * G_TYPE_BOXED:
  158. *
  159. * The fundamental type from which all boxed types are derived.
  160. */
  161. #define G_TYPE_BOXED G_TYPE_MAKE_FUNDAMENTAL (18)
  162. /**
  163. * G_TYPE_PARAM:
  164. *
  165. * The fundamental type from which all #GParamSpec types are derived.
  166. */
  167. #define G_TYPE_PARAM G_TYPE_MAKE_FUNDAMENTAL (19)
  168. /**
  169. * G_TYPE_OBJECT:
  170. *
  171. * The fundamental type for #GObject.
  172. */
  173. #define G_TYPE_OBJECT G_TYPE_MAKE_FUNDAMENTAL (20)
  174. /**
  175. * G_TYPE_VARIANT:
  176. *
  177. * The fundamental type corresponding to #GVariant.
  178. *
  179. * All floating #GVariant instances passed through the #GType system are
  180. * consumed.
  181. *
  182. * Note that callbacks in closures, and signal handlers
  183. * for signals of return type %G_TYPE_VARIANT, must never return floating
  184. * variants.
  185. *
  186. * Note: GLib 2.24 did include a boxed type with this name. It was replaced
  187. * with this fundamental type in 2.26.
  188. *
  189. * Since: 2.26
  190. */
  191. #define G_TYPE_VARIANT G_TYPE_MAKE_FUNDAMENTAL (21)
  192. /* Reserved fundamental type numbers to create new fundamental
  193. * type IDs with G_TYPE_MAKE_FUNDAMENTAL().
  194. * Send email to [email protected] for reservations.
  195. */
  196. /**
  197. * G_TYPE_FUNDAMENTAL_SHIFT:
  198. *
  199. * Shift value used in converting numbers to type IDs.
  200. */
  201. #define G_TYPE_FUNDAMENTAL_SHIFT (2)
  202. /**
  203. * G_TYPE_MAKE_FUNDAMENTAL:
  204. * @x: the fundamental type number.
  205. *
  206. * Get the type ID for the fundamental type number @x.
  207. * Use g_type_fundamental_next() instead of this macro to create new fundamental
  208. * types.
  209. *
  210. * Returns: the GType
  211. */
  212. #define G_TYPE_MAKE_FUNDAMENTAL(x) ((GType) ((x) << G_TYPE_FUNDAMENTAL_SHIFT))
  213. /**
  214. * G_TYPE_RESERVED_GLIB_FIRST:
  215. *
  216. * First fundamental type number to create a new fundamental type id with
  217. * G_TYPE_MAKE_FUNDAMENTAL() reserved for GLib.
  218. */
  219. #define G_TYPE_RESERVED_GLIB_FIRST (22)
  220. /**
  221. * G_TYPE_RESERVED_GLIB_LAST:
  222. *
  223. * Last fundamental type number reserved for GLib.
  224. */
  225. #define G_TYPE_RESERVED_GLIB_LAST (31)
  226. /**
  227. * G_TYPE_RESERVED_BSE_FIRST:
  228. *
  229. * First fundamental type number to create a new fundamental type id with
  230. * G_TYPE_MAKE_FUNDAMENTAL() reserved for BSE.
  231. */
  232. #define G_TYPE_RESERVED_BSE_FIRST (32)
  233. /**
  234. * G_TYPE_RESERVED_BSE_LAST:
  235. *
  236. * Last fundamental type number reserved for BSE.
  237. */
  238. #define G_TYPE_RESERVED_BSE_LAST (48)
  239. /**
  240. * G_TYPE_RESERVED_USER_FIRST:
  241. *
  242. * First available fundamental type number to create new fundamental
  243. * type id with G_TYPE_MAKE_FUNDAMENTAL().
  244. */
  245. #define G_TYPE_RESERVED_USER_FIRST (49)
  246. /* Type Checking Macros
  247. */
  248. /**
  249. * G_TYPE_IS_FUNDAMENTAL:
  250. * @type: A #GType value.
  251. *
  252. * Checks if @type is a fundamental type.
  253. *
  254. * Returns: %TRUE on success.
  255. */
  256. #define G_TYPE_IS_FUNDAMENTAL(type) ((type) <= G_TYPE_FUNDAMENTAL_MAX)
  257. /**
  258. * G_TYPE_IS_DERIVED:
  259. * @type: A #GType value.
  260. *
  261. * Checks if @type is derived (or in object-oriented terminology:
  262. * inherited) from another type (this holds true for all non-fundamental
  263. * types).
  264. *
  265. * Returns: %TRUE on success.
  266. */
  267. #define G_TYPE_IS_DERIVED(type) ((type) > G_TYPE_FUNDAMENTAL_MAX)
  268. /**
  269. * G_TYPE_IS_INTERFACE:
  270. * @type: A #GType value.
  271. *
  272. * Checks if @type is an interface type.
  273. * An interface type provides a pure API, the implementation
  274. * of which is provided by another type (which is then said to conform
  275. * to the interface). GLib interfaces are somewhat analogous to Java
  276. * interfaces and C++ classes containing only pure virtual functions,
  277. * with the difference that GType interfaces are not derivable (but see
  278. * g_type_interface_add_prerequisite() for an alternative).
  279. *
  280. * Returns: %TRUE on success.
  281. */
  282. #define G_TYPE_IS_INTERFACE(type) (G_TYPE_FUNDAMENTAL (type) == G_TYPE_INTERFACE)
  283. /**
  284. * G_TYPE_IS_CLASSED:
  285. * @type: A #GType value.
  286. *
  287. * Checks if @type is a classed type.
  288. *
  289. * Returns: %TRUE on success.
  290. */
  291. #define G_TYPE_IS_CLASSED(type) (g_type_test_flags ((type), G_TYPE_FLAG_CLASSED))
  292. /**
  293. * G_TYPE_IS_INSTANTIATABLE:
  294. * @type: A #GType value.
  295. *
  296. * Checks if @type can be instantiated. Instantiation is the
  297. * process of creating an instance (object) of this type.
  298. *
  299. * Returns: %TRUE on success.
  300. */
  301. #define G_TYPE_IS_INSTANTIATABLE(type) (g_type_test_flags ((type), G_TYPE_FLAG_INSTANTIATABLE))
  302. /**
  303. * G_TYPE_IS_DERIVABLE:
  304. * @type: A #GType value.
  305. *
  306. * Checks if @type is a derivable type. A derivable type can
  307. * be used as the base class of a flat (single-level) class hierarchy.
  308. *
  309. * Returns: %TRUE on success.
  310. */
  311. #define G_TYPE_IS_DERIVABLE(type) (g_type_test_flags ((type), G_TYPE_FLAG_DERIVABLE))
  312. /**
  313. * G_TYPE_IS_DEEP_DERIVABLE:
  314. * @type: A #GType value.
  315. *
  316. * Checks if @type is a deep derivable type. A deep derivable type
  317. * can be used as the base class of a deep (multi-level) class hierarchy.
  318. *
  319. * Returns: %TRUE on success.
  320. */
  321. #define G_TYPE_IS_DEEP_DERIVABLE(type) (g_type_test_flags ((type), G_TYPE_FLAG_DEEP_DERIVABLE))
  322. /**
  323. * G_TYPE_IS_ABSTRACT:
  324. * @type: A #GType value.
  325. *
  326. * Checks if @type is an abstract type. An abstract type cannot be
  327. * instantiated and is normally used as an abstract base class for
  328. * derived classes.
  329. *
  330. * Returns: %TRUE on success.
  331. */
  332. #define G_TYPE_IS_ABSTRACT(type) (g_type_test_flags ((type), G_TYPE_FLAG_ABSTRACT))
  333. /**
  334. * G_TYPE_IS_VALUE_ABSTRACT:
  335. * @type: A #GType value.
  336. *
  337. * Checks if @type is an abstract value type. An abstract value type introduces
  338. * a value table, but can't be used for g_value_init() and is normally used as
  339. * an abstract base type for derived value types.
  340. *
  341. * Returns: %TRUE on success.
  342. */
  343. #define G_TYPE_IS_VALUE_ABSTRACT(type) (g_type_test_flags ((type), G_TYPE_FLAG_VALUE_ABSTRACT))
  344. /**
  345. * G_TYPE_IS_VALUE_TYPE:
  346. * @type: A #GType value.
  347. *
  348. * Checks if @type is a value type and can be used with g_value_init().
  349. *
  350. * Returns: %TRUE on success.
  351. */
  352. #define G_TYPE_IS_VALUE_TYPE(type) (g_type_check_is_value_type (type))
  353. /**
  354. * G_TYPE_HAS_VALUE_TABLE:
  355. * @type: A #GType value.
  356. *
  357. * Checks if @type has a #GTypeValueTable.
  358. *
  359. * Returns: %TRUE on success.
  360. */
  361. #define G_TYPE_HAS_VALUE_TABLE(type) (g_type_value_table_peek (type) != NULL)
  362. /* Typedefs
  363. */
  364. /**
  365. * GType:
  366. *
  367. * A numerical value which represents the unique identifier of a registered
  368. * type.
  369. */
  370. #if GLIB_SIZEOF_SIZE_T != GLIB_SIZEOF_LONG || !defined __cplusplus
  371. typedef gsize GType;
  372. #else /* for historic reasons, C++ links against gulong GTypes */
  373. typedef gulong GType;
  374. #endif
  375. typedef struct _GValue GValue;
  376. typedef union _GTypeCValue GTypeCValue;
  377. typedef struct _GTypePlugin GTypePlugin;
  378. typedef struct _GTypeClass GTypeClass;
  379. typedef struct _GTypeInterface GTypeInterface;
  380. typedef struct _GTypeInstance GTypeInstance;
  381. typedef struct _GTypeInfo GTypeInfo;
  382. typedef struct _GTypeFundamentalInfo GTypeFundamentalInfo;
  383. typedef struct _GInterfaceInfo GInterfaceInfo;
  384. typedef struct _GTypeValueTable GTypeValueTable;
  385. typedef struct _GTypeQuery GTypeQuery;
  386. /* Basic Type Structures
  387. */
  388. /**
  389. * GTypeClass:
  390. *
  391. * An opaque structure used as the base of all classes.
  392. */
  393. struct _GTypeClass
  394. {
  395. /*< private >*/
  396. GType g_type;
  397. };
  398. /**
  399. * GTypeInstance:
  400. *
  401. * An opaque structure used as the base of all type instances.
  402. */
  403. struct _GTypeInstance
  404. {
  405. /*< private >*/
  406. GTypeClass *g_class;
  407. };
  408. /**
  409. * GTypeInterface:
  410. *
  411. * An opaque structure used as the base of all interface types.
  412. */
  413. struct _GTypeInterface
  414. {
  415. /*< private >*/
  416. GType g_type; /* iface type */
  417. GType g_instance_type;
  418. };
  419. /**
  420. * GTypeQuery:
  421. * @type: the #GType value of the type.
  422. * @type_name: the name of the type.
  423. * @class_size: the size of the class structure.
  424. * @instance_size: the size of the instance structure.
  425. *
  426. * A structure holding information for a specific type. It is
  427. * filled in by the g_type_query() function.
  428. */
  429. struct _GTypeQuery
  430. {
  431. GType type;
  432. const gchar *type_name;
  433. guint class_size;
  434. guint instance_size;
  435. };
  436. /* Casts, checks and accessors for structured types
  437. * usage of these macros is reserved to type implementations only
  438. */
  439. /*< protected >*/
  440. /**
  441. * G_TYPE_CHECK_INSTANCE:
  442. * @instance: Location of a #GTypeInstance structure.
  443. *
  444. * Checks if @instance is a valid #GTypeInstance structure,
  445. * otherwise issues a warning and returns %FALSE.
  446. *
  447. * This macro should only be used in type implementations.
  448. *
  449. * Returns: %TRUE on success.
  450. */
  451. #define G_TYPE_CHECK_INSTANCE(instance) (_G_TYPE_CHI ((GTypeInstance*) (instance)))
  452. /**
  453. * G_TYPE_CHECK_INSTANCE_CAST:
  454. * @instance: Location of a #GTypeInstance structure.
  455. * @g_type: The type to be returned.
  456. * @c_type: The corresponding C type of @g_type.
  457. *
  458. * Checks that @instance is an instance of the type identified by @g_type
  459. * and issues a warning if this is not the case. Returns @instance casted
  460. * to a pointer to @c_type.
  461. *
  462. * This macro should only be used in type implementations.
  463. */
  464. #define G_TYPE_CHECK_INSTANCE_CAST(instance, g_type, c_type) (_G_TYPE_CIC ((instance), (g_type), c_type))
  465. /**
  466. * G_TYPE_CHECK_INSTANCE_TYPE:
  467. * @instance: Location of a #GTypeInstance structure.
  468. * @g_type: The type to be checked
  469. *
  470. * Checks if @instance is an instance of the type identified by @g_type.
  471. *
  472. * This macro should only be used in type implementations.
  473. *
  474. * Returns: %TRUE on success.
  475. */
  476. #define G_TYPE_CHECK_INSTANCE_TYPE(instance, g_type) (_G_TYPE_CIT ((instance), (g_type)))
  477. /**
  478. * G_TYPE_INSTANCE_GET_CLASS:
  479. * @instance: Location of the #GTypeInstance structure.
  480. * @g_type: The #GType of the class to be returned.
  481. * @c_type: The C type of the class structure.
  482. *
  483. * Get the class structure of a given @instance, casted
  484. * to a specified ancestor type @g_type of the instance.
  485. *
  486. * Note that while calling a GInstanceInitFunc(), the class pointer gets
  487. * modified, so it might not always return the expected pointer.
  488. *
  489. * This macro should only be used in type implementations.
  490. *
  491. * Returns: a pointer to the class structure
  492. */
  493. #define G_TYPE_INSTANCE_GET_CLASS(instance, g_type, c_type) (_G_TYPE_IGC ((instance), (g_type), c_type))
  494. /**
  495. * G_TYPE_INSTANCE_GET_INTERFACE:
  496. * @instance: Location of the #GTypeInstance structure.
  497. * @g_type: The #GType of the interface to be returned.
  498. * @c_type: The C type of the interface structure.
  499. *
  500. * Get the interface structure for interface @g_type of a given @instance.
  501. *
  502. * This macro should only be used in type implementations.
  503. *
  504. * Returns: a pointer to the interface structure
  505. */
  506. #define G_TYPE_INSTANCE_GET_INTERFACE(instance, g_type, c_type) (_G_TYPE_IGI ((instance), (g_type), c_type))
  507. /**
  508. * G_TYPE_CHECK_CLASS_CAST:
  509. * @g_class: Location of a #GTypeClass structure.
  510. * @g_type: The type to be returned.
  511. * @c_type: The corresponding C type of class structure of @g_type.
  512. *
  513. * Checks that @g_class is a class structure of the type identified by @g_type
  514. * and issues a warning if this is not the case. Returns @g_class casted
  515. * to a pointer to @c_type.
  516. *
  517. * This macro should only be used in type implementations.
  518. */
  519. #define G_TYPE_CHECK_CLASS_CAST(g_class, g_type, c_type) (_G_TYPE_CCC ((g_class), (g_type), c_type))
  520. /**
  521. * G_TYPE_CHECK_CLASS_TYPE:
  522. * @g_class: Location of a #GTypeClass structure.
  523. * @g_type: The type to be checked.
  524. *
  525. * Checks if @g_class is a class structure of the type identified by
  526. * @g_type.
  527. *
  528. * This macro should only be used in type implementations.
  529. *
  530. * Returns: %TRUE on success.
  531. */
  532. #define G_TYPE_CHECK_CLASS_TYPE(g_class, g_type) (_G_TYPE_CCT ((g_class), (g_type)))
  533. /**
  534. * G_TYPE_CHECK_VALUE:
  535. * @value: a #GValue
  536. *
  537. * Checks if @value has been initialized to hold values
  538. * of a value type.
  539. *
  540. * This macro should only be used in type implementations.
  541. *
  542. * Returns: %TRUE on success.
  543. */
  544. #define G_TYPE_CHECK_VALUE(value) (_G_TYPE_CHV ((value)))
  545. /**
  546. * G_TYPE_CHECK_VALUE_TYPE:
  547. * @value: a #GValue
  548. * @g_type: The type to be checked.
  549. *
  550. * Checks if @value has been initialized to hold values
  551. * of type @g_type.
  552. *
  553. * This macro should only be used in type implementations.
  554. *
  555. * Returns: %TRUE on success.
  556. */
  557. #define G_TYPE_CHECK_VALUE_TYPE(value, g_type) (_G_TYPE_CVH ((value), (g_type)))
  558. /**
  559. * G_TYPE_FROM_INSTANCE:
  560. * @instance: Location of a valid #GTypeInstance structure.
  561. *
  562. * Get the type identifier from a given @instance structure.
  563. *
  564. * This macro should only be used in type implementations.
  565. *
  566. * Returns: the #GType
  567. */
  568. #define G_TYPE_FROM_INSTANCE(instance) (G_TYPE_FROM_CLASS (((GTypeInstance*) (instance))->g_class))
  569. /**
  570. * G_TYPE_FROM_CLASS:
  571. * @g_class: Location of a valid #GTypeClass structure.
  572. *
  573. * Get the type identifier from a given @class structure.
  574. *
  575. * This macro should only be used in type implementations.
  576. *
  577. * Returns: the #GType
  578. */
  579. #define G_TYPE_FROM_CLASS(g_class) (((GTypeClass*) (g_class))->g_type)
  580. /**
  581. * G_TYPE_FROM_INTERFACE:
  582. * @g_iface: Location of a valid #GTypeInterface structure.
  583. *
  584. * Get the type identifier from a given @interface structure.
  585. *
  586. * This macro should only be used in type implementations.
  587. *
  588. * Returns: the #GType
  589. */
  590. #define G_TYPE_FROM_INTERFACE(g_iface) (((GTypeInterface*) (g_iface))->g_type)
  591. /**
  592. * G_TYPE_INSTANCE_GET_PRIVATE:
  593. * @instance: the instance of a type deriving from @private_type.
  594. * @g_type: the type identifying which private data to retrieve.
  595. * @c_type: The C type for the private structure.
  596. *
  597. * Gets the private structure for a particular type.
  598. * The private structure must have been registered in the
  599. * class_init function with g_type_class_add_private().
  600. *
  601. * This macro should only be used in type implementations.
  602. *
  603. * Since: 2.4
  604. * Returns: a pointer to the private data structure.
  605. */
  606. #define G_TYPE_INSTANCE_GET_PRIVATE(instance, g_type, c_type) ((c_type*) g_type_instance_get_private ((GTypeInstance*) (instance), (g_type)))
  607. /**
  608. * G_TYPE_CLASS_GET_PRIVATE:
  609. * @klass: the class of a type deriving from @private_type.
  610. * @g_type: the type identifying which private data to retrieve.
  611. * @c_type: The C type for the private structure.
  612. *
  613. * Gets the private class structure for a particular type.
  614. * The private structure must have been registered in the
  615. * get_type() function with g_type_add_class_private().
  616. *
  617. * This macro should only be used in type implementations.
  618. *
  619. * Since: 2.24
  620. * Returns: a pointer to the private data structure.
  621. */
  622. #define G_TYPE_CLASS_GET_PRIVATE(klass, g_type, c_type) ((c_type*) g_type_class_get_private ((GTypeClass*) (klass), (g_type)))
  623. /**
  624. * GTypeDebugFlags:
  625. * @G_TYPE_DEBUG_NONE: Print no messages.
  626. * @G_TYPE_DEBUG_OBJECTS: Print messages about object bookkeeping.
  627. * @G_TYPE_DEBUG_SIGNALS: Print messages about signal emissions.
  628. * @G_TYPE_DEBUG_MASK: Mask covering all debug flags.
  629. *
  630. * These flags used to be passed to g_type_init_with_debug_flags() which
  631. * is now deprecated.
  632. *
  633. * If you need to enable debugging features, use the GOBJECT_DEBUG
  634. * environment variable.
  635. *
  636. * Deprecated: 2.36: g_type_init() is now done automatically
  637. */
  638. typedef enum /*< skip >*/
  639. {
  640. G_TYPE_DEBUG_NONE = 0,
  641. G_TYPE_DEBUG_OBJECTS = 1 << 0,
  642. G_TYPE_DEBUG_SIGNALS = 1 << 1,
  643. G_TYPE_DEBUG_MASK = 0x03
  644. } GTypeDebugFlags;
  645. /* --- prototypes --- */
  646. GLIB_DEPRECATED_IN_2_36
  647. void g_type_init (void);
  648. GLIB_DEPRECATED_IN_2_36
  649. void g_type_init_with_debug_flags (GTypeDebugFlags debug_flags);
  650. GLIB_AVAILABLE_IN_ALL
  651. const gchar * g_type_name (GType type);
  652. GLIB_AVAILABLE_IN_ALL
  653. GQuark g_type_qname (GType type);
  654. GLIB_AVAILABLE_IN_ALL
  655. GType g_type_from_name (const gchar *name);
  656. GLIB_AVAILABLE_IN_ALL
  657. GType g_type_parent (GType type);
  658. GLIB_AVAILABLE_IN_ALL
  659. guint g_type_depth (GType type);
  660. GLIB_AVAILABLE_IN_ALL
  661. GType g_type_next_base (GType leaf_type,
  662. GType root_type);
  663. GLIB_AVAILABLE_IN_ALL
  664. gboolean g_type_is_a (GType type,
  665. GType is_a_type);
  666. GLIB_AVAILABLE_IN_ALL
  667. gpointer g_type_class_ref (GType type);
  668. GLIB_AVAILABLE_IN_ALL
  669. gpointer g_type_class_peek (GType type);
  670. GLIB_AVAILABLE_IN_ALL
  671. gpointer g_type_class_peek_static (GType type);
  672. GLIB_AVAILABLE_IN_ALL
  673. void g_type_class_unref (gpointer g_class);
  674. GLIB_AVAILABLE_IN_ALL
  675. gpointer g_type_class_peek_parent (gpointer g_class);
  676. GLIB_AVAILABLE_IN_ALL
  677. gpointer g_type_interface_peek (gpointer instance_class,
  678. GType iface_type);
  679. GLIB_AVAILABLE_IN_ALL
  680. gpointer g_type_interface_peek_parent (gpointer g_iface);
  681. GLIB_AVAILABLE_IN_ALL
  682. gpointer g_type_default_interface_ref (GType g_type);
  683. GLIB_AVAILABLE_IN_ALL
  684. gpointer g_type_default_interface_peek (GType g_type);
  685. GLIB_AVAILABLE_IN_ALL
  686. void g_type_default_interface_unref (gpointer g_iface);
  687. /* g_free() the returned arrays */
  688. GLIB_AVAILABLE_IN_ALL
  689. GType* g_type_children (GType type,
  690. guint *n_children);
  691. GLIB_AVAILABLE_IN_ALL
  692. GType* g_type_interfaces (GType type,
  693. guint *n_interfaces);
  694. /* per-type _static_ data */
  695. GLIB_AVAILABLE_IN_ALL
  696. void g_type_set_qdata (GType type,
  697. GQuark quark,
  698. gpointer data);
  699. GLIB_AVAILABLE_IN_ALL
  700. gpointer g_type_get_qdata (GType type,
  701. GQuark quark);
  702. GLIB_AVAILABLE_IN_ALL
  703. void g_type_query (GType type,
  704. GTypeQuery *query);
  705. /* --- type registration --- */
  706. /**
  707. * GBaseInitFunc:
  708. * @g_class: The #GTypeClass structure to initialize.
  709. *
  710. * A callback function used by the type system to do base initialization
  711. * of the class structures of derived types. It is called as part of the
  712. * initialization process of all derived classes and should reallocate
  713. * or reset all dynamic class members copied over from the parent class.
  714. * For example, class members (such as strings) that are not sufficiently
  715. * handled by a plain memory copy of the parent class into the derived class
  716. * have to be altered. See GClassInitFunc() for a discussion of the class
  717. * intialization process.
  718. */
  719. typedef void (*GBaseInitFunc) (gpointer g_class);
  720. /**
  721. * GBaseFinalizeFunc:
  722. * @g_class: The #GTypeClass structure to finalize.
  723. *
  724. * A callback function used by the type system to finalize those portions
  725. * of a derived types class structure that were setup from the corresponding
  726. * GBaseInitFunc() function. Class finalization basically works the inverse
  727. * way in which class intialization is performed.
  728. * See GClassInitFunc() for a discussion of the class intialization process.
  729. */
  730. typedef void (*GBaseFinalizeFunc) (gpointer g_class);
  731. /**
  732. * GClassInitFunc:
  733. * @g_class: The #GTypeClass structure to initialize.
  734. * @class_data: The @class_data member supplied via the #GTypeInfo structure.
  735. *
  736. * A callback function used by the type system to initialize the class
  737. * of a specific type. This function should initialize all static class
  738. * members.
  739. * The initialization process of a class involves:
  740. * <itemizedlist>
  741. * <listitem><para>
  742. * 1 - Copying common members from the parent class over to the
  743. * derived class structure.
  744. * </para></listitem>
  745. * <listitem><para>
  746. * 2 - Zero initialization of the remaining members not copied
  747. * over from the parent class.
  748. * </para></listitem>
  749. * <listitem><para>
  750. * 3 - Invocation of the GBaseInitFunc() initializers of all parent
  751. * types and the class' type.
  752. * </para></listitem>
  753. * <listitem><para>
  754. * 4 - Invocation of the class' GClassInitFunc() initializer.
  755. * </para></listitem>
  756. * </itemizedlist>
  757. * Since derived classes are partially initialized through a memory copy
  758. * of the parent class, the general rule is that GBaseInitFunc() and
  759. * GBaseFinalizeFunc() should take care of necessary reinitialization
  760. * and release of those class members that were introduced by the type
  761. * that specified these GBaseInitFunc()/GBaseFinalizeFunc().
  762. * GClassInitFunc() should only care about initializing static
  763. * class members, while dynamic class members (such as allocated strings
  764. * or reference counted resources) are better handled by a GBaseInitFunc()
  765. * for this type, so proper initialization of the dynamic class members
  766. * is performed for class initialization of derived types as well.
  767. * An example may help to correspond the intend of the different class
  768. * initializers:
  769. *
  770. * |[
  771. * typedef struct {
  772. * GObjectClass parent_class;
  773. * gint static_integer;
  774. * gchar *dynamic_string;
  775. * } TypeAClass;
  776. * static void
  777. * type_a_base_class_init (TypeAClass *class)
  778. * {
  779. * class->dynamic_string = g_strdup ("some string");
  780. * }
  781. * static void
  782. * type_a_base_class_finalize (TypeAClass *class)
  783. * {
  784. * g_free (class->dynamic_string);
  785. * }
  786. * static void
  787. * type_a_class_init (TypeAClass *class)
  788. * {
  789. * class->static_integer = 42;
  790. * }
  791. *
  792. * typedef struct {
  793. * TypeAClass parent_class;
  794. * gfloat static_float;
  795. * GString *dynamic_gstring;
  796. * } TypeBClass;
  797. * static void
  798. * type_b_base_class_init (TypeBClass *class)
  799. * {
  800. * class->dynamic_gstring = g_string_new ("some other string");
  801. * }
  802. * static void
  803. * type_b_base_class_finalize (TypeBClass *class)
  804. * {
  805. * g_string_free (class->dynamic_gstring);
  806. * }
  807. * static void
  808. * type_b_class_init (TypeBClass *class)
  809. * {
  810. * class->static_float = 3.14159265358979323846;
  811. * }
  812. * ]|
  813. * Initialization of TypeBClass will first cause initialization of
  814. * TypeAClass (derived classes reference their parent classes, see
  815. * g_type_class_ref() on this).
  816. * Initialization of TypeAClass roughly involves zero-initializing its fields,
  817. * then calling its GBaseInitFunc() type_a_base_class_init() to allocate
  818. * its dynamic members (dynamic_string), and finally calling its GClassInitFunc()
  819. * type_a_class_init() to initialize its static members (static_integer).
  820. * The first step in the initialization process of TypeBClass is then
  821. * a plain memory copy of the contents of TypeAClass into TypeBClass and
  822. * zero-initialization of the remaining fields in TypeBClass.
  823. * The dynamic members of TypeAClass within TypeBClass now need
  824. * reinitialization which is performed by calling type_a_base_class_init()
  825. * with an argument of TypeBClass.
  826. * After that, the GBaseInitFunc() of TypeBClass, type_b_base_class_init()
  827. * is called to allocate the dynamic members of TypeBClass (dynamic_gstring),
  828. * and finally the GClassInitFunc() of TypeBClass, type_b_class_init(),
  829. * is called to complete the initialization process with the static members
  830. * (static_float).
  831. * Corresponding finalization counter parts to the GBaseInitFunc() functions
  832. * have to be provided to release allocated resources at class finalization
  833. * time.
  834. */
  835. typedef void (*GClassInitFunc) (gpointer g_class,
  836. gpointer class_data);
  837. /**
  838. * GClassFinalizeFunc:
  839. * @g_class: The #GTypeClass structure to finalize.
  840. * @class_data: The @class_data member supplied via the #GTypeInfo structure.
  841. *
  842. * A callback function used by the type system to finalize a class.
  843. * This function is rarely needed, as dynamically allocated class resources
  844. * should be handled by GBaseInitFunc() and GBaseFinalizeFunc().
  845. * Also, specification of a GClassFinalizeFunc() in the #GTypeInfo
  846. * structure of a static type is invalid, because classes of static types
  847. * will never be finalized (they are artificially kept alive when their
  848. * reference count drops to zero).
  849. */
  850. typedef void (*GClassFinalizeFunc) (gpointer g_class,
  851. gpointer class_data);
  852. /**
  853. * GInstanceInitFunc:
  854. * @instance: The instance to initialize.
  855. * @g_class: The class of the type the instance is created for.
  856. *
  857. * A callback function used by the type system to initialize a new
  858. * instance of a type. This function initializes all instance members and
  859. * allocates any resources required by it.
  860. * Initialization of a derived instance involves calling all its parent
  861. * types instance initializers, so the class member of the instance
  862. * is altered during its initialization to always point to the class that
  863. * belongs to the type the current initializer was introduced for.
  864. */
  865. typedef void (*GInstanceInitFunc) (GTypeInstance *instance,
  866. gpointer g_class);
  867. /**
  868. * GInterfaceInitFunc:
  869. * @g_iface: The interface structure to initialize.
  870. * @iface_data: The @interface_data supplied via the #GInterfaceInfo structure.
  871. *
  872. * A callback function used by the type system to initialize a new
  873. * interface. This function should initialize all internal data and
  874. * allocate any resources required by the interface.
  875. */
  876. typedef void (*GInterfaceInitFunc) (gpointer g_iface,
  877. gpointer iface_data);
  878. /**
  879. * GInterfaceFinalizeFunc:
  880. * @g_iface: The interface structure to finalize.
  881. * @iface_data: The @interface_data supplied via the #GInterfaceInfo structure.
  882. *
  883. * A callback function used by the type system to finalize an interface.
  884. * This function should destroy any internal data and release any resources
  885. * allocated by the corresponding GInterfaceInitFunc() function.
  886. */
  887. typedef void (*GInterfaceFinalizeFunc) (gpointer g_iface,
  888. gpointer iface_data);
  889. /**
  890. * GTypeClassCacheFunc:
  891. * @cache_data: data that was given to the g_type_add_class_cache_func() call
  892. * @g_class: The #GTypeClass structure which is unreferenced
  893. *
  894. * A callback function which is called when the reference count of a class
  895. * drops to zero. It may use g_type_class_ref() to prevent the class from
  896. * being freed. You should not call g_type_class_unref() from a
  897. * #GTypeClassCacheFunc function to prevent infinite recursion, use
  898. * g_type_class_unref_uncached() instead.
  899. *
  900. * The functions have to check the class id passed in to figure
  901. * whether they actually want to cache the class of this type, since all
  902. * classes are routed through the same #GTypeClassCacheFunc chain.
  903. *
  904. * Returns: %TRUE to stop further #GTypeClassCacheFunc<!-- -->s from being
  905. * called, %FALSE to continue.
  906. */
  907. typedef gboolean (*GTypeClassCacheFunc) (gpointer cache_data,
  908. GTypeClass *g_class);
  909. /**
  910. * GTypeInterfaceCheckFunc:
  911. * @check_data: data passed to g_type_add_interface_check().
  912. * @g_iface: the interface that has been initialized
  913. *
  914. * A callback called after an interface vtable is initialized.
  915. * See g_type_add_interface_check().
  916. *
  917. * Since: 2.4
  918. */
  919. typedef void (*GTypeInterfaceCheckFunc) (gpointer check_data,
  920. gpointer g_iface);
  921. /**
  922. * GTypeFundamentalFlags:
  923. * @G_TYPE_FLAG_CLASSED: Indicates a classed type.
  924. * @G_TYPE_FLAG_INSTANTIATABLE: Indicates an instantiable type (implies classed).
  925. * @G_TYPE_FLAG_DERIVABLE: Indicates a flat derivable type.
  926. * @G_TYPE_FLAG_DEEP_DERIVABLE: Indicates a deep derivable type (implies derivable).
  927. *
  928. * Bit masks used to check or determine specific characteristics of a
  929. * fundamental type.
  930. */
  931. typedef enum /*< skip >*/
  932. {
  933. G_TYPE_FLAG_CLASSED = (1 << 0),
  934. G_TYPE_FLAG_INSTANTIATABLE = (1 << 1),
  935. G_TYPE_FLAG_DERIVABLE = (1 << 2),
  936. G_TYPE_FLAG_DEEP_DERIVABLE = (1 << 3)
  937. } GTypeFundamentalFlags;
  938. /**
  939. * GTypeFlags:
  940. * @G_TYPE_FLAG_ABSTRACT: Indicates an abstract type. No instances can be
  941. * created for an abstract type.
  942. * @G_TYPE_FLAG_VALUE_ABSTRACT: Indicates an abstract value type, i.e. a type
  943. * that introduces a value table, but can't be used for
  944. * g_value_init().
  945. *
  946. * Bit masks used to check or determine characteristics of a type.
  947. */
  948. typedef enum /*< skip >*/
  949. {
  950. G_TYPE_FLAG_ABSTRACT = (1 << 4),
  951. G_TYPE_FLAG_VALUE_ABSTRACT = (1 << 5)
  952. } GTypeFlags;
  953. /**
  954. * GTypeInfo:
  955. * @class_size: Size of the class structure (required for interface, classed and instantiatable types).
  956. * @base_init: Location of the base initialization function (optional).
  957. * @base_finalize: Location of the base finalization function (optional).
  958. * @class_init: Location of the class initialization function for
  959. * classed and instantiatable types. Location of the default vtable
  960. * inititalization function for interface types. (optional) This function
  961. * is used both to fill in virtual functions in the class or default vtable,
  962. * and to do type-specific setup such as registering signals and object
  963. * properties.
  964. * @class_finalize: Location of the class finalization function for
  965. * classed and instantiatable types. Location fo the default vtable
  966. * finalization function for interface types. (optional)
  967. * @class_data: User-supplied data passed to the class init/finalize functions.
  968. * @instance_size: Size of the instance (object) structure (required for instantiatable types only).
  969. * @n_preallocs: Prior to GLib 2.10, it specified the number of pre-allocated (cached) instances to reserve memory for (0 indicates no caching). Since GLib 2.10, it is ignored, since instances are allocated with the <link linkend="glib-Memory-Slices">slice allocator</link> now.
  970. * @instance_init: Location of the instance initialization function (optional, for instantiatable types only).
  971. * @value_table: A #GTypeValueTable function table for generic handling of GValues of this type (usually only
  972. * useful for fundamental types).
  973. *
  974. * This structure is used to provide the type system with the information
  975. * required to initialize and destruct (finalize) a type's class and
  976. * its instances.
  977. * The initialized structure is passed to the g_type_register_static() function
  978. * (or is copied into the provided #GTypeInfo structure in the
  979. * g_type_plugin_complete_type_info()). The type system will perform a deep
  980. * copy of this structure, so its memory does not need to be persistent
  981. * across invocation of g_type_register_static().
  982. */
  983. struct _GTypeInfo
  984. {
  985. /* interface types, classed types, instantiated types */
  986. guint16 class_size;
  987. GBaseInitFunc base_init;
  988. GBaseFinalizeFunc base_finalize;
  989. /* interface types, classed types, instantiated types */
  990. GClassInitFunc class_init;
  991. GClassFinalizeFunc class_finalize;
  992. gconstpointer class_data;
  993. /* instantiated types */
  994. guint16 instance_size;
  995. guint16 n_preallocs;
  996. GInstanceInitFunc instance_init;
  997. /* value handling */
  998. const GTypeValueTable *value_table;
  999. };
  1000. /**
  1001. * GTypeFundamentalInfo:
  1002. * @type_flags: #GTypeFundamentalFlags describing the characteristics of the fundamental type
  1003. *
  1004. * A structure that provides information to the type system which is
  1005. * used specifically for managing fundamental types.
  1006. */
  1007. struct _GTypeFundamentalInfo
  1008. {
  1009. GTypeFundamentalFlags type_flags;
  1010. };
  1011. /**
  1012. * GInterfaceInfo:
  1013. * @interface_init: location of the interface initialization function
  1014. * @interface_finalize: location of the interface finalization function
  1015. * @interface_data: user-supplied data passed to the interface init/finalize functions
  1016. *
  1017. * A structure that provides information to the type system which is
  1018. * used specifically for managing interface types.
  1019. */
  1020. struct _GInterfaceInfo
  1021. {
  1022. GInterfaceInitFunc interface_init;
  1023. GInterfaceFinalizeFunc interface_finalize;
  1024. gpointer interface_data;
  1025. };
  1026. /**
  1027. * GTypeValueTable:
  1028. * @value_init: Default initialize @values contents by poking values
  1029. * directly into the value->data array. The data array of
  1030. * the #GValue passed into this function was zero-filled
  1031. * with <function>memset()</function>, so no care has to
  1032. * be taken to free any
  1033. * old contents. E.g. for the implementation of a string
  1034. * value that may never be %NULL, the implementation might
  1035. * look like:
  1036. * |[
  1037. * value->data[0].v_pointer = g_strdup ("");
  1038. * ]|
  1039. * @value_free: Free any old contents that might be left in the
  1040. * data array of the passed in @value. No resources may
  1041. * remain allocated through the #GValue contents after
  1042. * this function returns. E.g. for our above string type:
  1043. * |[
  1044. * // only free strings without a specific flag for static storage
  1045. * if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
  1046. * g_free (value->data[0].v_pointer);
  1047. * ]|
  1048. * @value_copy: @dest_value is a #GValue with zero-filled data section
  1049. * and @src_value is a properly setup #GValue of same or
  1050. * derived type.
  1051. * The purpose of this function is to copy the contents of
  1052. * @src_value into @dest_value in a way, that even after
  1053. * @src_value has been freed, the contents of @dest_value
  1054. * remain valid. String type example:
  1055. * |[
  1056. * dest_value->data[0].v_pointer = g_strdup (src_value->data[0].v_pointer);
  1057. * ]|
  1058. * @value_peek_pointer: If the value contents fit into a pointer, such as objects
  1059. * or strings, return this pointer, so the caller can peek at
  1060. * the current contents. To extend on our above string example:
  1061. * |[
  1062. * return value->data[0].v_pointer;
  1063. * ]|
  1064. * @collect_format: A string format describing how to collect the contents of
  1065. * this value bit-by-bit. Each character in the format represents
  1066. * an argument to be collected, and the characters themselves indicate
  1067. * the type of the argument. Currently supported arguments are:
  1068. * <variablelist>
  1069. * <varlistentry><term /><listitem><para>
  1070. * 'i' - Integers. passed as collect_values[].v_int.
  1071. * </para></listitem></varlistentry>
  1072. * <varlistentry><term /><listitem><para>
  1073. * 'l' - Longs. passed as collect_values[].v_long.
  1074. * </para></listitem></varlistentry>
  1075. * <varlistentry><term /><listitem><para>
  1076. * 'd' - Doubles. passed as collect_values[].v_double.
  1077. * </para></listitem></varlistentry>
  1078. * <varlistentry><term /><listitem><para>
  1079. * 'p' - Pointers. passed as collect_values[].v_pointer.
  1080. * </para></listitem></varlistentry>
  1081. * </variablelist>
  1082. * It should be noted that for variable argument list construction,
  1083. * ANSI C promotes every type smaller than an integer to an int, and
  1084. * floats to doubles. So for collection of short int or char, 'i'
  1085. * needs to be used, and for collection of floats 'd'.
  1086. * @collect_value: The collect_value() function is responsible for converting the
  1087. * values collected from a variable argument list into contents
  1088. * suitable for storage in a GValue. This function should setup
  1089. * @value similar to value_init(); e.g. for a string value that
  1090. * does not allow %NULL pointers, it needs to either spew an error,
  1091. * or do an implicit conversion by storing an empty string.
  1092. * The @value passed in to this function has a zero-filled data
  1093. * array, so just like for value_init() it is guaranteed to not
  1094. * contain any old contents that might need freeing.
  1095. * @n_collect_values is exactly the string length of @collect_format,
  1096. * and @collect_values is an array of unions #GTypeCValue with
  1097. * length @n_collect_values, containing the collected values
  1098. * according to @collect_format.
  1099. * @collect_flags is an argument provided as a hint by the caller.
  1100. * It may contain the flag %G_VALUE_NOCOPY_CONTENTS indicating,
  1101. * that the collected value contents may be considered "static"
  1102. * for the duration of the @value lifetime.
  1103. * Thus an extra copy of the contents stored in @collect_values is
  1104. * not required for assignment to @value.
  1105. * For our above string example, we continue with:
  1106. * |[
  1107. * if (!collect_values[0].v_pointer)
  1108. * value->data[0].v_pointer = g_strdup ("");
  1109. * else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
  1110. * {
  1111. * value->data[0].v_pointer = collect_values[0].v_pointer;
  1112. * // keep a flag for the value_free() implementation to not free this string
  1113. * value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
  1114. * }
  1115. * else
  1116. * value->data[0].v_pointer = g_strdup (collect_values[0].v_pointer);
  1117. * return NULL;
  1118. * ]|
  1119. * It should be noted, that it is generally a bad idea to follow the
  1120. * #G_VALUE_NOCOPY_CONTENTS hint for reference counted types. Due to
  1121. * reentrancy requirements and reference count assertions performed
  1122. * by the signal emission code, reference counts should always be
  1123. * incremented for reference counted contents stored in the value->data
  1124. * array. To deviate from our string example for a moment, and taking
  1125. * a look at an exemplary implementation for collect_value() of
  1126. * #GObject:
  1127. * |[
  1128. * if (collect_values[0].v_pointer)
  1129. * {
  1130. * GObject *object = G_OBJECT (collect_values[0].v_pointer);
  1131. * // never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types
  1132. * value->data[0].v_pointer = g_object_ref (object);
  1133. * return NULL;
  1134. * }
  1135. * else
  1136. * return g_strdup_printf ("Object passed as invalid NULL pointer");
  1137. * }
  1138. * ]|
  1139. * The reference count for valid objects is always incremented,
  1140. * regardless of @collect_flags. For invalid objects, the example
  1141. * returns a newly allocated string without altering @value.
  1142. * Upon success, collect_value() needs to return %NULL. If, however,
  1143. * an error condition occurred, collect_value() may spew an
  1144. * error by returning a newly allocated non-%NULL string, giving
  1145. * a suitable description of the error condition.
  1146. * The calling code makes no assumptions about the @value
  1147. * contents being valid upon error returns, @value
  1148. * is simply thrown away without further freeing. As such, it is
  1149. * a good idea to not allocate #GValue contents, prior to returning
  1150. * an error, however, collect_values() is not obliged to return
  1151. * a correctly setup @value for error returns, simply because
  1152. * any non-%NULL return is considered a fatal condition so further
  1153. * program behaviour is undefined.
  1154. * @lcopy_format: Format description of the arguments to collect for @lcopy_value,
  1155. * analogous to @collect_format. Usually, @lcopy_format string consists
  1156. * only of 'p's to provide lcopy_value() with pointers to storage locations.
  1157. * @lcopy_value: This function is responsible for storing the @value contents into
  1158. * arguments passed through a variable argument list which got
  1159. * collected into @collect_values according to @lcopy_format.
  1160. * @n_collect_values equals the string length of @lcopy_format,
  1161. * and @collect_flags may contain %G_VALUE_NOCOPY_CONTENTS.
  1162. * In contrast to collect_value(), lcopy_value() is obliged to
  1163. * always properly support %G_VALUE_NOCOPY_CONTENTS.
  1164. * Similar to collect_value() the function may prematurely abort
  1165. * by returning a newly allocated string describing an error condition.
  1166. * To complete the string example:
  1167. * |[
  1168. * gchar **string_p = collect_values[0].v_pointer;
  1169. * if (!string_p)
  1170. * return g_strdup_printf ("string location passed as NULL");
  1171. * if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
  1172. * *string_p = value->data[0].v_pointer;
  1173. * else
  1174. * *string_p = g_strdup (value->data[0].v_pointer);
  1175. * ]|
  1176. * And an illustrative version of lcopy_value() for
  1177. * reference-counted types:
  1178. * |[
  1179. * GObject **object_p = collect_values[0].v_pointer;
  1180. * if (!object_p)
  1181. * return g_strdup_printf ("object location passed as NULL");
  1182. * if (!value->data[0].v_pointer)
  1183. * *object_p = NULL;
  1184. * else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) /&ast; always honour &ast;/
  1185. * *object_p = value->data[0].v_pointer;
  1186. * else
  1187. * *object_p = g_object_ref (value->data[0].v_pointer);
  1188. * return NULL;
  1189. * ]|
  1190. *
  1191. * The #GTypeValueTable provides the functions required by the #GValue implementation,
  1192. * to serve as a container for values of a type.
  1193. */
  1194. struct _GTypeValueTable
  1195. {
  1196. void (*value_init) (GValue *value);
  1197. void (*value_free) (GValue *value);
  1198. void (*value_copy) (const GValue *src_value,
  1199. GValue *dest_value);
  1200. /* varargs functionality (optional) */
  1201. gpointer (*value_peek_pointer) (const GValue *value);
  1202. const gchar *collect_format;
  1203. gchar* (*collect_value) (GValue *value,
  1204. guint n_collect_values,
  1205. GTypeCValue *collect_values,
  1206. guint collect_flags);
  1207. const gchar *lcopy_format;
  1208. gchar* (*lcopy_value) (const GValue *value,
  1209. guint n_collect_values,
  1210. GTypeCValue *collect_values,
  1211. guint collect_flags);
  1212. };
  1213. GLIB_AVAILABLE_IN_ALL
  1214. GType g_type_register_static (GType parent_type,
  1215. const gchar *type_name,
  1216. const GTypeInfo *info,
  1217. GTypeFlags flags);
  1218. GLIB_AVAILABLE_IN_ALL
  1219. GType g_type_register_static_simple (GType parent_type,
  1220. const gchar *type_name,
  1221. guint class_size,
  1222. GClassInitFunc class_init,
  1223. guint instance_size,
  1224. GInstanceInitFunc instance_init,
  1225. GTypeFlags flags);
  1226. GLIB_AVAILABLE_IN_ALL
  1227. GType g_type_register_dynamic (GType parent_type,
  1228. const gchar *type_name,
  1229. GTypePlugin *plugin,
  1230. GTypeFlags flags);
  1231. GLIB_AVAILABLE_IN_ALL
  1232. GType g_type_register_fundamental (GType type_id,
  1233. const gchar *type_name,
  1234. const GTypeInfo *info,
  1235. const GTypeFundamentalInfo *finfo,
  1236. GTypeFlags flags);
  1237. GLIB_AVAILABLE_IN_ALL
  1238. void g_type_add_interface_static (GType instance_type,
  1239. GType interface_type,
  1240. const GInterfaceInfo *info);
  1241. GLIB_AVAILABLE_IN_ALL
  1242. void g_type_add_interface_dynamic (GType instance_type,
  1243. GType interface_type,
  1244. GTypePlugin *plugin);
  1245. GLIB_AVAILABLE_IN_ALL
  1246. void g_type_interface_add_prerequisite (GType interface_type,
  1247. GType prerequisite_type);
  1248. GLIB_AVAILABLE_IN_ALL
  1249. GType*g_type_interface_prerequisites (GType interface_type,
  1250. guint *n_prerequisites);
  1251. GLIB_AVAILABLE_IN_ALL
  1252. void g_type_class_add_private (gpointer g_class,
  1253. gsize private_size);
  1254. GLIB_AVAILABLE_IN_2_38
  1255. gint g_type_add_instance_private (GType class_type,
  1256. gsize private_size);
  1257. GLIB_AVAILABLE_IN_ALL
  1258. gpointer g_type_instance_get_private (GTypeInstance *instance,
  1259. GType private_type);
  1260. GLIB_AVAILABLE_IN_2_38
  1261. void g_type_class_adjust_private_offset (gpointer g_class,
  1262. gint *private_size_or_offset);
  1263. GLIB_AVAILABLE_IN_ALL
  1264. void g_type_add_class_private (GType class_type,
  1265. gsize private_size);
  1266. GLIB_AVAILABLE_IN_ALL
  1267. gpointer g_type_class_get_private (GTypeClass *klass,
  1268. GType private_type);
  1269. GLIB_AVAILABLE_IN_2_38
  1270. gint g_type_class_get_instance_private_offset (gpointer g_class);
  1271. GLIB_AVAILABLE_IN_2_34
  1272. void g_type_ensure (GType type);
  1273. GLIB_AVAILABLE_IN_2_36
  1274. guint g_type_get_type_registration_serial (void);
  1275. /* --- GType boilerplate --- */
  1276. /**
  1277. * G_DEFINE_TYPE:
  1278. * @TN: The name of the new type, in Camel case.
  1279. * @t_n: The name of the new type, in lowercase, with words
  1280. * separated by '_'.
  1281. * @T_P: The #GType of the parent type.
  1282. *
  1283. * A convenience macro for type implementations, which declares a
  1284. * class initialization function, an instance initialization function (see #GTypeInfo for information about
  1285. * these) and a static variable named @t_n<!-- -->_parent_class pointing to the parent class. Furthermore, it defines
  1286. * a *_get_type() function. See G_DEFINE_TYPE_EXTENDED() for an example.
  1287. *
  1288. * Since: 2.4
  1289. */
  1290. #define G_DEFINE_TYPE(TN, t_n, T_P) G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, 0, {})
  1291. /**
  1292. * G_DEFINE_TYPE_WITH_CODE:
  1293. * @TN: The name of the new type, in Camel case.
  1294. * @t_n: The name of the new type in lowercase, with words separated by '_'.
  1295. * @T_P: The #GType of the parent type.
  1296. * @_C_: Custom code that gets inserted in the *_get_type() function.
  1297. *
  1298. * A convenience macro for type implementations.
  1299. * Similar to G_DEFINE_TYPE(), but allows you to insert custom code into the
  1300. * *_get_type() function, e.g. interface implementations via G_IMPLEMENT_INTERFACE().
  1301. * See G_DEFINE_TYPE_EXTENDED() for an example.
  1302. *
  1303. * Since: 2.4
  1304. */
  1305. #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()
  1306. /**
  1307. * G_DEFINE_TYPE_WITH_PRIVATE:
  1308. * @TN: The name of the new type, in Camel case.
  1309. * @t_n: The name of the new type, in lowercase, with words
  1310. * separated by '_'.
  1311. * @T_P: The #GType of the parent type.
  1312. *
  1313. * A convenience macro for type implementations, which declares a
  1314. * class initialization function, an instance initialization function (see #GTypeInfo for information about
  1315. * these), a static variable named @t_n<!-- -->_parent_class pointing to the parent class, and adds private
  1316. * instance data to the type. Furthermore, it defines a *_get_type() function. See G_DEFINE_TYPE_EXTENDED()
  1317. * for an example.
  1318. *
  1319. * Note that private structs added with this macros must have a struct
  1320. * name of the form <replaceable>@TN</replaceable>Private.
  1321. *
  1322. * Since: 2.38
  1323. */
  1324. #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))
  1325. /**
  1326. * G_DEFINE_ABSTRACT_TYPE:
  1327. * @TN: The name of the new type, in Camel case.
  1328. * @t_n: The name of the new type, in lowercase, with words
  1329. * separated by '_'.
  1330. * @T_P: The #GType of the parent type.
  1331. *
  1332. * A convenience macro for type implementations.
  1333. * Similar to G_DEFINE_TYPE(), but defines an abstract type.
  1334. * See G_DEFINE_TYPE_EXTENDED() for an example.
  1335. *
  1336. * Since: 2.4
  1337. */
  1338. #define G_DEFINE_ABSTRACT_TYPE(TN, t_n, T_P) G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, G_TYPE_FLAG_ABSTRACT, {})
  1339. /**
  1340. * G_DEFINE_ABSTRACT_TYPE_WITH_CODE:
  1341. * @TN: The name of the new type, in Camel case.
  1342. * @t_n: The name of the new type, in lowercase, with words
  1343. * separated by '_'.
  1344. * @T_P: The #GType of the parent type.
  1345. * @_C_: Custom code that gets inserted in the @type_name_get_type() function.
  1346. *
  1347. * A convenience macro for type implementations.
  1348. * Similar to G_DEFINE_TYPE_WITH_CODE(), but defines an abstract type and allows you to
  1349. * insert custom code into the *_get_type() function, e.g. interface implementations
  1350. * via G_IMPLEMENT_INTERFACE(). See G_DEFINE_TYPE_EXTENDED() for an example.
  1351. *
  1352. * Since: 2.4
  1353. */
  1354. #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()
  1355. /**
  1356. * G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE:
  1357. * @TN: The name of the new type, in Camel case.
  1358. * @t_n: The name of the new type, in lowercase, with words
  1359. * separated by '_'.
  1360. * @T_P: The #GType of the parent type.
  1361. *
  1362. * Similar to G_DEFINE_TYPE_WITH_PRIVATE(), but defines an abstract type.
  1363. * See G_DEFINE_TYPE_EXTENDED() for an example.
  1364. *
  1365. * Since: 2.38
  1366. */
  1367. #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))
  1368. /**
  1369. * G_DEFINE_TYPE_EXTENDED:
  1370. * @TN: The name of the new type, in Camel case.
  1371. * @t_n: The name of the new type, in lowercase, with words
  1372. * separated by '_'.
  1373. * @T_P: The #GType of the parent type.
  1374. * @_f_: #GTypeFlags to pass to g_type_register_static()
  1375. * @_C_: Custom code that gets inserted in the *_get_type() function.
  1376. *
  1377. * The most general convenience macro for type implementations, on which
  1378. * G_DEFINE_TYPE(), etc are based.
  1379. *
  1380. * |[
  1381. * G_DEFINE_TYPE_EXTENDED (GtkGadget,
  1382. * gtk_gadget,
  1383. * GTK_TYPE_WIDGET,
  1384. * 0,
  1385. * G_IMPLEMENT_INTERFACE (TYPE_GIZMO,
  1386. * gtk_gadget_gizmo_init));
  1387. * ]|
  1388. * expands to
  1389. * |[
  1390. * static void gtk_gadget_init (GtkGadget *self);
  1391. * static void gtk_gadget_class_init (GtkGadgetClass *klass);
  1392. * static gpointer gtk_gadget_parent_class = NULL;
  1393. * static void gtk_gadget_class_intern_init (gpointer klass)
  1394. * {
  1395. * gtk_gadget_parent_class = g_type_class_peek_parent (klass);
  1396. * gtk_gadget_class_init ((GtkGadgetClass*) klass);
  1397. * }
  1398. *
  1399. * GType
  1400. * gtk_gadget_get_type (void)
  1401. * {
  1402. * static volatile gsize g_define_type_id__volatile = 0;
  1403. * if (g_once_init_enter (&g_define_type_id__volatile))
  1404. * {
  1405. * GType g_define_type_id =
  1406. * g_type_register_static_simple (GTK_TYPE_WIDGET,
  1407. * g_intern_static_string ("GtkGadget"),
  1408. * sizeof (GtkGadgetClass),
  1409. * (GClassInitFunc) gtk_gadget_class_intern_init,
  1410. * sizeof (GtkGadget),
  1411. * (GInstanceInitFunc) gtk_gadget_init,
  1412. * 0);
  1413. * {
  1414. * const GInterfaceInfo g_implement_interface_info = {
  1415. * (GInterfaceInitFunc) gtk_gadget_gizmo_init
  1416. * };
  1417. * g_type_add_interface_static (g_define_type_id, TYPE_GIZMO, &g_implement_interface_info);
  1418. * }
  1419. * g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
  1420. * }
  1421. * return g_define_type_id__volatile;
  1422. * }
  1423. * ]|
  1424. * The only pieces which have to be manually provided are the definitions of
  1425. * the instance and class structure and the definitions of the instance and
  1426. * class init functions.
  1427. *
  1428. * Since: 2.4
  1429. */
  1430. #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()
  1431. /**
  1432. * G_DEFINE_INTERFACE:
  1433. * @TN: The name of the new type, in Camel case.
  1434. * @t_n: The name of the new type, in lowercase, with words separated by '_'.
  1435. * @T_P: The #GType of the prerequisite type for the interface, or 0
  1436. * (%G_TYPE_INVALID) for no prerequisite type.
  1437. *
  1438. * A convenience macro for #GTypeInterface definitions, which declares
  1439. * a default vtable initialization function and defines a *_get_type()
  1440. * function.
  1441. *
  1442. * The macro expects the interface initialization function to have the
  1443. * name `t_n ## _default_init`, and the interface structure to have the
  1444. * name `TN ## Interface`.
  1445. *
  1446. * Since: 2.24
  1447. */
  1448. #define G_DEFINE_INTERFACE(TN, t_n, T_P) G_DEFINE_INTERFACE_WITH_CODE(TN, t_n, T_P, ;)
  1449. /**
  1450. * G_DEFINE_INTERFACE_WITH_CODE:
  1451. * @TN: The name of the new type, in Camel case.
  1452. * @t_n: The name of the new type, in lowercase, with words separated by '_'.
  1453. * @T_P: The #GType of the prerequisite type for the interface, or 0
  1454. * (%G_TYPE_INVALID) for no prerequisite type.
  1455. * @_C_: Custom code that gets inserted in the *_get_type() function.
  1456. *
  1457. * A convenience macro for #GTypeInterface definitions. Similar to
  1458. * G_DEFINE_INTERFACE(), but allows you to insert custom code into the
  1459. * *_get_type() function, e.g. additional interface implementations
  1460. * via G_IMPLEMENT_INTERFACE(), or additional prerequisite types. See
  1461. * G_DEFINE_TYPE_EXTENDED() for a similar example using
  1462. * G_DEFINE_TYPE_WITH_CODE().
  1463. *
  1464. * Since: 2.24
  1465. */
  1466. #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()
  1467. /**
  1468. * G_IMPLEMENT_INTERFACE:
  1469. * @TYPE_IFACE: The #GType of the interface to add
  1470. * @iface_init: The interface init function
  1471. *
  1472. * A convenience macro to ease interface addition in the @_C_ section
  1473. * of G_DEFINE_TYPE_WITH_CODE() or G_DEFINE_ABSTRACT_TYPE_WITH_CODE().
  1474. * See G_DEFINE_TYPE_EXTENDED() for an example.
  1475. *
  1476. * Note that this macro can only be used together with the G_DEFINE_TYPE_*
  1477. * macros, since it depends on variable names from those macros.
  1478. *
  1479. * Since: 2.4
  1480. */
  1481. #define G_IMPLEMENT_INTERFACE(TYPE_IFACE, iface_init) { \
  1482. const GInterfaceInfo g_implement_interface_info = { \
  1483. (GInterfaceInitFunc) iface_init, NULL, NULL \
  1484. }; \
  1485. g_type_add_interface_static (g_define_type_id, TYPE_IFACE, &g_implement_interface_info); \
  1486. }
  1487. /**
  1488. * G_ADD_PRIVATE:
  1489. * @TypeName: the name of the type in CamelCase
  1490. *
  1491. * A convenience macro to ease adding private data to instances of a new type
  1492. * in the @_C_ section of G_DEFINE_TYPE_WITH_CODE() or
  1493. * G_DEFINE_ABSTRACT_TYPE_WITH_CODE().
  1494. *
  1495. * For instance:
  1496. *
  1497. * |[
  1498. * typedef struct _MyObject MyObject;
  1499. * typedef struct _MyObjectClass MyObjectClass;
  1500. *
  1501. * typedef struct {
  1502. * gint foo;
  1503. * gint bar;
  1504. * } MyObjectPrivate;
  1505. *
  1506. * G_DEFINE_TYPE_WITH_CODE (MyObject, my_object, G_TYPE_OBJECT,
  1507. * G_ADD_PRIVATE (MyObject))
  1508. * ]|
  1509. *
  1510. * Will add MyObjectPrivate as the private data to any instance of the MyObject
  1511. * type.
  1512. *
  1513. * G_DEFINE_TYPE_* macros will automatically create a private function
  1514. * based on the arguments to this macro, which can be used to safely
  1515. * retrieve the private data from an instance of the type; for instance:
  1516. *
  1517. * |[
  1518. * gint
  1519. * my_object_get_foo (MyObject *obj)
  1520. * {
  1521. * MyObjectPrivate *priv = my_object_get_instance_private (obj);
  1522. *
  1523. * return priv->foo;
  1524. * }
  1525. *
  1526. * void
  1527. * my_object_set_bar (MyObject *obj,
  1528. * gint bar)
  1529. * {
  1530. * MyObjectPrivate *priv = my_object_get_instance_private (obj);
  1531. *
  1532. * if (priv->bar != bar)
  1533. * priv->bar = bar;
  1534. * }
  1535. * ]|
  1536. *
  1537. * Note that this macro can only be used together with the G_DEFINE_TYPE_*
  1538. * macros, since it depends on variable names from those macros.
  1539. *
  1540. * Also note that private structs added with these macros must have a struct
  1541. * name of the form <replaceable>TypeName</replaceable>Private.
  1542. *
  1543. * Since: 2.38
  1544. */
  1545. #define G_ADD_PRIVATE(TypeName) { \
  1546. TypeName##_private_offset = \
  1547. g_type_add_instance_private (g_define_type_id, sizeof (TypeName##Private)); \
  1548. }
  1549. /**
  1550. * G_PRIVATE_OFFSET:
  1551. * @TypeName: the name of the type in CamelCase
  1552. * @field: the name of the field in the private data structure
  1553. *
  1554. * Evaluates to the offset of the @field inside the instance private data
  1555. * structure for @TypeName.
  1556. *
  1557. * Note that this macro can only be used together with the G_DEFINE_TYPE_*
  1558. * and G_ADD_PRIVATE() macros, since it depends on variable names from
  1559. * those macros.
  1560. *
  1561. * Since: 2.38
  1562. */
  1563. #define G_PRIVATE_OFFSET(TypeName, field) \
  1564. (TypeName##_private_offset + (G_STRUCT_OFFSET (TypeName##Private, field)))
  1565. /**
  1566. * G_PRIVATE_FIELD_P:
  1567. * @TypeName: the name of the type in CamelCase
  1568. * @inst: the instance of @TypeName you wish to access
  1569. * @field_name: the name of the field in the private data structure
  1570. *
  1571. * Evaluates to a pointer to the @field_name inside the @inst private data
  1572. * structure for @TypeName.
  1573. *
  1574. * Note that this macro can only be used together with the G_DEFINE_TYPE_*
  1575. * and G_ADD_PRIVATE() macros, since it depends on variable names from
  1576. * those macros.
  1577. *
  1578. * Since: 2.38
  1579. */
  1580. #define G_PRIVATE_FIELD_P(TypeName, inst, field_name) \
  1581. G_STRUCT_MEMBER_P (inst, G_PRIVATE_OFFSET (TypeName, field_name))
  1582. /**
  1583. * G_PRIVATE_FIELD:
  1584. * @TypeName: the name of the type in CamelCase
  1585. * @inst: the instance of @TypeName you wish to access
  1586. * @field_type: the type of the field in the private data structure
  1587. * @field_name: the name of the field in the private data structure
  1588. *
  1589. * Evaluates to the @field_name inside the @inst private data
  1590. * structure for @TypeName.
  1591. *
  1592. * Note that this macro can only be used together with the G_DEFINE_TYPE_*
  1593. * and G_ADD_PRIVATE() macros, since it depends on variable names from
  1594. * those macros.
  1595. *
  1596. * Since: 2.38
  1597. */
  1598. #define G_PRIVATE_FIELD(TypeName, inst, field_type, field_name) \
  1599. G_STRUCT_MEMBER (field_type, inst, G_PRIVATE_OFFSET (TypeName, field_name))
  1600. /* we need to have this macro under conditional expansion, as it references
  1601. * a function that has been added in 2.38. see bug:
  1602. * https://bugzilla.gnome.org/show_bug.cgi?id=703191
  1603. */
  1604. #if GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_38
  1605. #define _G_DEFINE_TYPE_EXTENDED_CLASS_INIT(TypeName, type_name) \
  1606. static void type_name##_class_intern_init (gpointer klass) \
  1607. { \
  1608. type_name##_parent_class = g_type_class_peek_parent (klass); \
  1609. if (TypeName##_private_offset != 0) \
  1610. g_type_class_adjust_private_offset (klass, &TypeName##_private_offset); \
  1611. type_name##_class_init ((TypeName##Class*) klass); \
  1612. }
  1613. #else
  1614. #define _G_DEFINE_TYPE_EXTENDED_CLASS_INIT(TypeName, type_name) \
  1615. static void type_name##_class_intern_init (gpointer klass) \
  1616. { \
  1617. type_name##_parent_class = g_type_class_peek_parent (klass); \
  1618. type_name##_class_init ((TypeName##Class*) klass); \
  1619. }
  1620. #endif /* GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_38 */
  1621. #define _G_DEFINE_TYPE_EXTENDED_BEGIN(TypeName, type_name, TYPE_PARENT, flags) \
  1622. \
  1623. static void type_name##_init (TypeName *self); \
  1624. static void type_name##_class_init (TypeName##Class *klass); \
  1625. static gpointer type_name##_parent_class = NULL; \
  1626. static gint TypeName##_private_offset; \
  1627. \
  1628. _G_DEFINE_TYPE_EXTENDED_CLASS_INIT(TypeName, type_name) \
  1629. \
  1630. G_GNUC_UNUSED \
  1631. static inline gpointer \
  1632. type_name##_get_instance_private (TypeName *self) \
  1633. { \
  1634. return (G_STRUCT_MEMBER_P (self, TypeName##_private_offset)); \
  1635. } \
  1636. \
  1637. GType \
  1638. type_name##_get_type (void) \
  1639. { \
  1640. static volatile gsize g_define_type_id__volatile = 0; \
  1641. if (g_once_init_enter (&g_define_type_id__volatile)) \
  1642. { \
  1643. GType g_define_type_id = \
  1644. g_type_register_static_simple (TYPE_PARENT, \
  1645. g_intern_static_string (#TypeName), \
  1646. sizeof (TypeName##Class), \
  1647. (GClassInitFunc) type_name##_class_intern_init, \
  1648. sizeof (TypeName), \
  1649. (GInstanceInitFunc) type_name##_init, \
  1650. (GTypeFlags) flags); \
  1651. { /* custom code follows */
  1652. #define _G_DEFINE_TYPE_EXTENDED_END() \
  1653. /* following custom code */ \
  1654. } \
  1655. g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); \
  1656. } \
  1657. return g_define_type_id__volatile; \
  1658. } /* closes type_name##_get_type() */
  1659. #define _G_DEFINE_INTERFACE_EXTENDED_BEGIN(TypeName, type_name, TYPE_PREREQ) \
  1660. \
  1661. static void type_name##_default_init (TypeName##Interface *klass); \
  1662. \
  1663. GType \
  1664. type_name##_get_type (void) \
  1665. { \
  1666. static volatile gsize g_define_type_id__volatile = 0; \
  1667. if (g_once_init_enter (&g_define_type_id__volatile)) \
  1668. { \
  1669. GType g_define_type_id = \
  1670. g_type_register_static_simple (G_TYPE_INTERFACE, \
  1671. g_intern_static_string (#TypeName), \
  1672. sizeof (TypeName##Interface), \
  1673. (GClassInitFunc)type_name##_default_init, \
  1674. 0, \
  1675. (GInstanceInitFunc)NULL, \
  1676. (GTypeFlags) 0); \
  1677. if (TYPE_PREREQ) \
  1678. g_type_interface_add_prerequisite (g_define_type_id, TYPE_PREREQ); \
  1679. { /* custom code follows */
  1680. #define _G_DEFINE_INTERFACE_EXTENDED_END() \
  1681. /* following custom code */ \
  1682. } \
  1683. g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); \
  1684. } \
  1685. return g_define_type_id__volatile; \
  1686. } /* closes type_name##_get_type() */
  1687. /**
  1688. * G_DEFINE_BOXED_TYPE:
  1689. * @TypeName: The name of the new type, in Camel case.
  1690. * @type_name: The name of the new type, in lowercase, with words
  1691. * separated by '_'.
  1692. * @copy_func: the #GBoxedCopyFunc for the new type
  1693. * @free_func: the #GBoxedFreeFunc for the new type
  1694. *
  1695. * A convenience macro for boxed type implementations, which defines a
  1696. * type_name_get_type() function registering the boxed type.
  1697. *
  1698. * Since: 2.26
  1699. */
  1700. #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, {})
  1701. /**
  1702. * G_DEFINE_BOXED_TYPE_WITH_CODE:
  1703. * @TypeName: The name of the new type, in Camel case.
  1704. * @type_name: The name of the new type, in lowercase, with words
  1705. * separated by '_'.
  1706. * @copy_func: the #GBoxedCopyFunc for the new type
  1707. * @free_func: the #GBoxedFreeFunc for the new type
  1708. * @_C_: Custom code that gets inserted in the *_get_type() function.
  1709. *
  1710. * A convenience macro for boxed type implementations.
  1711. * Similar to G_DEFINE_BOXED_TYPE(), but allows to insert custom code into the
  1712. * type_name_get_type() function, e.g. to register value transformations with
  1713. * g_value_register_transform_func().
  1714. *
  1715. * Since: 2.26
  1716. */
  1717. #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()
  1718. /* Only use this in non-C++ on GCC >= 2.7, except for Darwin/ppc64.
  1719. * See https://bugzilla.gnome.org/show_bug.cgi?id=647145
  1720. */
  1721. #if !defined (__cplusplus) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)) && !(defined (__APPLE__) && defined (__ppc64__))
  1722. #define _G_DEFINE_BOXED_TYPE_BEGIN(TypeName, type_name, copy_func, free_func) \
  1723. GType \
  1724. type_name##_get_type (void) \
  1725. { \
  1726. static volatile gsize g_define_type_id__volatile = 0; \
  1727. if (g_once_init_enter (&g_define_type_id__volatile)) \
  1728. { \
  1729. GType (* _g_register_boxed) \
  1730. (const gchar *, \
  1731. union \
  1732. { \
  1733. TypeName * (*do_copy_type) (TypeName *); \
  1734. TypeName * (*do_const_copy_type) (const TypeName *); \
  1735. GBoxedCopyFunc do_copy_boxed; \
  1736. } __attribute__((__transparent_union__)), \
  1737. union \
  1738. { \
  1739. void (* do_free_type) (TypeName *); \
  1740. GBoxedFreeFunc do_free_boxed; \
  1741. } __attribute__((__transparent_union__)) \
  1742. ) = g_boxed_type_register_static; \
  1743. GType g_define_type_id = \
  1744. _g_register_boxed (g_intern_static_string (#TypeName), copy_func, free_func); \
  1745. { /* custom code follows */
  1746. #else
  1747. #define _G_DEFINE_BOXED_TYPE_BEGIN(TypeName, type_name, copy_func, free_func) \
  1748. GType \
  1749. type_name##_get_type (void) \
  1750. { \
  1751. static volatile gsize g_define_type_id__volatile = 0; \
  1752. if (g_once_init_enter (&g_define_type_id__volatile)) \
  1753. { \
  1754. GType g_define_type_id = \
  1755. g_boxed_type_register_static (g_intern_static_string (#TypeName), \
  1756. (GBoxedCopyFunc) copy_func, \
  1757. (GBoxedFreeFunc) free_func); \
  1758. { /* custom code follows */
  1759. #endif /* __GNUC__ */
  1760. /**
  1761. * G_DEFINE_POINTER_TYPE:
  1762. * @TypeName: The name of the new type, in Camel case.
  1763. * @type_name: The name of the new type, in lowercase, with words
  1764. * separated by '_'.
  1765. *
  1766. * A convenience macro for pointer type implementations, which defines a
  1767. * type_name_get_type() function registering the pointer type.
  1768. *
  1769. * Since: 2.26
  1770. */
  1771. #define G_DEFINE_POINTER_TYPE(TypeName, type_name) G_DEFINE_POINTER_TYPE_WITH_CODE (TypeName, type_name, {})
  1772. /**
  1773. * G_DEFINE_POINTER_TYPE_WITH_CODE:
  1774. * @TypeName: The name of the new type, in Camel case.
  1775. * @type_name: The name of the new type, in lowercase, with words
  1776. * separated by '_'.
  1777. * @_C_: Custom code that gets inserted in the *_get_type() function.
  1778. *
  1779. * A convenience macro for pointer type implementations.
  1780. * Similar to G_DEFINE_POINTER_TYPE(), but allows to insert custom code into the
  1781. * type_name_get_type() function.
  1782. *
  1783. * Since: 2.26
  1784. */
  1785. #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()
  1786. #define _G_DEFINE_POINTER_TYPE_BEGIN(TypeName, type_name) \
  1787. GType \
  1788. type_name##_get_type (void) \
  1789. { \
  1790. static volatile gsize g_define_type_id__volatile = 0; \
  1791. if (g_once_init_enter (&g_define_type_id__volatile)) \
  1792. { \
  1793. GType g_define_type_id = \
  1794. g_pointer_type_register_static (g_intern_static_string (#TypeName)); \
  1795. { /* custom code follows */
  1796. /* --- protected (for fundamental type implementations) --- */
  1797. GLIB_AVAILABLE_IN_ALL
  1798. GTypePlugin* g_type_get_plugin (GType type);
  1799. GLIB_AVAILABLE_IN_ALL
  1800. GTypePlugin* g_type_interface_get_plugin (GType instance_type,
  1801. GType interface_type);
  1802. GLIB_AVAILABLE_IN_ALL
  1803. GType g_type_fundamental_next (void);
  1804. GLIB_AVAILABLE_IN_ALL
  1805. GType g_type_fundamental (GType type_id);
  1806. GLIB_AVAILABLE_IN_ALL
  1807. GTypeInstance* g_type_create_instance (GType type);
  1808. GLIB_AVAILABLE_IN_ALL
  1809. void g_type_free_instance (GTypeInstance *instance);
  1810. GLIB_AVAILABLE_IN_ALL
  1811. void g_type_add_class_cache_func (gpointer cache_data,
  1812. GTypeClassCacheFunc cache_func);
  1813. GLIB_AVAILABLE_IN_ALL
  1814. void g_type_remove_class_cache_func (gpointer cache_data,
  1815. GTypeClassCacheFunc cache_func);
  1816. GLIB_AVAILABLE_IN_ALL
  1817. void g_type_class_unref_uncached (gpointer g_class);
  1818. GLIB_AVAILABLE_IN_ALL
  1819. void g_type_add_interface_check (gpointer check_data,
  1820. GTypeInterfaceCheckFunc check_func);
  1821. GLIB_AVAILABLE_IN_ALL
  1822. void g_type_remove_interface_check (gpointer check_data,
  1823. GTypeInterfaceCheckFunc check_func);
  1824. GLIB_AVAILABLE_IN_ALL
  1825. GTypeValueTable* g_type_value_table_peek (GType type);
  1826. /*< private >*/
  1827. GLIB_AVAILABLE_IN_ALL
  1828. gboolean g_type_check_instance (GTypeInstance *instance) G_GNUC_PURE;
  1829. GLIB_AVAILABLE_IN_ALL
  1830. GTypeInstance* g_type_check_instance_cast (GTypeInstance *instance,
  1831. GType iface_type);
  1832. GLIB_AVAILABLE_IN_ALL
  1833. gboolean g_type_check_instance_is_a (GTypeInstance *instance,
  1834. GType iface_type) G_GNUC_PURE;
  1835. GLIB_AVAILABLE_IN_ALL
  1836. GTypeClass* g_type_check_class_cast (GTypeClass *g_class,
  1837. GType is_a_type);
  1838. GLIB_AVAILABLE_IN_ALL
  1839. gboolean g_type_check_class_is_a (GTypeClass *g_class,
  1840. GType is_a_type) G_GNUC_PURE;
  1841. GLIB_AVAILABLE_IN_ALL
  1842. gboolean g_type_check_is_value_type (GType type) G_GNUC_CONST;
  1843. GLIB_AVAILABLE_IN_ALL
  1844. gboolean g_type_check_value (GValue *value) G_GNUC_PURE;
  1845. GLIB_AVAILABLE_IN_ALL
  1846. gboolean g_type_check_value_holds (GValue *value,
  1847. GType type) G_GNUC_PURE;
  1848. GLIB_AVAILABLE_IN_ALL
  1849. gboolean g_type_test_flags (GType type,
  1850. guint flags) G_GNUC_CONST;
  1851. /* --- debugging functions --- */
  1852. GLIB_AVAILABLE_IN_ALL
  1853. const gchar * g_type_name_from_instance (GTypeInstance *instance);
  1854. GLIB_AVAILABLE_IN_ALL
  1855. const gchar * g_type_name_from_class (GTypeClass *g_class);
  1856. /* --- implementation bits --- */
  1857. #ifndef G_DISABLE_CAST_CHECKS
  1858. # define _G_TYPE_CIC(ip, gt, ct) \
  1859. ((ct*) g_type_check_instance_cast ((GTypeInstance*) ip, gt))
  1860. # define _G_TYPE_CCC(cp, gt, ct) \
  1861. ((ct*) g_type_check_class_cast ((GTypeClass*) cp, gt))
  1862. #else /* G_DISABLE_CAST_CHECKS */
  1863. # define _G_TYPE_CIC(ip, gt, ct) ((ct*) ip)
  1864. # define _G_TYPE_CCC(cp, gt, ct) ((ct*) cp)
  1865. #endif /* G_DISABLE_CAST_CHECKS */
  1866. #define _G_TYPE_CHI(ip) (g_type_check_instance ((GTypeInstance*) ip))
  1867. #define _G_TYPE_CHV(vl) (g_type_check_value ((GValue*) vl))
  1868. #define _G_TYPE_IGC(ip, gt, ct) ((ct*) (((GTypeInstance*) ip)->g_class))
  1869. #define _G_TYPE_IGI(ip, gt, ct) ((ct*) g_type_interface_peek (((GTypeInstance*) ip)->g_class, gt))
  1870. #ifdef __GNUC__
  1871. # define _G_TYPE_CIT(ip, gt) (G_GNUC_EXTENSION ({ \
  1872. GTypeInstance *__inst = (GTypeInstance*) ip; GType __t = gt; gboolean __r; \
  1873. if (!__inst) \
  1874. __r = FALSE; \
  1875. else if (__inst->g_class && __inst->g_class->g_type == __t) \
  1876. __r = TRUE; \
  1877. else \
  1878. __r = g_type_check_instance_is_a (__inst, __t); \
  1879. __r; \
  1880. }))
  1881. # define _G_TYPE_CCT(cp, gt) (G_GNUC_EXTENSION ({ \
  1882. GTypeClass *__class = (GTypeClass*) cp; GType __t = gt; gboolean __r; \
  1883. if (!__class) \
  1884. __r = FALSE; \
  1885. else if (__class->g_type == __t) \
  1886. __r = TRUE; \
  1887. else \
  1888. __r = g_type_check_class_is_a (__class, __t); \
  1889. __r; \
  1890. }))
  1891. # define _G_TYPE_CVH(vl, gt) (G_GNUC_EXTENSION ({ \
  1892. GValue *__val = (GValue*) vl; GType __t = gt; gboolean __r; \
  1893. if (!__val) \
  1894. __r = FALSE; \
  1895. else if (__val->g_type == __t) \
  1896. __r = TRUE; \
  1897. else \
  1898. __r = g_type_check_value_holds (__val, __t); \
  1899. __r; \
  1900. }))
  1901. #else /* !__GNUC__ */
  1902. # define _G_TYPE_CIT(ip, gt) (g_type_check_instance_is_a ((GTypeInstance*) ip, gt))
  1903. # define _G_TYPE_CCT(cp, gt) (g_type_check_class_is_a ((GTypeClass*) cp, gt))
  1904. # define _G_TYPE_CVH(vl, gt) (g_type_check_value_holds ((GValue*) vl, gt))
  1905. #endif /* !__GNUC__ */
  1906. /**
  1907. * G_TYPE_FLAG_RESERVED_ID_BIT:
  1908. *
  1909. * A bit in the type number that's supposed to be left untouched.
  1910. */
  1911. #define G_TYPE_FLAG_RESERVED_ID_BIT ((GType) (1 << 0))
  1912. extern GTypeDebugFlags _g_type_debug_flags;
  1913. G_END_DECLS
  1914. #endif /* __G_TYPE_H__ */