mirror of
https://git.libcamera.org/libcamera/libcamera.git
synced 2025-07-24 17:15:07 +03:00
libcamera: base: Introduce UniqueFD
This introduces UniqueFD. It acts like unique_ptr to a file descriptor. Signed-off-by: Hirokazu Honda <hiroh@chromium.org> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Hirokazu Honda <hiroh@chromium.org>
This commit is contained in:
parent
1546a74e64
commit
ccdaf8ec90
4 changed files with 194 additions and 0 deletions
|
@ -24,6 +24,7 @@ libcamera_base_headers = files([
|
|||
'thread.h',
|
||||
'thread_annotations.h',
|
||||
'timer.h',
|
||||
'unique_fd.h',
|
||||
'utils.h',
|
||||
])
|
||||
|
||||
|
|
69
include/libcamera/base/unique_fd.h
Normal file
69
include/libcamera/base/unique_fd.h
Normal file
|
@ -0,0 +1,69 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
/*
|
||||
* Copyright (C) 2021, Google Inc.
|
||||
*
|
||||
* unique_fd.h - File descriptor wrapper that owns a file descriptor.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include <libcamera/base/class.h>
|
||||
#include <libcamera/base/compiler.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 */
|
Loading…
Add table
Add a link
Reference in a new issue