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 gio.c.functions;
31 public  import gio.c.types;
32 private import glib.ErrorG;
33 private import glib.GException;
34 private import glib.Source;
35 private import gobject.ObjectG;
36 public  import gtkc.giotypes;
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  * Returns: 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, true);
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  * Returns: 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, true);
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  *     blocking = whether to do blocking I/O
111  *     cancellable = optional #GCancellable object, %NULL to ignore.
112  *
113  * Returns: the number of bytes read, or -1 on error.
114  *
115  * Since: 2.34
116  *
117  * Throws: GException on failure.
118  */
119 public ptrdiff_t pollableStreamRead(InputStream stream, ubyte[] buffer, bool blocking, Cancellable cancellable)
120 {
121 	GError* err = null;
122 
123 	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);
124 
125 	if (err !is null)
126 	{
127 		throw new GException( new ErrorG(err) );
128 	}
129 
130 	return p;
131 }
132 
133 /**
134  * Tries to write to @stream, as with g_output_stream_write() (if
135  * @blocking is %TRUE) or g_pollable_output_stream_write_nonblocking()
136  * (if @blocking is %FALSE). This can be used to more easily share
137  * code between blocking and non-blocking implementations of a method.
138  *
139  * If @blocking is %FALSE, then @stream must be a
140  * #GPollableOutputStream for which
141  * g_pollable_output_stream_can_poll() returns %TRUE or else the
142  * behavior is undefined. If @blocking is %TRUE, then @stream does not
143  * need to be a #GPollableOutputStream.
144  *
145  * Params:
146  *     stream = a #GOutputStream.
147  *     buffer = the buffer
148  *         containing the data to write.
149  *     blocking = whether to do blocking I/O
150  *     cancellable = optional #GCancellable object, %NULL to ignore.
151  *
152  * Returns: the number of bytes written, or -1 on error.
153  *
154  * Since: 2.34
155  *
156  * Throws: GException on failure.
157  */
158 public ptrdiff_t pollableStreamWrite(OutputStream stream, ubyte[] buffer, bool blocking, Cancellable cancellable)
159 {
160 	GError* err = null;
161 
162 	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);
163 
164 	if (err !is null)
165 	{
166 		throw new GException( new ErrorG(err) );
167 	}
168 
169 	return p;
170 }
171 
172 /**
173  * Tries to write @count bytes to @stream, as with
174  * g_output_stream_write_all(), but using g_pollable_stream_write()
175  * rather than g_output_stream_write().
176  *
177  * On a successful write of @count bytes, %TRUE is returned, and
178  * @bytes_written is set to @count.
179  *
180  * If there is an error during the operation (including
181  * %G_IO_ERROR_WOULD_BLOCK in the non-blocking case), %FALSE is
182  * returned and @error is set to indicate the error status,
183  * @bytes_written is updated to contain the number of bytes written
184  * into the stream before the error occurred.
185  *
186  * As with g_pollable_stream_write(), if @blocking is %FALSE, then
187  * @stream must be a #GPollableOutputStream for which
188  * g_pollable_output_stream_can_poll() returns %TRUE or else the
189  * behavior is undefined. If @blocking is %TRUE, then @stream does not
190  * need to be a #GPollableOutputStream.
191  *
192  * Params:
193  *     stream = a #GOutputStream.
194  *     buffer = the buffer
195  *         containing the data to write.
196  *     blocking = whether to do blocking I/O
197  *     bytesWritten = location to store the number of bytes that was
198  *         written to the stream
199  *     cancellable = optional #GCancellable object, %NULL to ignore.
200  *
201  * Returns: %TRUE on success, %FALSE if there was an error
202  *
203  * Since: 2.34
204  *
205  * Throws: GException on failure.
206  */
207 public bool pollableStreamWriteAll(OutputStream stream, ubyte[] buffer, bool blocking, out size_t bytesWritten, Cancellable cancellable)
208 {
209 	GError* err = null;
210 
211 	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;
212 
213 	if (err !is null)
214 	{
215 		throw new GException( new ErrorG(err) );
216 	}
217 
218 	return p;
219 }