libcamera v0.1.0+127-8e215127-dirty (2023-12-02T01:06:12+00:00)
Supporting cameras in Linux since 2019
shared_fd.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2019, Google Inc.
4 *
5 * shared_fd.h - File descriptor wrapper with shared ownership
6 */
7
8#pragma once
9
10#include <memory>
11
12namespace libcamera {
13
14class UniqueFD;
15
16class SharedFD final
17{
18public:
19 explicit SharedFD(const int &fd = -1);
20 explicit SharedFD(int &&fd);
21 explicit SharedFD(UniqueFD fd);
22 SharedFD(const SharedFD &other);
23 SharedFD(SharedFD &&other);
24 ~SharedFD();
25
26 SharedFD &operator=(const SharedFD &other);
27 SharedFD &operator=(SharedFD &&other);
28
29 bool isValid() const { return fd_ != nullptr; }
30 int get() const { return fd_ ? fd_->fd() : -1; }
31 UniqueFD dup() const;
32
33private:
34 class Descriptor
35 {
36 public:
37 Descriptor(int fd, bool duplicate);
38 ~Descriptor();
39
40 int fd() const { return fd_; }
41
42 private:
43 int fd_;
44 };
45
46 std::shared_ptr<Descriptor> fd_;
47};
48
49static inline bool operator==(const SharedFD &lhs, const SharedFD &rhs)
50{
51 return lhs.get() == rhs.get();
52}
53
54static inline bool operator!=(const SharedFD &lhs, const SharedFD &rhs)
55{
56 return !(lhs == rhs);
57}
58
59} /* namespace libcamera */
RAII-style wrapper for file descriptors.
Definition: shared_fd.h:17
~SharedFD()
Destroy the SharedFD instance.
Definition: shared_fd.cpp:160
UniqueFD dup() const
Duplicate a SharedFD.
Definition: shared_fd.cpp:254
int get() const
Retrieve the numerical file descriptor.
Definition: shared_fd.h:30
SharedFD(const int &fd=-1)
Create a SharedFD copying a given fd.
Definition: shared_fd.cpp:75
bool isValid() const
Check if the SharedFD instance is valid.
Definition: shared_fd.h:29
SharedFD & operator=(const SharedFD &other)
Copy assignment operator, replace the wrapped file descriptor with a copy of other.
Definition: shared_fd.cpp:177
unique_ptr-like wrapper for a file descriptor
Definition: unique_fd.h:18
Top-level libcamera namespace.
Definition: backtrace.h:17