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.PollableUtils;
26 
27 private import gio.Cancellable;
28 private import gio.InputStream;
29 private import gio.OutputStream;
30 private import glib.ErrorG;
31 private import glib.GException;
32 private import glib.Source;
33 private import gobject.ObjectG;
34 private import gtkc.gio;
35 public  import gtkc.giotypes;
36 
37 
38 /**
39  */
40 
41 /**
42  * Utility method for #GPollableInputStream and #GPollableOutputStream
43  * implementations. Creates a new #GSource that expects a callback of
44  * type #GPollableSourceFunc. The new source does not actually do
45  * anything on its own; use g_source_add_child_source() to add other
46  * sources to it to cause it to trigger.
47  *
48  * Params:
49  *     pollableStream = the stream associated with the new source
50  *
51  * Return: the new #GSource.
52  *
53  * Since: 2.28
54  */
55 public Source pollableSourceNew(ObjectG pollableStream)
56 {
57 	auto p = g_pollable_source_new((pollableStream is null) ? null : pollableStream.getObjectGStruct());
58 	
59 	if(p is null)
60 	{
61 		return null;
62 	}
63 	
64 	return new Source(cast(GSource*) p);
65 }
66 
67 /**
68  * Utility method for #GPollableInputStream and #GPollableOutputStream
69  * implementations. Creates a new #GSource, as with
70  * g_pollable_source_new(), but also attaching @child_source (with a
71  * dummy callback), and @cancellable, if they are non-%NULL.
72  *
73  * Params:
74  *     pollableStream = the stream associated with the
75  *         new source
76  *     childSource = optional child source to attach
77  *     cancellable = optional #GCancellable to attach
78  *
79  * Return: the new #GSource.
80  *
81  * Since: 2.34
82  */
83 public Source pollableSourceNewFull(ObjectG pollableStream, Source childSource, Cancellable cancellable)
84 {
85 	auto p = g_pollable_source_new_full((pollableStream is null) ? null : pollableStream.getObjectGStruct(), (childSource is null) ? null : childSource.getSourceStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct());
86 	
87 	if(p is null)
88 	{
89 		return null;
90 	}
91 	
92 	return new Source(cast(GSource*) p);
93 }
94 
95 /**
96  * Tries to read from @stream, as with g_input_stream_read() (if
97  * @blocking is %TRUE) or g_pollable_input_stream_read_nonblocking()
98  * (if @blocking is %FALSE). This can be used to more easily share
99  * code between blocking and non-blocking implementations of a method.
100  *
101  * If @blocking is %FALSE, then @stream must be a
102  * #GPollableInputStream for which g_pollable_input_stream_can_poll()
103  * returns %TRUE, or else the behavior is undefined. If @blocking is
104  * %TRUE, then @stream does not need to be a #GPollableInputStream.
105  *
106  * Params:
107  *     stream = a #GInputStream
108  *     buffer = a buffer to
109  *         read data into
110  *     count = the number of bytes to read
111  *     blocking = whether to do blocking I/O
112  *     cancellable = optional #GCancellable object, %NULL to ignore.
113  *
114  * Return: the number of bytes read, or -1 on error.
115  *
116  * Since: 2.34
117  *
118  * Throws: GException on failure.
119  */
120 public ptrdiff_t pollableStreamRead(InputStream stream, ubyte[] buffer, bool blocking, Cancellable cancellable)
121 {
122 	GError* err = null;
123 	
124 	auto p = g_pollable_stream_read((stream is null) ? null : stream.getInputStreamStruct(), buffer.ptr, cast(size_t)buffer.length, blocking, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
125 	
126 	if (err !is null)
127 	{
128 		throw new GException( new ErrorG(err) );
129 	}
130 	
131 	return p;
132 }
133 
134 /**
135  * Tries to write to @stream, as with g_output_stream_write() (if
136  * @blocking is %TRUE) or g_pollable_output_stream_write_nonblocking()
137  * (if @blocking is %FALSE). This can be used to more easily share
138  * code between blocking and non-blocking implementations of a method.
139  *
140  * If @blocking is %FALSE, then @stream must be a
141  * #GPollableOutputStream for which
142  * g_pollable_output_stream_can_poll() returns %TRUE or else the
143  * behavior is undefined. If @blocking is %TRUE, then @stream does not
144  * need to be a #GPollableOutputStream.
145  *
146  * Params:
147  *     stream = a #GOutputStream.
148  *     buffer = the buffer
149  *         containing the data to write.
150  *     count = the number of bytes to write
151  *     blocking = whether to do blocking I/O
152  *     cancellable = optional #GCancellable object, %NULL to ignore.
153  *
154  * Return: the number of bytes written, or -1 on error.
155  *
156  * Since: 2.34
157  *
158  * Throws: GException on failure.
159  */
160 public ptrdiff_t pollableStreamWrite(OutputStream stream, ubyte[] buffer, bool blocking, Cancellable cancellable)
161 {
162 	GError* err = null;
163 	
164 	auto p = g_pollable_stream_write((stream is null) ? null : stream.getOutputStreamStruct(), buffer.ptr, cast(size_t)buffer.length, blocking, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
165 	
166 	if (err !is null)
167 	{
168 		throw new GException( new ErrorG(err) );
169 	}
170 	
171 	return p;
172 }
173 
174 /**
175  * Tries to write @count bytes to @stream, as with
176  * g_output_stream_write_all(), but using g_pollable_stream_write()
177  * rather than g_output_stream_write().
178  *
179  * On a successful write of @count bytes, %TRUE is returned, and
180  * @bytes_written is set to @count.
181  *
182  * If there is an error during the operation (including
183  * %G_IO_ERROR_WOULD_BLOCK in the non-blocking case), %FALSE is
184  * returned and @error is set to indicate the error status,
185  * @bytes_written is updated to contain the number of bytes written
186  * into the stream before the error occurred.
187  *
188  * As with g_pollable_stream_write(), if @blocking is %FALSE, then
189  * @stream must be a #GPollableOutputStream for which
190  * g_pollable_output_stream_can_poll() returns %TRUE or else the
191  * behavior is undefined. If @blocking is %TRUE, then @stream does not
192  * need to be a #GPollableOutputStream.
193  *
194  * Params:
195  *     stream = a #GOutputStream.
196  *     buffer = the buffer
197  *         containing the data to write.
198  *     count = the number of bytes to write
199  *     blocking = whether to do blocking I/O
200  *     bytesWritten = location to store the number of bytes that was
201  *         written to the stream
202  *     cancellable = optional #GCancellable object, %NULL to ignore.
203  *
204  * Return: %TRUE on success, %FALSE if there was an error
205  *
206  * Since: 2.34
207  *
208  * Throws: GException on failure.
209  */
210 public bool pollableStreamWriteAll(OutputStream stream, ubyte[] buffer, bool blocking, out size_t bytesWritten, Cancellable cancellable)
211 {
212 	GError* err = null;
213 	
214 	auto p = g_pollable_stream_write_all((stream is null) ? null : stream.getOutputStreamStruct(), buffer.ptr, cast(size_t)buffer.length, blocking, &bytesWritten, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0;
215 	
216 	if (err !is null)
217 	{
218 		throw new GException( new ErrorG(err) );
219 	}
220 	
221 	return p;
222 }