blob: 500d8beb4bd24e3efc7b9b4517da2e2348743047 [file] [log] [blame]
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +05301#pragma once
2
3#include <unistd.h>
4namespace phosphor
5{
6namespace gpio
7{
8/** @class FileDescriptor
9 * @brief Responsible for handling file descriptor
10 */
11class FileDescriptor
12{
Patrick Venturedace6802018-11-01 16:52:10 -070013 private:
14 /** @brief File descriptor for the gpio input device */
15 int fd = -1;
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053016
Patrick Venturedace6802018-11-01 16:52:10 -070017 public:
18 FileDescriptor() = default;
19 FileDescriptor(const FileDescriptor&) = delete;
20 FileDescriptor& operator=(const FileDescriptor&) = delete;
21 FileDescriptor(FileDescriptor&&) = delete;
22 FileDescriptor& operator=(FileDescriptor&&) = delete;
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053023
Patrick Venturedace6802018-11-01 16:52:10 -070024 /** @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 Subbanna4902a102017-04-04 14:05:09 +053036 {
Patrick Venturedace6802018-11-01 16:52:10 -070037 close(fd);
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053038 }
Patrick Venturedace6802018-11-01 16:52:10 -070039 }
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053040
Patrick Venturedace6802018-11-01 16:52:10 -070041 int operator()()
42 {
43 return fd;
44 }
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053045
Patrick Venturedace6802018-11-01 16:52:10 -070046 operator bool() const
47 {
48 return fd != -1;
49 }
Matt Spinler36df1972017-05-25 10:23:05 -050050
Patrick Venturedace6802018-11-01 16:52:10 -070051 void set(int descriptor)
52 {
53 fd = descriptor;
54 }
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053055};
56
57} // namespace gpio
58} // namespace phosphor