From 01ba6e39b67f36a4fa0ba3f7d3990b555463a507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20S=C3=B6derlund?= Date: Thu, 21 Nov 2019 13:10:45 +0100 Subject: libcamera: Add FileDescriptor to help pass numerical fds around MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a helper to make it easier to pass file descriptors around. The helper class duplicates the fd which decouples it from the original fd which could be closed by its owner while the new FileDescriptor remains valid. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- include/libcamera/file_descriptor.h | 47 +++++++++++++++++++++++++++++++++++++ include/libcamera/meson.build | 1 + 2 files changed, 48 insertions(+) create mode 100644 include/libcamera/file_descriptor.h (limited to 'include') diff --git a/include/libcamera/file_descriptor.h b/include/libcamera/file_descriptor.h new file mode 100644 index 00000000..8612f865 --- /dev/null +++ b/include/libcamera/file_descriptor.h @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * file_descriptor.h - File descriptor wrapper + */ +#ifndef __LIBCAMERA_FILE_DESCRIPTOR_H__ +#define __LIBCAMERA_FILE_DESCRIPTOR_H__ + +#include + +namespace libcamera { + +class FileDescriptor final +{ +public: + explicit FileDescriptor(int fd = -1); + FileDescriptor(const FileDescriptor &other); + FileDescriptor(FileDescriptor &&other); + ~FileDescriptor(); + + FileDescriptor &operator=(const FileDescriptor &other); + FileDescriptor &operator=(FileDescriptor &&other); + + bool isValid() const { return fd_ != nullptr; } + int fd() const { return fd_ ? fd_->fd() : -1; } + FileDescriptor dup() const; + +private: + class Descriptor + { + public: + Descriptor(int fd); + ~Descriptor(); + + int fd() const { return fd_; } + + private: + int fd_; + }; + + std::shared_ptr fd_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_FILE_DESCRIPTOR_H__ */ diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index 99abf060..543e6773 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -6,6 +6,7 @@ libcamera_api = files([ 'controls.h', 'event_dispatcher.h', 'event_notifier.h', + 'file_descriptor.h', 'geometry.h', 'logging.h', 'object.h', -- cgit v1.2.1