1 #pragma once 2 3 #include <fcntl.h> 4 5 #include <string> 6 7 namespace openpower 8 { 9 namespace util 10 { 11 12 /** 13 * Class to wrap a file descriptor. 14 * 15 * Opens the descriptor in the constructor, and 16 * then closes it when destroyed. 17 */ 18 class FileDescriptor 19 { 20 public: 21 FileDescriptor() = delete; 22 FileDescriptor(const FileDescriptor&) = delete; 23 FileDescriptor(FileDescriptor&&) = default; 24 FileDescriptor& operator=(const FileDescriptor) = delete; 25 FileDescriptor& operator=(FileDescriptor&&) = default; 26 27 /** 28 * Creates a file descriptor by opening the device 29 * path passed in. 30 * 31 * @param path[in] - the device path that will be open 32 */ 33 FileDescriptor(const std::string& path); 34 35 /** 36 * Closes the file. 37 */ 38 ~FileDescriptor(); 39 40 /** 41 * The method to access the file descriptor value 42 */ 43 inline auto get() const 44 { 45 return fd; 46 } 47 48 private: 49 /** 50 * The actual file descriptor 51 */ 52 int fd; 53 }; 54 55 } // namespace util 56 } // namespace openpower 57