Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <unistd.h> |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 4 | |
| 5 | #include <sstream> |
| 6 | |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 7 | namespace openpower |
| 8 | { |
| 9 | namespace sbe |
| 10 | { |
| 11 | namespace internal |
| 12 | { |
| 13 | |
| 14 | /** @class FileDescriptor |
| 15 | * @brief Provide RAII file descriptor |
| 16 | */ |
| 17 | class FileDescriptor |
| 18 | { |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 19 | private: |
| 20 | /** @brief File descriptor for the SBE FIFO device */ |
| 21 | int fd = -1; |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 22 | |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 23 | public: |
| 24 | FileDescriptor() = delete; |
| 25 | FileDescriptor(const FileDescriptor&) = delete; |
| 26 | FileDescriptor& operator=(const FileDescriptor&) = delete; |
| 27 | FileDescriptor(FileDescriptor&&) = delete; |
| 28 | FileDescriptor& operator=(FileDescriptor&&) = delete; |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 29 | |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 30 | /** @brief Opens the input file and saves the file descriptor |
| 31 | * |
| 32 | * @param[in] devPath - Path of the file |
| 33 | * @para,[in] accessModes - File access modes |
| 34 | */ |
| 35 | FileDescriptor(const char* devPath, int accessModes) |
| 36 | { |
| 37 | fd = open(devPath, accessModes); |
| 38 | if (fd < 0) |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 39 | { |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 40 | // TODO:use elog infrastructure |
| 41 | std::ostringstream errMsg; |
| 42 | errMsg << "Opening the device with device path:" << devPath |
| 43 | << " and access modes:" << accessModes |
| 44 | << ",Failed with errno" << errno; |
| 45 | throw std::runtime_error(errMsg.str().c_str()); |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 46 | } |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 47 | } |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 48 | |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 49 | /** @brief Saves File descriptor and uses it to do file operation |
| 50 | * |
| 51 | * @param[in] fd - File descriptor |
| 52 | */ |
| 53 | FileDescriptor(int fd) : fd(fd) |
| 54 | { |
| 55 | // Nothing |
| 56 | } |
| 57 | |
| 58 | ~FileDescriptor() |
| 59 | { |
| 60 | if (fd >= 0) |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 61 | { |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 62 | close(fd); |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 63 | } |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 64 | } |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 65 | |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 66 | int operator()() |
| 67 | { |
| 68 | return fd; |
| 69 | } |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | } // namespace internal |
| 73 | } // namespace sbe |
| 74 | } // namespace openpower |