From ccdaf8ec9012b060b9cc49f5cdaa99b474c54e27 Mon Sep 17 00:00:00 2001 From: Hirokazu Honda Date: Thu, 10 Jun 2021 16:50:18 +0900 Subject: libcamera: base: Introduce UniqueFD This introduces UniqueFD. It acts like unique_ptr to a file descriptor. Signed-off-by: Hirokazu Honda Signed-off-by: Laurent Pinchart Reviewed-by: Hirokazu Honda --- include/libcamera/base/meson.build | 1 + include/libcamera/base/unique_fd.h | 69 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 include/libcamera/base/unique_fd.h (limited to 'include') diff --git a/include/libcamera/base/meson.build b/include/libcamera/base/meson.build index abd4cfc7..fce6eaa4 100644 --- a/include/libcamera/base/meson.build +++ b/include/libcamera/base/meson.build @@ -24,6 +24,7 @@ libcamera_base_headers = files([ 'thread.h', 'thread_annotations.h', 'timer.h', + 'unique_fd.h', 'utils.h', ]) diff --git a/include/libcamera/base/unique_fd.h b/include/libcamera/base/unique_fd.h new file mode 100644 index 00000000..ae4d96b7 --- /dev/null +++ b/include/libcamera/base/unique_fd.h @@ -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 + +#include +#include + +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 */ -- cgit v1.2.1