c++-gtk-utils
Public Member Functions | List of all members
Cgu::basic_fdostream< charT, Traits > Class Template Reference

Output stream for unix file descriptors. More...

#include <c++-gtk-utils/fdstream.h>

Inheritance diagram for Cgu::basic_fdostream< charT, Traits >:

Public Member Functions

 basic_fdostream (const basic_fdostream &)=delete
 
basic_fdostreamoperator= (const basic_fdostream &)=delete
 
 basic_fdostream (int fd, bool manage=true)
 
 basic_fdostream ()
 
void attach (int fd, bool manage=true)
 
void close ()
 
int filedesc () const
 
void set_buffered (bool buffered)
 
bool can_seek () const
 

Detailed Description

template<class charT, class Traits = std::char_traits<charT>>
class Cgu::basic_fdostream< charT, Traits >

Output stream for unix file descriptors.

See also
fdstreams

This class provides standard ostream services for unix file descriptors.

Constructor & Destructor Documentation

◆ basic_fdostream() [1/3]

template<class charT , class Traits = std::char_traits<charT>>
Cgu::basic_fdostream< charT, Traits >::basic_fdostream ( const basic_fdostream< charT, Traits > &  )
delete

This class cannot be copied. The copy constructor is deleted.

◆ basic_fdostream() [2/3]

template<class charT , class Traits = std::char_traits<charT>>
Cgu::basic_fdostream< charT, Traits >::basic_fdostream ( int  fd,
bool  manage = true 
)
inline

This is the constructor which passes a file descriptor. fdstreams do not offer concurrent access from multiple threads to the same stream object, and if that is required users should provide their own synchronisation.

Parameters
fdThe file descriptor to be attached to the stream object.
manageWhether the stream should manage the file descriptor (that is, close it in its destructor or when a new file descriptor is attached).
Exceptions
std::bad_allocThis constructor will throw std::bad_alloc if fd >= 0, memory is exhausted and the system throws on such exhaustion (unless the library has been installed using the --with-glib-memory-slices-compat or --with-glib-memory-slices-no-compat configuration option, in which case glib will terminate the program if it is unable to obtain memory from the operating system). No other exception will be thrown unless the constructor of std::basic_streambuf or std::basic_ostream throws.

◆ basic_fdostream() [3/3]

template<class charT , class Traits = std::char_traits<charT>>
Cgu::basic_fdostream< charT, Traits >::basic_fdostream ( )
inline

With this constructor, the file descriptor must be attached later with the attach() method. It will not throw unless the default constructor of std::basic_streambuf or std::basic_ostream throws. fdstreams do not offer concurrent access from multiple threads to the same stream object, and if that is required users should provide their own synchronisation.

Member Function Documentation

◆ attach()

template<class charT , class Traits = std::char_traits<charT>>
void Cgu::basic_fdostream< charT, Traits >::attach ( int  fd,
bool  manage = true 
)
inline

Attach a new file descriptor to the stream object (and close any file descriptor at present managed by it). From version 1.2.6, if output buffering was previously switched off, it is switched back on again. Also from version 1.2.6, if any stream state flags were set (eofbit, failbit or badbit), they will be cleared by a call to clear() (prior to that version, the user had to call clear() explicitly to do so). If this method closes a file descriptor at present managed by it and the close fails, failbit is not set and no exception will be thrown. Accordingly, if the user needs to know whether there was an error in this method closing any descriptor, she should call close() explicitly before calling this method. fdstreams do not offer concurrent access from multiple threads to the same stream object, and if that is required users should provide their own synchronisation.

Parameters
fdThe new file descriptor to be attached to the stream object.
manageWhether the stream object should manage the new file descriptor (that is, close it in its destructor or when a further file descriptor is attached).
Exceptions
std::bad_allocThis method will throw std::bad_alloc if fd >= 0, output buffering had previously been switched off, memory is exhausted and the system throws on such exhaustion (unless the library has been installed using the --with-glib-memory-slices-compat or --with-glib-memory-slices-no-compat configuration option, in which case glib will terminate the program if it is unable to obtain memory from the operating system).

◆ can_seek()

template<class charT , class Traits = std::char_traits<charT>>
bool Cgu::basic_fdostream< charT, Traits >::can_seek ( ) const
inline

This method indicates whether the output device concerned supports random access, so that a call to tellp() or seekp() can succeed. Note that in the seekp(off_type off, ios_base::seekdir dir) variant, on wide character streams the 'off' argument is dimensioned as the number of wchar_t units not the number of bytes (that is, it is bytes/sizeof(char_type)). This method does not throw. fdstreams do not offer concurrent access from multiple threads to the same stream object, and if that is required users should provide their own synchronisation.

Returns
true if random access is supported, otherwise false. The result is only meaningful if a file descriptor has been attached to this stream.

◆ close()

template<class charT , class Traits = std::char_traits<charT>>
void Cgu::basic_fdostream< charT, Traits >::close ( )
inline

Close the file descriptor at present attached to the stream object (if any). From version 1.2.6, if the close fails, the failbit will be set with setstate(std::ios_base::failbit). fdstreams do not offer concurrent access from multiple threads to the same stream object, and if that is required users should provide their own synchronisation.

Exceptions
std::ios_base::failureFrom version 1.2.6, this exception will be thrown if an error arises on closing the descriptor and such an exception has been required by a call to the exceptions() method of this class (inherited from std::basic_ios<>). No exception will be thrown if exceptions() has not been called.

◆ filedesc()

template<class charT , class Traits = std::char_traits<charT>>
int Cgu::basic_fdostream< charT, Traits >::filedesc ( ) const
inline

Get the file descriptor at present attached to the stream object (if any). This method does not throw. fdstreams do not offer concurrent access from multiple threads to the same stream object, and if that is required users should provide their own synchronisation.

Returns
The file descriptor at present attached to the stream object, or -1 if none has been attached

◆ operator=()

template<class charT , class Traits = std::char_traits<charT>>
basic_fdostream& Cgu::basic_fdostream< charT, Traits >::operator= ( const basic_fdostream< charT, Traits > &  )
delete

This class cannot be copied. The copy assignment operator is deleted.

◆ set_buffered()

template<class charT , class Traits = std::char_traits<charT>>
void Cgu::basic_fdostream< charT, Traits >::set_buffered ( bool  buffered)
inline

Stops output buffering if 'buffered' is false, or reverts to buffering if buffering has previously been switched off and 'buffered' is true. Buffering is on by default for any newly created fdostream object and any newly attached file descriptor. If buffering is turned off, all characters at present in the buffers which are stored for output are flushed. This method has no effect if no file descriptor has yet been attached. Switching output buffering off is similar in effect to setting the std::ios_base::unitbuf flag, but is slightly more efficient. fdstreams do not offer concurrent access from multiple threads to the same stream object, and if that is required users should provide their own synchronisation.

Parameters
buffered'false' if buffering is to be turned off, 'true' if it is to be turned back on.
Exceptions
std::bad_allocThis method will throw std::bad_alloc if 'buffered' is true, output buffering had previously been switched off, memory is exhausted and the system throws on such exhaustion (unless the library has been installed using the --with-glib-memory-slices-compat or --with-glib-memory-slices-no-compat configuration option, in which case glib will terminate the program if it is unable to obtain memory from the operating system).

The documentation for this class was generated from the following file: