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.SimpleAsyncResult; 26 27 private import gio.AsyncResultIF; 28 private import gio.AsyncResultT; 29 private import gio.Cancellable; 30 private import gio.c.functions; 31 public import gio.c.types; 32 private import glib.ConstructionException; 33 private import glib.ErrorG; 34 private import glib.GException; 35 private import glib.Str; 36 private import gobject.ObjectG; 37 public import gtkc.giotypes; 38 39 40 /** 41 * As of GLib 2.46, #GSimpleAsyncResult is deprecated in favor of 42 * #GTask, which provides a simpler API. 43 * 44 * #GSimpleAsyncResult implements #GAsyncResult. 45 * 46 * GSimpleAsyncResult handles #GAsyncReadyCallbacks, error 47 * reporting, operation cancellation and the final state of an operation, 48 * completely transparent to the application. Results can be returned 49 * as a pointer e.g. for functions that return data that is collected 50 * asynchronously, a boolean value for checking the success or failure 51 * of an operation, or a #gssize for operations which return the number 52 * of bytes modified by the operation; all of the simple return cases 53 * are covered. 54 * 55 * Most of the time, an application will not need to know of the details 56 * of this API; it is handled transparently, and any necessary operations 57 * are handled by #GAsyncResult's interface. However, if implementing a 58 * new GIO module, for writing language bindings, or for complex 59 * applications that need better control of how asynchronous operations 60 * are completed, it is important to understand this functionality. 61 * 62 * GSimpleAsyncResults are tagged with the calling function to ensure 63 * that asynchronous functions and their finishing functions are used 64 * together correctly. 65 * 66 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new(). 67 * If the result needs to be created for a #GError, use 68 * g_simple_async_result_new_from_error() or 69 * g_simple_async_result_new_take_error(). If a #GError is not available 70 * (e.g. the asynchronous operation's doesn't take a #GError argument), 71 * but the result still needs to be created for an error condition, use 72 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va() 73 * if your application or binding requires passing a variable argument list 74 * directly), and the error can then be propagated through the use of 75 * g_simple_async_result_propagate_error(). 76 * 77 * An asynchronous operation can be made to ignore a cancellation event by 78 * calling g_simple_async_result_set_handle_cancellation() with a 79 * #GSimpleAsyncResult for the operation and %FALSE. This is useful for 80 * operations that are dangerous to cancel, such as close (which would 81 * cause a leak if cancelled before being run). 82 * 83 * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop, 84 * or it can use #GThreads. 85 * g_simple_async_result_complete() will finish an I/O task directly 86 * from the point where it is called. g_simple_async_result_complete_in_idle() 87 * will finish it from an idle handler in the 88 * [thread-default main context][g-main-context-push-thread-default] 89 * where the #GSimpleAsyncResult was created. 90 * g_simple_async_result_run_in_thread() will run the job in a 91 * separate thread and then use 92 * g_simple_async_result_complete_in_idle() to deliver the result. 93 * 94 * To set the results of an asynchronous function, 95 * g_simple_async_result_set_op_res_gpointer(), 96 * g_simple_async_result_set_op_res_gboolean(), and 97 * g_simple_async_result_set_op_res_gssize() 98 * are provided, setting the operation's result to a gpointer, gboolean, or 99 * gssize, respectively. 100 * 101 * Likewise, to get the result of an asynchronous function, 102 * g_simple_async_result_get_op_res_gpointer(), 103 * g_simple_async_result_get_op_res_gboolean(), and 104 * g_simple_async_result_get_op_res_gssize() are 105 * provided, getting the operation's result as a gpointer, gboolean, and 106 * gssize, respectively. 107 * 108 * For the details of the requirements implementations must respect, see 109 * #GAsyncResult. A typical implementation of an asynchronous operation 110 * using GSimpleAsyncResult looks something like this: 111 * 112 * |[<!-- language="C" --> 113 * static void 114 * baked_cb (Cake *cake, 115 * gpointer user_data) 116 * { 117 * // In this example, this callback is not given a reference to the cake, 118 * // so the GSimpleAsyncResult has to take a reference to it. 119 * GSimpleAsyncResult *result = user_data; 120 * 121 * if (cake == NULL) 122 * g_simple_async_result_set_error (result, 123 * BAKER_ERRORS, 124 * BAKER_ERROR_NO_FLOUR, 125 * "Go to the supermarket"); 126 * else 127 * g_simple_async_result_set_op_res_gpointer (result, 128 * g_object_ref (cake), 129 * g_object_unref); 130 * 131 * 132 * // In this example, we assume that baked_cb is called as a callback from 133 * // the mainloop, so it's safe to complete the operation synchronously here. 134 * // If, however, _baker_prepare_cake () might call its callback without 135 * // first returning to the mainloop — inadvisable, but some APIs do so — 136 * // we would need to use g_simple_async_result_complete_in_idle(). 137 * g_simple_async_result_complete (result); 138 * g_object_unref (result); 139 * } 140 * 141 * void 142 * baker_bake_cake_async (Baker *self, 143 * guint radius, 144 * GAsyncReadyCallback callback, 145 * gpointer user_data) 146 * { 147 * GSimpleAsyncResult *simple; 148 * Cake *cake; 149 * 150 * if (radius < 3) 151 * { 152 * g_simple_async_report_error_in_idle (G_OBJECT (self), 153 * callback, 154 * user_data, 155 * BAKER_ERRORS, 156 * BAKER_ERROR_TOO_SMALL, 157 * "%ucm radius cakes are silly", 158 * radius); 159 * return; 160 * } 161 * 162 * simple = g_simple_async_result_new (G_OBJECT (self), 163 * callback, 164 * user_data, 165 * baker_bake_cake_async); 166 * cake = _baker_get_cached_cake (self, radius); 167 * 168 * if (cake != NULL) 169 * { 170 * g_simple_async_result_set_op_res_gpointer (simple, 171 * g_object_ref (cake), 172 * g_object_unref); 173 * g_simple_async_result_complete_in_idle (simple); 174 * g_object_unref (simple); 175 * // Drop the reference returned by _baker_get_cached_cake(); 176 * // the GSimpleAsyncResult has taken its own reference. 177 * g_object_unref (cake); 178 * return; 179 * } 180 * 181 * _baker_prepare_cake (self, radius, baked_cb, simple); 182 * } 183 * 184 * Cake * 185 * baker_bake_cake_finish (Baker *self, 186 * GAsyncResult *result, 187 * GError **error) 188 * { 189 * GSimpleAsyncResult *simple; 190 * Cake *cake; 191 * 192 * g_return_val_if_fail (g_simple_async_result_is_valid (result, 193 * G_OBJECT (self), 194 * baker_bake_cake_async), 195 * NULL); 196 * 197 * simple = (GSimpleAsyncResult *) result; 198 * 199 * if (g_simple_async_result_propagate_error (simple, error)) 200 * return NULL; 201 * 202 * cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple)); 203 * return g_object_ref (cake); 204 * } 205 * ]| 206 */ 207 public class SimpleAsyncResult : ObjectG, AsyncResultIF 208 { 209 /** the main Gtk struct */ 210 protected GSimpleAsyncResult* gSimpleAsyncResult; 211 212 /** Get the main Gtk struct */ 213 public GSimpleAsyncResult* getSimpleAsyncResultStruct(bool transferOwnership = false) 214 { 215 if (transferOwnership) 216 ownedRef = false; 217 return gSimpleAsyncResult; 218 } 219 220 /** the main Gtk struct as a void* */ 221 protected override void* getStruct() 222 { 223 return cast(void*)gSimpleAsyncResult; 224 } 225 226 /** 227 * Sets our main struct and passes it to the parent class. 228 */ 229 public this (GSimpleAsyncResult* gSimpleAsyncResult, bool ownedRef = false) 230 { 231 this.gSimpleAsyncResult = gSimpleAsyncResult; 232 super(cast(GObject*)gSimpleAsyncResult, ownedRef); 233 } 234 235 // add the AsyncResult capabilities 236 mixin AsyncResultT!(GSimpleAsyncResult); 237 238 239 /** */ 240 public static GType getType() 241 { 242 return g_simple_async_result_get_type(); 243 } 244 245 /** 246 * Creates a #GSimpleAsyncResult. 247 * 248 * The common convention is to create the #GSimpleAsyncResult in the 249 * function that starts the asynchronous operation and use that same 250 * function as the @source_tag. 251 * 252 * If your operation supports cancellation with #GCancellable (which it 253 * probably should) then you should provide the user's cancellable to 254 * g_simple_async_result_set_check_cancellable() immediately after 255 * this function returns. 256 * 257 * Deprecated: Use g_task_new() instead. 258 * 259 * Params: 260 * sourceObject = a #GObject, or %NULL. 261 * callback = a #GAsyncReadyCallback. 262 * userData = user data passed to @callback. 263 * sourceTag = the asynchronous function. 264 * 265 * Returns: a #GSimpleAsyncResult. 266 * 267 * Throws: ConstructionException GTK+ fails to create the object. 268 */ 269 public this(ObjectG sourceObject, GAsyncReadyCallback callback, void* userData, void* sourceTag) 270 { 271 auto p = g_simple_async_result_new((sourceObject is null) ? null : sourceObject.getObjectGStruct(), callback, userData, sourceTag); 272 273 if(p is null) 274 { 275 throw new ConstructionException("null returned by new"); 276 } 277 278 this(cast(GSimpleAsyncResult*) p, true); 279 } 280 281 /** 282 * Creates a #GSimpleAsyncResult from an error condition. 283 * 284 * Deprecated: Use g_task_new() and g_task_return_error() instead. 285 * 286 * Params: 287 * sourceObject = a #GObject, or %NULL. 288 * callback = a #GAsyncReadyCallback. 289 * userData = user data passed to @callback. 290 * error = a #GError 291 * 292 * Returns: a #GSimpleAsyncResult. 293 * 294 * Throws: ConstructionException GTK+ fails to create the object. 295 */ 296 public this(ObjectG sourceObject, GAsyncReadyCallback callback, void* userData, ErrorG error) 297 { 298 auto p = g_simple_async_result_new_from_error((sourceObject is null) ? null : sourceObject.getObjectGStruct(), callback, userData, (error is null) ? null : error.getErrorGStruct()); 299 300 if(p is null) 301 { 302 throw new ConstructionException("null returned by new_from_error"); 303 } 304 305 this(cast(GSimpleAsyncResult*) p, true); 306 } 307 308 /** 309 * Ensures that the data passed to the _finish function of an async 310 * operation is consistent. Three checks are performed. 311 * 312 * First, @result is checked to ensure that it is really a 313 * #GSimpleAsyncResult. Second, @source is checked to ensure that it 314 * matches the source object of @result. Third, @source_tag is 315 * checked to ensure that it is equal to the @source_tag argument given 316 * to g_simple_async_result_new() (which, by convention, is a pointer 317 * to the _async function corresponding to the _finish function from 318 * which this function is called). (Alternatively, if either 319 * @source_tag or @result's source tag is %NULL, then the source tag 320 * check is skipped.) 321 * 322 * Deprecated: Use #GTask and g_task_is_valid() instead. 323 * 324 * Params: 325 * result = the #GAsyncResult passed to the _finish function. 326 * source = the #GObject passed to the _finish function. 327 * sourceTag = the asynchronous function. 328 * 329 * Returns: #TRUE if all checks passed or #FALSE if any failed. 330 * 331 * Since: 2.20 332 */ 333 public static bool isValid(AsyncResultIF result, ObjectG source, void* sourceTag) 334 { 335 return g_simple_async_result_is_valid((result is null) ? null : result.getAsyncResultStruct(), (source is null) ? null : source.getObjectGStruct(), sourceTag) != 0; 336 } 337 338 /** 339 * Completes an asynchronous I/O job immediately. Must be called in 340 * the thread where the asynchronous result was to be delivered, as it 341 * invokes the callback directly. If you are in a different thread use 342 * g_simple_async_result_complete_in_idle(). 343 * 344 * Calling this function takes a reference to @simple for as long as 345 * is needed to complete the call. 346 * 347 * Deprecated: Use #GTask instead. 348 */ 349 public void complete() 350 { 351 g_simple_async_result_complete(gSimpleAsyncResult); 352 } 353 354 /** 355 * Completes an asynchronous function in an idle handler in the 356 * [thread-default main context][g-main-context-push-thread-default] 357 * of the thread that @simple was initially created in 358 * (and re-pushes that context around the invocation of the callback). 359 * 360 * Calling this function takes a reference to @simple for as long as 361 * is needed to complete the call. 362 * 363 * Deprecated: Use #GTask instead. 364 */ 365 public void completeInIdle() 366 { 367 g_simple_async_result_complete_in_idle(gSimpleAsyncResult); 368 } 369 370 /** 371 * Gets the operation result boolean from within the asynchronous result. 372 * 373 * Deprecated: Use #GTask and g_task_propagate_boolean() instead. 374 * 375 * Returns: %TRUE if the operation's result was %TRUE, %FALSE 376 * if the operation's result was %FALSE. 377 */ 378 public bool getOpResGboolean() 379 { 380 return g_simple_async_result_get_op_res_gboolean(gSimpleAsyncResult) != 0; 381 } 382 383 /** 384 * Gets a pointer result as returned by the asynchronous function. 385 * 386 * Deprecated: Use #GTask and g_task_propagate_pointer() instead. 387 * 388 * Returns: a pointer from the result. 389 */ 390 public void* getOpResGpointer() 391 { 392 return g_simple_async_result_get_op_res_gpointer(gSimpleAsyncResult); 393 } 394 395 /** 396 * Gets a gssize from the asynchronous result. 397 * 398 * Deprecated: Use #GTask and g_task_propagate_int() instead. 399 * 400 * Returns: a gssize returned from the asynchronous function. 401 */ 402 public ptrdiff_t getOpResGssize() 403 { 404 return g_simple_async_result_get_op_res_gssize(gSimpleAsyncResult); 405 } 406 407 /** 408 * Gets the source tag for the #GSimpleAsyncResult. 409 * 410 * Deprecated: Use #GTask and g_task_get_source_tag() instead. 411 * 412 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult. 413 */ 414 public void* getSourceTag() 415 { 416 return g_simple_async_result_get_source_tag(gSimpleAsyncResult); 417 } 418 419 /** 420 * Propagates an error from within the simple asynchronous result to 421 * a given destination. 422 * 423 * If the #GCancellable given to a prior call to 424 * g_simple_async_result_set_check_cancellable() is cancelled then this 425 * function will return %TRUE with @dest set appropriately. 426 * 427 * Deprecated: Use #GTask instead. 428 * 429 * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise. 430 * 431 * Throws: GException on failure. 432 */ 433 public bool propagateError() 434 { 435 GError* err = null; 436 437 auto p = g_simple_async_result_propagate_error(gSimpleAsyncResult, &err) != 0; 438 439 if (err !is null) 440 { 441 throw new GException( new ErrorG(err) ); 442 } 443 444 return p; 445 } 446 447 /** 448 * Runs the asynchronous job in a separate thread and then calls 449 * g_simple_async_result_complete_in_idle() on @simple to return 450 * the result to the appropriate main loop. 451 * 452 * Calling this function takes a reference to @simple for as long as 453 * is needed to run the job and report its completion. 454 * 455 * Deprecated: Use #GTask and g_task_run_in_thread() instead. 456 * 457 * Params: 458 * func = a #GSimpleAsyncThreadFunc. 459 * ioPriority = the io priority of the request. 460 * cancellable = optional #GCancellable object, %NULL to ignore. 461 */ 462 public void runInThread(GSimpleAsyncThreadFunc func, int ioPriority, Cancellable cancellable) 463 { 464 g_simple_async_result_run_in_thread(gSimpleAsyncResult, func, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct()); 465 } 466 467 /** 468 * Sets a #GCancellable to check before dispatching results. 469 * 470 * This function has one very specific purpose: the provided cancellable 471 * is checked at the time of g_simple_async_result_propagate_error() If 472 * it is cancelled, these functions will return an "Operation was 473 * cancelled" error (%G_IO_ERROR_CANCELLED). 474 * 475 * Implementors of cancellable asynchronous functions should use this in 476 * order to provide a guarantee to their callers that cancelling an 477 * async operation will reliably result in an error being returned for 478 * that operation (even if a positive result for the operation has 479 * already been sent as an idle to the main context to be dispatched). 480 * 481 * The checking described above is done regardless of any call to the 482 * unrelated g_simple_async_result_set_handle_cancellation() function. 483 * 484 * Deprecated: Use #GTask instead. 485 * 486 * Params: 487 * checkCancellable = a #GCancellable to check, or %NULL to unset 488 * 489 * Since: 2.32 490 */ 491 public void setCheckCancellable(Cancellable checkCancellable) 492 { 493 g_simple_async_result_set_check_cancellable(gSimpleAsyncResult, (checkCancellable is null) ? null : checkCancellable.getCancellableStruct()); 494 } 495 496 /** 497 * Sets an error within the asynchronous result without a #GError. 498 * Unless writing a binding, see g_simple_async_result_set_error(). 499 * 500 * Deprecated: Use #GTask and g_task_return_error() instead. 501 * 502 * Params: 503 * domain = a #GQuark (usually #G_IO_ERROR). 504 * code = an error code. 505 * format = a formatted error reporting string. 506 * args = va_list of arguments. 507 */ 508 public void setErrorVa(GQuark domain, int code, string format, void* args) 509 { 510 g_simple_async_result_set_error_va(gSimpleAsyncResult, domain, code, Str.toStringz(format), args); 511 } 512 513 /** 514 * Sets the result from a #GError. 515 * 516 * Deprecated: Use #GTask and g_task_return_error() instead. 517 * 518 * Params: 519 * error = #GError. 520 */ 521 public void setFromError(ErrorG error) 522 { 523 g_simple_async_result_set_from_error(gSimpleAsyncResult, (error is null) ? null : error.getErrorGStruct()); 524 } 525 526 /** 527 * Sets whether to handle cancellation within the asynchronous operation. 528 * 529 * This function has nothing to do with 530 * g_simple_async_result_set_check_cancellable(). It only refers to the 531 * #GCancellable passed to g_simple_async_result_run_in_thread(). 532 * 533 * Params: 534 * handleCancellation = a #gboolean. 535 */ 536 public void setHandleCancellation(bool handleCancellation) 537 { 538 g_simple_async_result_set_handle_cancellation(gSimpleAsyncResult, handleCancellation); 539 } 540 541 /** 542 * Sets the operation result to a boolean within the asynchronous result. 543 * 544 * Deprecated: Use #GTask and g_task_return_boolean() instead. 545 * 546 * Params: 547 * opRes = a #gboolean. 548 */ 549 public void setOpResGboolean(bool opRes) 550 { 551 g_simple_async_result_set_op_res_gboolean(gSimpleAsyncResult, opRes); 552 } 553 554 /** 555 * Sets the operation result within the asynchronous result to a pointer. 556 * 557 * Deprecated: Use #GTask and g_task_return_pointer() instead. 558 * 559 * Params: 560 * opRes = a pointer result from an asynchronous function. 561 * destroyOpRes = a #GDestroyNotify function. 562 */ 563 public void setOpResGpointer(void* opRes, GDestroyNotify destroyOpRes) 564 { 565 g_simple_async_result_set_op_res_gpointer(gSimpleAsyncResult, opRes, destroyOpRes); 566 } 567 568 /** 569 * Sets the operation result within the asynchronous result to 570 * the given @op_res. 571 * 572 * Deprecated: Use #GTask and g_task_return_int() instead. 573 * 574 * Params: 575 * opRes = a #gssize. 576 */ 577 public void setOpResGssize(ptrdiff_t opRes) 578 { 579 g_simple_async_result_set_op_res_gssize(gSimpleAsyncResult, opRes); 580 } 581 582 /** 583 * Sets the result from @error, and takes over the caller's ownership 584 * of @error, so the caller does not need to free it any more. 585 * 586 * Deprecated: Use #GTask and g_task_return_error() instead. 587 * 588 * Params: 589 * error = a #GError 590 * 591 * Since: 2.28 592 */ 593 public void takeError(ErrorG error) 594 { 595 g_simple_async_result_take_error(gSimpleAsyncResult, (error is null) ? null : error.getErrorGStruct()); 596 } 597 598 /** 599 * Reports an error in an idle function. Similar to 600 * g_simple_async_report_error_in_idle(), but takes a #GError rather 601 * than building a new one. 602 * 603 * Deprecated: Use g_task_report_error(). 604 * 605 * Params: 606 * object = a #GObject, or %NULL 607 * callback = a #GAsyncReadyCallback. 608 * userData = user data passed to @callback. 609 * error = the #GError to report 610 */ 611 public static void simpleAsyncReportGerrorInIdle(ObjectG object, GAsyncReadyCallback callback, void* userData, ErrorG error) 612 { 613 g_simple_async_report_gerror_in_idle((object is null) ? null : object.getObjectGStruct(), callback, userData, (error is null) ? null : error.getErrorGStruct()); 614 } 615 616 /** 617 * Reports an error in an idle function. Similar to 618 * g_simple_async_report_gerror_in_idle(), but takes over the caller's 619 * ownership of @error, so the caller does not have to free it any more. 620 * 621 * Deprecated: Use g_task_report_error(). 622 * 623 * Params: 624 * object = a #GObject, or %NULL 625 * callback = a #GAsyncReadyCallback. 626 * userData = user data passed to @callback. 627 * error = the #GError to report 628 * 629 * Since: 2.28 630 */ 631 public static void simpleAsyncReportTakeGerrorInIdle(ObjectG object, GAsyncReadyCallback callback, void* userData, ErrorG error) 632 { 633 g_simple_async_report_take_gerror_in_idle((object is null) ? null : object.getObjectGStruct(), callback, userData, (error is null) ? null : error.getErrorGStruct()); 634 } 635 }