libcamera: include/libcamera/base/shared_fd.h Source File

libcamera v0.7.2 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
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 * File descriptor wrapper with shared ownership
6 */
7
8#pragma once
9
10#include <memory>
11
12namespace libcamera {
13
14class UniqueFD;
15
16 class 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 */
libcamera::SharedFD
RAII-style wrapper for file descriptors.
Definition shared_fd.h:17
libcamera::SharedFD::dup
UniqueFD dup() const
Duplicate a SharedFD.
Definition shared_fd.cpp:254
libcamera::SharedFD::get
int get() const
Retrieve the numerical file descriptor.
Definition shared_fd.h:30
libcamera::SharedFD::SharedFD
SharedFD(const int &fd=-1)
Create a SharedFD copying a given fd.
Definition shared_fd.cpp:75
libcamera::SharedFD::isValid
bool isValid() const
Check if the SharedFD instance is valid.
Definition shared_fd.h:29
libcamera::SharedFD::operator=
SharedFD & operator=(const SharedFD &other)
Copy assignment operator, replace the wrapped file descriptor with a copy of other.
Definition shared_fd.cpp:177
libcamera::UniqueFD
unique_ptr-like wrapper for a file descriptor
Definition unique_fd.h:17
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17