blob: 6ebee33a3a5e14b1119b0a9ce87b3da46018dab4 [file] [log] [blame]
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05001#pragma once
2
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -05003#include <memory>
4#include <unistd.h>
5
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05006namespace phosphor
7{
8namespace dump
9{
10
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050011/* Need a custom deleter for freeing up sd_event */
12struct EventDeleter
13{
14 void operator()(sd_event* event) const
15 {
16 event = sd_event_unref(event);
17 }
18};
19using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
20
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050021/** @struct CustomFd
22 *
23 * RAII wrapper for file descriptor.
24 */
25struct 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