forked from X547/wayland-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppKitPtrs.h
80 lines (65 loc) · 1.74 KB
/
AppKitPtrs.h
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
#ifndef _APPKITPTRS_H_
#define _APPKITPTRS_H_
#include <utility>
namespace AppKitPtrs {
template <typename Base>
class LockedPtr {
Base *fPtr;
public:
LockedPtr(): fPtr(NULL) {}
LockedPtr(Base *ptr): fPtr(ptr) {if (fPtr != NULL) fPtr->LockLooper();}
~LockedPtr() {Unset();}
void Unset()
{
if (fPtr != NULL) {
fPtr->UnlockLooper();
fPtr = NULL;
}
}
LockedPtr& operator =(Base *ptr)
{
if (fPtr == ptr) return *this;
Unset();
fPtr = ptr;
if (fPtr != NULL) fPtr->LockLooper();
return *this;
}
Base& operator*() const {return *fPtr;}
Base* operator->() const {return fPtr;}
operator Base*() const {return fPtr;}
};
template <typename Base>
LockedPtr<Base> MakeLocked(Base *ptr)
{
return LockedPtr<Base>(ptr);
}
template <typename Base>
class ExternalPtr {
private:
Base *fPtr;
friend class AsyncReq;
template<class OtherBase> friend class ExternalPtr;
public:
ExternalPtr(): fPtr(NULL) {}
ExternalPtr(Base *ptr): fPtr(ptr) {}
template <typename OtherBase> ExternalPtr(ExternalPtr<OtherBase> other): fPtr(other.fPtr) {}
void Unset() {fPtr = NULL;}
ExternalPtr& operator =(Base *ptr) {fPtr = ptr; return *this;}
ExternalPtr& operator =(const ExternalPtr<Base> &ptr) {fPtr = ptr.fPtr; return *this;}
template <typename OtherBase> void operator=(ExternalPtr<OtherBase> other) {fPtr = other.fPtr;}
template <typename OtherBase> void operator=(LockedPtr<OtherBase> other){fPtr = other.fPtr;}
LockedPtr<Base> Lock() const
{
return LockedPtr<Base>(fPtr);
}
LockedPtr<Base> operator->() const {return Lock();}
Base *Get() const {return fPtr;}
};
template<typename T, typename... Args>
ExternalPtr<T> MakeExternal(Args&&... args)
{
ExternalPtr<T> ptr(new T(std::forward<Args>(args)...));
return ptr;
}
}
#endif // _APPKITPTRS_H_