Passenger::FileDescriptor Class Reference

Wrapper class around a file descriptor integer, for RAII behavior. More...

#include <FileDescriptor.h>

List of all members.

Public Member Functions

 FileDescriptor ()
 Creates a new empty FileDescriptor instance that has no underlying file descriptor.
 FileDescriptor (int fd)
 Creates a new FileDescriptor instance with the given fd as a handle.
void close (bool checkErrors=true)
 Close the underlying file descriptor.
int detach ()
 Detach from the underlying file descriptor without closing it.
 operator int () const
 Overloads the integer cast operator so that it will return the underlying file descriptor handle as an integer.

Detailed Description

Wrapper class around a file descriptor integer, for RAII behavior.

A FileDescriptor object behaves just like an int, so that you can pass it to system calls such as read(). It performs reference counting. When the last copy of a FileDescriptor has been destroyed, the underlying file descriptor will be automatically closed. In this case, any close() system call errors are silently ignored. If you are interested in whether the close() system call succeeded, then you should call FileDescriptor::close().

This class is *not* thread-safe. It is safe to call system calls on the underlying file descriptor from multiple threads, but it's not safe to call FileDescriptor::close() from multiple threads if all those FileDescriptor objects point to the same underlying file descriptor.


Constructor & Destructor Documentation

Passenger::FileDescriptor::FileDescriptor (  )  [inline]

Creates a new empty FileDescriptor instance that has no underlying file descriptor.

Postcondition:
*this == -1
Passenger::FileDescriptor::FileDescriptor ( int  fd  )  [inline]

Creates a new FileDescriptor instance with the given fd as a handle.

Postcondition:
*this == fd

Member Function Documentation

void Passenger::FileDescriptor::close ( bool  checkErrors = true  )  [inline]

Close the underlying file descriptor.

If it was already closed, then nothing will happen. If there are multiple copies of this FileDescriptor then the underlying file descriptor will be closed for every one of them.

checkErrors Whether a SystemException should be thrown in case closing the file descriptor fails. If false, errors are silently ignored.

Exceptions:
SystemException Something went wrong while closing the file descriptor. Only thrown if checkErrors is true.
Postcondition:
*this == -1
int Passenger::FileDescriptor::detach (  )  [inline]

Detach from the underlying file descriptor without closing it.

This FileDescriptor and all copies will no longer affect the underlying file descriptors.

Returns:
The underlying file descriptor, or -1 if already closed.
Postcondition:
*this == -1
Passenger::FileDescriptor::operator int (  )  const [inline]

Overloads the integer cast operator so that it will return the underlying file descriptor handle as an integer.

Returns -1 if FileDescriptor::close() was called.


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

Generated by  doxygen 1.6.2