summaryrefslogtreecommitdiff
path: root/gio/src/outputstream.hg
blob: a5bf35b394ba29f1e03ab6e7f9e2a550d28b25d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 2 -*-

/* Copyright (C) 2007 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <glibmm/object.h>
#include <giomm/asyncresult.h>
#include <giomm/cancellable.h>
#include <giomm/inputstream.h>

_DEFS(giomm,gio)
_PINCLUDE(glibmm/private/object_p.h)

namespace Gio
{

_WRAP_ENUM(OutputStreamSpliceFlags, GOutputStreamSpliceFlags, NO_GTYPE)

/** Base class for implementing streaming output.
 *
 * @ingroup Streams
 *
 * @newin2p16
 */
class OutputStream : public Glib::Object
{
  _CLASS_GOBJECT(OutputStream, GOutputStream, G_OUTPUT_STREAM, Glib::Object, GObject)

public:

  _WRAP_METHOD(gssize write(const void* buffer, gsize count, const Glib::RefPtr<Cancellable>& cancellable),
               g_output_stream_write,
               errthrow)

  /** Tries to write @a count  bytes from @a buffer  into the stream. Will block
   * during the operation.
   * 
   * If count is zero returns zero and does nothing. A value of @a count 
   * larger than MAXSSIZE will cause a Gio::Error with INVALID_ARGUMENT to be thrown.
   * 
   * On success, the number of bytes written to the stream is returned.
   * It is not an error if this is not the same as the requested size, as it
   * can happen e.g. on a partial i/o error, or if the there is not enough
   * storage in the stream. All writes either block until at least one byte
   * is written, so zero is never returned (unless @a count  is zero).
   * 
   * On error -1 is returned.
   * @param buffer The buffer containing the data to write.
   * @param count The number of bytes to write.
   * @return Number of bytes written, or -1 on error.
   */
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  gssize write(const void* buffer, gsize count);
  #else
  gssize write(const void* buffer, gsize count, std::auto_ptr<Glib::Error>& error);
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  /** Tries to write @a count  bytes from @a buffer  into the stream. Will block
   * during the operation.
   * 
   * If count is zero returns zero and does nothing. A value of @a count 
   * larger than MAXSSIZE will cause a Gio::Error with INVALID_ARGUMENT to be thrown.
   * 
   * On success, the number of bytes written to the stream is returned.
   * It is not an error if this is not the same as the requested size, as it
   * can happen e.g. on a partial i/o error, or if the there is not enough
   * storage in the stream. All writes either block until at least one byte
   * is written, so zero is never returned (unless @a count  is zero).
   * 
   * On error -1 is returned.
   * @param buffer The buffer containing the data to write.
   * @param cancellable Cancellable object.
   * @return Number of bytes written, or -1 on error.
   */
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  gssize write(const std::string& buffer, const Glib::RefPtr<Cancellable>& cancellable);
  #else
  gssize write(const std::string& buffer, const Glib::RefPtr<Cancellable>& cancellable, std::auto_ptr<Glib::Error>& error);
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  /** Tries to write @a count  bytes from @a buffer  into the stream. Will block
   * during the operation.
   * 
   * If string that is larger than MAXSSIZE bytes will cause a Gio::Error with INVALID_ARGUMENT to be thrown.
   * 
   * On success, the number of bytes written to the stream is returned.
   * It is not an error if this is not the same as the requested size, as it
   * can happen e.g. on a partial i/o error, or if the there is not enough
   * storage in the stream. All writes either block until at least one byte
   * is written, so zero is never returned (unless @a count  is zero).
   * 
   * On error -1 is returned.
   * @param buffer The buffer containing the data to write.
   * @return Number of bytes written, or -1 on error.
   */
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  gssize write(const std::string& buffer);
  #else
  gssize write(const std::string& buffer, std::auto_ptr<Glib::Error>& error);
  #endif //GLIBMM_EXCEPTIONS_ENABLED


  _WRAP_METHOD(bool write_all(const void* buffer, gsize count, gsize& bytes_written, const Glib::RefPtr<Cancellable>& cancellable),
               g_output_stream_write_all,
               errthrow)

