summaryrefslogtreecommitdiff
path: root/include/libcamera/base/shared_fd.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/libcamera/base/shared_fd.h')
-rw-r--r--include/libcamera/base/shared_fd.h49
1 files changed, 49 insertions, 0 deletions
diff --git a/include/libcamera/base/shared_fd.h b/include/libcamera/base/shared_fd.h
new file mode 100644
index 00000000..a786885c
--- /dev/null
+++ b/include/libcamera/base/shared_fd.h
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2019, Google Inc.
+ *
+ * shared_fd.h - File descriptor wrapper with shared ownership
+ */
+
+#pragma once
+
+#include <memory>
+
+namespace libcamera {
+
+class UniqueFD;
+
+class SharedFD final
+{
+public:
+ explicit SharedFD(const int &fd = -1);
+ explicit SharedFD(int &&fd);
+ explicit SharedFD(UniqueFD fd);
+ SharedFD(const SharedFD &other);
+ SharedFD(SharedFD &&other);
+ ~SharedFD();
+
+ SharedFD &operator=(const SharedFD &other);
+ SharedFD &operator=(SharedFD &&other);
+
+ bool isValid() const { return fd_ != nullptr; }
+ int fd() const { return fd_ ? fd_->fd() : -1; }
+ UniqueFD dup() const;
+
+private:
+ class Descriptor
+ {
+ public:
+ Descriptor(int fd, bool duplicate);
+ ~Descriptor();
+
+ int fd() const { return fd_; }
+
+ private:
+ int fd_;
+ };
+
+ std::shared_ptr<Descriptor> fd_;
+};
+
+} /* namespace libcamera */