Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 3 | #include <memory> |
| 4 | #include <unistd.h> |
| 5 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 6 | namespace phosphor |
| 7 | { |
| 8 | namespace dump |
| 9 | { |
| 10 | |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 11 | /* Need a custom deleter for freeing up sd_event */ |
| 12 | struct EventDeleter |
| 13 | { |
| 14 | void operator()(sd_event* event) const |
| 15 | { |
| 16 | event = sd_event_unref(event); |
| 17 | } |
| 18 | }; |
| 19 | using EventPtr = std::unique_ptr<sd_event, EventDeleter>; |
| 20 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 21 | /** @struct CustomFd |
| 22 | * |
| 23 | * RAII wrapper for file descriptor. |
| 24 | */ |
| 25 | struct CustomFd |
| 26 | { |
| 27 | private: |
| 28 | /** @brief File descriptor */ |
| 29 | int fd = -1; |
| 30 | |
| 31 | public: |
| 32 | CustomFd() = delete; |
| 33 | CustomFd(const CustomFd&) = delete; |
| 34 | CustomFd& operator=(const CustomFd&) = delete; |
| 35 | CustomFd(CustomFd&&) = delete; |
| 36 | CustomFd& operator=(CustomFd&&) = delete; |
| 37 | |
| 38 | /** @brief Saves File descriptor and uses it to do file operation |
| 39 | * |
| 40 | * @param[in] fd - File descriptor |
| 41 | */ |
| 42 | CustomFd(int fd) : fd(fd) {} |
| 43 | |
| 44 | ~CustomFd() |
| 45 | { |
| 46 | if (fd >= 0) |
| 47 | { |
| 48 | close(fd); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | int operator()() const |
| 53 | { |
| 54 | return fd; |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | } // namespace dump |
| 59 | } // namespace phosphor |