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 gio.AsyncInitableT; 26 27 public import gio.AsyncResultIF; 28 public import gio.Cancellable; 29 public import glib.ErrorG; 30 public import glib.GException; 31 public import glib.Str; 32 public import gobject.ObjectG; 33 public import gtkc.gio; 34 public import gtkc.giotypes; 35 36 37 /** 38 * This is the asynchronous version of #GInitable; it behaves the same 39 * in all ways except that initialization is asynchronous. For more details 40 * see the descriptions on #GInitable. 41 * 42 * A class may implement both the #GInitable and #GAsyncInitable interfaces. 43 * 44 * Users of objects implementing this are not intended to use the interface 45 * method directly; instead it will be used automatically in various ways. 46 * For C applications you generally just call g_async_initable_new_async() 47 * directly, or indirectly via a foo_thing_new_async() wrapper. This will call 48 * g_async_initable_init_async() under the cover, calling back with %NULL and 49 * a set %GError on failure. 50 * 51 * A typical implementation might look something like this: 52 * 53 * |[<!-- language="C" --> 54 * enum { 55 * NOT_INITIALIZED, 56 * INITIALIZING, 57 * INITIALIZED 58 * }; 59 * 60 * static void 61 * _foo_ready_cb (Foo *self) 62 * { 63 * GList *l; 64 * 65 * self->priv->state = INITIALIZED; 66 * 67 * for (l = self->priv->init_results; l != NULL; l = l->next) 68 * { 69 * GTask *task = l->data; 70 * 71 * if (self->priv->success) 72 * g_task_return_boolean (task, TRUE); 73 * else 74 * g_task_return_new_error (task, ...); 75 * g_object_unref (task); 76 * } 77 * 78 * g_list_free (self->priv->init_results); 79 * self->priv->init_results = NULL; 80 * } 81 * 82 * static void 83 * foo_init_async (GAsyncInitable *initable, 84 * int io_priority, 85 * GCancellable *cancellable, 86 * GAsyncReadyCallback callback, 87 * gpointer user_data) 88 * { 89 * Foo *self = FOO (initable); 90 * GTask *task; 91 * 92 * task = g_task_new (initable, cancellable, callback, user_data); 93 * 94 * switch (self->priv->state) 95 * { 96 * case NOT_INITIALIZED: 97 * _foo_get_ready (self); 98 * self->priv->init_results = g_list_append (self->priv->init_results, 99 * task); 100 * self->priv->state = INITIALIZING; 101 * break; 102 * case INITIALIZING: 103 * self->priv->init_results = g_list_append (self->priv->init_results, 104 * task); 105 * break; 106 * case INITIALIZED: 107 * if (!self->priv->success) 108 * g_task_return_new_error (task, ...); 109 * else 110 * g_task_return_boolean (task, TRUE); 111 * g_object_unref (task); 112 * break; 113 * } 114 * } 115 * 116 * static gboolean 117 * foo_init_finish (GAsyncInitable *initable, 118 * GAsyncResult *result, 119 * GError **error) 120 * { 121 * g_return_val_if_fail (g_task_is_valid (result, initable), FALSE); 122 * 123 * return g_task_propagate_boolean (G_TASK (result), error); 124 * } 125 * 126 * static void 127 * foo_async_initable_iface_init (gpointer g_iface, 128 * gpointer data) 129 * { 130 * GAsyncInitableIface *iface = g_iface; 131 * 132 * iface->init_async = foo_init_async; 133 * iface->init_finish = foo_init_finish; 134 * } 135 * ]| 136 * 137 * Since: 2.22 138 */ 139 public template AsyncInitableT(TStruct) 140 { 141 /** Get the main Gtk struct */ 142 public GAsyncInitable* getAsyncInitableStruct() 143 { 144 return cast(GAsyncInitable*)getStruct(); 145 } 146 147 /** 148 */ 149 150 /** 151 * Helper function for constructing #GAsyncInitable object. This is 152 * similar to g_object_new_valist() but also initializes the object 153 * asynchronously. 154 * 155 * When the initialization is finished, @callback will be called. You can 156 * then call g_async_initable_new_finish() to get the new object and check 157 * for any errors. 158 * 159 * Params: 160 * objectType = a #GType supporting #GAsyncInitable. 161 * firstPropertyName = the name of the first property, followed by 162 * the value, and other property value pairs, and ended by %NULL. 163 * varArgs = The var args list generated from @first_property_name. 164 * ioPriority = the [I/O priority][io-priority] of the operation 165 * cancellable = optional #GCancellable object, %NULL to ignore. 166 * callback = a #GAsyncReadyCallback to call when the initialization is 167 * finished 168 * userData = the data to pass to callback function 169 * 170 * Since: 2.22 171 */ 172 public static void newValistAsync(GType objectType, string firstPropertyName, void* varArgs, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 173 { 174 g_async_initable_new_valist_async(objectType, Str.toStringz(firstPropertyName), varArgs, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 175 } 176 177 /** 178 * Helper function for constructing #GAsyncInitable object. This is 179 * similar to g_object_newv() but also initializes the object asynchronously. 180 * 181 * When the initialization is finished, @callback will be called. You can 182 * then call g_async_initable_new_finish() to get the new object and check 183 * for any errors. 184 * 185 * Params: 186 * objectType = a #GType supporting #GAsyncInitable. 187 * nParameters = the number of parameters in @parameters 188 * parameters = the parameters to use to construct the object 189 * ioPriority = the [I/O priority][io-priority] of the operation 190 * cancellable = optional #GCancellable object, %NULL to ignore. 191 * callback = a #GAsyncReadyCallback to call when the initialization is 192 * finished 193 * userData = the data to pass to callback function 194 * 195 * Since: 2.22 196 */ 197 public static void newvAsync(GType objectType, uint nParameters, GParameter* parameters, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 198 { 199 g_async_initable_newv_async(objectType, nParameters, parameters, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 200 } 201 202 /** 203 * Starts asynchronous initialization of the object implementing the 204 * interface. This must be done before any real use of the object after 205 * initial construction. If the object also implements #GInitable you can 206 * optionally call g_initable_init() instead. 207 * 208 * When the initialization is finished, @callback will be called. You can 209 * then call g_async_initable_init_finish() to get the result of the 210 * initialization. 211 * 212 * Implementations may also support cancellation. If @cancellable is not 213 * %NULL, then initialization can be cancelled by triggering the cancellable 214 * object from another thread. If the operation was cancelled, the error 215 * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL, and 216 * the object doesn't support cancellable initialization, the error 217 * %G_IO_ERROR_NOT_SUPPORTED will be returned. 218 * 219 * As with #GInitable, if the object is not initialized, or initialization 220 * returns with an error, then all operations on the object except 221 * g_object_ref() and g_object_unref() are considered to be invalid, and 222 * have undefined behaviour. They will often fail with g_critical() or 223 * g_warning(), but this must not be relied on. 224 * 225 * Implementations of this method must be idempotent: i.e. multiple calls 226 * to this function with the same argument should return the same results. 227 * Only the first call initializes the object; further calls return the result 228 * of the first call. This is so that it's safe to implement the singleton 229 * pattern in the GObject constructor function. 230 * 231 * For classes that also support the #GInitable interface, the default 232 * implementation of this method will run the g_initable_init() function 233 * in a thread, so if you want to support asynchronous initialization via 234 * threads, just implement the #GAsyncInitable interface without overriding 235 * any interface methods. 236 * 237 * Params: 238 * ioPriority = the [I/O priority][io-priority] of the operation 239 * cancellable = optional #GCancellable object, %NULL to ignore. 240 * callback = a #GAsyncReadyCallback to call when the request is satisfied 241 * userData = the data to pass to callback function 242 * 243 * Since: 2.22 244 */ 245 public void initAsync(int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 246 { 247 g_async_initable_init_async(getAsyncInitableStruct(), ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 248 } 249 250 /** 251 * Finishes asynchronous initialization and returns the result. 252 * See g_async_initable_init_async(). 253 * 254 * Params: 255 * res = a #GAsyncResult. 256 * 257 * Return: %TRUE if successful. If an error has occurred, this function 258 * will return %FALSE and set @error appropriately if present. 259 * 260 * Since: 2.22 261 * 262 * Throws: GException on failure. 263 */ 264 public bool initFinish(AsyncResultIF res) 265 { 266 GError* err = null; 267 268 auto p = g_async_initable_init_finish(getAsyncInitableStruct(), (res is null) ? null : res.getAsyncResultStruct(), &err) != 0; 269 270 if (err !is null) 271 { 272 throw new GException( new ErrorG(err) ); 273 } 274 275 return p; 276 } 277 278 /** 279 * Finishes the async construction for the various g_async_initable_new 280 * calls, returning the created object or %NULL on error. 281 * 282 * Params: 283 * res = the #GAsyncResult from the callback 284 * 285 * Return: a newly created #GObject, 286 * or %NULL on error. Free with g_object_unref(). 287 * 288 * Since: 2.22 289 * 290 * Throws: GException on failure. 291 */ 292 public ObjectG newFinish(AsyncResultIF res) 293 { 294 GError* err = null; 295 296 auto p = g_async_initable_new_finish(getAsyncInitableStruct(), (res is null) ? null : res.getAsyncResultStruct(), &err); 297 298 if (err !is null) 299 { 300 throw new GException( new ErrorG(err) ); 301 } 302 303 if(p is null) 304 { 305 return null; 306 } 307 308 return ObjectG.getDObject!(ObjectG)(cast(GObject*) p, true); 309 } 310 }