root/trunk/src/gio/Cancellable.d

Revision 951, 13.7 kB (checked in by Mike Wey, 2 weeks ago)

Merge github pull request 7.

Line 
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  * Conversion parameters:
26  * inFile  = GCancellable.html
27  * outPack = gio
28  * outFile = Cancellable
29  * strct   = GCancellable
30  * realStrct=
31  * ctorStrct=
32  * clss    = Cancellable
33  * interf  =
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  =
38  * implements:
39  * prefixes:
40  *  - g_cancellable_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  *  - glib.ErrorG
47  *  - glib.GException
48  *  - glib.Source
49  * structWrap:
50  *  - GCancellable* -> Cancellable
51  *  - GSource* -> Source
52  * module aliases:
53  * local aliases:
54  * overrides:
55  */
56
57 module gio.Cancellable;
58
59 public  import gtkc.giotypes;
60
61 private import gtkc.gio;
62 private import glib.ConstructionException;
63
64 private import gobject.Signals;
65 public  import gtkc.gdktypes;
66
67 private import glib.ErrorG;
68 private import glib.GException;
69 private import glib.Source;
70
71
72
73 private import gobject.ObjectG;
74
75 /**
76  * Description
77  * GCancellable is a thread-safe operation cancellation stack used
78  * throughout GIO to allow for cancellation of synchronous and
79  * asynchronous operations.
80  */
81 public class Cancellable : ObjectG
82 {
83    
84     /** the main Gtk struct */
85     protected GCancellable* gCancellable;
86    
87    
88     public GCancellable* getCancellableStruct()
89     {
90         return gCancellable;
91     }
92    
93    
94     /** the main Gtk struct as a void* */
95     protected override void* getStruct()
96     {
97         return cast(void*)gCancellable;
98     }
99    
100     /**
101      * Sets our main struct and passes it to the parent class
102      */
103     public this (GCancellable* gCancellable)
104     {
105         if(gCancellable is null)
106         {
107             this = null;
108             return;
109         }
110         //Check if there already is a D object for this gtk struct
111         void* ptr = getDObject(cast(GObject*)gCancellable);
112         if( ptr !is null )
113         {
114             this = cast(Cancellable)ptr;
115             return;
116         }
117         super(cast(GObject*)gCancellable);
118         this.gCancellable = gCancellable;
119     }
120    
121     protected override void setStruct(GObject* obj)
122     {
123         super.setStruct(obj);
124         gCancellable = cast(GCancellable*)obj;
125     }
126    
127     /**
128      */
129     int[string] connectedSignals;
130    
131     void delegate(Cancellable)[] onCancelledListeners;
132     /**
133      * Emitted when the operation has been cancelled.
134      * Can be used by implementations of cancellable operations. If the
135      * operation is cancelled from another thread, the signal will be
136      * emitted in the thread that cancelled the operation, not the
137      * thread that is running the operation.
138      * Note that disconnecting from this signal (or any signal) in a
139      * multi-threaded program is prone to race conditions. For instance
140      * it is possible that a signal handler may be invoked even
141      * after a call to
142      * g_signal_handler_disconnect() for that handler has already
143      * returned.
144      * There is also a problem when cancellation happen
145      * right before connecting to the signal. If this happens the
146      * signal will unexpectedly not be emitted, and checking before
147      * connecting to the signal leaves a race condition where this is
148      * still happening.
149      * In order to make it safe and easy to connect handlers there
150      * are two helper functions: g_cancellable_connect() and
151      * g_cancellable_disconnect() which protect against problems
152      * like this.
153      * $(DDOC_COMMENT example)
154      * Note that the cancelled signal is emitted in the thread that
155      * the user cancelled from, which may be the main thread. So, the
156      * cancellable signal should not do something that can block.
157      */
158     void addOnCancelled(void delegate(Cancellable) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
159     {
160         if ( !("cancelled" in connectedSignals) )
161         {
162             Signals.connectData(
163             getStruct(),
164             "cancelled",
165             cast(GCallback)&callBackCancelled,
166             cast(void*)this,
167             null,
168             connectFlags);
169             connectedSignals["cancelled"] = 1;
170         }
171         onCancelledListeners ~= dlg;
172     }
173     extern(C) static void callBackCancelled(GCancellable* cancellableStruct, Cancellable cancellable)
174     {
175         foreach ( void delegate(Cancellable) dlg ; cancellable.onCancelledListeners )
176         {
177             dlg(cancellable);
178         }
179     }
180    
181    
182     /**
183      * Creates a new GCancellable object.
184      * Applications that want to start one or more operations
185      * that should be cancellable should create a GCancellable
186      * and pass it to the operations.
187      * One GCancellable can be used in multiple consecutive
188      * operations, but not in multiple concurrent operations.
189      * Throws: ConstructionException GTK+ fails to create the object.
190      */
191     public this ()
192     {
193         // GCancellable * g_cancellable_new (void);
194         auto p = g_cancellable_new();
195         if(p is null)
196         {
197             throw new ConstructionException("null returned by g_cancellable_new()");
198         }
199         this(cast(GCancellable*) p);
200     }
201    
202     /**
203      * Checks if a cancellable job has been cancelled.
204      * Returns: TRUE if cancellable is cancelled, FALSE if called with NULL or if item is not cancelled.
205      */
206     public int isCancelled()
207     {
208         // gboolean g_cancellable_is_cancelled (GCancellable *cancellable);
209         return g_cancellable_is_cancelled(gCancellable);
210     }
211    
212     /**
213      * If the cancellable is cancelled, sets the error to notify
214      * that the operation was cancelled.
215      * Returns: TRUE if cancellable was cancelled, FALSE if it was not.
216      */
217     public int setErrorIfCancelled()
218     {
219         // gboolean g_cancellable_set_error_if_cancelled  (GCancellable *cancellable,  GError **error);
220         GError* err = null;
221        
222         auto p = g_cancellable_set_error_if_cancelled(gCancellable, &err);
223        
224         if (err !is null)
225         {
226             throw new GException( new ErrorG(err) );
227         }
228        
229         return p;
230     }
231    
232     /**
233      * Gets the file descriptor for a cancellable job. This can be used to
234      * implement cancellable operations on Unix systems. The returned fd will
235      * turn readable when cancellable is cancelled.
236      * You are not supposed to read from the fd yourself, just check for
237      * readable status. Reading to unset the readable status is done
238      * with g_cancellable_reset().
239      * After a successful return from this function, you should use
240      * g_cancellable_release_fd() to free up resources allocated for
241      * the returned file descriptor.
242      * See also g_cancellable_make_pollfd().
243      * Returns: A valid file descriptor. -1 if the file descriptor is not supported, or on errors.
244      */
245     public int getFd()
246     {
247         // int g_cancellable_get_fd (GCancellable *cancellable);
248         return g_cancellable_get_fd(gCancellable);
249     }
250    
251     /**
252      * Creates a GPollFD corresponding to cancellable; this can be passed
253      * to g_poll() and used to poll for cancellation. This is useful both
254      * for unix systems without a native poll and for portability to
255      * windows.
256      * When this function returns TRUE, you should use
257      * g_cancellable_release_fd() to free up resources allocated for the
258      * pollfd. After a FALSE return, do not call g_cancellable_release_fd().
259      * If this function returns FALSE, either no cancellable was given or
260      * resource limits prevent this function from allocating the necessary
261      * structures for polling. (On Linux, you will likely have reached
262      * the maximum number of file descriptors.) The suggested way to handle
263      * these cases is to ignore the cancellable.
264      * You are not supposed to read from the fd yourself, just check for
265      * readable status. Reading to unset the readable status is done
266      * with g_cancellable_reset().
267      * Since 2.22
268      * Params:
269      * pollfd = a pointer to a GPollFD
270      * Returns: TRUE if pollfd was successfully initialized, FALSE on failure to prepare the cancellable.
271      */
272     public int makePollfd(GPollFD* pollfd)
273     {
274         // gboolean g_cancellable_make_pollfd (GCancellable *cancellable,  GPollFD *pollfd);
275         return g_cancellable_make_pollfd(gCancellable, pollfd);
276     }
277    
278     /**
279      * Releases a resources previously allocated by g_cancellable_get_fd()
280      * or g_cancellable_make_pollfd().
281      * For compatibility reasons with older releases, calling this function
282      * is not strictly required, the resources will be automatically freed
283      * when the cancellable is finalized. However, the cancellable will
284      * block scarce file descriptors until it is finalized if this function
285      * is not called. This can cause the application to run out of file
286      * descriptors when many GCancellables are used at the same time.
287      * Since 2.22
288      */
289     public void releaseFd()
290     {
291         // void g_cancellable_release_fd (GCancellable *cancellable);
292         g_cancellable_release_fd(gCancellable);
293     }
294    
295     /**
296      * Creates a source that triggers if cancellable is cancelled and
297      * calls its callback of type GCancellableSourceFunc. This is
298      * primarily useful for attaching to another (non-cancellable) source
299      * with g_source_add_child_source() to add cancellability to it.
300      * For convenience, you can call this with a NULL GCancellable,
301      * in which case the source will never trigger.
302      * Since 2.28
303      * Returns: the new GSource. [transfer full]
304      */
305     public Source sourceNew()
306     {
307         // GSource * g_cancellable_source_new (GCancellable *cancellable);
308         auto p = g_cancellable_source_new(gCancellable);
309         if(p is null)
310         {
311             return null;
312         }
313         return new Source(cast(GSource*) p);
314     }
315    
316     /**
317      * Gets the top cancellable from the stack.
318      * Returns: a GCancellable from the top of the stack, or NULL if the stack is empty. [transfer none]
319      */
320     public static Cancellable getCurrent()
321     {
322         // GCancellable * g_cancellable_get_current (void);
323         auto p = g_cancellable_get_current();
324         if(p is null)
325         {
326             return null;
327         }
328         return new Cancellable(cast(GCancellable*) p);
329     }
330    
331     /**
332      * Pops cancellable off the cancellable stack (verifying that cancellable
333      * is on the top of the stack).
334      */
335     public void popCurrent()
336     {
337         // void g_cancellable_pop_current (GCancellable *cancellable);
338         g_cancellable_pop_current(gCancellable);
339     }
340    
341     /**
342      * Pushes cancellable onto the cancellable stack. The current
343      * cancellable can then be recieved using g_cancellable_get_current().
344      * This is useful when implementing cancellable operations in
345      * code that does not allow you to pass down the cancellable object.
346      * This is typically called automatically by e.g. GFile operations,
347      * so you rarely have to call this yourself.
348      */
349     public void pushCurrent()
350     {
351         // void g_cancellable_push_current (GCancellable *cancellable);
352         g_cancellable_push_current(gCancellable);
353     }
354    
355     /**
356      * Resets cancellable to its uncancelled state.
357      */
358     public void reset()
359     {
360         // void g_cancellable_reset (GCancellable *cancellable);
361         g_cancellable_reset(gCancellable);
362     }
363    
364     /**
365      * Convenience function to connect to the "cancelled"
366      * signal. Also handles the race condition that may happen
367      * if the cancellable is cancelled right before connecting.
368      * callback is called at most once, either directly at the
369      * time of the connect if cancellable is already cancelled,
370      * or when cancellable is cancelled in some thread.
371      * data_destroy_func will be called when the handler is
372      * disconnected, or immediately if the cancellable is already
373      * cancelled.
374      * See "cancelled" for details on how to use this.
375      * Since 2.22
376      * Params:
377      * callback = The GCallback to connect.
378      * data = Data to pass to callback.
379      * dataDestroyFunc = Free function for data or NULL.
380      * Returns: The id of the signal handler or 0 if cancellable has already been cancelled.
381      */
382     public gulong connect(GCallback callback, void* data, GDestroyNotify dataDestroyFunc)
383     {
384         // gulong g_cancellable_connect (GCancellable *cancellable,  GCallback callback,  gpointer data,  GDestroyNotify data_destroy_func);
385         return g_cancellable_connect(gCancellable, callback, data, dataDestroyFunc);
386     }
387    
388     /**
389      * Disconnects a handler from a cancellable instance similar to
390      * g_signal_handler_disconnect(). Additionally, in the event that a
391      * signal handler is currently running, this call will block until the
392      * handler has finished. Calling this function from a
393      * "cancelled" signal handler will therefore result in a
394      * deadlock.
395      * This avoids a race condition where a thread cancels at the
396      * same time as the cancellable operation is finished and the
397      * signal handler is removed. See "cancelled" for
398      * details on how to use this.
399      * If cancellable is NULL or handler_id is 0 this function does
400      * nothing.
401      * Since 2.22
402      * Params:
403      * handlerId = Handler id of the handler to be disconnected, or 0.
404      */
405     public void disconnect(gulong handlerId)
406     {
407         // void g_cancellable_disconnect (GCancellable *cancellable,  gulong handler_id);
408         g_cancellable_disconnect(gCancellable, handlerId);
409     }
410    
411     /**
412      * Will set cancellable to cancelled, and will emit the
413      * "cancelled" signal. (However, see the warning about
414      * race conditions in the documentation for that signal if you are
415      * planning to connect to it.)
416      * This function is thread-safe. In other words, you can safely call
417      * it from a thread other than the one running the operation that was
418      * passed the cancellable.
419      * The convention within gio is that cancelling an asynchronous
420      * operation causes it to complete asynchronously. That is, if you
421      * cancel the operation from the same thread in which it is running,
422      * then the operation's GAsyncReadyCallback will not be invoked until
423      * the application returns to the main loop.
424      */
425     public void cancel()
426     {
427         // void g_cancellable_cancel (GCancellable *cancellable);
428         g_cancellable_cancel(gCancellable);
429     }
430 }
Note: See TracBrowser for help on using the browser.