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

libcamera v0.0.4 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
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 * unique_fd.h - 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#include <libcamera/base/compiler.h>
14
15namespace libcamera {
16
17 class UniqueFD final
18{
19public:
20 UniqueFD()
21 : fd_(-1)
22 {
23 }
24
25 explicit UniqueFD(int fd)
26 : fd_(fd)
27 {
28 }
29
30 UniqueFD(UniqueFD &&other)
31 : fd_(other.release())
32 {
33 }
34
35 ~UniqueFD()
36 {
37 reset();
38 }
39
40 UniqueFD &operator=(UniqueFD &&other)
41 {
42 reset(other.release());
43 return *this;
44 }
45
46 __nodiscard int release()
47 {
48 int fd = fd_;
49 fd_ = -1;
50 return fd;
51 }
52
53 void reset(int fd = -1);
54
55 void swap(UniqueFD &other)
56 {
57 std::swap(fd_, other.fd_);
58 }
59
60 int get() const { return fd_; }
61 bool isValid() const { return fd_ >= 0; }
62
63private:
64 LIBCAMERA_DISABLE_COPY(UniqueFD)
65
66 int fd_;
67};
68
69} /* namespace libcamera */
class.h
Utilities to help constructing class interfaces.
LIBCAMERA_DISABLE_COPY
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
libcamera::UniqueFD
unique_ptr-like wrapper for a file descriptor
Definition: unique_fd.h:18
libcamera::UniqueFD::get
int get() const
Retrieve the managed file descriptor.
Definition: unique_fd.h:60
libcamera::UniqueFD::~UniqueFD
~UniqueFD()
Destroy the UniqueFD instance.
Definition: unique_fd.h:35
libcamera::UniqueFD::UniqueFD
UniqueFD()
Construct a UniqueFD that owns no file descriptor.
Definition: unique_fd.h:20
libcamera::UniqueFD::UniqueFD
UniqueFD(UniqueFD &&other)
Move constructor, create a UniqueFD by taking over other.
Definition: unique_fd.h:30
libcamera::UniqueFD::UniqueFD
UniqueFD(int fd)
Construct a UniqueFD that owns fd.
Definition: unique_fd.h:25
libcamera::UniqueFD::isValid
bool isValid() const
Check if the UniqueFD owns a valid file descriptor.
Definition: unique_fd.h:61
libcamera::UniqueFD::release
__nodiscard int release()
Release ownership of the file descriptor without closing it.
Definition: unique_fd.h:46
libcamera::UniqueFD::swap
void swap(UniqueFD &other)
Swap the managed file descriptors with another UniqueFD.
Definition: unique_fd.h:55
libcamera::UniqueFD::operator=
UniqueFD & operator=(UniqueFD &&other)
Move assignment operator, replace a UniqueFD by taking over other.
Definition: unique_fd.h:40
libcamera::UniqueFD::reset
void reset(int fd=-1)
Replace the managed file descriptor.
Definition: unique_fd.cpp:95
libcamera
Top-level libcamera namespace.
Definition: backtrace.h:17