libcamera  v0.3.2+116-83c5ad0f
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  * File descriptor wrapper with shared ownership
6  */
7 
8 #pragma once
9 
10 #include <memory>
11 
12 namespace libcamera {
13 
14 class UniqueFD;
15 
16 class SharedFD final
17 {
18 public:
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 
33 private:
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 
49 static inline bool operator==(const SharedFD &lhs, const SharedFD &rhs)
50 {
51  return lhs.get() == rhs.get();
52 }
53 
54 static 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:16
~SharedFD()
Destroy the SharedFD instance.
Definition: shared_fd.cpp:160
UniqueFD dup() const
Duplicate a SharedFD.
Definition: shared_fd.cpp:254
Top-level libcamera namespace.
Definition: bound_method.h:15
unique_ptr-like wrapper for a file descriptor
Definition: unique_fd.h:17
bool isValid() const
Check if the SharedFD instance is valid.
Definition: shared_fd.h:29
SharedFD(const int &fd=-1)
Create a SharedFD copying a given fd.
Definition: shared_fd.cpp:75
int get() const
Retrieve the numerical file descriptor.
Definition: shared_fd.h:30
SharedFD & operator=(const SharedFD &other)
Copy assignment operator, replace the wrapped file descriptor with a copy of other.
Definition: shared_fd.cpp:177