c++-gtk-utils
gstream.h
Go to the documentation of this file.
1 /* Copyright (C) 2010 to 2017 Chris Vine
2 
3 
4 The library comprised in this file or of which this file is part is
5 distributed by Chris Vine under the GNU Lesser General Public
6 License as follows:
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public License
10  as published by the Free Software Foundation; either version 2.1 of
11  the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License, version 2.1, along with this library (see the file LGPL.TXT
20  which came with this source code package in the c++-gtk-utils
21  sub-directory); if not, write to the Free Software Foundation, Inc.,
22  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 
24 However, it is not intended that the object code of a program whose
25 source code instantiates a template from this file or uses macros or
26 inline functions (of any length) should by reason only of that
27 instantiation or use be subject to the restrictions of use in the GNU
28 Lesser General Public License. With that in mind, the words "and
29 macros, inline functions and instantiations of templates (of any
30 length)" shall be treated as substituted for the words "and small
31 macros and small inline functions (ten lines or less in length)" in
32 the fourth paragraph of section 5 of that licence. This does not
33 affect any other reason why object code may be subject to the
34 restrictions in that licence (nor for the avoidance of doubt does it
35 affect the application of section 2 of that licence to modifications
36 of the source code in this file).
37 */
38 
39 /**
40  * @defgroup gstreams gstreams
41  *
42  * \#include <c++-gtk-utils/gstream.h>
43  *
44  * The c++-gtk-utils library contains C++ classes providing a
45  * streambuffer and stream objects interfacing with GIO streams.
46  *
47  * Normally 'true' would be passed as the second (manage) argument of
48  * the gostream/gistream/giostream constructors or of the attach()
49  * methods, so that the destructors of these classes close the GIO
50  * streams concerned, which helps exception safety (the attach()
51  * method will also close any previous GIO stream). If this behaviour
52  * is not wanted, pass 'false' instead to that argument. The same
53  * applies to the wide stream classes.
54  *
55  * C++ stream objects are not suitable for asynchronous input and
56  * output. On sockets and pipes or other special devices, they may
57  * block on a read or write until the read or write request has been
58  * satisfied. In circumstances where asynchronous input and output is
59  * wanted, it will be necessary to start a new thread in which to
60  * carry out the input and output operations (which is what GIO does
61  * behind the scenes on its asynchronous operations) or use the GIO
62  * interfaces directly.
63  *
64  * Here are some examples of use:
65  *
66  * @code
67  * // open file for input, another for output, and make the
68  * // output file a gzipped copy of the input (gzipping a
69  * // file doesn't get much easier than this)
70  * Cgu::gistream input;
71  * Cgu::gostream output;
72  * Cgu::GobjHandle<GFile> file_in(g_file_new_for_path("filename"));
73  * GFileInputStream* is = g_file_read(file_in, 0, 0);
74  * if (is)
75  * input.attach(Cgu::GobjHandle<GInputStream>(G_INPUT_STREAM(is)), // takes ownership of 'is'
76  * true);
77  * else {
78  * std::cerr << "Can't open file 'filename'" << std::endl;
79  * return;
80  * }
81  * Cgu::GobjHandle<GFile> file_out(g_file_new_for_path("filename.gz"));
82  * GFileOutputStream* os = g_file_replace(file_out, 0, false,
83  * G_FILE_CREATE_REPLACE_DESTINATION,
84  * 0, 0);
85  * if (os) {
86  * output.attach(Cgu::GobjHandle<GOutputStream>(G_OUTPUT_STREAM(os)), // takes ownership of 'os'
87  * true,
88  * Cgu::GobjHandle<GConverter>(
89  * G_CONVERTER(g_zlib_compressor_new(G_ZLIB_COMPRESSOR_FORMAT_GZIP, -1)))
90  * );
91  * }
92  * else {
93  * std::cerr << "Can't create file 'filename.gz'" << std::endl;
94  * return;
95  * }
96  * // this does the copying, and is shorthand for creating your own buffer
97  * // and calling std::istream::read() and std::ostream::write() on it
98  * output << input.rdbuf();
99  *
100  * --------------------------------------------------------------------
101  *
102  * // establish a TCP socket on localhost, listen for connections on port
103  * // 1200 and receive whitespace-separated words for processing
104  * using Cgu::GobjHandle;
105  * GobjHandle<GInetAddress> i(g_inet_address_new_loopback(G_SOCKET_FAMILY_IPV4));
106  * GobjHandle<GSocketAddress> a(g_inet_socket_address_new(i, 1200));
107  * GobjHandle<GSocketListener> l(g_socket_listener_new());
108  *
109  * gboolean success = g_socket_listener_add_address(l,
110  * a,
111  * G_SOCKET_TYPE_STREAM,
112  * G_SOCKET_PROTOCOL_TCP,
113  * 0, 0, 0);
114  * if (!success) {
115  * std::cerr << "Can't bind socket on localhost" << std::endl;
116  * return;
117  * }
118  *
119  * GSocketConnection* c = g_socket_listener_accept(l, 0, 0, 0);
120  * if (!c) {
121  * std::cerr << "Can't listen on localhost" << std::endl;
122  * return;
123  * }
124  *
125  * Cgu::giostream sock_strm(GobjHandle<GIOStream>(G_IO_STREAM(c)), true); // takes ownership of c
126  * sock_strm.set_output_buffered(false);
127  * std::cout << "Connection accepted" << std::endl;
128  *
129  * std::string str;
130  * while (sock_strm >> str) {
131  * [ ... do something with the word in str ... ]
132  * // acknowledge the client
133  * sock_strm << "ACK\n";
134  * }
135  *
136  * --------------------------------------------------------------------
137  *
138  * // read line delimited text from a pipe until it is closed by the
139  * // writer: assume 'fd' is the read file descriptor of the pipe
140  * // (in real life you would probably want to use a Cgu::fdistream
141  * // object in this usage and bypass GIO)
142  * Cgu::gistream istrm(Cgu::GobjHandle<GInputStream>(g_unix_input_stream_new(fd, true)), true);
143  * if (!istrm.get_gio_stream().get()) {
144  * std::cerr << "Can't create gio file-descriptor input stream" << std::endl;
145  * return;
146  * }
147  * std::string line;
148  * while (std::getline(istrm, line)) {
149  * [ ... do something with the read text ... ]
150  * }
151  * @endcode
152  *
153  *
154  * @note 1. Users cannot (except by derivation) use the virtual
155  * protected methods of the streambuffer classes, including xsgetn()
156  * and xsputn(). Instead, if they want direct access to the
157  * streambuffer other than through the gostream/gistream/giostream
158  * methods (or their wide stream equivalents), they should use the
159  * public forwarding functions provided by std::streambuf base class.
160  * @note 2. These streambuffers and stream objects are not copiable.
161  * @note 3. The base glib requirement for the c++-gtk-utils library is
162  * glib >= 2.10.0, but the gstreams component will only be available
163  * if glib >= 2.16.0 is installed.
164  *
165  * Buffering
166  * ---------
167  *
168  * The classes implement buffering on input streams and (unless output
169  * buffering is switched off) on output streams. They implement the
170  * buffering internally and do not use GBufferedInputStream and
171  * GBufferedOutputStream. This has a number of efficiency advantages
172  * and also retains random access, on devices that support it, for
173  * buffered streams (GBufferedInputStream and GBufferedOutputStream do
174  * not do so). So far as concerns random access on GIOStream objects,
175  * which are opened for both read and write, see
176  * @ref GioRandomAccessAnchor "giostream and random access"
177  *
178  * The streambuf class provides a block read and write in xsgetn() and
179  * xsputn(), which will be called by the read() and write() methods
180  * (and some other output operators) inherited by (w)gistream,
181  * (w)gostream and (w)giostream from std::basic_istream and
182  * std::basic_ostream. They operate (after appropriately vacating and
183  * resetting the buffers) by doing a block read and write directly to
184  * and from the target, and are very efficient for large block reads
185  * (those significantly exceeding the buffer size). If users want all
186  * reads and writes to go through the buffers, by using
187  * std::basic_streambuf<>::xsputn() and
188  * std::basic_streambuf<>::xsgetn() then the symbol
189  * CGU_GSTREAM_USE_STD_N_READ_WRITE can be defined. (libstdc++-3
190  * provides efficient inbuilt versions of these std::basic_streambuf
191  * functions for block reads not significantly larger than the buffer
192  * size, provided output buffering has not been turned off by the
193  * set_output_buffered() method of these classes.)
194  *
195  * @b Note @b however that if CGU_GSTREAM_USE_STD_N_READ_WRITE is to
196  * be defined, it is best to do this by textually amending the
197  * installed gstream.h header file rather than by defining the symbol
198  * in user code before that file is included. This will ensure that
199  * all source files in a program which include the gstream.h header
200  * are guaranteed to see the same definitions so that the C++
201  * standard's one-definition-rule is complied with.
202  *
203  * One possible case for defining that symbol is where the user wants
204  * to use the tie() method of (w)gistream or (w)giostream (inherited
205  * from std::basic_ios) to procure flushing of an output stream before
206  * extraction from an input stream is made by (w)gistream::read() or
207  * (w)giostream::read(). Such flushing might not occur where a call
208  * to read() is made unless CGU_GSTREAM_USE_STD_N_READ_WRITE is
209  * defined, because an implementation is permitted to defer such
210  * flushing until underflow() occurs, and the block read by read(), as
211  * forwarded to xsgetn(), will never invoke underflow() if that symbol
212  * is not defined. (Having said that, any basic_istream
213  * implementation which does defer output flushing until underflow()
214  * is called makes tie() unusable anyway for a number of purposes,
215  * because the time of flushing would become dependent on whether a
216  * read request can be satisfied by what is already in the buffers.)
217  *
218  * 4 characters are stored and available for putback. However, if the
219  * symbol CGU_GSTREAM_USE_STD_N_READ_WRITE is not defined, then a call
220  * to (w)gstreambuf::xsgetn() via (w)gistream::read() or
221  * (w)giostream::read() with a request for less than 4 characters will
222  * result in less than 4 characters available for putback (if these
223  * block read methods obtain some characters but less than 4, only the
224  * number of characters obtained by them is guaranteed to be available
225  * for putback).
226  *
227  * @anchor GioRandomAccessAnchor
228  * giostream and random access
229  * ---------------------------
230  *
231  * For GIO objects which implement GSeekable (which are GFileIOStream,
232  * GFileInputStream, GFileOutputStream, GMemoryInputStream and
233  * GMemoryOutputStream), the classes in this c++-gtk-utils library
234  * implement the tellg(), tellp(), seekg() and seekp() random access
235  * methods.
236  *
237  * The presence of buffering does not impede this where a stream is
238  * only opened for reading or only opened for writing. However, it
239  * presents complications if the giostream or wgiostream classes are
240  * used with a GFIleIOStream object (GFileIOStream objects are opened
241  * for both read and write). Because the classes employ buffering of
242  * input, and optional buffering of output, the logical file position
243  * (the file position expected by the user from the reads and writes
244  * she has made) will usually differ from the actual file position
245  * seen by the underlying operating system. The gstreambuf class
246  * provided by this library implements intelligent tying between input
247  * and output streams for GFileIOStream objects which means that if
248  * output has been made unbuffered by a call to
249  * set_output_buffered(false) and no converter has been attached, all
250  * reads and writes onto the file system from the same giostream or
251  * wgiostream object will be made at the expected logical position.
252  *
253  * This cannot be done by the gstreambuf class where the output stream
254  * is set as buffered (the default). In that case, if the last
255  * operation on a giostream or wgiostream object 'strm' was a read,
256  * before the first write operation thereafter is made on it, the user
257  * should call strm.seekg(strm.tellg()) or strm.seekp(strm.tellp())
258  * (the two have the same effect), in order to synchronise the logical
259  * and actual file positions, or if the user does not want to maintain
260  * the current logical file position, make some other call to seekg()
261  * or seekp() which does not comprise only seekg(0,
262  * std::ios_base::cur) or seekp(0, std::ios_base::cur). Many
263  * std::basic_iostream implementations, as inherited by
264  * Cgu::(w)giostream, will synchronise positions automatically on
265  * seekable streams via their sentry objects in order to provide
266  * support for buffered random access on their std::basic_fstream
267  * class (gcc's libstdc++ library does this, for example), making
268  * these steps unnecessary, but following these steps will provide
269  * maximum portability.
270  *
271  * If a GFileIOStream object attached to a giostream or wgiostream
272  * object is not seekable (that is, can_seek() returns false), say
273  * because an input or output converter has been attached or the
274  * filesystem is a network file system, no random access may be
275  * attempted. In particular, the tellg(), tellp(), seekg() and
276  * seekp() methods will not work (they will return
277  * pos_type(off_type(-1))). Furthermore, if a giostream or wgiostream
278  * object which manages a GFileIOStream object (as opposed to a
279  * socket) has a converter attached or is not seekable for some other
280  * reason, then after a read has been made no further write may be
281  * made using the same GFileIOStream object, so the use of converters
282  * with giostream or wgiostream objects should generally be restricted
283  * to use with sockets (GSocketConnection objects) only. Where
284  * converters are used with files on a filesystem, it is best to use
285  * the (w)gostream and (w)gistream classes, and to close one stream
286  * before opening the other where they address the same file.
287  *
288  * None of these restrictions applies to GSocketConnection objects
289  * obtained by a call to g_socket_listener_accept() or
290  * g_socket_client_connect(), or obtained in some other way, as these
291  * do not maintain file pointers. They can be attached to a giostream
292  * or wgiostream object (with or without a converter) without any
293  * special precautions being taken, other than the normal step of
294  * calling giostream::flush() (or using the std::flush manipulator) to
295  * flush the output buffer to the socket if the user needs to know
296  * that that has happened (or setting output buffering off with the
297  * set_output_buffered() method). In summary, on a socket, a read
298  * does not automatically flush the output buffer: it is for the user
299  * to do that.
300  *
301  * Wide streams and endianness
302  * ---------------------------
303  *
304  * Unless a converter is attached (for, say, UTF-32LE to UTF-32BE, or
305  * vice versa), with the wide character classes wide characters are
306  * written out in the native endian format of the writing machine.
307  * Whether or not a converter from one endianness to another is
308  * attached, special steps need to be taken if the text which is sent
309  * for output might be read by machines of unknown endianness.
310  *
311  * No such special steps are required where the wgostream, wgistream
312  * and wgiostream classes are used with temporary files, pipes, fifos,
313  * unix domain sockets and network sockets on localhost, because in
314  * those cases they will be read by the same machine that writes; but
315  * they are required where sockets communicate with other computers
316  * over a network or when writing to files which may be distributed to
317  * and read by other computers with different endianness.
318  *
319  * Where wide characters are to be exported to other machines, one
320  * useful approach is to convert to and from UTF-8 with
321  * Utf8::uniwide_from_utf8(), Utf8::uniwide_to_utf8(),
322  * Utf8::wide_from_utf8() or Utf8::wide_to_utf8(), and to use
323  * gostream/gistream/giostream with the converted text, or to attach a
324  * converter for UTF-8, generated by GIO's g_charset_converter_new(),
325  * directly to a wgostream, wgistream or wgiostream object.
326  *
327  * Instead of converting exported text to UTF-8, another approach is
328  * to use a byte order marker (BOM) as the first character of the wide
329  * stream output. UCS permits a BOM character to be inserted,
330  * comprising static_cast<wchar_t>(0xfeff), at the beginning of the
331  * output to the wide character stream. At the receiving end, this
332  * will appear as 0xfffe (UTF-16) or 0xfffe0000 (UTF-32) to a big
333  * endian machine with 8 bit char type if the text is little endian,
334  * or to a little endian machine with big endian text, so signaling a
335  * need to undertake byte swapping of text read from the stream.
336  * Another alternative is to label the physical medium conveying the
337  * file as UTF-16LE, UTF-16BE, UTF-32LE or UTF-32BE, as the case may
338  * be, in which case a BOM character should not be prepended.
339  *
340  * Where it is established by either means that the input stream
341  * requires byte swapping, the wgistream, wgiostream and wgstreambuf
342  * classes have a set_byteswap() member function which should be
343  * called on opening the input stream as soon as it has been
344  * established that byte swapping is required. Once this function has
345  * been called with an argument of 'true', all further calls to stream
346  * functions which provide characters will provide those characters
347  * with the correct native endianness. Calling set_byteswap() on the
348  * narrow stream gistream, giostream and gstreambuf objects has no
349  * effect (byte order is irrelevant to narrow streams).
350  *
351  * Here is an example of such use in a case where sizeof(wchar_t) is
352  * 4:
353  *
354  * @code
355  * using Cgu::GobjHandle;
356  * Cgu::wgistream input;
357  * GobjHandle<GFile> file_in(g_file_new_for_path("filename"));
358  * GFileInputStream* is = g_file_read(file_in, 0, 0);
359  * if (is)
360  * input.attach(GobjHandle<GInputStream>(G_INPUT_STREAM(is)), true); // takes ownership of 'is'
361  * else {
362  * std::cerr << "Can't open file 'filename'"
363  * << std::endl;
364  * return;
365  * }
366  * wchar_t item;
367  * input.get(item);
368  * if (!input) {
369  * std::cerr << "File 'filename' is empty" << std::endl;
370  * return;
371  * }
372  * if (item == static_cast<wchar_t>(0xfffe0000))
373  * input.set_byteswap(true);
374  * else if (item != static_cast<wchar_t>(0xfeff)) {
375  * // calling set_byteswap() will manipulate the buffers, so
376  * // either call putback() before we call set_byteswap(), or
377  * // call unget() instead
378  * input.putback(item);
379  * // the first character is not a BOM character so assume big endian
380  * // format, and byte swap if the local machine is little endian
381  * #if G_BYTE_ORDER == G_LITTLE_ENDIAN
382  * input.set_byteswap(true);
383  * #endif
384  * }
385  * [ ... do something with the input file ... ]
386  * @endcode
387  *
388  * Other wide stream issues
389  * ------------------------
390  *
391  * basic_gstreambuf objects can be instantiated for any integer type
392  * which has an appropriate traits class provided for it which has the
393  * copy(), eof(), eq_int_type(), move(), not_eof() and to_int_type()
394  * static member functions. The integer type could in fact have any
395  * size, but the set_byteswap() methods for basic_gistream,
396  * basic_giostream and basic_gstreambuf will only have an effect if
397  * its size is either 2 or 4. A typedef'ed instance of the
398  * basic_gstreambuf class is provided by the library for characters of
399  * type wchar_t.
400  *
401  * basic_gostream, basic_gistream and basic_giostream objects can be
402  * instantiated for the wchar_t type, and there are
403  * wgostream. wgistream and wgiostream typedefs for these.
404  *
405  * gtkmm users
406  * -----------
407  *
408  * gtkmm/giomm does not provide C++ streams for GIO objects: instead,
409  * it provides a literal function-for-function wrapping. However,
410  * giomm users can use the stream classes provided by this library by
411  * converting the relevant Glib::RefPtr object to a Cgu::GobjHandle
412  * object. This can be done as follows:
413  *
414  * @code
415  * // Glib::RefPtr<Gio::InputStream> to Cgu::GobjHandle<GInputStream>
416  * inline
417  * Cgu::GobjHandle<GInputStream> giomm_input_convert(const Glib::RefPtr<Gio::InputStream>& in) {
418  * return Cgu::GobjHandle<GInputStream>(static_cast<GInputStream*>(g_object_ref(in.operator->()->gobj())));
419  * }
420  *
421  * // Glib::RefPtr<Gio::OutputStream> to Cgu::GobjHandle<GOutputStream>
422  * inline
423  * Cgu::GobjHandle<GOutputStream> giomm_output_convert(const Glib::RefPtr<Gio::OutputStream>& out) {
424  * return Cgu::GobjHandle<GOutputStream>(static_cast<GOutputStream*>(g_object_ref(out.operator->()->gobj())));
425  * }
426  *
427  * // Glib::RefPtr<Gio::IOStream> to Cgu::GobjHandle<GIOStream>
428  * inline
429  * Cgu::GobjHandle<GIOStream> giomm_io_convert(const Glib::RefPtr<Gio::IOStream>& io) {
430  * return Cgu::GobjHandle<GIOStream>(static_cast<GIOStream*>(g_object_ref(io.operator->()->gobj())));
431  * }
432  *
433  * // example printing a text file to stdout with file opened with giomm
434  * Glib::RefPtr<Gio::File> file = Gio::File::create_for_path("filename");
435  * Glib::RefPtr<Gio::InputStream> is = file->read();
436  *
437  * Cgu::gistream filein(giomm_input_convert(is), true);
438  *
439  * std::string line;
440  * while (std::getline(filein, line)) {
441  * std::cout << line << '\n';
442  * }
443  * @endcode
444  */
445 
446 #ifndef CGU_GSTREAM_H
447 #define CGU_GSTREAM_H
448 
449 #include <glib.h>
450 
451 #if defined(DOXYGEN_PARSING) || GLIB_CHECK_VERSION(2,16,0)
452 
453 // see above for what this does
454 //#define CGU_GSTREAM_USE_STD_N_READ_WRITE 1
455 
456 #include <istream>
457 #include <ostream>
458 #include <streambuf>
459 #include <algorithm>
460 #include <string>
461 #include <cstddef>
462 
463 #include <glib.h>
464 #include <glib-object.h>
465 #include <gio/gio.h>
466 
471 
472 namespace Cgu {
473 
474 /*
475 The following convenience typedefs appear at the end of this file:
476 typedef basic_gstreambuf<char> gstreambuf;
477 typedef basic_gistream<char> gistream;
478 typedef basic_gostream<char> gostream;
479 typedef basic_giostream<char> giostream;
480 typedef basic_gstreambuf<wchar_t> wgstreambuf;
481 typedef basic_gistream<wchar_t> wgistream;
482 typedef basic_gostream<wchar_t> wgostream;
483 typedef basic_giostream<wchar_t> wgiostream;
484 */
485 
486 
487 /**
488  * @headerfile gstream.h c++-gtk-utils/gstream.h
489  * @brief C++ stream buffer for GIO streams
490  * @sa gstreams
491  * @ingroup gstreams
492  *
493  * This class provides a stream buffer for interfacing with GIO
494  * streams. It does the buffering for the basic_gostream,
495  * basic_gistream and basic_giostream stream classes. It is available
496  * since version 1.2.6.
497  */
498 
499 template <class charT , class Traits = std::char_traits<charT> >
500 class basic_gstreambuf: public std::basic_streambuf<charT, Traits> {
501 
502 public:
503  typedef charT char_type;
504  typedef Traits traits_type;
505  typedef typename traits_type::int_type int_type;
506  typedef typename traits_type::pos_type pos_type;
507  typedef typename traits_type::off_type off_type;
508 
509 private:
510  GobjHandle<GInputStream> input_stream;
511  GobjHandle<GOutputStream> output_stream;
512  GobjHandle<GIOStream> io_stream;
513 
514  bool manage;
515  bool byteswap;
516  bool seek_mismatch;
517  bool seekable;
518 
519  static const int output_buf_size = 1024; // size of the data write buffer
520  static const int putback_size = 4; // size of read putback area
521  static const int input_buf_size = 1024; // size of the data read buffer
522 
523 #if defined(CGU_USE_GLIB_MEMORY_SLICES_COMPAT) || defined(CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT)
525  GSliceFreeSize<(input_buf_size + putback_size) * sizeof(char_type)> > input_buffer;
527  GSliceFreeSize<output_buf_size * sizeof(char_type)> > output_buffer;
528 #else
529  ScopedHandle<char_type*> input_buffer;
530  ScopedHandle<char_type*> output_buffer;
531 #endif
532 
533  static void swap_element(char_type&);
534  GobjHandle<GInputStream> find_base_input_stream(const GobjHandle<GInputStream>&);
535  GobjHandle<GOutputStream> find_base_output_stream(const GobjHandle<GOutputStream>&);
536  void reset_input_buffer_pointers();
537  int flush_buffer();
538  bool wind_back_input_buffer();
539  bool is_input_stored();
540  bool is_output_stored();
541  void set_error(GError*);
542  void set_input_error(GError*);
543  void set_output_error(GError*);
544 
546  basic_gstreambuf& operator=(const basic_gstreambuf&);
547 
548 protected:
549 /**
550  * This method will not throw. This means that the input functions of
551  * stream objects which have this streambuffer as a member will not
552  * throw unless the underlying functions of the std::basic_istream
553  * class throw, which they would not normally do unless they have been
554  * required to do so on failbit, badbit or eofbit being set by an
555  * explicit call to the exceptions() method of that class. This class
556  * does not offer concurrent access from multiple threads to the same
557  * stream object, and if that is required users should provide their
558  * own synchronisation.
559  */
560  virtual int_type underflow();
561 
562 /**
563  * This method will not throw. This class does not offer concurrent
564  * access from multiple threads to the same stream object, and if that
565  * is required users should provide their own synchronisation.
566  */
567  virtual int sync();
568 
569 /**
570  * This method will not throw unless std::basic_streambuf<>::sputc()
571  * throws, which it would not do on any sane implementation. This
572  * means that the output functions of stream objects which have this
573  * streambuffer as a member will not throw unless the underlying
574  * functions of the std::basic_ostream class throw, which they would
575  * not normally do unless they have been required to do so on failbit,
576  * badbit or eofbit being set by an explicit call to the exceptions()
577  * method of that class. This class does not offer concurrent access
578  * from multiple threads to the same stream object, and if that is
579  * required users should provide their own synchronisation.
580  */
581  virtual int_type overflow(int_type);
582 #ifndef CGU_GSTREAM_USE_STD_N_READ_WRITE
583 /**
584  * This method will not throw. This means that the input functions of
585  * stream objects which have this streambuffer as a member will not
586  * throw unless the underlying functions of the std::basic_istream
587  * class throw, which they would not normally do unless they have been
588  * required to do so on failbit, badbit or eofbit being set by an
589  * explicit call to the exceptions() method of that class. This class
590  * does not offer concurrent access from multiple threads to the same
591  * stream object, and if that is required users should provide their
592  * own synchronisation.
593  */
594  virtual std::streamsize xsgetn(char_type*, std::streamsize);
595 
596 /**
597  * This method will not throw. This means that the output functions
598  * of stream objects which have this streambuffer as a member will not
599  * throw unless the underlying functions of the std::basic_ostream
600  * class throw, which they would not normally do unless they have been
601  * required to do so on failbit, badbit or eofbit being set by an
602  * explicit call to the exceptions() method of that class. This class
603  * does not offer concurrent access from multiple threads to the same
604  * stream object, and if that is required users should provide their
605  * own synchronisation.
606  */
607  virtual std::streamsize xsputn(const char_type*, std::streamsize);
608 #endif
609 /**
610  * This method provides random access on GIO streams that implement
611  * GSeekable, so supporting the tellg(), tellp(), seekg() and seekp()
612  * methods of the basic_gostream, basic_gistream and basic_giostream
613  * classes. Any output buffers will be flushed and if the seek
614  * succeeds any input buffers will be reset. This method does not
615  * throw, but if it returns pos_type(off_type(-1)) to indicate
616  * failure, it will cause the tellg(), tellp(), seekg() or seekp()
617  * methods of the relevant stream class to throw
618  * std::ios_base::failure if such an exception has been required by an
619  * explicit call to the exceptions() method of that class (but not
620  * otherwise). This class does not offer concurrent access from
621  * multiple threads to the same stream object, and if that is required
622  * users should provide their own synchronisation.
623  *
624  * @param off The offset to be applied to the 'way' argument when
625  * seeking. It is a signed integer type, and on wide character
626  * streams is dimensioned as the number of wchar_t units not the
627  * number of bytes (that is, it is bytes/sizeof(char_type)).
628  *
629  * @param way The file position to which the 'off' argument is to be
630  * applied (either std::ios_base::beg, std::ios_base::cur or
631  * std::ios_base::end).
632  *
633  * @param m The type of GIO stream which must have been attached to
634  * this streambuffer for this method to attempt a seek. For
635  * GInputStream the argument should have the std::ios_base::in bit
636  * set, for GOutputStream it should have the std::ios_base::out bit
637  * set and for GIOStream it should have either (or both) set.
638  * Provided the relevant bit is set, it doesn't matter if others are
639  * also set. However if, with a GIOStream object, both the
640  * std::ios_base::in and std::ios_base::out bits are set, a seek on
641  * both input and output streams will be attempted, unless the 'way'
642  * argument is std::ios_base::cur, in which case a seek on the output
643  * stream only will be attempted. (Note that the only GIOStream which
644  * at present supports seeking is GFileIOStream, and because
645  * filesystem files only have one file pointer, which is used for both
646  * input and output, both input seeking and output seeking have the
647  * same result and affect both streams.) As the tellg() and seekg()
648  * stream methods only pass std::ios_base::in, and the tellp() and
649  * seekp() methods only std::ios_base::out, these will always produce
650  * the expected result, and for GIOStream streams tellg() will be
651  * indistinguishable in effect from tellp(), and seekg() from seekp().
652  *
653  * @return If the seek succeeds, a std::char_traits<T>::pos_type
654  * object representing the new stream position of the streambuffer
655  * after the seek. (This type is std::streampos for narrow character
656  * (char) streams, std::wstreampos for wide character (wchar_t)
657  * streams, std::u16streampos for the C++11 char16_t type and
658  * std::u32streampos for the C++11 char32_t type.) If the seek
659  * failed, pos_type(off_type(-1)) is returned. If a seek is made on a
660  * GIOStream object with both std::ios_base::in and std::ios_base::out
661  * set and a 'way' argument of std::ios_base::beg or
662  * std::ios_base::end, the result of the seek which succeeds is
663  * returned, or if both succeed, the result of the output seek is
664  * returned. (Note that the only GIOStream which at present supports
665  * seeking is GFileIOStream, and because files only have one file
666  * pointer, which is used for both input and output, both input
667  * seeking and output seeking have the same result and affect both
668  * streams.)
669  *
670  * Since 1.2.6
671  */
672  virtual pos_type seekoff(off_type off,
673  std::ios_base::seekdir way,
674  std::ios_base::openmode m = std::ios_base::in | std::ios_base::out);
675 
676 /**
677  * This method provides random access on GIO streams that implement
678  * GSeekable, so supporting the seekg() and seekp() methods of the
679  * basic_gostream, basic_gistream and basic_giostream classes. It is
680  * equivalent to seekoff(off_type(p), std::ios_base::beg, m). Any
681  * output buffers will be flushed and if the seek succeeds any input
682  * buffers will be reset. This method does not throw, but if it
683  * returns pos_type(off_type(-1)) to indicate failure, it will cause
684  * the seekg() or seekp() methods of the relevant stream class to
685  * throw std::ios_base::failure if such an exception has been required
686  * by an explicit call to the exceptions() method of that class (but
687  * not otherwise). This class does not offer concurrent access from
688  * multiple threads to the same stream object, and if that is required
689  * users should provide their own synchronisation.
690  *
691  * @param p The absolute position to which the seek is to be made,
692  * obtained by a previous call to seekoff() or to this method.
693  *
694  * @param m The type of GIO stream which must have been attached to
695  * this streambuffer for this method to attempt a seek. For
696  * GInputStream the argument should have the std::ios_base::in bit
697  * set, for GOutputStream it should have the std::ios_base::out bit
698  * set and for GIOStream it should have either (or both) set.
699  * Provided the relevant bit is set, it doesn't matter if others are
700  * also set. However if, with a GIOStream object, both the
701  * std::ios_base::in and std::ios_base::out bits are set, a seek on
702  * both input and output streams will be attempted. (Note that the
703  * only GIOStream which at present supports seeking is GFileIOStream,
704  * and because filesystem files only have one file pointer, which is
705  * used for both input and output, both input seeking and output
706  * seeking have the same result and affect both streams.) As the
707  * seekg() stream method only passes std::ios_base::in, and the
708  * seekp() method only std::ios_base::out, these will always produce
709  * the expected result, and seekg() will be indistinguishable in
710  * effect from seekp().
711  *
712  * @return If the seek succeeds, a std::char_traits<T>::pos_type
713  * object representing the new stream position of the streambuffer
714  * after the seek. (This type is std::streampos for narrow character
715  * (char) streams, std::wstreampos for wide character (wchar_t)
716  * streams, std::u16streampos for the C++11 char16_t type and
717  * std::u32streampos for the C++11 char32_t type.) If the seek
718  * failed, pos_type(off_type(-1)) is returned.
719  *
720  * Since 1.2.6
721  */
722  virtual pos_type seekpos(pos_type p,
723  std::ios_base::openmode m = std::ios_base::in | std::ios_base::out);
724 
725 public:
726 /**
727  * The default constructor: the GIO stream is attached later using the
728  * attach_stream() method. It will not throw unless the default
729  * constructor of std::basic_streambuf throws. This class does not
730  * offer concurrent access from multiple threads to the same stream
731  * object, and if that is required users should provide their own
732  * synchronisation.
733  *
734  * Since 1.2.6
735  */
737 
738  /**
739  * The constructor taking a GIO input stream. This class does not
740  * offer concurrent access from multiple threads to the same stream
741  * object, and if that is required users should provide their own
742  * synchronisation.
743  *
744  * @param input_stream_ A GIO input stream to be attached to the
745  * streambuffer. If the caller wants the input stream to survive
746  * this class's destruction or a call to close_stream() or
747  * attach_stream(), the caller should keep a separate GobjHandle
748  * object which references the stream (obtained by, say, calling
749  * get_istream()) and pass 'manage_' as false. If this is a
750  * GFilterInputStream object (that is, a GBufferedInputStream or
751  * GConverterInputStream stream), only the underlying base input
752  * stream will be attached and the other higher level streams will be
753  * closed (buffering of input streams is always provided by this C++
754  * streambuffer, and converting is controlled solely by the
755  * 'converter_' argument).
756  *
757  * @param manage_ Whether the streambuffer should call
758  * g_input_stream_close() on the stream in its destructor or when
759  * another stream is attached. Passing 'true' is usually what is
760  * wanted - 'false' only makes sense if the caller keeps a separate
761  * GobjHandle object which references the stream to keep it alive
762  * (obtained by, say, calling get_istream()). Unlike its fdstreams
763  * equivalent, this parameter does not have a default value of
764  * 'true': this is partly to make it less likely that a converter is
765  * passed to this argument by mistake (that would not normally cause
766  * a compiler warning because GobjHandle has a type conversion
767  * operator providing the underlying C object by pointer, so
768  * GobjHandles are type convertible to pointers, and such a pointer
769  * will in turn provide a type match with a bool argument); and
770  * partly because, given a GInputStream* p, the construction
771  * \"Cgu::gstreambuf str(Cgu::GobjHandle<GInputStream>(p));\"
772  * without an additional argument or additional parentheses would
773  * cause a compiler error as it would be interpreted as a function
774  * declaration.
775  *
776  * @param converter_ A converter (if any) to be attached to the GIO
777  * input stream (note that this does not affect the operation of
778  * set_byteswap()). The default value of an empty
779  * GobjHandle<GConverter> object indicates no converter.
780  *
781  * @exception std::bad_alloc This constructor will throw
782  * std::bad_alloc if memory is exhausted and the system throws on
783  * such exhaustion (unless the library has been installed using the
784  * \--with-glib-memory-slices-compat or
785  * \--with-glib-memory-slices-no-compat configuration option, in
786  * which case glib will terminate the program if it is unable to
787  * obtain memory from the operating system). No other exception will
788  * be thrown unless the constructor of std::basic_streambuf throws.
789  *
790  * @note If a converter is provided, the stream will no longer be
791  * seekable even if it otherwise would be, so tellg() and seekg()
792  * will no longer work (they will return pos_type(off_type(-1)).
793  *
794  * Since 1.2.6
795  */
796  basic_gstreambuf(const GobjHandle<GInputStream>& input_stream_,
797  bool manage_,
798  const GobjHandle<GConverter>& converter_ = GobjHandle<GConverter>());
799 
800  /**
801  * The constructor taking a GIO output stream. This class does not
802  * offer concurrent access from multiple threads to the same stream
803  * object, and if that is required users should provide their own
804  * synchronisation.
805  *
806  * @param output_stream_ A GIO output stream to be attached to the
807  * streambuffer. If the caller wants the output stream to survive
808  * this class's destruction or a call to close_stream() or
809  * attach_stream(), the caller should keep a separate GobjHandle
810  * object which references the stream (obtained by, say, calling
811  * get_ostream()) and pass 'manage_' as false. If this is a
812  * GFilterOutputStream object (that is, a GBufferedOutputStream,
813  * GConverterOutputStream or GDataOutputStream stream), only the
814  * underlying base output stream will be attached and the other
815  * higher level streams will be closed (buffering and converting are
816  * controlled solely by the set_output_buffered() method and
817  * 'converter_' argument).
818  *
819  * @param manage_ Whether the streambuffer should call
820  * g_output_stream_close() on the stream in its destructor or when
821  * another stream is attached. Passing 'true' is usually what is
822  * wanted, and is particularly relevant on output streams because
823  * unless g_output_stream_close() is called, GIO may not commit to
824  * disk - 'false' only makes sense if the caller keeps a separate
825  * GobjHandle object which references the stream to keep it alive
826  * (obtained by, say, calling get_ostream()). Unlike its fdstreams
827  * equivalent, this parameter does not have a default value of
828  * 'true': this is partly to make it less likely that a converter is
829  * passed to this argument by mistake (that would not normally cause
830  * a compiler warning because GobjHandle has a type conversion
831  * operator providing the underlying C object by pointer, so
832  * GobjHandles are type convertible to pointers, and such a pointer
833  * will in turn provide a type match with a bool argument); and
834  * partly because, given a GOutputStream* p, the construction
835  * \"Cgu::gstreambuf str(Cgu::GobjHandle<GOutputStream>(p));\"
836  * without an additional argument or additional parentheses would
837  * cause a compiler error as it would be interpreted as a function
838  * declaration.
839  *
840  * @param converter_ A converter (if any) to be attached to the GIO
841  * output stream. The default value of an empty
842  * GobjHandle<GConverter> object indicates no converter.
843  *
844  * @exception std::bad_alloc This constructor will throw
845  * std::bad_alloc if memory is exhausted and the system throws on
846  * such exhaustion (unless the library has been installed using the
847  * \--with-glib-memory-slices-compat or
848  * \--with-glib-memory-slices-no-compat configuration option, in
849  * which case glib will terminate the program if it is unable to
850  * obtain memory from the operating system). No other exception will
851  * be thrown unless the constructor of std::basic_streambuf throws.
852  *
853  * @note If a converter is provided, the stream will no longer be
854  * seekable even if it otherwise would be, so tellp() and seekp()
855  * will no longer work (they will return pos_type(off_type(-1)).
856  *
857  * Since 1.2.6
858  */
859  basic_gstreambuf(const GobjHandle<GOutputStream>& output_stream_,
860  bool manage_,
861  const GobjHandle<GConverter>& converter_ = GobjHandle<GConverter>());
862 
863  /**
864  * The constructor taking a GIO input-output stream. This class does
865  * not offer concurrent access from multiple threads to the same
866  * stream object, and if that is required users should provide their
867  * own synchronisation.
868  *
869  * @param io_stream_ A GIO input-output stream to be attached to the
870  * streambuffer. If the caller wants the stream to survive this
871  * class's destruction or a call to close_stream() or
872  * attach_stream(), the caller should keep a separate GobjHandle
873  * object which references the stream (obtained by, say, calling
874  * get_iostream()) and pass 'manage_' as false.
875  *
876  * @param manage_ Whether the streambuffer should call
877  * g_io_stream_close() on the stream in its destructor or when
878  * another stream is attached. Passing 'true' is usually what is
879  * wanted, and is particularly relevant on output streams because
880  * unless g_io_stream_close() is called, GIO may not commit to disk -
881  * 'false' only makes sense if the caller keeps a separate GobjHandle
882  * object which references the stream to keep it alive (obtained by,
883  * say, calling get_iostream()). Unlike its fdstreams equivalent,
884  * this parameter does not have a default value of 'true': this is
885  * partly to make it less likely that a converter is passed to this
886  * argument by mistake (that would not normally cause a compiler
887  * warning because GobjHandle has a type conversion operator
888  * providing the underlying C object by pointer, so GobjHandles are
889  * type convertible to pointers, and such a pointer will in turn
890  * provide a type match with a bool argument); and partly because,
891  * given a GIOStream* p, the construction \"Cgu::gstreambuf
892  * str(Cgu::GobjHandle<GIOStream>(p));\" without an additional
893  * argument or additional parentheses would cause a compiler error as
894  * it would be interpreted as a function declaration.
895  *
896  * @param input_converter_ A converter (if any) to be attached to the
897  * input stream (note that this does not affect the operation of
898  * set_byteswap()). The default value of an empty
899  * GobjHandle<GConverter> object indicates no converter.
900  *
901  * @param output_converter_ A converter (if any) to be attached to the
902  * output stream. The default value of an empty
903  * GobjHandle<GConverter> object indicates no converter.
904  *
905  * @exception std::bad_alloc This constructor will throw
906  * std::bad_alloc if memory is exhausted and the system throws on
907  * such exhaustion (unless the library has been installed using the
908  * \--with-glib-memory-slices-compat or
909  * \--with-glib-memory-slices-no-compat configuration option, in
910  * which case glib will terminate the program if it is unable to
911  * obtain memory from the operating system). No other exception will
912  * be thrown unless the constructor of std::basic_streambuf throws.
913  *
914  * @note If a converter is provided, the stream will no longer be
915  * seekable even if it otherwise would be, so tellg(), tellp(),
916  * seekg() and seekp() will no longer work (they will return
917  * pos_type(off_type(-1)). If the stream to which a converter has
918  * been attached represents a file on the file system (rather than a
919  * socket), after a read has been made, no further write may be made
920  * using the same GFileIOStream object. These restrictions do not
921  * apply to sockets (which are not seekable) so the use of converters
922  * with input-output streams (GIOStream) should generally be
923  * restricted to sockets.
924  *
925  * Since 1.2.6
926  */
927  basic_gstreambuf(const GobjHandle<GIOStream>& io_stream_,
928  bool manage_,
929  const GobjHandle<GConverter>& input_converter_ = GobjHandle<GConverter>(),
930  const GobjHandle<GConverter>& output_converter_ = GobjHandle<GConverter>());
931 
932 /**
933  * The destructor does not throw.
934  */
935  virtual ~basic_gstreambuf();
936 
937  /**
938  * Attach a new GIO input stream to the streambuffer (and close any
939  * GIO stream at present managed by it). In the case of wide
940  * character input streams, it also switches off byte swapping, if it
941  * was previously on. This class does not offer concurrent access
942  * from multiple threads to the same stream object, and if that is
943  * required users should provide their own synchronisation.
944  *
945  * @param input_stream_ The GIO input stream to be attached to the
946  * streambuffer. If the caller wants the input stream to survive a
947  * subsequent call to close_stream() or attach_stream() or this
948  * class's destruction, the caller should keep a separate GobjHandle
949  * object which references the stream (obtained by, say, calling
950  * get_istream()) and pass 'manage_' as false. If this is a
951  * GFilterInputStream object (that is, a GBufferedInputStream or
952  * GConverterInputStream stream), only the underlying base input
953  * stream will be attached and the other higher level streams will be
954  * closed (buffering of input streams is always provided by this C++
955  * streambuffer, and converting is controlled solely by the
956  * 'converter_' argument).
957  *
958  * @param manage_ Whether the streambuffer should call
959  * g_input_stream_close() on the stream in its destructor or when
960  * another stream is attached. Passing 'true' is usually what is
961  * wanted - 'false' only makes sense if the caller keeps a separate
962  * GobjHandle object which references the stream to keep it alive
963  * (obtained by, say, calling get_istream()). Unlike its fdstreams
964  * equivalent, this parameter does not have a default value of
965  * 'true': this is partly to make it less likely that a converter is
966  * passed to this argument by mistake (that would not normally cause
967  * a compiler warning because GobjHandle has a type conversion
968  * operator providing the underlying C object by pointer, so
969  * GobjHandles are type convertible to pointers, and such a pointer
970  * will in turn provide a type match with a bool argument); and
971  * partly to maintain compatibility with the constructor's interface,
972  * which has separate syntactic constraints.
973  *
974  * @param converter_ A converter (if any) to be attached to the GIO
975  * input stream (note that this does not affect the operation of
976  * set_byteswap()). The default value of an empty
977  * GobjHandle<GConverter> object indicates no converter.
978  *
979  * @exception std::bad_alloc This method will throw std::bad_alloc if
980  * memory is exhausted and the system throws on such exhaustion
981  * (unless the library has been installed using the
982  * \--with-glib-memory-slices-compat or
983  * \--with-glib-memory-slices-no-compat configuration option, in which
984  * case glib will terminate the program if it is unable to obtain
985  * memory from the operating system).
986  *
987  * @note If a converter is provided, the stream will no longer be
988  * seekable even if it otherwise would be, so tellg() and seekg()
989  * will no longer work (they will return pos_type(off_type(-1)).
990  *
991  * Since 1.2.6
992  */
993  void attach_stream(const GobjHandle<GInputStream>& input_stream_,
994  bool manage_,
995  const GobjHandle<GConverter>& converter_ = GobjHandle<GConverter>());
996 
997  /**
998  * Attach a new GIO output stream to the streambuffer (and close any
999  * GIO stream at present managed by it). If output buffering was
1000  * previously switched off, it is switched back on again. This class
1001  * does not offer concurrent access from multiple threads to the same
1002  * stream object, and if that is required users should provide their
1003  * own synchronisation.
1004  *
1005  * @param output_stream_ The GIO output stream to be attached to the
1006  * streambuffer. If the caller wants the output stream to survive a
1007  * subsequent call to close_stream() or attach_stream() or this
1008  * class's destruction, the caller should keep a separate GobjHandle
1009  * object which references the stream (obtained by, say, calling
1010  * get_ostream()) and pass 'manage_' as false. If this is a
1011  * GFilterOutputStream object (that is, a GBufferedOutputStream,
1012  * GConverterOutputStream or GDataOutputStream stream), only the
1013  * underlying base output stream will be attached and the other
1014  * higher level streams will be closed (buffering and converting are
1015  * controlled solely by the set_output_buffered() method and
1016  * 'converter_' argument).
1017  *
1018  * @param manage_ Whether the streambuffer should call
1019  * g_output_stream_close() on the stream in its destructor or when
1020  * another stream is attached. Passing 'true' is usually what is
1021  * wanted, and is particularly relevant on output streams because
1022  * unless g_output_stream_close() is called, GIO may not commit to
1023  * disk - 'false' only makes sense if the caller keeps a separate
1024  * GobjHandle object which references the stream to keep it alive
1025  * (obtained by, say, calling get_ostream()). Unlike its fdstreams
1026  * equivalent, this parameter does not have a default value of
1027  * 'true': this is partly to make it less likely that a converter is
1028  * passed to this argument by mistake (that would not normally cause
1029  * a compiler warning because GobjHandle has a type conversion
1030  * operator providing the underlying C object by pointer, so
1031  * GobjHandles are type convertible to pointers, and such a pointer
1032  * will in turn provide a type match with a bool argument); and
1033  * partly to maintain compatibility with the constructor's interface,
1034  * which has separate syntactic constraints.
1035  *
1036  * @param converter_ A converter (if any) to be attached to the GIO
1037  * output stream. The default value of an empty
1038  * GobjHandle<GConverter> object indicates no converter.
1039  *
1040  * @exception std::bad_alloc This method will throw std::bad_alloc if
1041  * memory is exhausted and the system throws on such exhaustion
1042  * (unless the library has been installed using the
1043  * \--with-glib-memory-slices-compat or
1044  * \--with-glib-memory-slices-no-compat configuration option, in
1045  * which case glib will terminate the program if it is unable to
1046  * obtain memory from the operating system).
1047  *
1048  * @note If a converter is provided, the stream will no longer be
1049  * seekable even if it otherwise would be, so tellp() and seekp()
1050  * will no longer work (they will return pos_type(off_type(-1)).
1051  *
1052  * Since 1.2.6
1053  */
1054  void attach_stream(const GobjHandle<GOutputStream>& output_stream_,
1055  bool manage_,
1056  const GobjHandle<GConverter>& converter_ = GobjHandle<GConverter>());
1057 
1058  /**
1059  * Attach a new GIO input-output stream to the streambuffer (and
1060  * close any GIO stream at present managed by it). If output
1061  * buffering was previously switched off, it is switched back on
1062  * again. In the case of wide character input-output streams, it
1063  * also switches off byte swapping on input, if it was previously on.
1064  * This class does not offer concurrent access from multiple threads
1065  * to the same stream object, and if that is required users should
1066  * provide their own synchronisation.
1067  *
1068  * @param io_stream_ The GIO input-output stream to be attached to
1069  * the streambuffer. If the caller wants the stream to survive a
1070  * subsequent call to close_stream() or attach_stream() or this
1071  * class's destruction, the caller should keep a separate GobjHandle
1072  * object which references the stream (obtained by, say, calling
1073  * get_iostream()) and pass 'manage_' as false.
1074  *
1075  * @param manage_ Whether the streambuffer should call
1076  * g_io_stream_close() on the stream in its destructor or when
1077  * another stream is attached. Passing 'true' is usually what is
1078  * wanted, and is particularly relevant on output streams because
1079  * unless g_io_stream_close() is called, GIO may not commit to disk -
1080  * 'false' only makes sense if the caller keeps a separate GobjHandle
1081  * object which references the stream to keep it alive (obtained by,
1082  * say, calling get_iostream()). Unlike its fdstreams equivalent,
1083  * this parameter does not have a default value of 'true': this is
1084  * partly to make it less likely that a converter is passed to this
1085  * argument by mistake (that would not normally cause a compiler
1086  * warning because GobjHandle has a type conversion operator
1087  * providing the underlying C object by pointer, so GobjHandles are
1088  * type convertible to pointers, and such a pointer will in turn
1089  * provide a type match with a bool argument); and partly to maintain
1090  * compatibility with the constructor's interface, which has separate
1091  * syntactic constraints.
1092  *
1093  * @param input_converter_ A converter (if any) to be attached to the
1094  * input stream (note that this does not affect the operation of
1095  * set_byteswap()). The default value of an empty
1096  * GobjHandle<GConverter> object indicates no converter.
1097  *
1098  * @param output_converter_ A converter (if any) to be attached to the
1099  * output stream. The default value of an empty
1100  * GobjHandle<GConverter> object indicates no converter.
1101  *
1102  * @exception std::bad_alloc This method will throw std::bad_alloc if
1103  * memory is exhausted and the system throws on such exhaustion
1104  * (unless the library has been installed using the
1105  * \--with-glib-memory-slices-compat or
1106  * \--with-glib-memory-slices-no-compat configuration option, in
1107  * which case glib will terminate the program if it is unable to
1108  * obtain memory from the operating system).
1109  *
1110  * @note If a converter is provided, the stream will no longer be
1111  * seekable even if it otherwise would be, so tellg(), tellp(),
1112  * seekg() and seekp() will no longer work (they will return
1113  * pos_type(off_type(-1)). If the stream to which a converter has
1114  * been attached represents a file on the file system (rather than a
1115  * socket), after a read has been made, no further write may be made
1116  * using the same GFileIOStream object. These restrictions do not
1117  * apply to sockets (which are not seekable) so the use of converters
1118  * with input-output streams (GIOStream) should generally be
1119  * restricted to sockets.
1120  *
1121  * Since 1.2.6
1122  */
1123  void attach_stream(const GobjHandle<GIOStream>& io_stream_,
1124  bool manage_,
1125  const GobjHandle<GConverter>& input_converter_ = GobjHandle<GConverter>(),
1126  const GobjHandle<GConverter>& output_converter_ = GobjHandle<GConverter>());
1127 
1128 
1129  /**
1130  * Call g_input_stream_close(), g_output_stream_close() or
1131  * g_io_stream_close(), as the case may be, on the GIO stream at
1132  * present attached to the streambuffer (if any), and release the
1133  * streambuffer's reference to that stream (the reference will be
1134  * released even if an error arose in closing the stream). If the
1135  * caller wants the GIO stream to survive the call to this method
1136  * (albeit in a closed state), the caller should, before the call is
1137  * made, keep a separate GobjHandle object which references the
1138  * stream. This method does not throw. This class does not offer
1139  * concurrent access from multiple threads to the same stream object,
1140  * and if that is required users should provide their own
1141  * synchronisation.
1142  *
1143  * @return true if the close succeeded, false if an error arose
1144  * (including in a case where no GIO stream has been attached or it
1145  * has already been closed).
1146  *
1147  * Since 1.2.6
1148  */
1149  bool close_stream();
1150 
1151  /**
1152  * Get the GIO input stream at present attached to the streambuffer
1153  * (if any), by GobjHandle. If a GOutputStream object rather than a
1154  * GInputStream or GIOStream object has been attached (or no stream
1155  * has been attached) or it has been closed, then this method will
1156  * return an empty GobjHandle object. If a GIOStream object has been
1157  * attached, this streambuffer's maintained GInputStream object will
1158  * be returned, which may be a converting stream manufactured from
1159  * the GInputStream object maintained by the GIOStream object.
1160  * Retaining the return value will cause the stream to survive a
1161  * subsequent call to attach_stream() or the destruction of this
1162  * object. The return value may be a different stream from the one
1163  * originally passed to this object's constructor or to attach(). It
1164  * will be different if a converter has been attached to it. This
1165  * method does not throw. This class does not offer concurrent
1166  * access from multiple threads to the same stream object, and if
1167  * that is required users should provide their own synchronisation.
1168  *
1169  * @return The GIO input stream at present attached to the
1170  * streambuffer, or an empty GobjHandle object if none has been
1171  * attached
1172  *
1173  * Since 1.2.6
1174  */
1176 
1177  /**
1178  * Get the GIO output stream at present attached to the streambuffer
1179  * (if any), by GobjHandle. If a GInputStream object rather than a
1180  * GOutputStream or GIOStream object has been attached (or no stream
1181  * has been attached) or it has been closed, then this method will
1182  * return an empty GobjHandle object. If a GIOStream object has been
1183  * attached, this streambuffer's maintained GOutputStream object will
1184  * be returned, which may be a converting stream manufactured from
1185  * the GOutputStream object maintained by the GIOStream object.
1186  * Retaining the return value will cause the stream to survive a
1187  * subsequent call to attach_stream() or the destruction of this
1188  * object. The return value may be a different stream from the one
1189  * originally passed to this object's constructor or to attach(). It
1190  * will be different if a converter has been attached to it. This
1191  * method does not throw. This class does not offer concurrent
1192  * access from multiple threads to the same stream object, and if
1193  * that is required users should provide their own synchronisation.
1194  *
1195  * @return The GIO output stream at present attached to the
1196  * streambuffer, or an empty GobjHandle object if none has been
1197  * attached
1198  *
1199  * Since 1.2.6
1200  */
1202 
1203  /**
1204  * Get the GIOStream input-output stream at present attached to the
1205  * streambuffer (if any), by GobjHandle. If a GInputStream or
1206  * GOutputStream object rather than a GIOStream object has been
1207  * attached (or no stream has been attached) or it has been closed,
1208  * then this method will return an empty GobjHandle object.
1209  * Retaining the return value will cause the stream to survive a
1210  * subsequent call to attach_stream() or the destruction of this
1211  * object. This method does not throw. This class does not offer
1212  * concurrent access from multiple threads to the same stream object,
1213  * and if that is required users should provide their own
1214  * synchronisation.
1215  *
1216  * @return The GIOStream stream at present attached to the
1217  * streambuffer, or an empty GobjHandle object if none has been
1218  * attached
1219  *
1220  * Since 1.2.6
1221  */
1223 
1224  /**
1225  * Causes the streambuffer to swap bytes in incoming text, so as to
1226  * convert big endian text to little endian text, or little endian
1227  * text to big endian text. It is called by the user in response to
1228  * finding a byte order marker (BOM) 0xfffe (UTF-16) or 0xfffe0000
1229  * (UTF-32) as the first character of a newly opened file/stream, or
1230  * if the user knows by some other means that the native endianness
1231  * of the machine doing the reading differs from the endianness of
1232  * the file/stream being read. This only has effect on wide
1233  * character streambuffers for input (for example, a wgstreambuf to
1234  * which a GInputStream or GIOStream object has been attached), and
1235  * not the gstreambuf narrow character stream buffer. Note that
1236  * characters held for output are always outputted in native endian
1237  * format unless a GConverter object has been attached, and this
1238  * method does not affect that. This method does not throw. This
1239  * class does not offer concurrent access from multiple threads to
1240  * the same stream object, and if that is required users should
1241  * provide their own synchronisation.
1242  *
1243  * @param swap 'true' if byte swapping for input is to be turned on,
1244  * 'false' if it is to be turned off. This will affect all
1245  * characters extracted from the underlying streambuffer after this
1246  * call is made. If a previously extracted character is to be
1247  * putback(), it must be put back before this function is called (or
1248  * unget() should be called instead) to avoid a putback mismatch,
1249  * because this call will byte-swap anything already in the buffers.
1250  * (Characters extracted after the call to this method may be putback
1251  * normally.)
1252  *
1253  * Since 1.2.6
1254  */
1255  void set_byteswap(bool swap);
1256 
1257 /**
1258  * If the GIO stream attached to this object is GOutputStream or
1259  * GIOStream, this method converts it to an unbuffered stream for
1260  * output if 'buffered' is false, or back to a buffered stream if
1261  * buffering has previously been switched off and 'buffered' is true.
1262  * Buffering is on by default for any newly created gstreambuf object
1263  * and any newly attached GIO output stream or input-output stream.
1264  * If buffering is turned off, all characters at present in the
1265  * buffers which are stored for output are flushed (but if writing to
1266  * a file which is being written over/replaced, output from this
1267  * streambuffer may not appear in the destination until the GIO stream
1268  * is closed). This method has no effect if no GIO output stream or
1269  * input-output stream has yet been attached to this streambuffer.
1270  * Switching output buffering off is similar in effect to setting the
1271  * std::ios_base::unitbuf flag in the relevant gostream or giostream
1272  * object, except that switching buffering off is slightly more
1273  * efficient, and setting the std::ios_base::unitbuf flag will not
1274  * retain the automatic tying of logical and actual file positions
1275  * that occurs when output buffering is switched off, as explained
1276  * @ref GioRandomAccessAnchor "here". This class does not offer
1277  * concurrent access from multiple threads to the same stream object,
1278  * and if that is required users should provide their own
1279  * synchronisation.
1280  *
1281  * @param buffered 'false' if buffering is to be turned off, 'true' if
1282  * it is to be turned back on.
1283  *
1284  * @exception std::bad_alloc This method will throw std::bad_alloc if
1285  * 'buffered' is true, output buffering had previously been switched
1286  * off, memory is exhausted and the system throws on such exhaustion
1287  * (unless the library has been installed using the
1288  * \--with-glib-memory-slices-compat or
1289  * \--with-glib-memory-slices-no-compat configuration option, in which
1290  * case glib will terminate the program if it is unable to obtain
1291  * memory from the operating system).
1292  *
1293  * Since 1.2.6
1294  */
1295  void set_output_buffered(bool buffered);
1296 
1297 /**
1298  * This method indicates whether the attached GIO stream implements
1299  * GSeekable, so that a call to seekoff() or seekpos() can succeed.
1300  * This method does not throw. This class does not offer concurrent
1301  * access from multiple threads to the same stream object, and if that
1302  * is required users should provide their own synchronisation.
1303  *
1304  * @return true if random access is supported, otherwise false. The
1305  * result is only meaningful if a GIO stream has been attached to this
1306  * streambuffer.
1307  *
1308  * Since 1.2.6
1309  */
1310  bool can_seek() const {return seekable;}
1311 
1312 /**
1313  * This method indicates whether any attached GIO input stream is in
1314  * an error state. It can be useful for detecting conversion errors
1315  * on converting streams. This class does not offer concurrent access
1316  * from multiple threads to the same stream object, and if that is
1317  * required users should provide their own synchronisation.
1318  *
1319  * @return NULL if no input stream is attached, or it is not in an
1320  * error state. If an attached input stream is in an error state, say
1321  * because it is a converting input stream which has encountered a
1322  * conversion error, the most recent GError object emitted by a read
1323  * operation on it is returned. Ownership of the return value is
1324  * retained, so if it is intended to be used after the next read
1325  * operation, it should be copied using g_error_copy().
1326  *
1327  * Since 1.2.19
1328  */
1329  GError* is_input_error();
1330 
1331 /**
1332  * This method indicates whether any attached GIO output stream is in
1333  * an error state. It can be useful for detecting conversion errors
1334  * on converting streams. This class does not offer concurrent access
1335  * from multiple threads to the same stream object, and if that is
1336  * required users should provide their own synchronisation.
1337  *
1338  * @return NULL if no output stream is attached, or it is not in an
1339  * error state. If an attached output stream is in an error state,
1340  * say because it is a converting output stream which has encountered
1341  * a conversion error, the most recent GError object emitted by a
1342  * write operation on it is returned. Ownership of the return value
1343  * is retained, so if it is intended to be used after the next write
1344  * operation, it should be copied using g_error_copy().
1345  *
1346  * Since 1.2.19
1347  */
1348  GError* is_output_error();
1349 
1350 /* Only has effect if --with-glib-memory-slices-compat or
1351  * --with-glib-memory-slices-no-compat option picked */
1353 };
1354 
1355 /**
1356  * @headerfile gstream.h c++-gtk-utils/gstream.h
1357  * @brief C++ output stream for GIO streams
1358  * @sa gstreams
1359  * @ingroup gstreams
1360  *
1361  * This class provides standard ostream services for GIO output
1362  * streams.
1363  */
1364 template <class charT , class Traits = std::char_traits<charT> >
1365 class basic_gostream: public std::basic_ostream<charT, Traits> {
1366 
1368 
1370  basic_gostream& operator=(const basic_gostream&);
1371 
1372 public:
1373  /**
1374  * The constructor taking a GIO output stream. This class does not
1375  * offer concurrent access from multiple threads to the same stream
1376  * object, and if that is required users should provide their own
1377  * synchronisation.
1378  *
1379  * @param stream A GIO output stream to be attached. If the caller
1380  * wants the output stream to survive this class's destruction or a
1381  * call to close() or attach(), the caller should keep a separate
1382  * GobjHandle object which references the stream (obtained by, say,
1383  * calling get_gio_stream()) and pass 'manage' as false. If this is
1384  * a GFilterOutputStream object (that is, a GBufferedOutputStream,
1385  * GConverterOutputStream or GDataOutputStream stream), only the
1386  * underlying base output stream will be attached and the other
1387  * higher level streams will be closed (buffering and converting are
1388  * controlled solely by the set_buffered() method and 'converter'
1389  * argument).
1390  *
1391  * @param manage Whether the underlying streambuffer should call
1392  * g_output_stream_close() on the GIO stream in the streambuffer's
1393  * destructor or when another stream is attached. Passing 'true' is
1394  * usually what is wanted, and is particularly relevant on output
1395  * streams because unless g_output_stream_close() is called, GIO may
1396  * not commit to disk - 'false' only makes sense if the caller keeps
1397  * a separate GobjHandle object which references the stream to keep
1398  * it alive (obtained by, say, calling get_gio_stream()). Unlike its
1399  * fdstreams equivalent, this parameter does not have a default value
1400  * of 'true': this is partly to make it less likely that a converter
1401  * is passed to this argument by mistake (that would not normally
1402  * cause a compiler warning because GobjHandle has a type conversion
1403  * operator providing the underlying C object by pointer, so
1404  * GobjHandles are type convertible to pointers, and such a pointer
1405  * will in turn provide a type match with a bool argument); and
1406  * partly because, given a GOutputStream* p, the construction
1407  * \"Cgu::gostream str(Cgu::GobjHandle<GOutputStream>(p));\"
1408  * without an additional argument or additional parentheses would
1409  * cause a compiler error as it would be interpreted as a function
1410  * declaration.
1411  *
1412  * @param converter A converter (if any) to be attached to the GIO
1413  * output stream. The default value of an empty
1414  * GobjHandle<GConverter> object indicates no converter.
1415  *
1416  * @exception std::bad_alloc This constructor will throw
1417  * std::bad_alloc if memory is exhausted and the system throws on
1418  * such exhaustion (unless the library has been installed using the
1419  * \--with-glib-memory-slices-compat or
1420  * \--with-glib-memory-slices-no-compat configuration option, in
1421  * which case glib will terminate the program if it is unable to
1422  * obtain memory from the operating system). No other exception will
1423  * be thrown unless the constructor of std::basic_streambuf or
1424  * std::basic_ostream throws.
1425  *
1426  * @note If a converter is provided, the stream will no longer be
1427  * seekable even if it otherwise would be, so tellp() and seekp()
1428  * will no longer work (they will return pos_type(off_type(-1)).
1429  *
1430  * Since 1.2.6
1431  */
1433  bool manage,
1434  const GobjHandle<GConverter>& converter = GobjHandle<GConverter>()):
1435  std::basic_ostream<charT, Traits>(0),
1436  buf(stream, manage, converter) {
1437  this->rdbuf(&buf);
1438  }
1439 
1440  /**
1441  * With this constructor, the GIO output stream must be attached
1442  * later with the attach() method. It will not throw unless the
1443  * default constructor of std::basic_streambuf or std::basic_ostream
1444  * throws. This class does not offer concurrent access from multiple
1445  * threads to the same stream object, and if that is required users
1446  * should provide their own synchronisation.
1447  *
1448  * Since 1.2.6
1449  */
1450  basic_gostream(): std::basic_ostream<charT, Traits>(0) {
1451  this->rdbuf(&buf);
1452  }
1453 
1454  /**
1455  * Attach a new GIO output stream to this object (and close any GIO
1456  * stream at present managed by it). If output buffering was
1457  * previously switched off, it is switched back on again. If any
1458  * stream state flags were set (eofbit, failbit or badbit), they will
1459  * be cleared by a call to clear(). If this method closes a stream
1460  * at present managed by it and the close fails, failbit is not set
1461  * and no exception will be thrown. Accordingly, if the user needs
1462  * to know whether there was an error in this method closing any
1463  * managed stream, she should call close() explicitly before calling
1464  * this method. This class does not offer concurrent access from
1465  * multiple threads to the same stream object, and if that is
1466  * required users should provide their own synchronisation.
1467  *
1468  * @param stream A GIO output stream to be attached. If the caller
1469  * wants the GIO output stream to survive a subsequent call to
1470  * close() or attach() or this class's destruction, the caller should
1471  * keep a separate GobjHandle object which references the stream
1472  * (obtained by, say, calling get_gio_stream()) and pass 'manage' as
1473  * false. If this is a GFilterOutputStream object (that is, a
1474  * GBufferedOutputStream, GConverterOutputStream or GDataOutputStream
1475  * stream), only the underlying base output stream will be attached
1476  * and the other higher level streams will be closed (buffering and
1477  * converting are controlled solely by the set_buffered() method and
1478  * 'converter' argument).
1479  *
1480  * @param manage Whether the underlying streambuffer should call
1481  * g_output_stream_close() on the GIO stream in the streambuffer's
1482  * destructor or when another stream is attached. Passing 'true' is
1483  * usually what is wanted, and is particularly relevant on output
1484  * streams because unless g_output_stream_close() is called, GIO may
1485  * not commit to disk - 'false' only makes sense if the caller keeps
1486  * a separate GobjHandle object which references the stream to keep
1487  * it alive (obtained by, say, calling get_gio_stream()). Unlike its
1488  * fdstreams equivalent, this parameter does not have a default value
1489  * of 'true': this is partly to make it less likely that a converter
1490  * is passed to this argument by mistake (that would not normally
1491  * cause a compiler warning because GobjHandle has a type conversion
1492  * operator providing the underlying C object by pointer, so
1493  * GobjHandles are type convertible to pointers, and such a pointer
1494  * will in turn provide a type match with a bool argument); and
1495  * partly to maintain compatibility with the constructor's interface,
1496  * which has separate syntactic constraints.
1497  *
1498  * @param converter A converter (if any) to be attached to the GIO
1499  * output stream. The default value of an empty
1500  * GobjHandle<GConverter> object indicates no converter.
1501  *
1502  * @exception std::bad_alloc This method will throw std::bad_alloc if
1503  * memory is exhausted and the system throws on such exhaustion
1504  * (unless the library has been installed using the
1505  * \--with-glib-memory-slices-compat or
1506  * \--with-glib-memory-slices-no-compat configuration option, in
1507  * which case glib will terminate the program if it is unable to
1508  * obtain memory from the operating system).
1509  *
1510  * @note If a converter is provided, the stream will no longer be
1511  * seekable even if it otherwise would be, so tellp() and seekp()
1512  * will no longer work (they will return pos_type(off_type(-1)).
1513  *
1514  * Since 1.2.6
1515  */
1516  void attach(const GobjHandle<GOutputStream>& stream,
1517  bool manage,
1518  const GobjHandle<GConverter>& converter = GobjHandle<GConverter>())
1519  {buf.attach_stream(stream, manage, converter); this->clear();}
1520 
1521  /**
1522  * Call g_output_stream_close() on the GIO stream at present attached
1523  * (if any), and release the underlying C++ streambuffer's reference
1524  * to that stream. If the caller wants the GIO stream to survive the
1525  * call to this method (albeit in a closed state), the caller should,
1526  * before the call is made, keep a separate GobjHandle object which
1527  * references the stream. If the close fails, the failbit will be
1528  * set with setstate(std::ios_base::failbit). This class does not
1529  * offer concurrent access from multiple threads to the same stream
1530  * object, and if that is required users should provide their own
1531  * synchronisation.
1532  *
1533  * @exception std::ios_base::failure This exception will be thrown if
1534  * an error arises on closing the stream and such an exception has
1535  * been required by a call to the exceptions() method of this class
1536  * (inherited from std::basic_ios<>). No exception will be thrown if
1537  * exceptions() has not been called.
1538  *
1539  * Since 1.2.6
1540  */
1541  void close() {if (!buf.close_stream()) this->setstate(std::ios_base::failbit);}
1542 
1543  /**
1544  * Get the GIO output stream at present attached (if any), by
1545  * GobjHandle. If no stream has been attached, this method will
1546  * return an empty GobjHandle object. Retaining the return value
1547  * will cause the GIO output stream to survive the destruction of
1548  * this object. The return value may be a different stream from the
1549  * one originally passed to this object's constructor or to attach().
1550  * It will be different if a converter has been attached to it. This
1551  * method does not throw. This class does not offer concurrent
1552  * access from multiple threads to the same stream object, and if
1553  * that is required users should provide their own synchronisation.
1554  *
1555  * @return The GIO output stream at present attached, or an empty
1556  * GobjHandle object if none has been attached
1557  *
1558  * Since 1.2.6
1559  */
1561 
1562 /**
1563  * This method converts the attached GIO output stream to an
1564  * unbuffered stream for output if 'buffered' is false, or back to a
1565  * buffered stream if buffering has previously been switched off and
1566  * 'buffered' is true. Buffering is on by default for any newly
1567  * created gostream object and any newly attached GIO output stream.
1568  * If buffering is turned off, all characters at present in the
1569  * buffers which are stored for output are flushed (but if writing to
1570  * a file which is being written over/replaced, output may not appear
1571  * in the destination until the GIO stream is closed). This method
1572  * has no effect if no GIO output stream has yet been attached.
1573  * Switching output buffering off is similar in effect to setting the
1574  * std::ios_base::unitbuf flag, but is slightly more efficient. This
1575  * class does not offer concurrent access from multiple threads to the
1576  * same stream object, and if that is required users should provide
1577  * their own synchronisation.
1578  *
1579  * @param buffered 'false' if buffering is to be turned off, 'true' if
1580  * it is to be turned back on.
1581  *
1582  * @exception std::bad_alloc This method will throw std::bad_alloc if
1583  * 'buffered' is true, output buffering had previously been switched
1584  * off, memory is exhausted and the system throws on such exhaustion
1585  * (unless the library has been installed using the
1586  * \--with-glib-memory-slices-compat or
1587  * \--with-glib-memory-slices-no-compat configuration option, in which
1588  * case glib will terminate the program if it is unable to obtain
1589  * memory from the operating system).
1590  *
1591  * Since 1.2.6
1592  */
1593  void set_buffered(bool buffered) {buf.set_output_buffered(buffered);}
1594 
1595 /**
1596  * This method indicates whether the attached GIO output stream
1597  * implements GSeekable, so that a call to tellp() or seekp() can
1598  * succeed. Note that in the seekp(off_type off, ios_base::seekdir
1599  * dir) variant, on wide character streams the 'off' argument is
1600  * dimensioned as the number of wchar_t units not the number of bytes
1601  * (that is, it is bytes/sizeof(char_type)). This method does not
1602  * throw. This class does not offer concurrent access from multiple
1603  * threads to the same stream object, and if that is required users
1604  * should provide their own synchronisation.
1605  *
1606  * @return true if the attached GIO stream implements GSeekable,
1607  * otherwise false. The result is only meaningful if a GIO stream has
1608  * been attached to this C++ stream object.
1609  *
1610  * Since 1.2.6
1611  */
1612  bool can_seek() const {return buf.can_seek();}
1613 
1614 /**
1615  * This method reports the error status of any attached GIO output
1616  * stream, and is intended to be called where failbit or badbit has
1617  * been set. It can be useful for interpreting conversion errors on
1618  * converting streams where one of those bits is set. This class does
1619  * not offer concurrent access from multiple threads to the same
1620  * stream object, and if that is required users should provide their
1621  * own synchronisation.
1622  *
1623  * @return NULL if no output stream is attached, or it is not in an
1624  * error state. If an attached output stream is in an error state,
1625  * say because it is a converting output stream which has encountered
1626  * a conversion error, the most recent GError object emitted by a
1627  * write operation on it is returned. Ownership of the return value
1628  * is retained, so if it is intended to be used after the next write
1629  * operation, it should be copied using g_error_copy().
1630  *
1631  * Since 1.2.19
1632  */
1633  GError* is_error() {return buf.is_output_error();}
1634 
1635 /* Only has effect if --with-glib-memory-slices-compat or
1636  * --with-glib-memory-slices-no-compat option picked */
1638 };
1639 
1640 
1641 /**
1642  * @headerfile gstream.h c++-gtk-utils/gstream.h
1643  * @brief C++ input stream for GIO streams
1644  * @sa gstreams
1645  * @ingroup gstreams
1646  *
1647  * This class provides standard istream services for GIO input
1648  * streams.
1649  */
1650 template <class charT , class Traits = std::char_traits<charT> >
1651 class basic_gistream : public std::basic_istream<charT, Traits> {
1652 
1654 
1656  basic_gistream& operator=(const basic_gistream&);
1657 
1658 public:
1659  /**
1660  * The constructor taking a GIO input stream. This class does not
1661  * offer concurrent access from multiple threads to the same stream
1662  * object, and if that is required users should provide their own
1663  * synchronisation.
1664  *
1665  * @param stream A GIO input stream to be attached. If the caller
1666  * wants the GIO input stream to survive this class's destruction or
1667  * a call to close() or attach(), the caller should keep a separate
1668  * GobjHandle object which references the stream (obtained by, say,
1669  * calling get_gio_stream()) and pass 'manage' as false. If this is
1670  * a GFilterInputStream object (that is, a GBufferedInputStream or
1671  * GConverterInputStream stream), only the underlying base input
1672  * stream will be attached and the other higher level streams will be
1673  * closed (buffering of input streams is always provided by the
1674  * underlying C++ streambuffer, and converting is controlled solely
1675  * by the 'converter' argument).
1676  *
1677  * @param manage Whether the underlying streambuffer should call
1678  * g_input_stream_close() on the GIO stream in the streambuffer's
1679  * destructor or when another stream is attached. Passing 'true' is
1680  * usually what is wanted - 'false' only makes sense if the caller
1681  * keeps a separate GobjHandle object which references the stream to
1682  * keep it alive (obtained by, say, calling get_gio_stream()).
1683  * Unlike its fdstreams equivalent, this parameter does not have a
1684  * default value of 'true': this is partly to make it less likely
1685  * that a converter is passed to this argument by mistake (that would
1686  * not normally cause a compiler warning because GobjHandle has a
1687  * type conversion operator providing the underlying C object by
1688  * pointer, so GobjHandles are type convertible to pointers, and such
1689  * a pointer will in turn provide a type match with a bool argument);
1690  * and partly because, given a GInputStream* p, the construction
1691  * \"Cgu::gistream str(Cgu::GobjHandle<GInputStream>(p));\" without
1692  * an additional argument or additional parentheses would cause a
1693  * compiler error as it would be interpreted as a function
1694  * declaration.
1695  *
1696  * @param converter A converter (if any) to be attached to the GIO
1697  * input stream (note that this does not affect the operation of
1698  * set_byteswap()). The default value of an empty
1699  * GobjHandle<GConverter> object indicates no converter.
1700  *
1701  * @exception std::bad_alloc This constructor will throw
1702  * std::bad_alloc if memory is exhausted and the system throws on
1703  * such exhaustion (unless the library has been installed using the
1704  * \--with-glib-memory-slices-compat or
1705  * \--with-glib-memory-slices-no-compat configuration option, in
1706  * which case glib will terminate the program if it is unable to
1707  * obtain memory from the operating system). No other exception will
1708  * be thrown unless the constructor of std::basic_streambuf or
1709  * std::basic_istream throws.
1710  *
1711  * @note If a converter is provided, the stream will no longer be
1712  * seekable even if it otherwise would be, so tellg() and seekg()
1713  * will no longer work (they will return pos_type(off_type(-1)).
1714  *
1715  * Since 1.2.6
1716  */
1718  bool manage,
1719  const GobjHandle<GConverter>& converter = GobjHandle<GConverter>()):
1720  std::basic_istream<charT, Traits>(0),
1721  buf(stream, manage, converter) {
1722  this->rdbuf(&buf);
1723  }
1724 
1725  /**
1726  * With this constructor, the GIO input stream must be attached later
1727  * with the attach() method. It will not throw unless the default
1728  * constructor of std::basic_streambuf or std::basic_istream throws.
1729  * This class does not offer concurrent access from multiple threads
1730  * to the same stream object, and if that is required users should
1731  * provide their own synchronisation.
1732  *
1733  * Since 1.2.6
1734  */
1735  basic_gistream(): std::basic_istream<charT, Traits>(0) {
1736  this->rdbuf(&buf);
1737  }
1738 
1739  /**
1740  * Attach a new GIO input stream to this object (and close any GIO
1741  * stream at present managed by it). In the case of wide character
1742  * input streams, it also switches off byte swapping, if it was
1743  * previously on. If any stream state flags were set (eofbit,
1744  * failbit or badbit), they will be cleared by a call to clear(). If
1745  * this method closes a stream at present managed by it and the close
1746  * fails, failbit is not set and no exception will be thrown.
1747  * Accordingly, if the user needs to know whether there was an error
1748  * in this method closing any managed stream, she should call close()
1749  * explicitly before calling this method. This class does not offer
1750  * concurrent access from multiple threads to the same stream object,
1751  * and if that is required users should provide their own
1752  * synchronisation.
1753  *
1754  * @param stream A GIO input stream to be attached. If the caller
1755  * wants the GIO input stream to survive a subsequent call to close()
1756  * or attach() or this class's destruction, the caller should keep a
1757  * separate GobjHandle object which references the stream (obtained
1758  * by, say, calling get_gio_stream()) and pass 'manage' as false. If
1759  * this is a GFilterInputStream object (that is, a
1760  * GBufferedInputStream or GConverterInputStream stream), only the
1761  * underlying base input stream will be attached and the other higher
1762  * level streams will be closed (buffering of input streams is always
1763  * provided by the underlying C++ streambuffer, and converting is
1764  * controlled solely by the 'converter' argument).
1765  *
1766  * @param manage Whether the underlying streambuffer should call
1767  * g_input_stream_close() on the GIO stream in the streambuffer's
1768  * destructor or when another stream is attached. Passing 'true' is
1769  * usually what is wanted - 'false' only makes sense if the caller
1770  * keeps a separate GobjHandle object which references the stream to
1771  * keep it alive (obtained by, say, calling get_gio_stream()).
1772  * Unlike its fdstreams equivalent, this parameter does not have a
1773  * default value of 'true': this is partly to make it less likely
1774  * that a converter is passed to this argument by mistake (that would
1775  * not normally cause a compiler warning because GobjHandle has a
1776  * type conversion operator providing the underlying C object by
1777  * pointer, so GobjHandles are type convertible to pointers, and such
1778  * a pointer will in turn provide a type match with a bool argument);
1779  * and partly to maintain compatibility with the constructor's
1780  * interface, which has separate syntactic constraints.
1781  *
1782  * @param converter A converter (if any) to be attached to the GIO
1783  * input stream (note that this does not affect the operation of
1784  * set_byteswap()). The default value of an empty
1785  * GobjHandle<GConverter> object indicates no converter.
1786  *
1787  * @exception std::bad_alloc This method will throw std::bad_alloc if
1788  * memory is exhausted and the system throws on such exhaustion
1789  * (unless the library has been installed using the
1790  * \--with-glib-memory-slices-compat or
1791  * \--with-glib-memory-slices-no-compat configuration option, in
1792  * which case glib will terminate the program if it is unable to
1793  * obtain memory from the operating system).
1794  *
1795  * @note If a converter is provided, the stream will no longer be
1796  * seekable even if it otherwise would be, so tellg() and seekg()
1797  * will no longer work (they will return pos_type(off_type(-1)).
1798  *
1799  * Since 1.2.6
1800  */
1801  void attach(const GobjHandle<GInputStream>& stream,
1802  bool manage,
1803  const GobjHandle<GConverter>& converter = GobjHandle<GConverter>())
1804  {buf.attach_stream(stream, manage, converter); this->clear();}
1805 
1806  /**
1807  * Call g_input_stream_close() on the GIO stream at present attached
1808  * (if any), and release the underlying C++ streambuffer's reference
1809  * to that stream. If the caller wants the GIO stream to survive the
1810  * call to this method (albeit in a closed state), the caller should,
1811  * before the call is made, keep a separate GobjHandle object which
1812  * references the stream. If the close fails, the failbit will be
1813  * set with setstate(std::ios_base::failbit). This class does not
1814  * offer concurrent access from multiple threads to the same stream
1815  * object, and if that is required users should provide their own
1816  * synchronisation.
1817  *
1818  * @exception std::ios_base::failure This exception will be thrown if
1819  * an error arises on closing the stream and such an exception has
1820  * been required by a call to the exceptions() method of this class
1821  * (inherited from std::basic_ios<>). No exception will be thrown if
1822  * exceptions() has not been called.
1823  *
1824  * Since 1.2.6
1825  */
1826  void close() {if (!buf.close_stream()) this->setstate(std::ios_base::failbit);}
1827 
1828  /**
1829  * Get the GIO input stream at present attached (if any), by
1830  * GobjHandle. If no stream has been attached, this method will
1831  * return an empty GobjHandle object. Retaining the return value
1832  * will cause the GIO input stream to survive the destruction of this
1833  * object. The return value may be a different stream from the one
1834  * originally passed to this object's constructor or to attach(). It
1835  * will be different if a converter has been attached to it. This
1836  * method does not throw. This class does not offer concurrent
1837  * access from multiple threads to the same stream object, and if
1838  * that is required users should provide their own synchronisation.
1839  *
1840  * @return The GIO input stream at present attached, or an empty
1841  * GobjHandle object if none has been attached
1842  *
1843  * Since 1.2.6
1844  */
1846 
1847  /**
1848  * Causes the underlying streambuffer to swap bytes in the incoming
1849  * text, so as to convert big endian text to little endian text, or
1850  * little endian text to big endian text. It is called by the user
1851  * in response to finding a byte order marker (BOM) 0xfffe (UTF-16)
1852  * or 0xfffe0000 (UTF-32) as the first character of a newly opened
1853  * file/stream, or if the user knows by some other means that the
1854  * native endianness of the machine doing the reading differs from
1855  * the endianness of the file/stream being read. This only has
1856  * effect on wide character streams (for example, a wgistream
1857  * object), and not the gistream narrow character stream. This
1858  * method does not throw. This class does not offer concurrent
1859  * access from multiple threads to the same stream object, and if
1860  * that is required users should provide their own synchronisation.
1861  *
1862  * @param swap 'true' if byte swapping is to be turned on, 'false' if
1863  * it is to be turned off. This will affect all characters extracted
1864  * from the underlying streambuffer after this call is made. If a
1865  * previously extracted character is to be putback(), it must be put
1866  * back before this function is called (or unget() should be called
1867  * instead) to avoid a putback mismatch, because this call will
1868  * byte-swap anything already in the buffers. (Characters extracted
1869  * after the call to this method may be putback normally.)
1870  *
1871  * Since 1.2.6
1872  */
1873  void set_byteswap(bool swap) {buf.set_byteswap(swap);}
1874 
1875 /**
1876  * This method indicates whether the attached GIO input stream
1877  * implements GSeekable, so that a call to tellg() or seekg() can
1878  * succeed. Note that in the seekg(off_type off, ios_base::seekdir
1879  * dir) variant, on wide character streams the 'off' argument is
1880  * dimensioned as the number of wchar_t units not the number of bytes
1881  * (that is, it is bytes/sizeof(char_type)). This method does not
1882  * throw. This class does not offer concurrent access from multiple
1883  * threads to the same stream object, and if that is required users
1884  * should provide their own synchronisation.
1885  *
1886  * @return true if the attached GIO stream implements GSeekable,
1887  * otherwise false. The result is only meaningful if a GIO stream has
1888  * been attached to this C++ stream object.
1889  *
1890  * Since 1.2.6
1891  */
1892  bool can_seek() const {return buf.can_seek();}
1893 
1894 /**
1895  * This method reports the error status of any attached GIO input
1896  * stream, and is intended to be called where failbit has been set.
1897  * It can be useful for establishing, where that bit is set, whether
1898  * failbit indicates normal end-of-file or a conversion error on a
1899  * converting stream. This class does not offer concurrent access
1900  * from multiple threads to the same stream object, and if that is
1901  * required users should provide their own synchronisation.
1902  *
1903  * @return NULL if no input stream is attached, or it is not in an
1904  * error state. If an attached input stream is in an error state, say
1905  * because it is a converting input stream which has encountered a
1906  * conversion error, the most recent GError object emitted by a read
1907  * operation on it is returned. Ownership of the return value is
1908  * retained, so if it is intended to be used after the next read
1909  * operation, it should be copied using g_error_copy().
1910  *
1911  * Since 1.2.19
1912  */
1913  GError* is_error() {return buf.is_input_error();}
1914 
1915 /* Only has effect if --with-glib-memory-slices-compat or
1916  * --with-glib-memory-slices-no-compat option picked */
1918 };
1919 
1920 
1921 
1922 /**
1923  * @headerfile gstream.h c++-gtk-utils/gstream.h
1924  * @brief C++ input-output stream for GIO streams
1925  * @sa gstreams
1926  * @ingroup gstreams
1927  *
1928  * This class provides standard iostream services for GIO streams.
1929  */
1930 template <class charT , class Traits = std::char_traits<charT> >
1931 class basic_giostream : public std::basic_iostream<charT, Traits> {
1932 
1934 
1936  basic_giostream& operator=(const basic_giostream&);
1937 
1938 public:
1939  /**
1940  * The constructor taking a GIO input-output stream. This class does
1941  * not offer concurrent access from multiple threads to the same
1942  * stream object, and if that is required users should provide their
1943  * own synchronisation.
1944  *
1945  * @param stream A GIO input-output stream to be attached. If the
1946  * caller wants the GIO stream to survive this class's destruction or
1947  * a call to close() or attach(), the caller should keep a separate
1948  * GobjHandle object which references the stream (obtained by, say,
1949  * calling get_gio_io_stream()) and pass 'manage' as false.
1950  *
1951  * @param manage Whether the underlying streambuffer should call
1952  * g_io_stream_close() on the GIO stream in the streambuffer's
1953  * destructor or when another stream is attached. Passing 'true' is
1954  * usually what is wanted, and is particularly relevant on output
1955  * streams because unless g_io_stream_close() is called, GIO may not
1956  * commit to disk - 'false' only makes sense if the caller keeps a
1957  * separate GobjHandle object which references the stream to keep it
1958  * alive (obtained by, say, calling get_gio_io_stream()). Unlike its
1959  * fdstreams equivalent, this parameter does not have a default value
1960  * of 'true': this is partly to make it less likely that a converter
1961  * is passed to this argument by mistake (that would not normally
1962  * cause a compiler warning because GobjHandle has a type conversion
1963  * operator providing the underlying C object by pointer, so
1964  * GobjHandles are type convertible to pointers, and such a pointer
1965  * will in turn provide a type match with a bool argument); and
1966  * partly because, given a GIOStream* p, the construction
1967  * \"Cgu::giostream str(Cgu::GobjHandle<GIOStream>(p));\" without
1968  * an additional argument or additional parentheses would cause a
1969  * compiler error as it would be interpreted as a function
1970  * declaration.
1971  *
1972  * @param input_converter A converter (if any) to be attached to the
1973  * input stream (note that this does not affect the operation of
1974  * set_byteswap()). The default value of an empty
1975  * GobjHandle<GConverter> object indicates no converter.
1976  *
1977  * @param output_converter A converter (if any) to be attached to the
1978  * output stream. The default value of an empty
1979  * GobjHandle<GConverter> object indicates no converter.
1980  *
1981  * @exception std::bad_alloc This constructor will throw
1982  * std::bad_alloc if memory is exhausted and the system throws on
1983  * such exhaustion (unless the library has been installed using the
1984  * \--with-glib-memory-slices-compat or
1985  * \--with-glib-memory-slices-no-compat configuration option, in
1986  * which case glib will terminate the program if it is unable to
1987  * obtain memory from the operating system). No other exception will
1988  * be thrown unless the constructor of std::basic_streambuf or
1989  * std::basic_iostream throws.
1990  *
1991  * @note If a converter is provided, the stream will no longer be
1992  * seekable even if it otherwise would be, so tellg(), tellp(),
1993  * seekg() and seekp() will no longer work (they will return
1994  * pos_type(off_type(-1)). If the stream to which a converter has
1995  * been attached represents a file on the file system (rather than a
1996  * socket), after a read has been made, no further write may be made
1997  * using the same GFileIOStream object. These restrictions do not
1998  * apply to sockets (which are not seekable) so the use of converters
1999  * with input-output streams (GIOStream) should generally be
2000  * restricted to sockets.
2001  *
2002  * Since 1.2.6
2003  */
2005  bool manage,
2006  const GobjHandle<GConverter>& input_converter = GobjHandle<GConverter>(),
2007  const GobjHandle<GConverter>& output_converter = GobjHandle<GConverter>()):
2008  std::basic_iostream<charT, Traits>(0),
2009  buf(stream, manage, input_converter, output_converter) {
2010  this->rdbuf(&buf); // std::basic_ios is a virtual base class
2011  }
2012 
2013  /**
2014  * With this constructor, the GIO input-output stream must be
2015  * attached later with the attach() method. It will not throw unless
2016  * the constructor of std::basic_streambuf or std::basic_iostream
2017  * throws. This class does not offer concurrent access from multiple
2018  * threads to the same stream object, and if that is required users
2019  * should provide their own synchronisation.
2020  *
2021  * Since 1.2.6
2022  */
2023  basic_giostream() : std::basic_iostream<charT, Traits>(0) {
2024  this->rdbuf(&buf); // std::basic_ios is a virtual base class
2025  }
2026 
2027  /**
2028  * Attach a new GIO input-output stream to this object (and close any
2029  * GIO stream at present managed by it). If output buffering was
2030  * previously switched off, it is switched back on again. In the
2031  * case of wide character input-output streams, it also switches off
2032  * byte swapping on input, if it was previously on. If any stream
2033  * state flags were set (eofbit, failbit or badbit), they will be
2034  * cleared by a call to clear(). If this method closes a stream at
2035  * present managed by it and the close fails, failbit is not set and
2036  * no exception will be thrown. Accordingly, if the user needs to
2037  * know whether there was an error in this method closing any managed
2038  * stream, she should call close() explicitly before calling this
2039  * method. This class does not offer concurrent access from multiple
2040  * threads to the same stream object, and if that is required users
2041  * should provide their own synchronisation.
2042  *
2043  * @param stream A GIO input-output stream to be attached. If the
2044  * caller wants the GIO stream to survive a subsequent call to
2045  * close() or attach() or this class's destruction, the caller should
2046  * keep a separate GobjHandle object which references the stream
2047  * (obtained by, say, calling get_gio_io_stream()) and pass 'manage'
2048  * as false.
2049  *
2050  * @param manage Whether the underlying streambuffer should call
2051  * g_io_stream_close() on the GIO stream in the streambuffer's
2052  * destructor or when another stream is attached. Passing 'true' is
2053  * usually what is wanted, and is particularly relevant on output
2054  * streams because unless g_io_stream_close() is called, GIO may not
2055  * commit to disk - 'false' only makes sense if the caller keeps a
2056  * separate GobjHandle object which references the stream to keep it
2057  * alive (obtained by, say, calling get_gio_io_stream()). Unlike its
2058  * fdstreams equivalent, this parameter does not have a default value
2059  * of 'true': this is partly to make it less likely that a converter
2060  * is passed to this argument by mistake (that would not normally
2061  * cause a compiler warning because GobjHandle has a type conversion
2062  * operator providing the underlying C object by pointer, so
2063  * GobjHandles are type convertible to pointers, and such a pointer
2064  * will in turn provide a type match with a bool argument); and
2065  * partly to maintain compatibility with the constructor's interface,
2066  * which has separate syntactic constraints.
2067  *
2068  * @param input_converter A converter (if any) to be attached to the
2069  * input stream (note that this does not affect the operation of
2070  * set_byteswap()). The default value of an empty
2071  * GobjHandle<GConverter> object indicates no converter.
2072  *
2073  * @param output_converter A converter (if any) to be attached to the
2074  * output stream. The default value of an empty
2075  * GobjHandle<GConverter> object indicates no converter.
2076  *
2077  * @exception std::bad_alloc This method will throw std::bad_alloc if
2078  * memory is exhausted and the system throws on such exhaustion
2079  * (unless the library has been installed using the
2080  * \--with-glib-memory-slices-compat or
2081  * \--with-glib-memory-slices-no-compat configuration option, in
2082  * which case glib will terminate the program if it is unable to
2083  * obtain memory from the operating system).
2084  *
2085  * @note If a converter is provided, the stream will no longer be
2086  * seekable even if it otherwise would be, so tellg(), tellp(),
2087  * seekg() and seekp() will no longer work (they will return
2088  * pos_type(off_type(-1)). If the stream to which a converter has
2089  * been attached represents a file on the file system (rather than a
2090  * socket), after a read has been made, no further write may be made
2091  * using the same GFileIOStream object. These restrictions do not
2092  * apply to sockets (which are not seekable) so the use of converters
2093  * with input-output streams (GIOStream) should generally be
2094  * restricted to sockets.
2095  *
2096  * Since 1.2.6
2097  */
2098  void attach(const GobjHandle<GIOStream>& stream,
2099  bool manage,
2100  const GobjHandle<GConverter>& input_converter = GobjHandle<GConverter>(),
2101  const GobjHandle<GConverter>& output_converter = GobjHandle<GConverter>())
2102  {buf.attach_stream(stream, manage, input_converter, output_converter); this->clear();}
2103 
2104  /**
2105  * Call g_io_stream_close() on the GIO stream at present attached (if
2106  * any), and release the underlying C++ streambuffer's reference to
2107  * that stream. If the caller wants the GIO stream to survive the
2108  * call to this method (albeit in a closed state), the caller should,
2109  * before the call is made, keep a separate GobjHandle object which
2110  * references the stream. If the close fails, the failbit will be
2111  * set with setstate(std::ios_base::failbit). This class does not
2112  * offer concurrent access from multiple threads to the same stream
2113  * object, and if that is required users should provide their own
2114  * synchronisation.
2115  *
2116  * @exception std::ios_base::failure This exception will be thrown if
2117  * an error arises on closing the stream and such an exception has
2118  * been required by a call to the exceptions() method of this class
2119  * (inherited from std::basic_ios<>). No exception will be thrown if
2120  * exceptions() has not been called.
2121  *
2122  * Since 1.2.6
2123  */
2124  void close() {if (!buf.close_stream()) this->setstate(std::ios_base::failbit);}
2125 
2126  /**
2127  * Get the GIO input-output stream at present attached (if any), by
2128  * GobjHandle. If no stream has been attached, this method will
2129  * return an empty GobjHandle object. Retaining the return value
2130  * will cause the GIO input-output stream to survive the destruction
2131  * of this object. This method does not throw. This class does not
2132  * offer concurrent access from multiple threads to the same stream
2133  * object, and if that is required users should provide their own
2134  * synchronisation.
2135  *
2136  * @return The GIO input-output stream at present attached, or an
2137  * empty GobjHandle object if none has been attached
2138  *
2139  * Since 1.2.6
2140  */
2142 
2143  /**
2144  * Get the underlying GIO output stream at present attached (if any),
2145  * by GobjHandle. If none has been attached, this method will return
2146  * an empty GobjHandle object. Retaining the return value will cause
2147  * the GIO output stream to survive the destruction of this object.
2148  * The return value may be a different stream from the one kept by
2149  * the GIOStream object passed to this object's constructor or to
2150  * attach(). It will be different if a converter has been attached
2151  * to it. This method does not throw. This class does not offer
2152  * concurrent access from multiple threads to the same stream object,
2153  * and if that is required users should provide their own
2154  * synchronisation.
2155  *
2156  * @return The GIO output stream at present attached, or an empty
2157  * GobjHandle object if none has been attached
2158  *
2159  * Since 1.2.6
2160  */
2162 
2163  /**
2164  * Get the GIO input stream at present attached (if any), by
2165  * GobjHandle. If none has been attached, this method will return an
2166  * empty GobjHandle object. Retaining the return value will cause
2167  * the GIO input stream to survive the destruction of this object. The
2168  * return value may be a different stream from the one kept by the
2169  * GIOStream object passed to this object's constructor or to
2170  * attach(). It will be different if a converter has been attached
2171  * to it. This method does not throw. This class does not offer
2172  * concurrent access from multiple threads to the same stream object,
2173  * and if that is required users should provide their own
2174  * synchronisation.
2175  *
2176  * @return The GIO input stream at present attached, or an empty
2177  * GobjHandle object if none has been attached
2178  *
2179  * Since 1.2.6
2180  */
2182 
2183  /**
2184  * Causes the underlying streambuffer to swap bytes in the incoming
2185  * text, so as to convert big endian text to little endian text, or
2186  * little endian text to big endian text. It is called by the user
2187  * in response to finding a byte order marker (BOM) 0xfffe (UTF-16)
2188  * or 0xfffe0000 (UTF-32) as the first character of a newly opened
2189  * file/stream, or if the user knows by some other means that the
2190  * native endianness of the machine doing the reading differs from
2191  * the endianness of the file/stream being read. This only has
2192  * effect on wide character streams for input (for example, a
2193  * wgiostream object), and not the giostream narrow character stream.
2194  * Note also that characters held for output are always outputted in
2195  * native endian format unless a GConverter object has been attached,
2196  * and this method does not affect that. This method does not throw.
2197  * This class does not offer concurrent access from multiple threads
2198  * to the same stream object, and if that is required users should
2199  * provide their own synchronisation.
2200  *
2201  * @param swap 'true' if byte swapping for input is to be turned on,
2202  * 'false' if it is to be turned off. This will affect all
2203  * characters extracted from the underlying streambuffer after this
2204  * call is made. If a previously extracted character is to be
2205  * putback(), it must be put back before this function is called (or
2206  * unget() should be called instead) to avoid a putback mismatch,
2207  * because this call will byte-swap anything already in the buffers.
2208  * (Characters extracted after the call to this method may be putback
2209  * normally.)
2210  *
2211  * Since 1.2.6
2212  */
2213  void set_byteswap(bool swap) {buf.set_byteswap(swap);}
2214 
2215 /**
2216  * This method converts the attached GIO input-output stream to an
2217  * unbuffered stream for output if 'buffered' is false, or back to a
2218  * buffered stream if buffering has previously been switched off and
2219  * 'buffered' is true. Buffering is on by default for any newly
2220  * created giostream object and any newly attached GIO input-output
2221  * stream. If buffering is turned off, all characters at present in
2222  * the buffers which are stored for output are flushed (but if writing
2223  * to a file which is being written over/replaced, output may not
2224  * appear in the destination until the GIO stream is closed). This
2225  * method has no effect if no GIO input-output stream has yet been
2226  * attached. Switching output buffering off is similar in effect to
2227  * setting the std::ios_base::unitbuf flag, except that switching
2228  * buffering off is slightly more efficient, and setting the
2229  * std::ios_base::unitbuf flag will not retain the automatic tying of
2230  * logical and actual file positions that occurs when output buffering
2231  * is switched off, as explained @ref GioRandomAccessAnchor "here".
2232  * This class does not offer concurrent access from multiple threads
2233  * to the same stream object, and if that is required users should
2234  * provide their own synchronisation.
2235  *
2236  * @param buffered 'false' if buffering for output is to be turned
2237  * off, 'true' if it is to be turned back on.
2238  *
2239  * @exception std::bad_alloc This method will throw std::bad_alloc if
2240  * 'buffered' is true, output buffering had previously been switched
2241  * off, memory is exhausted and the system throws on such exhaustion
2242  * (unless the library has been installed using the
2243  * \--with-glib-memory-slices-compat or
2244  * \--with-glib-memory-slices-no-compat configuration option, in which
2245  * case glib will terminate the program if it is unable to obtain
2246  * memory from the operating system).
2247  *
2248  * Since 1.2.6
2249  */
2250  void set_output_buffered(bool buffered) {buf.set_output_buffered(buffered);}
2251 
2252 /**
2253  * This method indicates whether the attached GIO stream implements
2254  * GSeekable, so that a call to tellg(), tellp(), seekg() or seekp()
2255  * can succeed. Note that in the seekg(off_type off,
2256  * ios_base::seekdir dir) and seekp(off_type off, ios_base::seekdir
2257  * dir) variants, on wide character streams the 'off' argument is
2258  * dimensioned as the number of wchar_t units not the number of bytes
2259  * (that is, it is bytes/sizeof(char_type)). This method does not
2260  * throw. This class does not offer concurrent access from multiple
2261  * threads to the same stream object, and if that is required users
2262  * should provide their own synchronisation.
2263  *
2264  * @return true if the attached GIO stream implements GSeekable,
2265  * otherwise false. The result is only meaningful if a GIO stream has
2266  * been attached to this C++ stream object.
2267  *
2268  * Since 1.2.6
2269  */
2270  bool can_seek() const {return buf.can_seek();}
2271 
2272 /**
2273  * This method reports the error status of any attached GIO output
2274  * stream, and is intended to be called where failbit or badbit has
2275  * been set. It can be useful for interpreting conversion errors on
2276  * converting streams where one of those bits is set. This class does
2277  * not offer concurrent access from multiple threads to the same
2278  * stream object, and if that is required users should provide their
2279  * own synchronisation.
2280  *
2281  * @return NULL if no output stream is attached, or it is not in an
2282  * error state. If an attached output stream is in an error state,
2283  * say because it is a converting output stream which has encountered
2284  * a conversion error, the most recent GError object emitted by a
2285  * write operation on it is returned. Ownership of the return value
2286  * is retained, so if it is intended to be used after the next write
2287  * operation, it should be copied using g_error_copy().
2288  *
2289  * Since 1.2.19
2290  */
2291  GError* is_output_error() {return buf.is_output_error();}
2292 
2293 /**
2294  * This method reports the error status of any attached GIO input
2295  * stream, and is intended to be called where failbit has been set.
2296  * It can be useful for establishing, where that bit is set, whether
2297  * failbit indicates normal end-of-file or a conversion error on a
2298  * converting stream. This class does not offer concurrent access
2299  * from multiple threads to the same stream object, and if that is
2300  * required users should provide their own synchronisation.
2301  *
2302  * @return NULL if no input stream is attached, or it is not in an
2303  * error state. If an attached input stream is in an error state, say
2304  * because it is a converting input stream which has encountered a
2305  * conversion error, the most recent GError object emitted by a read
2306  * operation on it is returned. Ownership of the return value is
2307  * retained, so if it is intended to be used after the next read
2308  * operation, it should be copied using g_error_copy().
2309  *
2310  * Since 1.2.19
2311  */
2312  GError* is_input_error() {return buf.is_input_error();}
2313 
2314 /* Only has effect if --with-glib-memory-slices-compat or
2315  * --with-glib-memory-slices-no-compat option picked */
2317 };
2318 
2319 /**
2320  * @defgroup gstreams gstreams
2321  */
2322 /**
2323  * @typedef gstreambuf.
2324  * @brief C++ stream buffer for GIO streams for char type
2325  * @ingroup gstreams
2326  */
2328 
2329 /**
2330  * @typedef gistream.
2331  * @brief C++ input stream for GIO streams for char type
2332  * @anchor gistreamAnchor
2333  * @ingroup gstreams
2334  */
2336 
2337 /**
2338  * @typedef gostream.
2339  * @brief C++ output stream for GIO streams for char type
2340  * @anchor gostreamAnchor
2341  * @ingroup gstreams
2342  */
2344 
2345 /**
2346  * @typedef giostream.
2347  * @brief C++ input/output stream for GIO streams for char type
2348  * @anchor giostreamAnchor
2349  * @ingroup gstreams
2350  */
2352 
2353 /**
2354  * @typedef wgstreambuf.
2355  * @brief C++ stream buffer for GIO streams for wchar_t type
2356  * @ingroup gstreams
2357  */
2359 
2360 /**
2361  * @typedef wgistream.
2362  * @brief C++ input stream for GIO streams for wchar_t type
2363  * @anchor wgistreamAnchor
2364  * @ingroup gstreams
2365  */
2367 
2368 /**
2369  * @typedef wgostream.
2370  * @brief C++ output stream for GIO streams for wchar_t type
2371  * @anchor wgostreamAnchor
2372  * @ingroup gstreams
2373  */
2375 
2376 /**
2377  * @typedef wgiostream.
2378  * @brief C++ input/output stream for GIO streams for wchar_t type
2379  * @anchor wgiostreamAnchor
2380  * @ingroup gstreams
2381  */
2383 
2384 } // namespace Cgu
2385 
2386 #include <c++-gtk-utils/gstream.tpp>
2387 
2388 #else
2389 #warning gstreams are not available: glib >= 2.16.0 is required
2390 #endif /*GLIB_CHECK_VERSION(2,16,0)*/
2391 
2392 #endif /*CGU_GSTREAM_H*/
Cgu::basic_giostream::set_output_buffered
void set_output_buffered(bool buffered)
Definition: gstream.h:2250
Cgu::wgstreambuf
basic_gstreambuf< wchar_t > wgstreambuf
C++ stream buffer for GIO streams for wchar_t type.
Definition: gstream.h:2358
Cgu::basic_gstreambuf::~basic_gstreambuf
virtual ~basic_gstreambuf()
Cgu::basic_gostream
C++ output stream for GIO streams.
Definition: gstream.h:1365
Cgu
Definition: application.h:45
Cgu::gstreambuf
basic_gstreambuf< char > gstreambuf
C++ stream buffer for GIO streams for char type.
Definition: gstream.h:2327
Cgu::wgiostream
basic_giostream< wchar_t > wgiostream
C++ input/output stream for GIO streams for wchar_t type.
Definition: gstream.h:2382
Cgu::basic_giostream::is_output_error
GError * is_output_error()
Definition: gstream.h:2291
Cgu::basic_giostream::basic_giostream
basic_giostream(const GobjHandle< GIOStream > &stream, bool manage, const GobjHandle< GConverter > &input_converter=GobjHandle< GConverter >(), const GobjHandle< GConverter > &output_converter=GobjHandle< GConverter >())
Definition: gstream.h:2004
Cgu::basic_gstreambuf::seekoff
virtual pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode m=std::ios_base::in|std::ios_base::out)
Cgu::basic_giostream::close
void close()
Definition: gstream.h:2124
Cgu::GobjHandle< GInputStream >
Cgu::basic_gstreambuf::off_type
traits_type::off_type off_type
Definition: gstream.h:507
Cgu::wgostream
basic_gostream< wchar_t > wgostream
C++ output stream for GIO streams for wchar_t type.
Definition: gstream.h:2374
Cgu::basic_gstreambuf::is_output_error
GError * is_output_error()
Cgu::basic_gistream::basic_gistream
basic_gistream(const GobjHandle< GInputStream > &stream, bool manage, const GobjHandle< GConverter > &converter=GobjHandle< GConverter >())
Definition: gstream.h:1717
Cgu::basic_gistream
C++ input stream for GIO streams.
Definition: gstream.h:1651
Cgu::basic_gstreambuf::can_seek
bool can_seek() const
Definition: gstream.h:1310
Cgu::basic_giostream::attach
void attach(const GobjHandle< GIOStream > &stream, bool manage, const GobjHandle< GConverter > &input_converter=GobjHandle< GConverter >(), const GobjHandle< GConverter > &output_converter=GobjHandle< GConverter >())
Definition: gstream.h:2098
Cgu::basic_gstreambuf::xsgetn
virtual std::streamsize xsgetn(char_type *, std::streamsize)
Cgu::giostream
basic_giostream< char > giostream
C++ input/output stream for GIO streams for char type.
Definition: gstream.h:2351
Cgu::swap
void swap(Cgu::AsyncQueue< T, Container > &q1, Cgu::AsyncQueue< T, Container > &q2)
Definition: async_queue.h:784
Cgu::basic_gstreambuf::set_byteswap
void set_byteswap(bool swap)
Cgu::basic_gostream::is_error
GError * is_error()
Definition: gstream.h:1633
Cgu::basic_gistream::set_byteswap
void set_byteswap(bool swap)
Definition: gstream.h:1873
Cgu::GSliceFreeSize
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle,...
Definition: shared_handle.h:380
Cgu::basic_gstreambuf::close_stream
bool close_stream()
gobj_handle.h
Cgu::basic_gstreambuf::int_type
traits_type::int_type int_type
Definition: gstream.h:505
Cgu::basic_gistream::attach
void attach(const GobjHandle< GInputStream > &stream, bool manage, const GobjHandle< GConverter > &converter=GobjHandle< GConverter >())
Definition: gstream.h:1801
Cgu::basic_gostream::set_buffered
void set_buffered(bool buffered)
Definition: gstream.h:1593
Cgu::basic_giostream::basic_giostream
basic_giostream()
Definition: gstream.h:2023
Cgu::gostream
basic_gostream< char > gostream
C++ output stream for GIO streams for char type.
Definition: gstream.h:2343
Cgu::basic_gstreambuf::set_output_buffered
void set_output_buffered(bool buffered)
Cgu::basic_gstreambuf::get_istream
GobjHandle< GInputStream > get_istream() const
Cgu::basic_giostream
C++ input-output stream for GIO streams.
Definition: gstream.h:1931
Cgu::basic_giostream::get_gio_io_stream
GobjHandle< GIOStream > get_gio_io_stream() const
Definition: gstream.h:2141
Cgu::basic_giostream::is_input_error
GError * is_input_error()
Definition: gstream.h:2312
Cgu::basic_gstreambuf::underflow
virtual int_type underflow()
Cgu::basic_gstreambuf::overflow
virtual int_type overflow(int_type)
Cgu::basic_gstreambuf::get_iostream
GobjHandle< GIOStream > get_iostream() const
gerror_handle.h
Cgu::basic_gstreambuf::is_input_error
GError * is_input_error()
Cgu::basic_gostream::basic_gostream
basic_gostream()
Definition: gstream.h:1450
Cgu::basic_gostream::attach
void attach(const GobjHandle< GOutputStream > &stream, bool manage, const GobjHandle< GConverter > &converter=GobjHandle< GConverter >())
Definition: gstream.h:1516
Cgu::basic_gstreambuf::attach_stream
void attach_stream(const GobjHandle< GInputStream > &input_stream_, bool manage_, const GobjHandle< GConverter > &converter_=GobjHandle< GConverter >())
CGU_GLIB_MEMORY_SLICES_FUNCS
#define CGU_GLIB_MEMORY_SLICES_FUNCS
Definition: cgu_config.h:84
Cgu::basic_giostream::get_gio_output_stream
GobjHandle< GOutputStream > get_gio_output_stream() const
Definition: gstream.h:2161
shared_handle.h
Cgu::basic_gistream::get_gio_stream
GobjHandle< GInputStream > get_gio_stream() const
Definition: gstream.h:1845
Cgu::basic_gistream::close
void close()
Definition: gstream.h:1826
Cgu::basic_gistream::basic_gistream
basic_gistream()
Definition: gstream.h:1735
Cgu::basic_gostream::get_gio_stream
GobjHandle< GOutputStream > get_gio_stream() const
Definition: gstream.h:1560
Cgu::basic_giostream::get_gio_input_stream
GobjHandle< GInputStream > get_gio_input_stream() const
Definition: gstream.h:2181
Cgu::basic_gostream::close
void close()
Definition: gstream.h:1541
Cgu::basic_gstreambuf::seekpos
virtual pos_type seekpos(pos_type p, std::ios_base::openmode m=std::ios_base::in|std::ios_base::out)
Cgu::ScopedHandle
This is a generic scoped class for managing the lifetime of objects allocated on freestore.
Definition: shared_handle.h:411
Cgu::basic_gistream::can_seek
bool can_seek() const
Definition: gstream.h:1892
Cgu::basic_gstreambuf::sync
virtual int sync()
Cgu::basic_gstreambuf::pos_type
traits_type::pos_type pos_type
Definition: gstream.h:506
Cgu::basic_gstreambuf
C++ stream buffer for GIO streams.
Definition: gstream.h:500
Cgu::gistream
basic_gistream< char > gistream
C++ input stream for GIO streams for char type.
Definition: gstream.h:2335
Cgu::basic_giostream::can_seek
bool can_seek() const
Definition: gstream.h:2270
Cgu::basic_gstreambuf::xsputn
virtual std::streamsize xsputn(const char_type *, std::streamsize)
Cgu::basic_gostream::basic_gostream
basic_gostream(const GobjHandle< GOutputStream > &stream, bool manage, const GobjHandle< GConverter > &converter=GobjHandle< GConverter >())
Definition: gstream.h:1432
Cgu::basic_gstreambuf::basic_gstreambuf
basic_gstreambuf()
Cgu::wgistream
basic_gistream< wchar_t > wgistream
C++ input stream for GIO streams for wchar_t type.
Definition: gstream.h:2366
Cgu::basic_gstreambuf::get_ostream
GobjHandle< GOutputStream > get_ostream() const
Cgu::basic_giostream::set_byteswap
void set_byteswap(bool swap)
Definition: gstream.h:2213
Cgu::basic_gistream::is_error
GError * is_error()
Definition: gstream.h:1913
cgu_config.h
Cgu::basic_gstreambuf::char_type
charT char_type
Definition: gstream.h:503
Cgu::basic_gstreambuf::traits_type
Traits traits_type
Definition: gstream.h:504
Cgu::basic_gostream::can_seek
bool can_seek() const
Definition: gstream.h:1612