-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathcheckedweakptr.h
More file actions
52 lines (39 loc) · 859 Bytes
/
checkedweakptr.h
File metadata and controls
52 lines (39 loc) · 859 Bytes
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
#ifndef CHECKEDWEAKPTR_H
#define CHECKEDWEAKPTR_H
#include <memory>
class __attribute__((visibility("default"))) PointerNullException : public std::exception
{
public:
virtual const char* what() const noexcept override
{
return "CheckedWeakPtr pointer null";
}
};
template<typename T>
class CheckedWeakPtr
{
std::weak_ptr<T> d;
public:
CheckedWeakPtr() = default;
CheckedWeakPtr(const std::shared_ptr<T> org) :
d(org)
{
}
CheckedWeakPtr<T> &operator=(const std::shared_ptr<T> &other)
{
d = other;
return *this;
}
std::shared_ptr<T> lock()
{
std::shared_ptr<T> d2 = d.lock();
if (!d2)
throw PointerNullException();
return d2;
}
bool expired() const
{
return d.expired();
}
};
#endif // CHECKEDWEAKPTR_H