libcamera  v0.2.0+150-2031e2f2
Supporting cameras in Linux since 2019
Public Member Functions | List of all members
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. More...
 
 UniqueFD (UniqueFD &&other)
 Move constructor, create a UniqueFD by taking over other. More...
 
 ~UniqueFD ()
 Destroy the UniqueFD instance. More...
 
UniqueFDoperator= (UniqueFD &&other)
 Move assignment operator, replace a UniqueFD by taking over other. More...
 
__nodiscard int release ()
 Release ownership of the file descriptor without closing it. More...
 
void reset (int fd=-1)
 Replace the managed file descriptor. More...
 
void swap (UniqueFD &other)
 Swap the managed file descriptors with another UniqueFD. More...
 
int get () const
 Retrieve the managed file descriptor. More...
 
bool isValid () const
 Check if the UniqueFD owns a valid file descriptor. More...
 

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]fdA file descriptor to manage

◆ UniqueFD() [2/2]

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

Move constructor, create a UniqueFD by taking over other.

Parameters
[in]otherThe 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()

libcamera::UniqueFD::get ( ) const
inline

Retrieve the managed file descriptor.

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

◆ isValid()

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=()

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

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

Parameters
[in]otherThe 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()

libcamera::UniqueFD::release ( )
inline

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]fdThe 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()

libcamera::UniqueFD::swap ( UniqueFD other)
inline

Swap the managed file descriptors with another UniqueFD.

Parameters
[in]otherAnother UniqueFD to swap the file descriptor with

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