Files
external_libcamera/include/libcamera/base/file_descriptor.h
Laurent Pinchart fcf98514cb libcamera: base: file_descriptor: Return UniqueFD from dup()
The dup() function returns a duplicate of the file descriptor. Wrapping
it in a FileDescriptor isn't wrong as such, but it prevents from using
it in contexts where a UniqueFD is needed. As the duplicate is
guaranteed to have a single owner when created, return it as a UniqueFD
instead. A FileDescriptor can easily be created from the UniqueFD if
desired.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Hirokazu Honda <hiroh@chromium.org>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
2021-12-03 19:20:47 +02:00

50 lines
934 B
C++

/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* file_descriptor.h - File descriptor wrapper
*/
#pragma once
#include <memory>
namespace libcamera {
class UniqueFD;
class FileDescriptor final
{
public:
explicit FileDescriptor(const int &fd = -1);
explicit FileDescriptor(int &&fd);
explicit FileDescriptor(UniqueFD fd);
FileDescriptor(const FileDescriptor &other);
FileDescriptor(FileDescriptor &&other);
~FileDescriptor();
FileDescriptor &operator=(const FileDescriptor &other);
FileDescriptor &operator=(FileDescriptor &&other);
bool isValid() const { return fd_ != nullptr; }
int fd() 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_;
};
} /* namespace libcamera */