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.

169 lines
3.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef EHANDLE_H
  8. #define EHANDLE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #if defined( _DEBUG ) && defined( GAME_DLL )
  13. #include "tier0/dbg.h"
  14. #include "cbase.h"
  15. #endif
  16. #include "const.h"
  17. #include "basehandle.h"
  18. #include "entitylist_base.h"
  19. class IHandleEntity;
  20. // -------------------------------------------------------------------------------------------------- //
  21. // Game-code CBaseHandle implementation.
  22. // -------------------------------------------------------------------------------------------------- //
  23. inline IHandleEntity* CBaseHandle::Get() const
  24. {
  25. extern CBaseEntityList *g_pEntityList;
  26. return g_pEntityList->LookupEntity( *this );
  27. }
  28. // -------------------------------------------------------------------------------------------------- //
  29. // CHandle.
  30. // -------------------------------------------------------------------------------------------------- //
  31. template< class T >
  32. class CHandle : public CBaseHandle
  33. {
  34. public:
  35. CHandle();
  36. CHandle( int iEntry, int iSerialNumber );
  37. CHandle( const CBaseHandle &handle );
  38. CHandle( T *pVal );
  39. // The index should have come from a call to ToInt(). If it hasn't, you're in trouble.
  40. static CHandle<T> FromIndex( int index );
  41. T* Get() const;
  42. void Set( const T* pVal );
  43. operator T*();
  44. operator T*() const;
  45. bool operator !() const;
  46. bool operator==( T *val ) const;
  47. bool operator!=( T *val ) const;
  48. const CBaseHandle& operator=( const T *val );
  49. T* operator->() const;
  50. };
  51. // ----------------------------------------------------------------------- //
  52. // Inlines.
  53. // ----------------------------------------------------------------------- //
  54. template<class T>
  55. CHandle<T>::CHandle()
  56. {
  57. }
  58. template<class T>
  59. CHandle<T>::CHandle( int iEntry, int iSerialNumber )
  60. {
  61. Init( iEntry, iSerialNumber );
  62. }
  63. template<class T>
  64. CHandle<T>::CHandle( const CBaseHandle &handle )
  65. : CBaseHandle( handle )
  66. {
  67. }
  68. template<class T>
  69. CHandle<T>::CHandle( T *pObj )
  70. {
  71. Term();
  72. Set( pObj );
  73. }
  74. template<class T>
  75. inline CHandle<T> CHandle<T>::FromIndex( int index )
  76. {
  77. CHandle<T> ret;
  78. ret.m_Index = index;
  79. return ret;
  80. }
  81. template<class T>
  82. inline T* CHandle<T>::Get() const
  83. {
  84. return (T*)CBaseHandle::Get();
  85. }
  86. template<class T>
  87. inline CHandle<T>::operator T *()
  88. {
  89. return Get( );
  90. }
  91. template<class T>
  92. inline CHandle<T>::operator T *() const
  93. {
  94. return Get( );
  95. }
  96. template<class T>
  97. inline bool CHandle<T>::operator !() const
  98. {
  99. return !Get();
  100. }
  101. template<class T>
  102. inline bool CHandle<T>::operator==( T *val ) const
  103. {
  104. return Get() == val;
  105. }
  106. template<class T>
  107. inline bool CHandle<T>::operator!=( T *val ) const
  108. {
  109. return Get() != val;
  110. }
  111. template<class T>
  112. void CHandle<T>::Set( const T* pVal )
  113. {
  114. CBaseHandle::Set( reinterpret_cast<const IHandleEntity*>(pVal) );
  115. }
  116. template<class T>
  117. inline const CBaseHandle& CHandle<T>::operator=( const T *val )
  118. {
  119. Set( val );
  120. return *this;
  121. }
  122. template<class T>
  123. T* CHandle<T>::operator -> () const
  124. {
  125. return Get();
  126. }
  127. #endif // EHANDLE_H