-
Notifications
You must be signed in to change notification settings - Fork 6
/
basehandle.h
150 lines (113 loc) · 3.2 KB
/
basehandle.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#pragma once
class Entity;
#define NUM_ENT_ENTRY_BITS (11 + 2)
#define NUM_ENT_ENTRIES (1 << NUM_ENT_ENTRY_BITS)
#define INVALID_EHANDLE_INDEX 0xFFFFFFFF
#define NUM_SERIAL_NUM_BITS 16
#define NUM_SERIAL_NUM_SHIFT_BITS (32 - NUM_SERIAL_NUM_BITS)
#define ENT_ENTRY_MASK (( 1 << NUM_SERIAL_NUM_BITS) - 1)
class CBaseHandle {
public:
__forceinline CBaseHandle( ) {
m_Index = INVALID_EHANDLE_INDEX;
}
__forceinline CBaseHandle( const CBaseHandle& other ) {
m_Index = other.m_Index;
}
__forceinline CBaseHandle( ulong_t value ) {
m_Index = value;
}
__forceinline CBaseHandle( int iEntry, int iSerialNumber ) {
Init( iEntry, iSerialNumber );
}
__forceinline void Init( int iEntry, int iSerialNumber ) {
m_Index = iEntry | ( iSerialNumber << NUM_SERIAL_NUM_SHIFT_BITS );
}
__forceinline void Term( ) {
m_Index = INVALID_EHANDLE_INDEX;
}
__forceinline bool IsValid( ) const {
return m_Index != INVALID_EHANDLE_INDEX;
}
__forceinline int GetEntryIndex( ) const {
if( !IsValid( ) )
return NUM_ENT_ENTRIES - 1;
return m_Index & ENT_ENTRY_MASK;
}
__forceinline int GetSerialNumber( ) const {
return m_Index >> NUM_SERIAL_NUM_SHIFT_BITS;
}
__forceinline int ToInt( ) const {
return ( int )( m_Index );
}
__forceinline bool operator !=( const CBaseHandle& other ) const {
return m_Index != other.m_Index;
}
__forceinline bool operator ==( const CBaseHandle& other ) const {
return m_Index == other.m_Index;
}
__forceinline bool operator ==( const void* pEnt ) const {
return Get( ) == pEnt;
}
__forceinline bool operator !=( const void* pEnt ) const {
return Get( ) != pEnt;
}
__forceinline const CBaseHandle& operator=( const void* pEntity ) {
return Set( pEntity );
}
__forceinline const CBaseHandle& Set( const void* pEntity ) {
if( !pEntity )
m_Index = INVALID_EHANDLE_INDEX;
return *this;
}
__forceinline void* CBaseHandle::Get( ) const {
return nullptr;
}
ulong_t m_Index;
};
template< class t > class CHandle : public CBaseHandle {
public:
__forceinline CHandle( ) {}
__forceinline CHandle( int iEntry, int iSerialNumber ) {
Init( iEntry, iSerialNumber );
}
__forceinline CHandle( const CBaseHandle& handle ) : CBaseHandle( handle ) {}
__forceinline CHandle( t* pObj ) {
Term( );
Set( pObj );
}
__forceinline CHandle< t > FromIndex( int index ) {
CHandle< t > ret;
ret.m_Index = index;
return ret;
}
__forceinline t* Get( ) const {
return ( t * )( CBaseHandle::Get( ) );
}
__forceinline operator t*( ) {
return Get( );
}
__forceinline operator t*( ) const {
return Get( );
}
__forceinline bool operator !( ) const {
return !Get( );
}
__forceinline bool operator==( t* val ) const {
return Get( ) == val;
}
__forceinline bool operator!=( t* val ) const {
return Get( ) != val;
}
__forceinline void Set( const t* pVal ) {
CBaseHandle::Set( ( const void * )( pVal ) );
}
__forceinline const CBaseHandle& operator=( const t* val ) {
Set( val );
return *this;
}
__forceinline t* operator->( ) const {
return Get( );
}
};
using EHANDLE = CHandle< Entity >;