forked from sears2424/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasehandle.h
177 lines (134 loc) · 3.89 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#ifndef BASEHANDLE_H
#define BASEHANDLE_H
#ifdef _WIN32
#pragma once
#endif
#include "const.h"
#include "tier0/dbg.h"
class IHandleEntity;
// -------------------------------------------------------------------------------------------------- //
// CBaseHandle.
// -------------------------------------------------------------------------------------------------- //
class CBaseHandle
{
friend class CBaseEntityList;
public:
CBaseHandle();
CBaseHandle( const CBaseHandle &other );
CBaseHandle( unsigned long value );
CBaseHandle( int iEntry, int iSerialNumber );
void Init( int iEntry, int iSerialNumber );
void Term();
// Even if this returns true, Get() still can return return a non-null value.
// This just tells if the handle has been initted with any values.
bool IsValid() const;
int GetEntryIndex() const;
int GetSerialNumber() const;
int ToInt() const;
bool operator !=( const CBaseHandle &other ) const;
bool operator ==( const CBaseHandle &other ) const;
bool operator ==( const IHandleEntity* pEnt ) const;
bool operator !=( const IHandleEntity* pEnt ) const;
bool operator <( const CBaseHandle &other ) const;
bool operator <( const IHandleEntity* pEnt ) const;
// Assign a value to the handle.
const CBaseHandle& operator=( const IHandleEntity *pEntity );
const CBaseHandle& Set( const IHandleEntity *pEntity );
// Use this to dereference the handle.
// Note: this is implemented in game code (ehandle.h)
IHandleEntity* Get() const;
protected:
// The low NUM_SERIAL_BITS hold the index. If this value is less than MAX_EDICTS, then the entity is networkable.
// The high NUM_SERIAL_NUM_BITS bits are the serial number.
unsigned long m_Index;
};
#include "ihandleentity.h"
inline CBaseHandle::CBaseHandle()
{
m_Index = INVALID_EHANDLE_INDEX;
}
inline CBaseHandle::CBaseHandle( const CBaseHandle &other )
{
m_Index = other.m_Index;
}
inline CBaseHandle::CBaseHandle( unsigned long value )
{
m_Index = value;
}
inline CBaseHandle::CBaseHandle( int iEntry, int iSerialNumber )
{
Init( iEntry, iSerialNumber );
}
inline void CBaseHandle::Init( int iEntry, int iSerialNumber )
{
Assert( iEntry >= 0 && iEntry < NUM_ENT_ENTRIES );
Assert( iSerialNumber >= 0 && iSerialNumber < (1 << NUM_SERIAL_NUM_BITS) );
m_Index = iEntry | (iSerialNumber << NUM_ENT_ENTRY_BITS);
}
inline void CBaseHandle::Term()
{
m_Index = INVALID_EHANDLE_INDEX;
}
inline bool CBaseHandle::IsValid() const
{
return m_Index != INVALID_EHANDLE_INDEX;
}
inline int CBaseHandle::GetEntryIndex() const
{
return m_Index & ENT_ENTRY_MASK;
}
inline int CBaseHandle::GetSerialNumber() const
{
return m_Index >> NUM_ENT_ENTRY_BITS;
}
inline int CBaseHandle::ToInt() const
{
return (int)m_Index;
}
inline bool CBaseHandle::operator !=( const CBaseHandle &other ) const
{
return m_Index != other.m_Index;
}
inline bool CBaseHandle::operator ==( const CBaseHandle &other ) const
{
return m_Index == other.m_Index;
}
inline bool CBaseHandle::operator ==( const IHandleEntity* pEnt ) const
{
return Get() == pEnt;
}
inline bool CBaseHandle::operator !=( const IHandleEntity* pEnt ) const
{
return Get() != pEnt;
}
inline bool CBaseHandle::operator <( const CBaseHandle &other ) const
{
return m_Index < other.m_Index;
}
inline bool CBaseHandle::operator <( const IHandleEntity *pEntity ) const
{
unsigned long otherIndex = (pEntity) ? pEntity->GetRefEHandle().m_Index : INVALID_EHANDLE_INDEX;
return m_Index < otherIndex;
}
inline const CBaseHandle& CBaseHandle::operator=( const IHandleEntity *pEntity )
{
return Set( pEntity );
}
inline const CBaseHandle& CBaseHandle::Set( const IHandleEntity *pEntity )
{
if ( pEntity )
{
*this = pEntity->GetRefEHandle();
}
else
{
m_Index = INVALID_EHANDLE_INDEX;
}
return *this;
}
#endif // BASEHANDLE_H