  /** Tries to write @a count  bytes from @a buffer into the stream. Will block
   * during the operation.
   * 
   * This function is similar to write(), except it tries to
   * write as many bytes as requested, only stopping on an error.
   * 
   * On a successful write of @a count  bytes, <tt>true</tt> is returned, and @a bytes_written 
   * is set to @a count .
   * 
   * If there is an error during the operation <tt>false</tt> is returned and @a error 
   * is set to indicate the error status, @a bytes_written  is updated to contain
   * the number of bytes written into the stream before the error occured.
   * @param buffer The buffer containing the data to write.
   * @param count The number of bytes to write.
   * @param bytes_written Location to store the number of bytes that was 
   * written to the stream.
   * @return <tt>true</tt> on success, <tt>false</tt> if there was an error.
   */
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  bool write_all(const void* buffer, gsize count, gsize& bytes_written);
  #else
  bool write_all(const void* buffer, gsize count, gsize& bytes_written, std::auto_ptr<Glib::Error>& error);
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  /** Tries to write @a count  bytes from @a buffer into the stream. Will block
   * during the operation.
   * 
   * This function is similar to write(), except it tries to
   * write as many bytes as requested, only stopping on an error.
   * 
   * On a successful write of @a count  bytes, <tt>true</tt> is returned, and @a bytes_written 
   * is set to @a count .
   * 
   * If there is an error during the operation <tt>false</tt> is returned and @a error 
   * is set to indicate the error status, @a bytes_written  is updated to contain
   * the number of bytes written into the stream before the error occured.
   * @param buffer The buffer containing the data to write.
   * @param bytes_written Location to store the number of bytes that was 
   * written to the stream.
   * @param cancellable Cancellable object.
   * @return <tt>true</tt> on success, <tt>false</tt> if there was an error.
   */
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  bool write_all(const std::string& buffer, gsize& bytes_written, const Glib::RefPtr<Cancellable>& cancellable);
  #else
  bool write_all(const std::string& buffer, gsize& bytes_written, const Glib::RefPtr<Cancellable>& cancellable, std::auto_ptr<Glib::Error>& error);
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  /** Tries to write @a count  bytes from @a buffer into the stream. Will block
   * during the operation.
   * 
   * This function is similar to write(), except it tries to
   * write as many bytes as requested, only stopping on an error.
   * 
   * On a successful write of @a count  bytes, <tt>true</tt> is returned, and @a bytes_written 
   * is set to @a count .
   * 
   * If there is an error during the operation <tt>false</tt> is returned and @a error 
   * is set to indicate the error status, @a bytes_written  is updated to contain
   * the number of bytes written into the stream before the error occured.
   * @param buffer The buffer containing the data to write.
   * @param bytes_written Location to store the number of bytes that was 
   * written to the stream.
   * @return <tt>true</tt> on success, <tt>false</tt> if there was an error.
   */
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  bool write_all(const std::string& buffer, gsize& bytes_written);
  #else
  bool write_all(const std::string& buffer, gsize& bytes_written, std::auto_ptr<Glib::Error>& error);
  #endif //GLIBMM_EXCEPTIONS_ENABLED
   
  /** Splices an input stream into an output stream.
   *
   * @param source An InputStream.
   * @param flags A set of OutputStreamSpliceFlags.
   * @param cancellable A Cancellable object.
   * ignore.
   * @return A #gssize containing the size of the data spliced.
   */
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  gssize splice(const Glib::RefPtr<InputStream>& source, const Glib::RefPtr<Cancellable>& cancellable, OutputStreamSpliceFlags flags = OUTPUT_STREAM_SPLICE_NONE);
  #else
  gssize splice(const Glib::RefPtr<InputStream>& source, const Glib::RefPtr<Cancellable>& cancellable, OutputStreamSpliceFlags flags, std::auto_ptr<Glib::Error>& error);
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  /** Splices an input stream into an output stream.
   *
   * @param source An InputStream.
   * @param flags A set of OutputStreamSpliceFlags.
   * ignore.
   * @return A #gssize containing the size of the data spliced.
   */
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  gssize splice(const Glib::RefPtr<InputStream>& source, OutputStreamSpliceFlags flags = OUTPUT_STREAM_SPLICE_NONE);
  #else
  gssize splice(const Glib::RefPtr<InputStream>& source, OutputStreamSpliceFlags flags, std::auto_ptr<Glib::Error>& error);
  #endif //GLIBMM_EXCEPTIONS_ENABLED
  _IGNORE(g_output_stream_splice)

  _WRAP_METHOD(bool flush(const Glib::RefPtr<Cancellable>& cancellable),
               g_output_stream_flush,
               errthrow)

  /** Flushed any outstanding buffers in the stream. Will block during 
   * the operation. Closing the stream will implicitly cause a flush.
   * 
   * This function is optional for inherited classes.
   * 
   * The operation can be cancelled by
   * triggering the cancellable object from another thread. If the operation
   * was cancelled, a Gio::Error with CANCELLED will be thrown.
   * @param cancellable Optional cancellable object.
   * @return <tt>true</tt> on success, <tt>false</tt> on error.
   */
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  bool flush();
  #else
  bool flush(std::auto_ptr<Glib::Error>& error);
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  _WRAP_METHOD(bool close(const Glib::RefPtr<Cancellable>& cancellable),
               g_output_stream_close,
               errthrow)

