libcamera  v0.0.0
Supporting cameras in Linux since 2019
file_descriptor.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.h - File descriptor wrapper
6  */
7 #ifndef __LIBCAMERA_FILE_DESCRIPTOR_H__
8 #define __LIBCAMERA_FILE_DESCRIPTOR_H__
9 
10 #include <memory>
11 
12 namespace libcamera {
13 
14 class FileDescriptor final
15 {
16 public:
17  explicit FileDescriptor(const int &fd = -1);
18  explicit FileDescriptor(int &&fd);
19  FileDescriptor(const FileDescriptor &other);
22 
25 
26  bool isValid() const { return fd_ != nullptr; }
27  int fd() const { return fd_ ? fd_->fd() : -1; }
28  FileDescriptor dup() const;
29 
30 private:
31  class Descriptor
32  {
33  public:
34  Descriptor(int fd, bool duplicate);
35  ~Descriptor();
36 
37  int fd() const { return fd_; }
38 
39  private:
40  int fd_;
41  };
42 
43  std::shared_ptr<Descriptor> fd_;
44 };
45 
46 } /* namespace libcamera */
47 
48 #endif /* __LIBCAMERA_FILE_DESCRIPTOR_H__ */
RAII-style wrapper for file descriptors.
Definition: file_descriptor.h:15
int fd() const
Retrieve the numerical file descriptor.
Definition: file_descriptor.h:27
FileDescriptor dup() const
Duplicate a FileDescriptor.
Definition: file_descriptor.cpp:219
~FileDescriptor()
Destroy the FileDescriptor instance.
Definition: file_descriptor.cpp:148
bool isValid() const
Check if the FileDescriptor instance is valid.
Definition: file_descriptor.h:26
FileDescriptor & operator=(const FileDescriptor &other)
Copy assignment operator, replace the wrapped file descriptor with a copy of other.
Definition: file_descriptor.cpp:166
FileDescriptor(const int &fd=-1)
Create a FileDescriptor copying a given fd.
Definition: file_descriptor.cpp:73