1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gobject.TypeClass; 26 27 private import gobject.ObjectG; 28 private import gobject.c.functions; 29 public import gobject.c.types; 30 public import gtkc.gobjecttypes; 31 private import gtkd.Loader; 32 33 34 /** 35 * An opaque structure used as the base of all classes. 36 */ 37 public class TypeClass 38 { 39 /** the main Gtk struct */ 40 protected GTypeClass* gTypeClass; 41 protected bool ownedRef; 42 43 /** Get the main Gtk struct */ 44 public GTypeClass* getTypeClassStruct(bool transferOwnership = false) 45 { 46 if (transferOwnership) 47 ownedRef = false; 48 return gTypeClass; 49 } 50 51 /** the main Gtk struct as a void* */ 52 protected void* getStruct() 53 { 54 return cast(void*)gTypeClass; 55 } 56 57 /** 58 * Sets our main struct and passes it to the parent class. 59 */ 60 public this (GTypeClass* gTypeClass, bool ownedRef = false) 61 { 62 this.gTypeClass = gTypeClass; 63 this.ownedRef = ownedRef; 64 } 65 66 ~this () 67 { 68 if ( Linker.isLoaded(LIBRARY_GOBJECT) && ownedRef ) 69 g_type_class_unref(gTypeClass); 70 } 71 72 73 /** 74 * Registers a private structure for an instantiatable type. 75 * 76 * When an object is allocated, the private structures for 77 * the type and all of its parent types are allocated 78 * sequentially in the same memory block as the public 79 * structures, and are zero-filled. 80 * 81 * Note that the accumulated size of the private structures of 82 * a type and all its parent types cannot exceed 64 KiB. 83 * 84 * This function should be called in the type's class_init() function. 85 * The private structure can be retrieved using the 86 * G_TYPE_INSTANCE_GET_PRIVATE() macro. 87 * 88 * The following example shows attaching a private structure 89 * MyObjectPrivate to an object MyObject defined in the standard 90 * GObject fashion in the type's class_init() function. 91 * 92 * Note the use of a structure member "priv" to avoid the overhead 93 * of repeatedly calling MY_OBJECT_GET_PRIVATE(). 94 * 95 * |[<!-- language="C" --> 96 * typedef struct _MyObject MyObject; 97 * typedef struct _MyObjectPrivate MyObjectPrivate; 98 * 99 * struct _MyObject { 100 * GObject parent; 101 * 102 * MyObjectPrivate *priv; 103 * }; 104 * 105 * struct _MyObjectPrivate { 106 * int some_field; 107 * }; 108 * 109 * static void 110 * my_object_class_init (MyObjectClass *klass) 111 * { 112 * g_type_class_add_private (klass, sizeof (MyObjectPrivate)); 113 * } 114 * 115 * static void 116 * my_object_init (MyObject *my_object) 117 * { 118 * my_object->priv = G_TYPE_INSTANCE_GET_PRIVATE (my_object, 119 * MY_TYPE_OBJECT, 120 * MyObjectPrivate); 121 * // my_object->priv->some_field will be automatically initialised to 0 122 * } 123 * 124 * static int 125 * my_object_get_some_field (MyObject *my_object) 126 * { 127 * MyObjectPrivate *priv; 128 * 129 * g_return_val_if_fail (MY_IS_OBJECT (my_object), 0); 130 * 131 * priv = my_object->priv; 132 * 133 * return priv->some_field; 134 * } 135 * ]| 136 * 137 * Params: 138 * privateSize = size of private structure 139 * 140 * Since: 2.4 141 */ 142 public void addPrivate(size_t privateSize) 143 { 144 g_type_class_add_private(gTypeClass, privateSize); 145 } 146 147 /** 148 * Gets the offset of the private data for instances of @g_class. 149 * 150 * This is how many bytes you should add to the instance pointer of a 151 * class in order to get the private data for the type represented by 152 * @g_class. 153 * 154 * You can only call this function after you have registered a private 155 * data area for @g_class using g_type_class_add_private(). 156 * 157 * Returns: the offset, in bytes 158 * 159 * Since: 2.38 160 */ 161 public int getInstancePrivateOffset() 162 { 163 return g_type_class_get_instance_private_offset(gTypeClass); 164 } 165 166 /** */ 167 public void* getPrivate(GType privateType) 168 { 169 return g_type_class_get_private(gTypeClass, privateType); 170 } 171 172 /** 173 * This is a convenience function often needed in class initializers. 174 * It returns the class structure of the immediate parent type of the 175 * class passed in. Since derived classes hold a reference count on 176 * their parent classes as long as they are instantiated, the returned 177 * class will always exist. 178 * 179 * This function is essentially equivalent to: 180 * g_type_class_peek (g_type_parent (G_TYPE_FROM_CLASS (g_class))) 181 * 182 * Returns: the parent class 183 * of @g_class 184 */ 185 public TypeClass peekParent() 186 { 187 auto p = g_type_class_peek_parent(gTypeClass); 188 189 if(p is null) 190 { 191 return null; 192 } 193 194 return ObjectG.getDObject!(TypeClass)(cast(GTypeClass*) p); 195 } 196 197 /** 198 * Decrements the reference count of the class structure being passed in. 199 * Once the last reference count of a class has been released, classes 200 * may be finalized by the type system, so further dereferencing of a 201 * class pointer after g_type_class_unref() are invalid. 202 */ 203 public void unref() 204 { 205 g_type_class_unref(gTypeClass); 206 } 207 208 /** 209 * A variant of g_type_class_unref() for use in #GTypeClassCacheFunc 210 * implementations. It unreferences a class without consulting the chain 211 * of #GTypeClassCacheFuncs, avoiding the recursion which would occur 212 * otherwise. 213 */ 214 public void unrefUncached() 215 { 216 g_type_class_unref_uncached(gTypeClass); 217 } 218 219 /** */ 220 public static void adjustPrivateOffset(void* gClass, int* privateSizeOrOffset) 221 { 222 g_type_class_adjust_private_offset(gClass, privateSizeOrOffset); 223 } 224 225 /** 226 * This function is essentially the same as g_type_class_ref(), 227 * except that the classes reference count isn't incremented. 228 * As a consequence, this function may return %NULL if the class 229 * of the type passed in does not currently exist (hasn't been 230 * referenced before). 231 * 232 * Params: 233 * type = type ID of a classed type 234 * 235 * Returns: the #GTypeClass 236 * structure for the given type ID or %NULL if the class does not 237 * currently exist 238 */ 239 public static TypeClass peek(GType type) 240 { 241 auto p = g_type_class_peek(type); 242 243 if(p is null) 244 { 245 return null; 246 } 247 248 return ObjectG.getDObject!(TypeClass)(cast(GTypeClass*) p); 249 } 250 251 /** 252 * A more efficient version of g_type_class_peek() which works only for 253 * static types. 254 * 255 * Params: 256 * type = type ID of a classed type 257 * 258 * Returns: the #GTypeClass 259 * structure for the given type ID or %NULL if the class does not 260 * currently exist or is dynamically loaded 261 * 262 * Since: 2.4 263 */ 264 public static TypeClass peekStatic(GType type) 265 { 266 auto p = g_type_class_peek_static(type); 267 268 if(p is null) 269 { 270 return null; 271 } 272 273 return ObjectG.getDObject!(TypeClass)(cast(GTypeClass*) p); 274 } 275 276 /** 277 * Increments the reference count of the class structure belonging to 278 * @type. This function will demand-create the class if it doesn't 279 * exist already. 280 * 281 * Params: 282 * type = type ID of a classed type 283 * 284 * Returns: the #GTypeClass 285 * structure for the given type ID 286 */ 287 public static TypeClass doref(GType type) 288 { 289 auto p = g_type_class_ref(type); 290 291 if(p is null) 292 { 293 return null; 294 } 295 296 return ObjectG.getDObject!(TypeClass)(cast(GTypeClass*) p); 297 } 298 }