Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame^] | 1 | #pragma once |
| 2 | |
| 3 | namespace phosphor |
| 4 | { |
| 5 | namespace dump |
| 6 | { |
| 7 | |
| 8 | /** @struct CustomFd |
| 9 | * |
| 10 | * RAII wrapper for file descriptor. |
| 11 | */ |
| 12 | struct CustomFd |
| 13 | { |
| 14 | private: |
| 15 | /** @brief File descriptor */ |
| 16 | int fd = -1; |
| 17 | |
| 18 | public: |
| 19 | CustomFd() = delete; |
| 20 | CustomFd(const CustomFd&) = delete; |
| 21 | CustomFd& operator=(const CustomFd&) = delete; |
| 22 | CustomFd(CustomFd&&) = delete; |
| 23 | CustomFd& operator=(CustomFd&&) = delete; |
| 24 | |
| 25 | /** @brief Saves File descriptor and uses it to do file operation |
| 26 | * |
| 27 | * @param[in] fd - File descriptor |
| 28 | */ |
| 29 | CustomFd(int fd) : fd(fd) {} |
| 30 | |
| 31 | ~CustomFd() |
| 32 | { |
| 33 | if (fd >= 0) |
| 34 | { |
| 35 | close(fd); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | int operator()() const |
| 40 | { |
| 41 | return fd; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | } // namespace dump |
| 46 | } // namespace phosphor |