Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <fcntl.h> |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 4 | |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 5 | #include <string> |
| 6 | |
| 7 | namespace openpower |
| 8 | { |
Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 9 | namespace util |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 10 | { |
| 11 | |
| 12 | /** |
| 13 | * Class to wrap a file descriptor. |
| 14 | * |
| 15 | * Opens the descriptor in the constructor, and |
| 16 | * then closes it when destroyed. |
| 17 | */ |
| 18 | class FileDescriptor |
| 19 | { |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 20 | public: |
| 21 | FileDescriptor() = delete; |
| 22 | FileDescriptor(const FileDescriptor&) = delete; |
| 23 | FileDescriptor(FileDescriptor&&) = default; |
| 24 | FileDescriptor& operator=(const FileDescriptor) = delete; |
| 25 | FileDescriptor& operator=(FileDescriptor&&) = default; |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 26 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Creates a file descriptor by opening the device |
| 29 | * path passed in. |
| 30 | * |
| 31 | * @param path[in] - the device path that will be open |
| 32 | */ |
| 33 | FileDescriptor(const std::string& path); |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 34 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Closes the file. |
| 37 | */ |
| 38 | ~FileDescriptor(); |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 39 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 40 | /** |
| 41 | * The method to access the file descriptor value |
| 42 | */ |
| 43 | inline auto get() const |
| 44 | { |
| 45 | return fd; |
| 46 | } |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 47 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 48 | private: |
| 49 | /** |
| 50 | * The actual file descriptor |
| 51 | */ |
| 52 | int fd; |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 53 | }; |
| 54 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 55 | } // namespace util |
| 56 | } // namespace openpower |