  /** Closes the stream, releasing resources related to it.
   * 
   * Once the stream is closed, all other operations will throw a Gio::Error with CLOSED.
   * Closing a stream multiple times will not return an error.
   * 
   * Closing a stream will automatically flush any outstanding buffers in the
   * stream.
   * 
   * Streams will be automatically closed when the last reference
   * is dropped, but you might want to call make sure resources
   * are released as early as possible.
   * 
   * Some streams might keep the backing store of the stream (e.g. a file descriptor)
   * open after the stream is closed. See the documentation for the individual
   * stream for details.
   * 
   * On failure the first error that happened will be reported, but the close
   * operation will finish as much as possible. A stream that failed to
   * close will still throw a Gio::Error with CLOSED for all operations. Still, it
   * is important to check and report the error to the user, otherwise
   * there might be a loss of data as all data might not be written.
   * 
   * The operation can be cancelled by
   * triggering the cancellable object from another thread. If the operation
   * was cancelled, a Gio::Error with CANCELLED will be thrown.
   * Cancelling a close will still leave the stream closed, but there some streams
   * can use a faster close that doesn't block to e.g. check errors. On
   * cancellation (as with any error) there is no guarantee that all written
   * data will reach the target.
   * @param cancellable Optional cancellable object.
   * @return <tt>true</tt> on success, <tt>false</tt> on failure.
   */
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  bool close();
  #else
  bool close(std::auto_ptr<Glib::Error>& error);
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  /** Request an asynchronous write of @a count bytes from @a buffer into 
   * the stream. When the operation is finished @a slot will be called. 
   * You can then call write_finish() to get the result of the 
   * operation.
   *
   * During an async request no other sync and async calls are allowed, 
   * and will result in Gio::Error with PENDING being thrown. 
   *
   * A value of @a count larger than MAXSSIZE will cause a Gio::Error with  
   * NVALID_ARGUMENT to be thrown.
   *
   * On success, the number of bytes written will be passed to the
   * callback @a slot. It is not an error if this is not the same as the 
   * requested size, as it can happen e.g. on a partial I/O error, 
   * but generally we try to write as many bytes as requested. 
   *
   * Any outstanding I/O request with higher priority (lower numerical 
   * value) will be executed before an outstanding request with lower 
   * priority. Default priority is Glib::PRIORITY_DEFAULT.
   *
   * The asyncronous methods have a default fallback that uses threads 
   * to implement asynchronicity, so they are optional for inheriting 
   * classes. However, if you override one you must override all.
   *
   * For the synchronous, blocking version of this function, see 
   * write().
   *
   * @param buffer The buffer containing the data to write. 
   * @param count The number of bytes to write
   * @param slot Callback slot to call when the request is satisfied.
   * @param cancellable Cancellable object.
   * @param io_priority The io priority of the request.
   */
  void write_async(const void* buffer, gsize count, const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable, int io_priority = Glib::PRIORITY_DEFAULT);

  /** Request an asynchronous write of @a count bytes from @a buffer into 
   * the stream. When the operation is finished @a slot will be called. 
   * You can then call write_finish() to get the result of the 
   * operation.
   *
   * During an async request no other sync and async calls are allowed, 
   * and will result in Gio::Error with PENDING being thrown. 
   *
   * A value of @a count larger than MAXSSIZE will cause a Gio::Error with  
   * INVALID_ARGUMENT to be thrown.
   *
   * On success, the number of bytes written will be passed to the
   * callback @a slot. It is not an error if this is not the same as the 
   * requested size, as it can happen e.g. on a partial I/O error, 
   * but generally we try to write as many bytes as requested. 
   *
   * Any outstanding I/O request with higher priority (lower numerical 
   * value) will be executed before an outstanding request with lower 
   * priority. Default priority is Glib::PRIORITY_DEFAULT.
   *
   * The asyncronous methods have a default fallback that uses threads 
   * to implement asynchronicity, so they are optional for inheriting 
   * classes. However, if you override one you must override all.
   *
   * For the synchronous, blocking version of this function, see 
   * write().
   *
   * @param buffer The buffer containing the data to write. 
   * @param count The number of bytes to write
   * @param slot Callback slot to call when the request is satisfied.
   * @param io_priority The io priority of the request.
   */
  void write_async(const void* buffer, gsize count, const SlotAsyncReady& slot, int io_priority = Glib::PRIORITY_DEFAULT);
  _IGNORE(g_output_stream_write_async)

