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