-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandle.h
98 lines (86 loc) · 3.36 KB
/
Handle.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//==========
// Handle.h
//==========
#pragma once
//=======
// Using
//=======
#include "Object.h"
//========
// Handle
//========
template <class _obj_t>
class Handle
{
public:
// Friends
template <class _friend_t> friend class Handle;
// Con-/Destructors
inline Handle(): m_Object(nullptr) {}
inline Handle(nullptr_t): m_Object(nullptr) {}
inline Handle(_obj_t* Object) { Create(&m_Object, Object); }
inline Handle(Handle const& Copy) { Create(&m_Object, Copy.m_Object); }
Handle(Handle&& Move)noexcept: m_Object(Move.m_Object)
{
Move.m_Object=nullptr;
}
template <class _convert_t> inline Handle(_convert_t* Object) { Create(&m_Object, Object); }
template <class _convert_t> inline Handle(Handle<_convert_t>const& Copy) { Create(&m_Object, Copy.m_Object); }
inline ~Handle() { Clear(&m_Object); }
// Access
inline operator BOOL()const { return m_Object!=nullptr; }
inline operator _obj_t*()const { return m_Object; }
inline _obj_t* operator->()const { return m_Object; }
template <class _convert_t> inline Handle<_convert_t> As() { return dynamic_cast<_convert_t*>(m_Object); }
// Comparison
inline BOOL operator==(nullptr_t)const { return m_Object==nullptr; }
inline BOOL operator==(_obj_t* Object)const { return m_Object==Object; }
inline BOOL operator==(Handle const& Compare)const { return m_Object==Compare.m_Object; }
template <class _convert_t> inline BOOL operator==(_convert_t* Object)const { return Equal(m_Object, Object); }
template <class _convert_t> inline BOOL operator==(Handle<_convert_t> const& Compare)const { return Equal(m_Object, Compare.m_Object); }
inline BOOL operator!=(nullptr_t)const { return !operator==(nullptr); }
inline BOOL operator!=(_obj_t* Object)const { return !operator==(Object); }
inline BOOL operator!=(Handle const& Compare)const { return !operator==(Compare.m_Object); }
template <class _convert_t> inline BOOL operator!=(_convert_t* Object)const { return !Equal(m_Object, Object); }
template <class _convert_t> inline BOOL operator!=(Handle<_convert_t> const& Compare)const { return !Equal(m_Object, Compare.m_Object); }
// Assignment
inline Handle& operator=(nullptr_t) { Clear(&m_Object); return *this; }
inline Handle& operator=(_obj_t* Object) { Set(&m_Object, Object); return *this; }
inline Handle& operator=(Handle const& Copy) { Set(&m_Object, Copy.m_Object); return *this; }
template <class _convert_t> inline Handle& operator=(_convert_t* Object) { Set(&m_Object, Object); return *this; }
template <class _convert_t> inline Handle& operator=(Handle<_convert_t> const& Copy) { Set(&m_Object, Copy.m_Object); return *this; }
private:
// Common
template <class _clear_t> static VOID Clear(_clear_t** Object)
{
if(*Object)
{
(*Object)->Release();
*Object=nullptr;
}
}
template <class _create_t, class _convert_t> static VOID Create(_create_t** Object, _convert_t* To)
{
auto to=static_cast<_create_t*>(To);
if(to)
to->m_RefCount++;
*Object=to;
}
template <class _cmp_t, class _convert_t> static inline BOOL Equal(_cmp_t* Object, _convert_t* Compare)
{
auto cmp=static_cast<_cmp_t*>(Compare);
return Object==cmp;
}
template <class _set_t, class _convert_t> static VOID Set(_set_t** Object, _convert_t* To)
{
auto to=static_cast<_set_t*>(To);
if(*Object==to)
return;
if(*Object)
(*Object)->Release();
*Object=to;
if(*Object)
(*Object)->m_RefCount++;
}
_obj_t* m_Object;
};