  _WRAP_METHOD(gssize write_finish(const Glib::RefPtr<AsyncResult>& result),
               g_output_stream_write_finish,
               errthrow)

  /** Splices a stream asynchronously.
   *  When the operation is finished @a slot will be called. 
   * You can then call splice_finish() to get the result of the 
   * operation.
   *
   * For the synchronous, blocking version of this function, see 
   * splice().
   *
   * @param source An InputStream.
   * @param slot Callback slot to call when the request is satisfied.
   * @param cancellable Cancellable object.
   * @param io_priority The io priority of the request.
   */
  void splice_async(const Glib::RefPtr<InputStream>& source, const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable, OutputStreamSpliceFlags flags = OUTPUT_STREAM_SPLICE_NONE, int io_priority = Glib::PRIORITY_DEFAULT);

  /** Splices a stream asynchronously.
   *  When the operation is finished @a slot will be called. 
   * You can then call splice_finish() to get the result of the 
   * operation.
   *
   * For the synchronous, blocking version of this function, see 
   * splice().
   *
   * @param source An InputStream.
   * @param slot Callback slot to call when the request is satisfied.
   * @param io_priority The io priority of the request.
   */
  void splice_async(const Glib::RefPtr<InputStream>& source, const SlotAsyncReady& slot, OutputStreamSpliceFlags flags = OUTPUT_STREAM_SPLICE_NONE, int io_priority = Glib::PRIORITY_DEFAULT);

  _IGNORE(g_output_stream_splice_async)

  _WRAP_METHOD(gssize splice_finish(const Glib::RefPtr<AsyncResult>& result),
               g_output_stream_splice_finish,
               errthrow)

  /** Flushes a stream asynchronously.
   * When the operation is finished the @a slot will be called, giving the results.
   * You can then call flush_finish() to get the result of the operation.
   * For behaviour details see flush().
   *
   * @param slot Callback slot to call when the request is satisfied.
   * @param cancellable Cancellable object.
   * @param io_priority The io priority of the request.
   */
  void flush_async(const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable, int io_priority = Glib::PRIORITY_DEFAULT);

  /** Flushes a stream asynchronously.
   * When the operation is finished the @a slot will be called, giving the results.
   * You can then call flush_finish() to get the result of the operation.
   * For behaviour details see flush().
   *
   * @param slot Callback slot to call when the request is satisfied.
   * @param io_priority The io priority of the request.
   */
  void flush_async(const SlotAsyncReady& slot, int io_priority = Glib::PRIORITY_DEFAULT);
  _IGNORE(g_output_stream_flush_async)

  _WRAP_METHOD(bool flush_finish(const Glib::RefPtr<AsyncResult>& result),
               g_output_stream_flush_finish,
               errthrow)

  /** Requests an asynchronous close of the stream, releasing resources related to it. 
   * When the operation is finished the @a slot will be called, giving the results.
   * You can then call close_finish() to get the result of the operation.
   * For behaviour details see close().
   *
   * The asyncronous methods have a default fallback that uses threads to implement asynchronicity, 
   * so they are optional for inheriting classes. However, if you override one you must override all.
   *
   * @param slot Callback slot to call when the request is satisfied.
   * @param cancellable Cancellable object.
   * @param io_priority The io priority of the request.
   */
  void close_async(const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable, int io_priority = Glib::PRIORITY_DEFAULT);

  /** Requests an asynchronous close of the stream, releasing resources related to it. 
   * When the operation is finished the @a slot will be called, giving the results. 
   * You can then call close_finish() to get the result of the operation.
   * For behaviour details see close().
   *
   * The asyncronous methods have a default fallback that uses threads to implement asynchronicity, 
   * so they are optional for inheriting classes. However, if you override one you must override all.
   *
   * @param slot Callback slot to call when the request is satisfied.
   * @param io_priority The io priority of the request.
   */
  void close_async(const SlotAsyncReady& slot, int io_priority = Glib::PRIORITY_DEFAULT);
  _IGNORE(g_output_stream_close_async)

  _WRAP_METHOD(bool close_finish(const Glib::RefPtr<AsyncResult>& result),
               g_output_stream_close_finish,
               errthrow)

  // These are private inside the module (for implementations)
  _IGNORE(g_output_stream_has_pending, g_output_stream_is_closed, g_output_stream_set_pending, g_output_stream_clear_pending)

};

} // namespace Gio