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.PollableInputStreamT;
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  * #GPollableInputStream is implemented by #GInputStreams that
37  * can be polled for readiness to read. 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 PollableInputStreamT(TStruct)
44 {
45 	/** Get the main Gtk struct */
46 	public GPollableInputStream* getPollableInputStreamStruct()
47 	{
48 		return cast(GPollableInputStream*)getStruct();
49 	}
50 
51 
52 	/**
53 	 * Checks if @stream is actually pollable. Some classes may implement
54 	 * #GPollableInputStream but have only certain instances of that class
55 	 * be pollable. If this method returns %FALSE, then the behavior of
56 	 * other #GPollableInputStream 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_input_stream_can_poll(getPollableInputStreamStruct()) != 0;
68 	}
69 
70 	/**
71 	 * Creates a #GSource that triggers when @stream can be read, 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_input_stream_is_readable(), it is possible that
76 	 * the stream may not actually be readable even after the source
77 	 * triggers, so you should use g_pollable_input_stream_read_nonblocking()
78 	 * rather than g_input_stream_read() 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_input_stream_create_source(getPollableInputStreamStruct(), (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);
97 	}
98 
99 	/**
100 	 * Checks if @stream can be read.
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_input_stream_read()
104 	 * after this returns %TRUE would still block. To guarantee
105 	 * non-blocking behavior, you should always use
106 	 * g_pollable_input_stream_read_nonblocking(), which will return a
107 	 * %G_IO_ERROR_WOULD_BLOCK error rather than blocking.
108 	 *
109 	 * Return: %TRUE if @stream is readable, %FALSE if not. If an error
110 	 *     has occurred on @stream, this will result in
111 	 *     g_pollable_input_stream_is_readable() returning %TRUE, and the
112 	 *     next attempt to read will return the error.
113 	 *
114 	 * Since: 2.28
115 	 */
116 	public bool isReadable()
117 	{
118 		return g_pollable_input_stream_is_readable(getPollableInputStreamStruct()) != 0;
119 	}
120 
121 	/**
122 	 * Attempts to read up to @count bytes from @stream into @buffer, as
123 	 * with g_input_stream_read(). If @stream is not currently readable,
124 	 * this will immediately return %G_IO_ERROR_WOULD_BLOCK, and you can
125 	 * use g_pollable_input_stream_create_source() to create a #GSource
126 	 * that will be triggered when @stream is readable.
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
136 	 *         read data into (which should be at least @count bytes long).
137 	 *     count = the number of bytes you want to read
138 	 *     cancellable = a #GCancellable, or %NULL
139 	 *
140 	 * Return: the number of bytes read, or -1 on error (including
141 	 *     %G_IO_ERROR_WOULD_BLOCK).
142 	 *
143 	 * Throws: GException on failure.
144 	 */
145 	public ptrdiff_t readNonblocking(ubyte[] buffer, Cancellable cancellable)
146 	{
147 		GError* err = null;
148 		
149 		auto p = g_pollable_input_stream_read_nonblocking(getPollableInputStreamStruct(), 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 }