summaryrefslogtreecommitdiff
path: root/include/libcamera/file_descriptor.h
diff options
context:
space:
mode:
authorNiklas Söderlund <niklas.soderlund@ragnatech.se>2019-11-21 13:10:45 +0100
committerNiklas Söderlund <niklas.soderlund@ragnatech.se>2020-01-12 16:10:37 +0100
commit01ba6e39b67f36a4fa0ba3f7d3990b555463a507 (patch)
treea733bfce34602b644a63831e2728627e8f95f647 /include/libcamera/file_descriptor.h
parent856a4a25278ce5e4b64497deaa3b20c4df16fdee (diff)
libcamera: Add FileDescriptor to help pass numerical fds around
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 <niklas.soderlund@ragnatech.se> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'include/libcamera/file_descriptor.h')
-rw-r--r--include/libcamera/file_descriptor.h47
1 files changed, 47 insertions, 0 deletions
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 <memory>
+
+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<Descriptor> fd_;
+};
+
+} /* namespace libcamera */
+
+#endif /* __LIBCAMERA_FILE_DESCRIPTOR_H__ */