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.PollableOutputStreamT;
26 
27 public  import gio.Cancellable;
28 public  import glib.ErrorG;
29 public  import glib.GException;
30 public  import glib.Source;
31 public  import gtkc.gio;
32 public  import gtkc.giotypes;
33 
34 
35 /**
36  * #GPollableOutputStream is implemented by #GOutputStreams that
37  * can be polled for readiness to write. This can be used when
38  * interfacing with a non-GIO API that expects
39  * UNIX-file-descriptor-style asynchronous I/O rather than GIO-style.
40  *
41  * Since: 2.28
42  */
43 public template PollableOutputStreamT(TStruct)
44 {
45 	/** Get the main Gtk struct */
46 	public GPollableOutputStream* getPollableOutputStreamStruct()
47 	{
48 		return cast(GPollableOutputStream*)getStruct();
49 	}
50 
51 
52 	/**
53 	 * Checks if @stream is actually pollable. Some classes may implement
54 	 * #GPollableOutputStream but have only certain instances of that
55 	 * class be pollable. If this method returns %FALSE, then the behavior
56 	 * of other #GPollableOutputStream methods is undefined.
57 	 *
58 	 * For any given stream, the value returned by this method is constant;
59 	 * a stream cannot switch from pollable to non-pollable or vice versa.
60 	 *
61 	 * Return: %TRUE if @stream is pollable, %FALSE if not.
62 	 *
63 	 * Since: 2.28
64 	 */
65 	public bool canPoll()
66 	{
67 		return g_pollable_output_stream_can_poll(getPollableOutputStreamStruct()) != 0;
68 	}
69 
70 	/**
71 	 * Creates a #GSource that triggers when @stream can be written, or
72 	 * @cancellable is triggered or an error occurs. The callback on the
73 	 * source is of the #GPollableSourceFunc type.
74 	 *
75 	 * As with g_pollable_output_stream_is_writable(), it is possible that
76 	 * the stream may not actually be writable even after the source
77 	 * triggers, so you should use g_pollable_output_stream_write_nonblocking()
78 	 * rather than g_output_stream_write() from the callback.
79 	 *
80 	 * Params:
81 	 *     cancellable = a #GCancellable, or %NULL
82 	 *
83 	 * Return: a new #GSource
84 	 *
85 	 * Since: 2.28
86 	 */
87 	public Source createSource(Cancellable cancellable)
88 	{
89 		auto p = g_pollable_output_stream_create_source(getPollableOutputStreamStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct());
90 		
91 		if(p is null)
92 		{
93 			return null;
94 		}
95 		
96 		return new Source(cast(GSource*) p, true);
97 	}
98 
99 	/**
100 	 * Checks if @stream can be written.
101 	 *
102 	 * Note that some stream types may not be able to implement this 100%
103 	 * reliably, and it is possible that a call to g_output_stream_write()
104 	 * after this returns %TRUE would still block. To guarantee
105 	 * non-blocking behavior, you should always use
106 	 * g_pollable_output_stream_write_nonblocking(), which will return a
107 	 * %G_IO_ERROR_WOULD_BLOCK error rather than blocking.
108 	 *
109 	 * Return: %TRUE if @stream is writable, %FALSE if not. If an error
110 	 *     has occurred on @stream, this will result in
111 	 *     g_pollable_output_stream_is_writable() returning %TRUE, and the
112 	 *     next attempt to write will return the error.
113 	 *
114 	 * Since: 2.28
115 	 */
116 	public bool isWritable()
117 	{
118 		return g_pollable_output_stream_is_writable(getPollableOutputStreamStruct()) != 0;
119 	}
120 
121 	/**
122 	 * Attempts to write up to @count bytes from @buffer to @stream, as
123 	 * with g_output_stream_write(). If @stream is not currently writable,
124 	 * this will immediately return %G_IO_ERROR_WOULD_BLOCK, and you can
125 	 * use g_pollable_output_stream_create_source() to create a #GSource
126 	 * that will be triggered when @stream is writable.
127 	 *
128 	 * Note that since this method never blocks, you cannot actually
129 	 * use @cancellable to cancel it. However, it will return an error
130 	 * if @cancellable has already been cancelled when you call, which
131 	 * may happen if you call this method after a source triggers due
132 	 * to having been cancelled.
133 	 *
134 	 * Params:
135 	 *     buffer = a buffer to write
136 	 *         data from
137 	 *     count = the number of bytes you want to write
138 	 *     cancellable = a #GCancellable, or %NULL
139 	 *
140 	 * Return: the number of bytes written, or -1 on error (including
141 	 *     %G_IO_ERROR_WOULD_BLOCK).
142 	 *
143 	 * Throws: GException on failure.
144 	 */
145 	public ptrdiff_t writeNonblocking(ubyte[] buffer, Cancellable cancellable)
146 	{
147 		GError* err = null;
148 		
149 		auto p = g_pollable_output_stream_write_nonblocking(getPollableOutputStreamStruct(), buffer.ptr, cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
150 		
151 		if (err !is null)
152 		{
153 			throw new GException( new ErrorG(err) );
154 		}
155 		
156 		return p;
157 	}
158 }