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 gio.c.functions;
29 public  import gio.c.types;
30 public  import glib.ErrorG;
31 public  import glib.GException;
32 public  import glib.Source;
33 public  import gtkc.giotypes;
34 
35 
36 /**
37  * #GPollableOutputStream is implemented by #GOutputStreams that
38  * can be polled for readiness to write. This can be used when
39  * interfacing with a non-GIO API that expects
40  * UNIX-file-descriptor-style asynchronous I/O rather than GIO-style.
41  *
42  * Since: 2.28
43  */
44 public template PollableOutputStreamT(TStruct)
45 {
46 	/** Get the main Gtk struct */
47 	public GPollableOutputStream* getPollableOutputStreamStruct(bool transferOwnership = false)
48 	{
49 		if (transferOwnership)
50 			ownedRef = false;
51 		return cast(GPollableOutputStream*)getStruct();
52 	}
53 
54 
55 	/**
56 	 * Checks if @stream is actually pollable. Some classes may implement
57 	 * #GPollableOutputStream but have only certain instances of that
58 	 * class be pollable. If this method returns %FALSE, then the behavior
59 	 * of other #GPollableOutputStream methods is undefined.
60 	 *
61 	 * For any given stream, the value returned by this method is constant;
62 	 * a stream cannot switch from pollable to non-pollable or vice versa.
63 	 *
64 	 * Returns: %TRUE if @stream is pollable, %FALSE if not.
65 	 *
66 	 * Since: 2.28
67 	 */
68 	public bool canPoll()
69 	{
70 		return g_pollable_output_stream_can_poll(getPollableOutputStreamStruct()) != 0;
71 	}
72 
73 	/**
74 	 * Creates a #GSource that triggers when @stream can be written, or
75 	 * @cancellable is triggered or an error occurs. The callback on the
76 	 * source is of the #GPollableSourceFunc type.
77 	 *
78 	 * As with g_pollable_output_stream_is_writable(), it is possible that
79 	 * the stream may not actually be writable even after the source
80 	 * triggers, so you should use g_pollable_output_stream_write_nonblocking()
81 	 * rather than g_output_stream_write() from the callback.
82 	 *
83 	 * Params:
84 	 *     cancellable = a #GCancellable, or %NULL
85 	 *
86 	 * Returns: a new #GSource
87 	 *
88 	 * Since: 2.28
89 	 */
90 	public Source createSource(Cancellable cancellable)
91 	{
92 		auto __p = g_pollable_output_stream_create_source(getPollableOutputStreamStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct());
93 
94 		if(__p is null)
95 		{
96 			return null;
97 		}
98 
99 		return new Source(cast(GSource*) __p, true);
100 	}
101 
102 	/**
103 	 * Checks if @stream can be written.
104 	 *
105 	 * Note that some stream types may not be able to implement this 100%
106 	 * reliably, and it is possible that a call to g_output_stream_write()
107 	 * after this returns %TRUE would still block. To guarantee
108 	 * non-blocking behavior, you should always use
109 	 * g_pollable_output_stream_write_nonblocking(), which will return a
110 	 * %G_IO_ERROR_WOULD_BLOCK error rather than blocking.
111 	 *
112 	 * Returns: %TRUE if @stream is writable, %FALSE if not. If an error
113 	 *     has occurred on @stream, this will result in
114 	 *     g_pollable_output_stream_is_writable() returning %TRUE, and the
115 	 *     next attempt to write will return the error.
116 	 *
117 	 * Since: 2.28
118 	 */
119 	public bool isWritable()
120 	{
121 		return g_pollable_output_stream_is_writable(getPollableOutputStreamStruct()) != 0;
122 	}
123 
124 	/**
125 	 * Attempts to write up to @count bytes from @buffer to @stream, as
126 	 * with g_output_stream_write(). If @stream is not currently writable,
127 	 * this will immediately return %G_IO_ERROR_WOULD_BLOCK, and you can
128 	 * use g_pollable_output_stream_create_source() to create a #GSource
129 	 * that will be triggered when @stream is writable.
130 	 *
131 	 * Note that since this method never blocks, you cannot actually
132 	 * use @cancellable to cancel it. However, it will return an error
133 	 * if @cancellable has already been cancelled when you call, which
134 	 * may happen if you call this method after a source triggers due
135 	 * to having been cancelled.
136 	 *
137 	 * Also note that if %G_IO_ERROR_WOULD_BLOCK is returned some underlying
138 	 * transports like D/TLS require that you re-send the same @buffer and
139 	 * @count in the next write call.
140 	 *
141 	 * Params:
142 	 *     buffer = a buffer to write
143 	 *         data from
144 	 *     cancellable = a #GCancellable, or %NULL
145 	 *
146 	 * Returns: the number of bytes written, or -1 on error (including
147 	 *     %G_IO_ERROR_WOULD_BLOCK).
148 	 *
149 	 * Throws: GException on failure.
150 	 */
151 	public ptrdiff_t writeNonblocking(ubyte[] buffer, Cancellable cancellable)
152 	{
153 		GError* err = null;
154 
155 		auto __p = g_pollable_output_stream_write_nonblocking(getPollableOutputStreamStruct(), buffer.ptr, cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
156 
157 		if (err !is null)
158 		{
159 			throw new GException( new ErrorG(err) );
160 		}
161 
162 		return __p;
163 	}
164 
165 	/**
166 	 * Attempts to write the bytes contained in the @n_vectors @vectors to @stream,
167 	 * as with g_output_stream_writev(). If @stream is not currently writable,
168 	 * this will immediately return %@G_POLLABLE_RETURN_WOULD_BLOCK, and you can
169 	 * use g_pollable_output_stream_create_source() to create a #GSource
170 	 * that will be triggered when @stream is writable. @error will *not* be
171 	 * set in that case.
172 	 *
173 	 * Note that since this method never blocks, you cannot actually
174 	 * use @cancellable to cancel it. However, it will return an error
175 	 * if @cancellable has already been cancelled when you call, which
176 	 * may happen if you call this method after a source triggers due
177 	 * to having been cancelled.
178 	 *
179 	 * Also note that if %G_POLLABLE_RETURN_WOULD_BLOCK is returned some underlying
180 	 * transports like D/TLS require that you re-send the same @vectors and
181 	 * @n_vectors in the next write call.
182 	 *
183 	 * Params:
184 	 *     vectors = the buffer containing the #GOutputVectors to write.
185 	 *     bytesWritten = location to store the number of bytes that were
186 	 *         written to the stream
187 	 *     cancellable = a #GCancellable, or %NULL
188 	 *
189 	 * Returns: %@G_POLLABLE_RETURN_OK on success, %G_POLLABLE_RETURN_WOULD_BLOCK
190 	 *     if the stream is not currently writable (and @error is *not* set), or
191 	 *     %G_POLLABLE_RETURN_FAILED if there was an error in which case @error will
192 	 *     be set.
193 	 *
194 	 * Since: 2.60
195 	 *
196 	 * Throws: GException on failure.
197 	 */
198 	public GPollableReturn writevNonblocking(GOutputVector[] vectors, out size_t bytesWritten, Cancellable cancellable)
199 	{
200 		GError* err = null;
201 
202 		auto __p = g_pollable_output_stream_writev_nonblocking(getPollableOutputStreamStruct(), vectors.ptr, cast(size_t)vectors.length, &bytesWritten, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
203 
204 		if (err !is null)
205 		{
206 			throw new GException( new ErrorG(err) );
207 		}
208 
209 		return __p;
210 	}
211 }