Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame^] | 1 | #pragma once |
| 2 | |
| 3 | #include <fcntl.h> |
| 4 | #include <string> |
| 5 | |
| 6 | namespace openpower |
| 7 | { |
| 8 | namespace p9_util |
| 9 | { |
| 10 | |
| 11 | /** |
| 12 | * Class to wrap a file descriptor. |
| 13 | * |
| 14 | * Opens the descriptor in the constructor, and |
| 15 | * then closes it when destroyed. |
| 16 | */ |
| 17 | class FileDescriptor |
| 18 | { |
| 19 | public: |
| 20 | |
| 21 | FileDescriptor() = delete; |
| 22 | FileDescriptor(const FileDescriptor&) = delete; |
| 23 | FileDescriptor(FileDescriptor&&) = default; |
| 24 | FileDescriptor& operator=(const FileDescriptor) = delete; |
| 25 | FileDescriptor& operator=(FileDescriptor&&) = default; |
| 26 | |
| 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); |
| 34 | |
| 35 | /** |
| 36 | * Closes the file. |
| 37 | */ |
| 38 | ~FileDescriptor(); |
| 39 | |
| 40 | /** |
| 41 | * The method to access the file descriptor value |
| 42 | */ |
| 43 | inline int operator()() const |
| 44 | { |
| 45 | return fd; |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | |
| 50 | /** |
| 51 | * The actual file descriptor |
| 52 | */ |
| 53 | int fd; |
| 54 | }; |
| 55 | |
| 56 | } |
| 57 | } |