libcamera  v0.4.0+85-d476f835
Supporting cameras in Linux since 2019
unique_fd.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * Copyright (C) 2021, Google Inc.
4  *
5  * File descriptor wrapper that owns a file descriptor.
6  */
7 
8 #pragma once
9 
10 #include <utility>
11 
12 #include <libcamera/base/class.h>
13 
14 namespace libcamera {
15 
16 class UniqueFD final
17 {
18 public:
20  : fd_(-1)
21  {
22  }
23 
24  explicit UniqueFD(int fd)
25  : fd_(fd)
26  {
27  }
28 
29  UniqueFD(UniqueFD &&other)
30  : fd_(other.release())
31  {
32  }
33 
35  {
36  reset();
37  }
38 
40  {
41  reset(other.release());
42  return *this;
43  }
44 
45  [[nodiscard]] int release()
46  {
47  int fd = fd_;
48  fd_ = -1;
49  return fd;
50  }
51 
52  void reset(int fd = -1);
53 
54  void swap(UniqueFD &other)
55  {
56  std::swap(fd_, other.fd_);
57  }
58 
59  int get() const { return fd_; }
60  bool isValid() const { return fd_ >= 0; }
61 
62 private:
64 
65  int fd_;
66 };
67 
68 } /* namespace libcamera */
UniqueFD(UniqueFD &&other)
Move constructor, create a UniqueFD by taking over other.
Definition: unique_fd.h:29
Utilities to help constructing class interfaces.
UniqueFD()
Construct a UniqueFD that owns no file descriptor.
Definition: unique_fd.h:19
void swap(UniqueFD &other)
Swap the managed file descriptors with another UniqueFD.
Definition: unique_fd.h:54
Top-level libcamera namespace.
Definition: backtrace.h:17
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
unique_ptr-like wrapper for a file descriptor
Definition: unique_fd.h:16
UniqueFD(int fd)
Construct a UniqueFD that owns fd.
Definition: unique_fd.h:24
bool isValid() const
Check if the UniqueFD owns a valid file descriptor.
Definition: unique_fd.h:60
void reset(int fd=-1)
Replace the managed file descriptor.
Definition: unique_fd.cpp:95
UniqueFD & operator=(UniqueFD &&other)
Move assignment operator, replace a UniqueFD by taking over other.
Definition: unique_fd.h:39
int release()
Release ownership of the file descriptor without closing it.
Definition: unique_fd.h:45
~UniqueFD()
Destroy the UniqueFD instance.
Definition: unique_fd.h:34