Loading...
Searching...
No Matches
shared_fd.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* File descriptor wrapper with shared ownership
*/
#pragma once
#include <memory>
namespace libcamera {
class UniqueFD;
class SharedFD final
{
public:
explicit SharedFD(const int &fd = -1);
explicit SharedFD(int &&fd);
explicit SharedFD(UniqueFD fd);
SharedFD(const SharedFD &other);
SharedFD(SharedFD &&other);
~SharedFD();
SharedFD &operator=(const SharedFD &other);
SharedFD &operator=(SharedFD &&other);
bool isValid() const { return fd_ != nullptr; }
int get() const { return fd_ ? fd_->fd() : -1; }
UniqueFD dup() const;
private:
class Descriptor
{
public:
Descriptor(int fd, bool duplicate);
~Descriptor();
int fd() const { return fd_; }
private:
int fd_;
};
std::shared_ptr<Descriptor> fd_;
};
static inline bool operator==(const SharedFD &lhs, const SharedFD &rhs)
{
return lhs.get() == rhs.get();
}
static inline bool operator!=(const SharedFD &lhs, const SharedFD &rhs)
{
return !(lhs == rhs);
}
} /* namespace libcamera */
libcamera::SharedFD
RAII-style wrapper for file descriptors.
Definition shared_fd.h:17
libcamera::SharedFD::~SharedFD
~SharedFD()
Destroy the SharedFD instance.
Definition shared_fd.cpp:160
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 bound_method.h:15