libcamera: libcamera::UniqueFD Class Reference

libcamera v0.7.0 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
libcamera::UniqueFD Class Referencefinal

unique_ptr-like wrapper for a file descriptor More...

## Public Member Functions
| UniqueFD ()
| Construct a UniqueFD that owns no file descriptor.
| UniqueFD (int fd)
| Construct a UniqueFD that owns fd.
| UniqueFD (UniqueFD &&other)
| Move constructor, create a UniqueFD by taking over other.
| ~UniqueFD ()
| Destroy the UniqueFD instance.
UniqueFD & | operator= (UniqueFD &&other)
| Move assignment operator, replace a UniqueFD by taking over other.
int | release ()
| Release ownership of the file descriptor without closing it.
void | reset (int fd=-1)
| Replace the managed file descriptor.
void | swap (UniqueFD &other)
| Swap the managed file descriptors with another UniqueFD.
int | get () const
| Retrieve the managed file descriptor.
bool | isValid () const
| Check if the UniqueFD owns a valid file descriptor.

## Detailed Description

unique_ptr-like wrapper for a file descriptor

The UniqueFD is a wrapper that owns and manages the lifetime of a file descriptor. It is constructed from a numerical file descriptor, and takes over its ownership. The file descriptor is closed when the UniqueFD is destroyed, or when it is assigned another file descriptor with operator=() or reset().

## Constructor & Destructor Documentation

## ◆ UniqueFD() [1/2]

libcamera::UniqueFD::UniqueFD | ( | int | fd | ) |
| inlineexplicit

Construct a UniqueFD that owns fd.

Parameters

  [in] | fd | A file descriptor to manage

## ◆ UniqueFD() [2/2]

libcamera::UniqueFD::UniqueFD | ( | UniqueFD && | other | ) |
| inline

Move constructor, create a UniqueFD by taking over other.

Parameters

  [in] | other | The other UniqueFD

Create a UniqueFD by transferring ownership of the file descriptor owned by other. Upon return, the other UniqueFD is invalid.

## ◆ ~UniqueFD()

libcamera::UniqueFD::~UniqueFD | ( |  | ) |
| inline

Destroy the UniqueFD instance.

If a file descriptor is owned, it is closed.

## Member Function Documentation

## ◆ get()

int libcamera::UniqueFD::get | ( |  | ) | const
| inline

Retrieve the managed file descriptor.

Returns
  The managed file descriptor, or -1 if no file descriptor is owned

## ◆ isValid()

bool libcamera::UniqueFD::isValid | ( |  | ) | const
| inline

Check if the UniqueFD owns a valid file descriptor.

Returns
  True if the UniqueFD owns a valid file descriptor, false otherwise

## ◆ operator=()

UniqueFD & libcamera::UniqueFD::operator= | ( | UniqueFD && | other | ) |
| inline

Move assignment operator, replace a UniqueFD by taking over other.

Parameters

  [in] | other | The other UniqueFD

If this UniqueFD owns a file descriptor, the file descriptor is closed first. The file descriptor is then replaced by the one of other. Upon return, other is invalid.

Returns
  A reference to this UniqueFD

## ◆ release()

int libcamera::UniqueFD::release | ( |  | ) |
| inlinenodiscard

Release ownership of the file descriptor without closing it.

This function releases and returns the owned file descriptor without closing it. The caller owns the returned value and must take care of handling its life time to avoid file descriptor leakages. Upon return this UniqueFD is invalid.

Returns
  The managed file descriptor, or -1 if no file descriptor was owned

## ◆ reset()

void libcamera::UniqueFD::reset | ( | int | fd = -1 | ) |

Replace the managed file descriptor.

Parameters

  [in] | fd | The new file descriptor to manage

Close the managed file descriptor, if any, and replace it with the new fd.

Self-resetting (passing an fd already managed by this instance) is invalid and results in undefined behaviour.

## ◆ swap()

void libcamera::UniqueFD::swap | ( | UniqueFD & | other | ) |
| inline

Swap the managed file descriptors with another UniqueFD.

Parameters

  [in] | other | Another UniqueFD to swap the file descriptor with

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

- include/libcamera/base/unique_fd.h
- src/libcamera/base/unique_fd.cpp