Loading...
Searching...
No Matches
unique_fd.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2021, Google Inc.
*
* File descriptor wrapper that owns a file descriptor.
*/
#pragma once
#include <utility>
#include <libcamera/base/class.h>
namespace libcamera {
class UniqueFD final
{
public:
UniqueFD()
: fd_(-1)
{
}
explicit UniqueFD(int fd)
: fd_(fd)
{
}
UniqueFD(UniqueFD &&other)
: fd_(other.release())
{
}
~UniqueFD()
{
reset();
}
UniqueFD &operator=(UniqueFD &&other)
{
reset(other.release());
return *this;
}
[[nodiscard]] int release()
{
int fd = fd_;
fd_ = -1;
return fd;
}
void reset(int fd = -1);
void swap(UniqueFD &other)
{
std::swap(fd_, other.fd_);
}
int get() const { return fd_; }
bool isValid() const { return fd_ >= 0; }
private:
LIBCAMERA_DISABLE_COPY(UniqueFD)
int fd_;
};
} /* namespace libcamera */
libcamera::UniqueFD
unique_ptr-like wrapper for a file descriptor
Definition unique_fd.h:17
libcamera::UniqueFD::release
int release()
Release ownership of the file descriptor without closing it.
Definition unique_fd.h:45
libcamera::UniqueFD::operator=
UniqueFD & operator=(UniqueFD &&other)
Move assignment operator, replace a UniqueFD by taking over other.
Definition unique_fd.h:39
libcamera::UniqueFD::~UniqueFD
~UniqueFD()
Destroy the UniqueFD instance.
Definition unique_fd.h:34
libcamera::UniqueFD::swap
void swap(UniqueFD &other)
Swap the managed file descriptors with another UniqueFD.
Definition unique_fd.h:54
libcamera::UniqueFD::UniqueFD
UniqueFD()
Construct a UniqueFD that owns no file descriptor.
Definition unique_fd.h:19
libcamera::UniqueFD::UniqueFD
UniqueFD(UniqueFD &&other)
Move constructor, create a UniqueFD by taking over other.
Definition unique_fd.h:29
libcamera::UniqueFD::UniqueFD
UniqueFD(int fd)
Construct a UniqueFD that owns fd.
Definition unique_fd.h:24
libcamera::UniqueFD::isValid
bool isValid() const
Check if the UniqueFD owns a valid file descriptor.
Definition unique_fd.h:60
libcamera::UniqueFD::get
int get() const
Retrieve the managed file descriptor.
Definition unique_fd.h:59
libcamera::UniqueFD::reset
void reset(int fd=-1)
Replace the managed file descriptor.
Definition unique_fd.cpp:96
libcamera
Top-level libcamera namespace.
Definition bound_method.h:15