Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
3.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef BASEHANDLE_H
  7. #define BASEHANDLE_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "const.h"
  12. #include "tier0/dbg.h"
  13. class IHandleEntity;
  14. // -------------------------------------------------------------------------------------------------- //
  15. // CBaseHandle.
  16. // -------------------------------------------------------------------------------------------------- //
  17. class CBaseHandle
  18. {
  19. friend class CBaseEntityList;
  20. public:
  21. CBaseHandle();
  22. CBaseHandle( const CBaseHandle &other );
  23. CBaseHandle( unsigned long value );
  24. CBaseHandle( int iEntry, int iSerialNumber );
  25. void Init( int iEntry, int iSerialNumber );
  26. void Term();
  27. // Even if this returns true, Get() still can return return a non-null value.
  28. // This just tells if the handle has been initted with any values.
  29. bool IsValid() const;
  30. int GetEntryIndex() const;
  31. int GetSerialNumber() const;
  32. int ToInt() const;
  33. bool operator !=( const CBaseHandle &other ) const;
  34. bool operator ==( const CBaseHandle &other ) const;
  35. bool operator ==( const IHandleEntity* pEnt ) const;
  36. bool operator !=( const IHandleEntity* pEnt ) const;
  37. bool operator <( const CBaseHandle &other ) const;
  38. bool operator <( const IHandleEntity* pEnt ) const;
  39. // Assign a value to the handle.
  40. const CBaseHandle& operator=( const IHandleEntity *pEntity );
  41. const CBaseHandle& Set( const IHandleEntity *pEntity );
  42. // Use this to dereference the handle.
  43. // Note: this is implemented in game code (ehandle.h)
  44. IHandleEntity* Get() const;
  45. protected:
  46. // The low NUM_SERIAL_BITS hold the index. If this value is less than MAX_EDICTS, then the entity is networkable.
  47. // The high NUM_SERIAL_NUM_BITS bits are the serial number.
  48. unsigned long m_Index;
  49. };
  50. #include "ihandleentity.h"
  51. inline CBaseHandle::CBaseHandle()
  52. {
  53. m_Index = INVALID_EHANDLE_INDEX;
  54. }
  55. inline CBaseHandle::CBaseHandle( const CBaseHandle &other )
  56. {
  57. m_Index = other.m_Index;
  58. }
  59. inline CBaseHandle::CBaseHandle( unsigned long value )
  60. {
  61. m_Index = value;
  62. }
  63. inline CBaseHandle::CBaseHandle( int iEntry, int iSerialNumber )
  64. {
  65. Init( iEntry, iSerialNumber );
  66. }
  67. inline void CBaseHandle::Init( int iEntry, int iSerialNumber )
  68. {
  69. Assert( iEntry >= 0 && iEntry < NUM_ENT_ENTRIES );
  70. Assert( iSerialNumber >= 0 && iSerialNumber < (1 << NUM_SERIAL_NUM_BITS) );
  71. m_Index = iEntry | (iSerialNumber << NUM_ENT_ENTRY_BITS);
  72. }
  73. inline void CBaseHandle::Term()
  74. {
  75. m_Index = INVALID_EHANDLE_INDEX;
  76. }
  77. inline bool CBaseHandle::IsValid() const
  78. {
  79. return m_Index != INVALID_EHANDLE_INDEX;
  80. }
  81. inline int CBaseHandle::GetEntryIndex() const
  82. {
  83. return m_Index & ENT_ENTRY_MASK;
  84. }
  85. inline int CBaseHandle::GetSerialNumber() const
  86. {
  87. return m_Index >> NUM_ENT_ENTRY_BITS;
  88. }
  89. inline int CBaseHandle::ToInt() const
  90. {
  91. return (int)m_Index;
  92. }
  93. inline bool CBaseHandle::operator !=( const CBaseHandle &other ) const
  94. {
  95. return m_Index != other.m_Index;
  96. }
  97. inline bool CBaseHandle::operator ==( const CBaseHandle &other ) const
  98. {
  99. return m_Index == other.m_Index;
  100. }
  101. inline bool CBaseHandle::operator ==( const IHandleEntity* pEnt ) const
  102. {
  103. return Get() == pEnt;
  104. }
  105. inline bool CBaseHandle::operator !=( const IHandleEntity* pEnt ) const
  106. {
  107. return Get() != pEnt;
  108. }
  109. inline bool CBaseHandle::operator <( const CBaseHandle &other ) const
  110. {
  111. return m_Index < other.m_Index;
  112. }
  113. inline bool CBaseHandle::operator <( const IHandleEntity *pEntity ) const
  114. {
  115. unsigned long otherIndex = (pEntity) ? pEntity->GetRefEHandle().m_Index : INVALID_EHANDLE_INDEX;
  116. return m_Index < otherIndex;
  117. }
  118. inline const CBaseHandle& CBaseHandle::operator=( const IHandleEntity *pEntity )
  119. {
  120. return Set( pEntity );
  121. }
  122. inline const CBaseHandle& CBaseHandle::Set( const IHandleEntity *pEntity )
  123. {
  124. if ( pEntity )
  125. {
  126. *this = pEntity->GetRefEHandle();
  127. }
  128. else
  129. {
  130. m_Index = INVALID_EHANDLE_INDEX;
  131. }
  132. return *this;
  133. }
  134. #endif // BASEHANDLE_H