BeeeOn Gateway
v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
Main Page
Related Pages
Data Structures
Files
File List
base
src
util
UnsafePtr.h
1
#pragma once
2
3
#include <algorithm>
4
5
#include <Poco/Exception.h>
6
7
namespace
BeeeOn {
8
13
template
<
typename
T>
14
class
UnsafePtr
{
15
public
:
16
UnsafePtr
():
17
m_p(
nullptr
)
18
{
19
}
20
21
UnsafePtr
(T *p):
22
m_p(p)
23
{
24
}
25
26
UnsafePtr
(
const
UnsafePtr
&ptr):
27
m_p(ptr.m_p)
28
{
29
}
30
31
template
<
typename
Other>
32
UnsafePtr
(
const
UnsafePtr<Other>
&ptr):
33
m_p(const_cast<Other *>(ptr.get()))
34
{
35
}
36
37
~
UnsafePtr
()
38
{
39
m_p =
nullptr
;
40
}
41
42
void
clear()
43
{
44
m_p =
nullptr
;
45
}
46
47
UnsafePtr
&assign(T *p)
48
{
49
m_p = p;
50
return
*
this
;
51
}
52
53
UnsafePtr
&assign(
const
UnsafePtr
&p)
54
{
55
m_p = p.m_p;
56
return
*
this
;
57
}
58
59
template
<
typename
Other>
60
UnsafePtr
&assign(
const
UnsafePtr<Other>
&p)
61
{
62
UnsafePtr
other(p);
63
swap(other);
64
return
*
this
;
65
}
66
67
UnsafePtr
&operator =(T *p)
68
{
69
return
assign(p);
70
}
71
72
UnsafePtr
&operator =(
const
UnsafePtr
&p)
73
{
74
return
assign(p);
75
}
76
77
template
<
typename
Other>
78
UnsafePtr
&operator =(
const
UnsafePtr<Other>
&p)
79
{
80
return
assign<Other>(p);
81
}
82
83
void
swap(
UnsafePtr
&p)
84
{
85
std::swap(m_p, p.m_p);
86
}
87
88
template
<
typename
Other>
89
UnsafePtr<Other>
cast()
const
90
{
91
Other *other =
dynamic_cast<
Other *
>
(m_p);
92
if
(other)
93
return
UnsafePtr<Other>
(other);
94
95
return
UnsafePtr<Other>
();
96
}
97
98
template
<
typename
Other>
99
UnsafePtr<Other>
unsafeCast()
const
100
{
101
Other *other =
static_cast<
Other *
>
(m_p);
102
return
UnsafePtr<Other>
(other);
103
}
104
105
T *operator ->()
106
{
107
return
deref();
108
}
109
110
const
T *operator ->()
const
111
{
112
return
deref();
113
}
114
115
T &operator *()
116
{
117
return
*deref();
118
}
119
120
const
T &operator *()
const
121
{
122
return
*deref();
123
}
124
125
T *
get
()
126
{
127
return
m_p;
128
}
129
130
const
T *
get
()
const
131
{
132
return
m_p;
133
}
134
135
operator
T* ()
136
{
137
return
m_p;
138
}
139
140
operator
const
T*()
const
141
{
142
return
m_p;
143
}
144
145
bool
isNull()
const
146
{
147
return
m_p ==
nullptr
;
148
}
149
150
bool
operator !()
const
151
{
152
return
!isNull();
153
}
154
155
bool
operator ==(
const
UnsafePtr
&p)
const
156
{
157
return
get
() == p.get();
158
}
159
160
bool
operator ==(
const
T *p)
const
161
{
162
return
get
() == p;
163
}
164
165
bool
operator ==(T *p)
const
166
{
167
return
get
() == p;
168
}
169
170
bool
operator !=(
const
UnsafePtr
&p)
const
171
{
172
return
get
() != p.get();
173
}
174
175
bool
operator !=(
const
T *p)
const
176
{
177
return
get
() != p;
178
}
179
180
bool
operator !=(T *p)
const
181
{
182
return
get
() != p;
183
}
184
185
bool
operator <(
const
UnsafePtr
&p)
const
186
{
187
return
get
() < p.get();
188
}
189
190
bool
operator <(
const
T *p)
const
191
{
192
return
get
() < p;
193
}
194
195
bool
operator <(T *p)
const
196
{
197
return
get
() < p;
198
}
199
200
bool
operator <=(
const
UnsafePtr
&p)
const
201
{
202
return
get
() <= p.get();
203
}
204
205
bool
operator <=(
const
T *p)
const
206
{
207
return
get
() <= p;
208
}
209
210
bool
operator <=(T *p)
const
211
{
212
return
get
() <= p;
213
}
214
215
bool
operator >(
const
UnsafePtr
&p)
const
216
{
217
return
get
() > p.get();
218
}
219
220
bool
operator >(
const
T *p)
const
221
{
222
return
get
() > p;
223
}
224
225
bool
operator >(T *p)
const
226
{
227
return
get
() > p;
228
}
229
230
bool
operator >=(
const
UnsafePtr
&p)
const
231
{
232
return
get
() >= p.get();
233
}
234
235
bool
operator >=(
const
T *p)
const
236
{
237
return
get
() >= p;
238
}
239
240
bool
operator >=(T *p)
const
241
{
242
return
get
() >= p;
243
}
244
245
protected
:
246
T *deref()
const
247
{
248
if
(m_p ==
nullptr
)
249
throw
Poco::NullPointerException();
250
251
return
m_p;
252
}
253
254
private
:
255
T *m_p;
256
};
257
258
template
<
typename
T>
259
inline
void
swap(
UnsafePtr<T>
&a,
UnsafePtr<T>
&b)
260
{
261
a.swap(b);
262
}
263
264
}
BeeeOn::UnsafePtr
Definition:
UnsafePtr.h:14
Generated by
1.8.5