442
<td class="listing_code"><pre class="programlisting"><span class="gtkdoc kwb">static void</span>
443
<span class="function">baked_cb</span> <span class="gtkdoc opt">(</span>Cake <span class="gtkdoc opt">*</span>cake<span class="gtkdoc opt">,</span>
444
gpointer user_data<span class="gtkdoc opt">)</span>
445
<span class="gtkdoc opt">{</span>
446
<span class="gtkdoc slc">// In this example, this callback is not given a reference to the cake,</span>
447
<span class="gtkdoc slc">// so the GSimpleAsyncResult has to take a reference to it.</span>
448
GSimpleAsyncResult <span class="gtkdoc opt">*</span>result <span class="gtkdoc opt">=</span> user_data<span class="gtkdoc opt">;</span>
442
<td class="listing_code"><pre class="programlisting"><span class="type">static</span> <span class="type">void</span>
443
baked_cb (Cake *cake,
444
<span class="type">gpointer</span> user_data)
446
<span class="comment">// In this example, this callback is not given a reference to the cake,</span>
447
<span class="comment">// so the GSimpleAsyncResult has to take a reference to it.</span>
448
<span class="type">GSimpleAsyncResult</span> *result = user_data;
450
<span class="keyword">if</span> <span class="gtkdoc opt">(</span>cake <span class="gtkdoc opt">==</span> NULL<span class="gtkdoc opt">)</span>
451
<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-set-error">g_simple_async_result_set_error</a></span> <span class="gtkdoc opt">(</span>result<span class="gtkdoc opt">,</span>
452
BAKER_ERRORS<span class="gtkdoc opt">,</span>
453
BAKER_ERROR_NO_FLOUR<span class="gtkdoc opt">,</span>
454
<span class="string">"Go to the supermarket"</span><span class="gtkdoc opt">);</span>
450
<span class="keyword">if</span> (cake == <span class="number">NULL</span>)
451
<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-set-error">g_simple_async_result_set_error</a></span> (result,
453
BAKER_ERROR_NO_FLOUR,
454
<span class="number">"Go to the supermarket"</span>);
455
455
<span class="keyword">else</span>
456
<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-set-op-res-gpointer">g_simple_async_result_set_op_res_gpointer</a></span> <span class="gtkdoc opt">(</span>result<span class="gtkdoc opt">,</span>
457
<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-ref">g_object_ref</a></span> <span class="gtkdoc opt">(</span>cake<span class="gtkdoc opt">),</span>
458
g_object_unref<span class="gtkdoc opt">);</span>
461
<span class="gtkdoc slc">// In this example, we assume that baked_cb is called as a callback from</span>
462
<span class="gtkdoc slc">// the mainloop, so it's safe to complete the operation synchronously here.</span>
463
<span class="gtkdoc slc">// If, however, _baker_prepare_cake () might call its callback without</span>
464
<span class="gtkdoc slc">// first returning to the mainloop — inadvisable, but some APIs do so —</span>
465
<span class="gtkdoc slc">// we would need to use g_simple_async_result_complete_in_idle().</span>
466
<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-complete">g_simple_async_result_complete</a></span> <span class="gtkdoc opt">(</span>result<span class="gtkdoc opt">);</span>
467
<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-unref">g_object_unref</a></span> <span class="gtkdoc opt">(</span>result<span class="gtkdoc opt">);</span>
468
<span class="gtkdoc opt">}</span>
470
<span class="gtkdoc kwb">void</span>
471
<span class="function">baker_bake_cake_async</span> <span class="gtkdoc opt">(</span>Baker <span class="gtkdoc opt">*</span>self<span class="gtkdoc opt">,</span>
472
guint radius<span class="gtkdoc opt">,</span>
473
GAsyncReadyCallback callback<span class="gtkdoc opt">,</span>
474
gpointer user_data<span class="gtkdoc opt">)</span>
475
<span class="gtkdoc opt">{</span>
476
GSimpleAsyncResult <span class="gtkdoc opt">*</span>simple<span class="gtkdoc opt">;</span>
477
Cake <span class="gtkdoc opt">*</span>cake<span class="gtkdoc opt">;</span>
479
<span class="keyword">if</span> <span class="gtkdoc opt">(</span>radius <span class="gtkdoc opt"><</span> <span class="number">3</span><span class="gtkdoc opt">)</span>
480
<span class="gtkdoc opt">{</span>
481
<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-report-error-in-idle">g_simple_async_report_error_in_idle</a></span> <span class="gtkdoc opt">(</span><span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#G-OBJECT:CAPS">G_OBJECT</a></span> <span class="gtkdoc opt">(</span>self<span class="gtkdoc opt">),</span>
482
callback<span class="gtkdoc opt">,</span>
483
user_data<span class="gtkdoc opt">,</span>
484
BAKER_ERRORS<span class="gtkdoc opt">,</span>
485
BAKER_ERROR_TOO_SMALL<span class="gtkdoc opt">,</span>
486
<span class="string">"%ucm radius cakes are silly"</span><span class="gtkdoc opt">,</span>
487
radius<span class="gtkdoc opt">);</span>
488
<span class="keyword">return</span><span class="gtkdoc opt">;</span>
489
<span class="gtkdoc opt">}</span>
491
simple <span class="gtkdoc opt">=</span> <span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-new">g_simple_async_result_new</a></span> <span class="gtkdoc opt">(</span><span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#G-OBJECT:CAPS">G_OBJECT</a></span> <span class="gtkdoc opt">(</span>self<span class="gtkdoc opt">),</span>
492
callback<span class="gtkdoc opt">,</span>
493
user_data<span class="gtkdoc opt">,</span>
494
baker_bake_cake_async<span class="gtkdoc opt">);</span>
495
cake <span class="gtkdoc opt">=</span> <span class="function">_baker_get_cached_cake</span> <span class="gtkdoc opt">(</span>self<span class="gtkdoc opt">,</span> radius<span class="gtkdoc opt">);</span>
497
<span class="keyword">if</span> <span class="gtkdoc opt">(</span>cake <span class="gtkdoc opt">!=</span> NULL<span class="gtkdoc opt">)</span>
498
<span class="gtkdoc opt">{</span>
499
<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-set-op-res-gpointer">g_simple_async_result_set_op_res_gpointer</a></span> <span class="gtkdoc opt">(</span>simple<span class="gtkdoc opt">,</span>
500
<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-ref">g_object_ref</a></span> <span class="gtkdoc opt">(</span>cake<span class="gtkdoc opt">),</span>
501
g_object_unref<span class="gtkdoc opt">);</span>
502
<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-complete-in-idle">g_simple_async_result_complete_in_idle</a></span> <span class="gtkdoc opt">(</span>simple<span class="gtkdoc opt">);</span>
503
<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-unref">g_object_unref</a></span> <span class="gtkdoc opt">(</span>simple<span class="gtkdoc opt">);</span>
504
<span class="gtkdoc slc">// Drop the reference returned by _baker_get_cached_cake();</span>
505
<span class="gtkdoc slc">// the GSimpleAsyncResult has taken its own reference.</span>
506
<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-unref">g_object_unref</a></span> <span class="gtkdoc opt">(</span>cake<span class="gtkdoc opt">);</span>
507
<span class="keyword">return</span><span class="gtkdoc opt">;</span>
508
<span class="gtkdoc opt">}</span>
510
<span class="function">_baker_prepare_cake</span> <span class="gtkdoc opt">(</span>self<span class="gtkdoc opt">,</span> radius<span class="gtkdoc opt">,</span> baked_cb<span class="gtkdoc opt">,</span> simple<span class="gtkdoc opt">);</span>
511
<span class="gtkdoc opt">}</span>
513
Cake <span class="gtkdoc opt">*</span>
514
<span class="function">baker_bake_cake_finish</span> <span class="gtkdoc opt">(</span>Baker <span class="gtkdoc opt">*</span>self<span class="gtkdoc opt">,</span>
515
GAsyncResult <span class="gtkdoc opt">*</span>result<span class="gtkdoc opt">,</span>
516
GError <span class="gtkdoc opt">**</span>error<span class="gtkdoc opt">)</span>
517
<span class="gtkdoc opt">{</span>
518
GSimpleAsyncResult <span class="gtkdoc opt">*</span>simple<span class="gtkdoc opt">;</span>
519
Cake <span class="gtkdoc opt">*</span>cake<span class="gtkdoc opt">;</span>
521
<span class="function"><a href="https://developer.gnome.org/glib/unstable/glib-Warnings-and-Assertions.html#g-return-val-if-fail">g_return_val_if_fail</a></span> <span class="gtkdoc opt">(</span><span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-is-valid">g_simple_async_result_is_valid</a></span> <span class="gtkdoc opt">(</span>result<span class="gtkdoc opt">,</span>
522
<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#G-OBJECT:CAPS">G_OBJECT</a></span> <span class="gtkdoc opt">(</span>self<span class="gtkdoc opt">),</span>
523
baker_bake_cake_async<span class="gtkdoc opt">),</span>
524
NULL<span class="gtkdoc opt">);</span>
526
simple <span class="gtkdoc opt">= (</span>GSimpleAsyncResult <span class="gtkdoc opt">*)</span> result<span class="gtkdoc opt">;</span>
528
<span class="keyword">if</span> <span class="gtkdoc opt">(</span><span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-propagate-error">g_simple_async_result_propagate_error</a></span> <span class="gtkdoc opt">(</span>simple<span class="gtkdoc opt">,</span> error<span class="gtkdoc opt">))</span>
529
<span class="keyword">return</span> NULL<span class="gtkdoc opt">;</span>
531
cake <span class="gtkdoc opt">=</span> <span class="function">CAKE</span> <span class="gtkdoc opt">(</span><span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-get-op-res-gpointer">g_simple_async_result_get_op_res_gpointer</a></span> <span class="gtkdoc opt">(</span>simple<span class="gtkdoc opt">));</span>
532
<span class="keyword">return</span> <span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-ref">g_object_ref</a></span> <span class="gtkdoc opt">(</span>cake<span class="gtkdoc opt">);</span>
533
<span class="gtkdoc opt">}</span></pre></td>
456
<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-set-op-res-gpointer">g_simple_async_result_set_op_res_gpointer</a></span> (result,
457
<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-ref">g_object_ref</a></span> (cake),
458
<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-unref">g_object_unref</a></span>);
461
<span class="comment">// In this example, we assume that baked_cb is called as a callback from</span>
462
<span class="comment">// the mainloop, so it's safe to complete the operation synchronously here.</span>
463
<span class="comment">// If, however, _baker_prepare_cake () might call its callback without</span>
464
<span class="comment">// first returning to the mainloop — inadvisable, but some APIs do so —</span>
465
<span class="comment">// we would need to use g_simple_async_result_complete_in_idle().</span>
466
<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-complete">g_simple_async_result_complete</a></span> (result);
467
<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-unref">g_object_unref</a></span> (result);
470
<span class="type">void</span>
471
baker_bake_cake_async (Baker *self,
472
<span class="type">guint</span> radius,
473
<span class="type">GAsyncReadyCallback</span> callback,
474
<span class="type">gpointer</span> user_data)
476
<span class="type">GSimpleAsyncResult</span> *simple;
479
<span class="keyword">if</span> (radius < <span class="number">3</span>)
481
<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-report-error-in-idle">g_simple_async_report_error_in_idle</a></span> (<span class="preproc">G_OBJECT</span> (self),
485
BAKER_ERROR_TOO_SMALL,
486
<span class="number">"</span><span class="symbol">%u</span><span class="number">cm radius cakes are silly"</span>,
488
<span class="keyword">return</span>;
491
simple = <span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-new">g_simple_async_result_new</a></span> (<span class="preproc">G_OBJECT</span> (self),
494
baker_bake_cake_async);
495
cake = _baker_get_cached_cake (self, radius);
497
<span class="keyword">if</span> (cake != <span class="number">NULL</span>)
499
<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-set-op-res-gpointer">g_simple_async_result_set_op_res_gpointer</a></span> (simple,
500
<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-ref">g_object_ref</a></span> (cake),
501
<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-unref">g_object_unref</a></span>);
502
<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-complete-in-idle">g_simple_async_result_complete_in_idle</a></span> (simple);
503
<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-unref">g_object_unref</a></span> (simple);
504
<span class="comment">// Drop the reference returned by _baker_get_cached_cake();</span>
505
<span class="comment">// the GSimpleAsyncResult has taken its own reference.</span>
506
<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-unref">g_object_unref</a></span> (cake);
507
<span class="keyword">return</span>;
510
_baker_prepare_cake (self, radius, baked_cb, simple);
514
baker_bake_cake_finish (Baker *self,
515
<span class="type">GAsyncResult</span> *result,
516
<span class="type">GError</span> **error)
518
<span class="type">GSimpleAsyncResult</span> *simple;
521
<span class="preproc">g_return_val_if_fail</span> (<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-is-valid">g_simple_async_result_is_valid</a></span> (result,
522
<span class="preproc">G_OBJECT</span> (self),
523
baker_bake_cake_async),
524
<span class="number">NULL</span>);
526
simple = (<span class="type">GSimpleAsyncResult</span> *) result;
528
<span class="keyword">if</span> (<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-propagate-error">g_simple_async_result_propagate_error</a></span> (simple, error))
529
<span class="keyword">return</span> <span class="number">NULL</span>;
531
cake = CAKE (<span class="function"><a href="GSimpleAsyncResult.html#g-simple-async-result-get-op-res-gpointer">g_simple_async_result_get_op_res_gpointer</a></span> (simple));
532
<span class="keyword">return</span> <span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-ref">g_object_ref</a></span> (cake);