libcamera
v0.0.0+3310-4b24b0bf
Supporting cameras in Linux since 2019
|
RAII-style wrapper for file descriptors. More...
Public Member Functions | |
FileDescriptor (const int &fd=-1) | |
Create a FileDescriptor copying a given fd. More... | |
FileDescriptor (int &&fd) | |
Create a FileDescriptor taking ownership of a given fd. More... | |
FileDescriptor (const FileDescriptor &other) | |
Copy constructor, create a FileDescriptor from a copy of other. More... | |
FileDescriptor (FileDescriptor &&other) | |
Move constructor, create a FileDescriptor by taking over other. More... | |
~FileDescriptor () | |
Destroy the FileDescriptor instance. More... | |
FileDescriptor & | operator= (const FileDescriptor &other) |
Copy assignment operator, replace the wrapped file descriptor with a copy of other. More... | |
FileDescriptor & | operator= (FileDescriptor &&other) |
Move assignment operator, replace the wrapped file descriptor by taking over other. More... | |
bool | isValid () const |
Check if the FileDescriptor instance is valid. More... | |
int | fd () const |
Retrieve the numerical file descriptor. More... | |
FileDescriptor | dup () const |
Duplicate a FileDescriptor. More... | |
ino_t | inode () const |
Retrieve the file descriptor inode. More... | |
RAII-style wrapper for file descriptors.
The FileDescriptor class provides RAII-style lifetime management of file descriptors with an efficient mechanism for ownership sharing. At its core, an internal Descriptor object wraps a file descriptor (expressed as a signed integer) with an RAII-style interface. The Descriptor is then implicitly shared with all FileDescriptor instances constructed as copies.
When constructed from a numerical file descriptor, the FileDescriptor instance either duplicates or takes over the file descriptor:
The copy constructor and assignment operator create copies that share the Descriptor, while the move versions of those functions additionally make the other FileDescriptor invalid. When the last FileDescriptor that references a Descriptor is destroyed, the file descriptor is closed.
The numerical file descriptor is available through the fd() function. All FileDescriptor instances created as copies of a FileDescriptor will report the same fd() value. Callers can perform operations on the fd(), but shall never close it manually.
|
explicit |
Create a FileDescriptor copying a given fd.
[in] | fd | File descriptor |
Construct a FileDescriptor from a numerical file descriptor by duplicating the fd, and take ownership of the copy. The original fd is left untouched, and the caller is responsible for closing it when appropriate. The duplicated file descriptor will be closed automatically when all FileDescriptor instances that reference it are destroyed.
If the fd is negative, the FileDescriptor is constructed as invalid and the fd() function will return -1.
|
explicit |
Create a FileDescriptor taking ownership of a given fd.
[in] | fd | File descriptor |
Construct a FileDescriptor from a numerical file descriptor by taking ownership of the fd. The original fd is set to -1 and shall not be touched by the caller anymore. In particular, the caller shall not close the original fd manually. The duplicated file descriptor will be closed automatically when all FileDescriptor instances that reference it are destroyed.
If the fd is negative, the FileDescriptor is constructed as invalid and the fd() function will return -1.
libcamera::FileDescriptor::FileDescriptor | ( | const FileDescriptor & | other | ) |
Copy constructor, create a FileDescriptor from a copy of other.
[in] | other | The other FileDescriptor |
Copying a FileDescriptor implicitly shares ownership of the wrapped file descriptor. The original FileDescriptor is left untouched, and the caller is responsible for destroying it when appropriate. The wrapped file descriptor will be closed automatically when all FileDescriptor instances that reference it are destroyed.
libcamera::FileDescriptor::FileDescriptor | ( | FileDescriptor && | other | ) |
Move constructor, create a FileDescriptor by taking over other.
[in] | other | The other FileDescriptor |
Moving a FileDescriptor moves the reference to the wrapped descriptor owned by other to the new FileDescriptor. The other FileDescriptor is invalidated and its fd() function will return -1. The wrapped file descriptor will be closed automatically when all FileDescriptor instances that reference it are destroyed.
libcamera::FileDescriptor::~FileDescriptor | ( | ) |
Destroy the FileDescriptor instance.
Destroying a FileDescriptor instance releases its reference to the wrapped descriptor, if any. When the last instance that references a wrapped descriptor is destroyed, the file descriptor is automatically closed.
FileDescriptor libcamera::FileDescriptor::dup | ( | ) | const |
Duplicate a FileDescriptor.
Duplicating a FileDescriptor creates a duplicate of the wrapped file descriptor and returns a new FileDescriptor instance that wraps the duplicate. The fd() function of the original and duplicate instances will return different values. The duplicate instance will not be affected by destruction of the original instance or its copies.
|
inline |
Retrieve the numerical file descriptor.
ino_t libcamera::FileDescriptor::inode | ( | ) | const |
|
inline |
Check if the FileDescriptor instance is valid.
FileDescriptor & libcamera::FileDescriptor::operator= | ( | const FileDescriptor & | other | ) |
Copy assignment operator, replace the wrapped file descriptor with a copy of other.
[in] | other | The other FileDescriptor |
Copying a FileDescriptor creates a new reference to the wrapped file descriptor owner by other. If other is invalid, *this will also be invalid. The original FileDescriptor is left untouched, and the caller is responsible for destroying it when appropriate. The wrapped file descriptor will be closed automatically when all FileDescriptor instances that reference it are destroyed.
FileDescriptor & libcamera::FileDescriptor::operator= | ( | FileDescriptor && | other | ) |
Move assignment operator, replace the wrapped file descriptor by taking over other.
[in] | other | The other FileDescriptor |
Moving a FileDescriptor moves the reference to the wrapped descriptor owned by other to the new FileDescriptor. If other is invalid, *this will also be invalid. The other FileDescriptor is invalidated and its fd() function will return -1. The wrapped file descriptor will be closed automatically when all FileDescriptor instances that reference it are destroyed.