diff options
| author | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
|---|---|---|
| committer | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
| commit | 3bf9df6b2785fa6d951086978a3e66f49427166a (patch) | |
| tree | 2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /public/basehandle.h | |
| download | archived-source-engine-2018-hl2-src-master.tar.xz archived-source-engine-2018-hl2-src-master.zip | |
Diffstat (limited to 'public/basehandle.h')
| -rw-r--r-- | public/basehandle.h | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/public/basehandle.h b/public/basehandle.h new file mode 100644 index 0000000..91eb02d --- /dev/null +++ b/public/basehandle.h @@ -0,0 +1,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 |