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 * Helper function for constructing #GAsyncInitable object. This is 150 * similar to g_object_new_valist() but also initializes the object 151 * asynchronously. 152 * 153 * When the initialization is finished, @callback will be called. You can 154 * then call g_async_initable_new_finish() to get the new object and check 155 * for any errors. 156 * 157 * Params: 158 * objectType = a #GType supporting #GAsyncInitable. 159 * firstPropertyName = the name of the first property, followed by 160 * the value, and other property value pairs, and ended by %NULL. 161 * varArgs = The var args list generated from @first_property_name. 162 * ioPriority = the [I/O priority][io-priority] of the operation 163 * cancellable = optional #GCancellable object, %NULL to ignore. 164 * callback = a #GAsyncReadyCallback to call when the initialization is 165 * finished 166 * userData = the data to pass to callback function 167 * 168 * Since: 2.22 169 */ 170 public static void newValistAsync(GType objectType, string firstPropertyName, void* varArgs, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 171 { 172 g_async_initable_new_valist_async(objectType, Str.toStringz(firstPropertyName), varArgs, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 173 } 174 175 /** 176 * Helper function for constructing #GAsyncInitable object. This is 177 * similar to g_object_newv() but also initializes the object asynchronously. 178 * 179 * When the initialization is finished, @callback will be called. You can 180 * then call g_async_initable_new_finish() to get the new object and check 181 * for any errors. 182 * 183 * Params: 184 * objectType = a #GType supporting #GAsyncInitable. 185 * nParameters = the number of parameters in @parameters 186 * parameters = the parameters to use to construct the object 187 * ioPriority = the [I/O priority][io-priority] of the operation 188 * cancellable = optional #GCancellable object, %NULL to ignore. 189 * callback = a #GAsyncReadyCallback to call when the initialization is 190 * finished 191 * userData = the data to pass to callback function 192 * 193 * Since: 2.22 194 */ 195 public static void newvAsync(GType objectType, uint nParameters, GParameter* parameters, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 196 { 197 g_async_initable_newv_async(objectType, nParameters, parameters, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 198 } 199 200 /** 201 * Starts asynchronous initialization of the object implementing the 202 * interface. This must be done before any real use of the object after 203 * initial construction. If the object also implements #GInitable you can 204 * optionally call g_initable_init() instead. 205 * 206 * When the initialization is finished, @callback will be called. You can 207 * then call g_async_initable_init_finish() to get the result of the 208 * initialization. 209 * 210 * Implementations may also support cancellation. If @cancellable is not 211 * %NULL, then initialization can be cancelled by triggering the cancellable 212 * object from another thread. If the operation was cancelled, the error 213 * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL, and 214 * the object doesn't support cancellable initialization, the error 215 * %G_IO_ERROR_NOT_SUPPORTED will be returned. 216 * 217 * As with #GInitable, if the object is not initialized, or initialization 218 * returns with an error, then all operations on the object except 219 * g_object_ref() and g_object_unref() are considered to be invalid, and 220 * have undefined behaviour. They will often fail with g_critical() or 221 * g_warning(), but this must not be relied on. 222 * 223 * Implementations of this method must be idempotent: i.e. multiple calls 224 * to this function with the same argument should return the same results. 225 * Only the first call initializes the object; further calls return the result 226 * of the first call. This is so that it's safe to implement the singleton 227 * pattern in the GObject constructor function. 228 * 229 * For classes that also support the #GInitable interface, the default 230 * implementation of this method will run the g_initable_init() function 231 * in a thread, so if you want to support asynchronous initialization via 232 * threads, just implement the #GAsyncInitable interface without overriding 233 * any interface methods. 234 * 235 * Params: 236 * ioPriority = the [I/O priority][io-priority] of the operation 237 * cancellable = optional #GCancellable object, %NULL to ignore. 238 * callback = a #GAsyncReadyCallback to call when the request is satisfied 239 * userData = the data to pass to callback function 240 * 241 * Since: 2.22 242 */ 243 public void initAsync(int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 244 { 245 g_async_initable_init_async(getAsyncInitableStruct(), ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 246 } 247 248 /** 249 * Finishes asynchronous initialization and returns the result. 250 * See g_async_initable_init_async(). 251 * 252 * Params: 253 * res = a #GAsyncResult. 254 * 255 * Return: %TRUE if successful. If an error has occurred, this function 256 * will return %FALSE and set @error appropriately if present. 257 * 258 * Since: 2.22 259 * 260 * Throws: GException on failure. 261 */ 262 public bool initFinish(AsyncResultIF res) 263 { 264 GError* err = null; 265 266 auto p = g_async_initable_init_finish(getAsyncInitableStruct(), (res is null) ? null : res.getAsyncResultStruct(), &err) != 0; 267 268 if (err !is null) 269 { 270 throw new GException( new ErrorG(err) ); 271 } 272 273 return p; 274 } 275 276 /** 277 * Finishes the async construction for the various g_async_initable_new 278 * calls, returning the created object or %NULL on error. 279 * 280 * Params: 281 * res = the #GAsyncResult from the callback 282 * 283 * Return: a newly created #GObject, 284 * or %NULL on error. Free with g_object_unref(). 285 * 286 * Since: 2.22 287 * 288 * Throws: GException on failure. 289 */ 290 public ObjectG newFinish(AsyncResultIF res) 291 { 292 GError* err = null; 293 294 auto p = g_async_initable_new_finish(getAsyncInitableStruct(), (res is null) ? null : res.getAsyncResultStruct(), &err); 295 296 if (err !is null) 297 { 298 throw new GException( new ErrorG(err) ); 299 } 300 301 if(p is null) 302 { 303 return null; 304 } 305 306 return ObjectG.getDObject!(ObjectG)(cast(GObject*) p, true); 307 } 308 }