Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <unistd.h> |
| 4 | namespace open_power |
| 5 | { |
| 6 | namespace occ |
| 7 | { |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 8 | /** @class FileDescriptor |
| 9 | * @brief Responsible for handling file descriptor |
| 10 | */ |
| 11 | class FileDescriptor |
| 12 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 13 | private: |
| 14 | /** @brief File descriptor for the gpio input device */ |
| 15 | int fd = -1; |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 16 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 17 | public: |
| 18 | FileDescriptor() = delete; |
| 19 | FileDescriptor(const FileDescriptor&) = delete; |
| 20 | FileDescriptor& operator=(const FileDescriptor&) = delete; |
| 21 | FileDescriptor(FileDescriptor&&) = delete; |
| 22 | FileDescriptor& operator=(FileDescriptor&&) = delete; |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 23 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 24 | /** @brief Saves File descriptor and uses it to do file operation |
| 25 | * |
| 26 | * @param[in] fd - File descriptor |
| 27 | */ |
George Liu | f3a4a69 | 2021-12-28 13:59:51 +0800 | [diff] [blame] | 28 | explicit FileDescriptor(int fd) : fd(fd) |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 29 | { |
| 30 | // Nothing |
| 31 | } |
| 32 | |
| 33 | ~FileDescriptor() |
| 34 | { |
| 35 | if (fd >= 0) |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 36 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 37 | close(fd); |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 38 | } |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 39 | } |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 40 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 41 | int operator()() |
| 42 | { |
| 43 | return fd; |
| 44 | } |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 45 | }; |
| 46 | |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 47 | } // namespace occ |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 48 | } // namespace open_power |