-
Notifications
You must be signed in to change notification settings - Fork 1
/
AutoDelete.hh
108 lines (91 loc) · 1.71 KB
/
AutoDelete.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once
#include <libudev.h>
#include "gui/decl.hxx"
#include "MutexGuard.hpp"
#include "io/io.hh"
namespace cornus {
class UdevAutoUnref {
public:
UdevAutoUnref(struct udev *p): p_(p) {}
~UdevAutoUnref() {
if (p_)
udev_unref(p_);
}
private:
struct udev *p_ = nullptr;
};
class UdevDeviceAutoUnref {
public:
UdevDeviceAutoUnref(struct udev_device *p): p_(p) {}
~UdevDeviceAutoUnref() {
if (p_)
udev_device_unref(p_);
}
private:
struct udev_device *p_ = nullptr;
};
class UdevMonitorAutoUnref {
public:
UdevMonitorAutoUnref(struct udev_monitor *p): p_(p) {}
~UdevMonitorAutoUnref() {
if (p_)
udev_monitor_unref(p_);
}
private:
struct udev_monitor *p_ = nullptr;
};
class AutoCloseFd {
public:
AutoCloseFd(int fd): fd_(fd) {}
~AutoCloseFd() {
if (fd_ != -1)
::close(fd_);
}
private:
int fd_ = -1;
};
template <class A_Type> class AutoFree {
public:
AutoFree(A_Type x) : x_(x) {}
virtual ~AutoFree() { free(x_); }
private:
A_Type x_ = nullptr;
};
template <class A_Type> class AutoDelete {
public:
AutoDelete(A_Type x) : x_(x) {}
virtual ~AutoDelete() { delete x_; }
private:
A_Type x_ = nullptr;
};
template <class A_Type> class AutoDeleteArr {
public:
AutoDeleteArr(A_Type x) : x_(x) {}
virtual ~AutoDeleteArr() { delete [] x_; x_ = nullptr; }
private:
A_Type x_ = nullptr;
};
template <class VecType> class AutoDeleteVec {
public:
AutoDeleteVec(VecType &x) : vec_(x) {}
~AutoDeleteVec() {
for (auto *item : vec_) {
delete item;
}
}
private:
VecType &vec_;
};
template <class VecType> class AutoDeleteVecP {
public:
AutoDeleteVecP(VecType &x) : vec_(x) {}
~AutoDeleteVecP() {
for (auto *item : *vec_) {
delete item;
}
delete vec_;
}
private:
VecType vec_;
};
}