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