Counter Strike : Global Offensive Source Code
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.

196 lines
4.6 KiB

  1. //========= Copyright � 1996-2005, 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. // Only safe to use in cases where you can statically verify that T* can safely be reinterpret-casted
  32. // to IHandleEntity*; that is, that it's derived from IHandleEntity and IHandleEntity is the
  33. // first base class.
  34. //
  35. // Unfortunately some classes are forward-declared and the compiler can't determine at compile time
  36. // how to static_cast<> them to IHandleEntity.
  37. // -------------------------------------------------------------------------------------------------- //
  38. template< class T >
  39. class CHandle : public CBaseHandle
  40. {
  41. public:
  42. CHandle();
  43. CHandle( int iEntry, int iSerialNumber );
  44. /*implicit*/ CHandle( T *pVal );
  45. /*implicit*/ CHandle( INVALID_EHANDLE_tag );
  46. // NOTE: The following two constructor functions are not type-safe, and can allow creating a
  47. // CHandle<T> that doesn't actually point to an object of type T.
  48. //
  49. // It is your responsibility to ensure that the target of the handle actually points to the
  50. // correct type of object before calling these functions.
  51. static CHandle<T> UnsafeFromBaseHandle( const CBaseHandle& handle );
  52. // The index should have come from a call to CBaseHandle::ToInt(). If it hasn't, you're in trouble.
  53. static CHandle<T> UnsafeFromIndex( int index );
  54. T* Get() const;
  55. void Set( const T* pVal );
  56. /*implicit*/ operator T*();
  57. /*implicit*/ operator T*() const;
  58. bool operator !() const;
  59. bool operator==( T *val ) const;
  60. bool operator!=( T *val ) const;
  61. CHandle& operator=( const T *val );
  62. T* operator->() const;
  63. };
  64. // ----------------------------------------------------------------------- //
  65. // Inlines.
  66. // ----------------------------------------------------------------------- //
  67. template<class T>
  68. inline CHandle<T>::CHandle()
  69. {
  70. }
  71. template<class T>
  72. inline CHandle<T>::CHandle( INVALID_EHANDLE_tag )
  73. : CBaseHandle( INVALID_EHANDLE )
  74. {
  75. }
  76. template<class T>
  77. inline CHandle<T>::CHandle( int iEntry, int iSerialNumber )
  78. {
  79. Init( iEntry, iSerialNumber );
  80. }
  81. template<class T>
  82. inline CHandle<T>::CHandle( T *pObj )
  83. : CBaseHandle( INVALID_EHANDLE )
  84. {
  85. Set( pObj );
  86. }
  87. template<class T>
  88. inline CHandle<T> CHandle<T>::UnsafeFromBaseHandle( const CBaseHandle &handle )
  89. {
  90. CHandle<T> ret;
  91. ret.m_Index = handle.m_Index;
  92. return ret;
  93. }
  94. template<class T>
  95. inline CHandle<T> CHandle<T>::UnsafeFromIndex( int index )
  96. {
  97. CHandle<T> ret;
  98. ret.m_Index = index;
  99. return ret;
  100. }
  101. template<class T>
  102. inline T* CHandle<T>::Get() const
  103. {
  104. return (T*)CBaseHandle::Get();
  105. }
  106. template<class T>
  107. inline CHandle<T>::operator T *()
  108. {
  109. return Get( );
  110. }
  111. template<class T>
  112. inline CHandle<T>::operator T *() const
  113. {
  114. return Get( );
  115. }
  116. template<class T>
  117. inline bool CHandle<T>::operator !() const
  118. {
  119. return !Get();
  120. }
  121. template<class T>
  122. inline bool CHandle<T>::operator==( T *val ) const
  123. {
  124. return Get() == val;
  125. }
  126. template<class T>
  127. inline bool CHandle<T>::operator!=( T *val ) const
  128. {
  129. return Get() != val;
  130. }
  131. template<class T>
  132. void CHandle<T>::Set( const T* pVal )
  133. {
  134. // We can't even verify that the class can successfully reinterpret-cast to IHandleEntity*
  135. // because that will cause this function to fail to compile in Debug in the case of forward-declared
  136. // pointer types.
  137. //Assert( reinterpret_cast< const IHandleEntity* >( pVal ) == static_cast< IHandleEntity* >( pVal ) );
  138. const IHandleEntity* pValInterface = reinterpret_cast<const IHandleEntity*>( pVal );
  139. CBaseHandle::Set( pValInterface );
  140. }
  141. template<class T>
  142. inline CHandle<T>& CHandle<T>::operator=( const T *val )
  143. {
  144. Set( val );
  145. return *this;
  146. }
  147. template<class T>
  148. T* CHandle<T>::operator -> () const
  149. {
  150. return Get();
  151. }
  152. // specialization of EnsureValidValue for CHandle<T>
  153. template<typename T>
  154. FORCEINLINE void EnsureValidValue( CHandle<T> &x ) { x = INVALID_EHANDLE; }
  155. #endif // EHANDLE_H