blob: 9ad5213bfd08c13a86344043ae1f1d8b3a7f583f [file] [log] [blame]
Vishwanatha Subbanna38b08d72017-04-14 18:12:14 +05301#pragma once
2
3#include <unistd.h>
4namespace open_power
5{
6namespace occ
7{
Vishwanatha Subbanna38b08d72017-04-14 18:12:14 +05308/** @class FileDescriptor
9 * @brief Responsible for handling file descriptor
10 */
11class FileDescriptor
12{
Gunnar Mills94df8c92018-09-14 14:50:03 -050013 private:
14 /** @brief File descriptor for the gpio input device */
15 int fd = -1;
Vishwanatha Subbanna38b08d72017-04-14 18:12:14 +053016
Gunnar Mills94df8c92018-09-14 14:50:03 -050017 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 Subbanna38b08d72017-04-14 18:12:14 +053023
Gunnar Mills94df8c92018-09-14 14:50:03 -050024 /** @brief Saves File descriptor and uses it to do file operation
25 *
26 * @param[in] fd - File descriptor
27 */
28 FileDescriptor(int fd) : fd(fd)
29 {
30 // Nothing
31 }
32
33 ~FileDescriptor()
34 {
35 if (fd >= 0)
Vishwanatha Subbanna38b08d72017-04-14 18:12:14 +053036 {
Gunnar Mills94df8c92018-09-14 14:50:03 -050037 close(fd);
Vishwanatha Subbanna38b08d72017-04-14 18:12:14 +053038 }
Gunnar Mills94df8c92018-09-14 14:50:03 -050039 }
Vishwanatha Subbanna38b08d72017-04-14 18:12:14 +053040
Gunnar Mills94df8c92018-09-14 14:50:03 -050041 int operator()()
42 {
43 return fd;
44 }
Vishwanatha Subbanna38b08d72017-04-14 18:12:14 +053045};
46
Vishwanatha Subbanna38b08d72017-04-14 18:12:14 +053047} // namespace occ
Gunnar Mills94df8c92018-09-14 14:50:03 -050048} // namespace open_power