WARPXM v1.10.0
Loading...
Searching...
No Matches
raii.h
Go to the documentation of this file.
1#ifndef WXM_RAII_H
2#define WXM_RAII_H
3
4#include <utility>
5
6namespace wxm
7{
8template<class T, class PTR_TYPE = T*, class CPTR_TYPE = const T*> class raii_base
9{
10 PTR_TYPE d_ = PTR_TYPE();
11
12 raii_base(const raii_base&) = delete;
13 raii_base& operator=(const raii_base&) = delete;
14
15protected:
16 virtual void dec(PTR_TYPE d) = 0;
17 virtual void inc(PTR_TYPE d) = 0;
18
19 raii_base& operator=(PTR_TYPE d)
20 {
21 dec(d_);
22 d_ = d;
23 return *this;
24 }
25
29 ~raii_base() = default;
30
31public:
32 raii_base() noexcept = default;
33
34 raii_base(raii_base&& o) noexcept : d_(o.d_)
35 {
36 o.d_ = PTR_TYPE();
37 }
38
39 void swap(raii_base& o) noexcept
40 {
41 auto tmp = d_;
42 d_ = o.d_;
43 o.d_ = tmp;
44 }
45
46 raii_base(PTR_TYPE d) noexcept : d_(d)
47 {
48 }
49
50 PTR_TYPE release()
51 {
52 auto res = d_;
53 d_ = PTR_TYPE();
54 return res;
55 }
56
57 PTR_TYPE& reset(PTR_TYPE p = PTR_TYPE())
58 {
59 dec(d_);
60 d_ = p;
61 return d_;
62 }
63
64 operator PTR_TYPE() noexcept
65 {
66 return d_;
67 }
68
69 operator CPTR_TYPE() const noexcept
70 {
71 return d_;
72 }
73
75 {
76 return *d_;
77 }
78
79 const T& operator*() const
80 {
81 return *d_;
82 }
83
84 PTR_TYPE get() noexcept
85 {
86 return d_;
87 }
88
89 CPTR_TYPE get() const noexcept
90 {
91 return d_;
92 }
93};
94
95template<class T> class raii_base<T, T, T>
96{
97 T d_ = T();
98
99 raii_base(const raii_base&) = delete;
100 raii_base& operator=(const raii_base&) = delete;
101
102protected:
103 virtual void dec(T d) = 0;
104 virtual void inc(T d) = 0;
105
107 {
108 dec(d_);
109 d_ = d;
110 return *this;
111 }
112
116 ~raii_base() = default;
117
118public:
119 raii_base() noexcept = default;
120
121 raii_base(raii_base&& o) noexcept : d_(o.d_)
122 {
123 o.d_ = T();
124 }
125
126 void swap(raii_base& o) noexcept
127 {
128 auto tmp = d_;
129 d_ = o.d_;
130 o.d_ = tmp;
131 }
132
133 raii_base(T d) noexcept : d_(d)
134 {
135 }
136
138 {
139 auto res = d_;
140 d_ = T();
141 return res;
142 }
143
144 T& reset(T p = T())
145 {
146 dec(d_);
147 d_ = p;
148 return d_;
149 }
150
151 operator T() noexcept
152 {
153 return d_;
154 }
155
156 operator const T() const noexcept
157 {
158 return d_;
159 }
160
162 {
163 return d_;
164 }
165
166 const T& operator*() const
167 {
168 return d_;
169 }
170
171 T get() noexcept
172 {
173 return d_;
174 }
175
176 const T get() const noexcept
177 {
178 return d_;
179 }
180};
181} // namespace wxm
182
183#endif
raii_base & operator=(T d)
Definition: raii.h:106
T & operator*()
Definition: raii.h:161
raii_base(T d) noexcept
Definition: raii.h:133
void swap(raii_base &o) noexcept
Definition: raii.h:126
T & reset(T p=T())
Definition: raii.h:144
T release()
Definition: raii.h:137
raii_base() noexcept=default
virtual void dec(T d)=0
~raii_base()=default
Child classes need to add reset();.
virtual void inc(T d)=0
const T get() const noexcept
Definition: raii.h:176
const T & operator*() const
Definition: raii.h:166
T get() noexcept
Definition: raii.h:171
Definition: raii.h:9
raii_base & operator=(PTR_TYPE d)
Definition: raii.h:19
~raii_base()=default
Child classes need to add reset();.
raii_base(PTR_TYPE d) noexcept
Definition: raii.h:46
T & operator*()
Definition: raii.h:74
PTR_TYPE & reset(PTR_TYPE p=PTR_TYPE())
Definition: raii.h:57
raii_base() noexcept=default
PTR_TYPE get() noexcept
Definition: raii.h:84
PTR_TYPE release()
Definition: raii.h:50
void swap(raii_base &o) noexcept
Definition: raii.h:39
const T & operator*() const
Definition: raii.h:79
virtual void inc(PTR_TYPE d)=0
CPTR_TYPE get() const noexcept
Definition: raii.h:89
virtual void dec(PTR_TYPE d)=0
Base namespace for everything not included in the global namespace.
Definition: field_source.h:8