xref: /openbmc/dbus-sensors/src/FileHandle.hpp (revision 779c96a2)
1 #pragma once
2 
3 #include <filesystem>
4 #include <ios>
5 
6 // An RAII compliant object for holding a posix file descriptor
7 class FileHandle
8 {
9   private:
10     int fd;
11 
12   public:
13     FileHandle() = delete;
14     FileHandle(const FileHandle&) = delete;
15     FileHandle& operator=(const FileHandle&) = delete;
16     FileHandle(FileHandle&& /*in*/) noexcept;
17     FileHandle& operator=(FileHandle&& /*in*/) noexcept;
18 
19     explicit FileHandle(const std::filesystem::path& name,
20                         std::ios_base::openmode mode = std::ios_base::in |
21                                                        std::ios_base::out);
22 
23     explicit FileHandle(int fd);
24 
25     ~FileHandle();
26     int handle() const;
27 };
28