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.

355 lines
8.9 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #ifndef STRONGHANDLE_H
  9. #define STRONGHANDLE_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "tier2/tier2.h"
  14. #include "resourcesystem/iresourcesystem.h"
  15. //-----------------------------------------------------------------------------
  16. // Resource pointer;
  17. //-----------------------------------------------------------------------------
  18. template< class T >
  19. class CStrongHandle
  20. {
  21. public:
  22. typedef const ResourceBinding_t< T > *ResourceHandleTyped_t;
  23. // Constructors
  24. CStrongHandle();
  25. CStrongHandle( ResourceHandleTyped_t hResource );
  26. CStrongHandle( ResourceId_t nResourceId );
  27. CStrongHandle( const char *pFileName, const char *pSubResourceName );
  28. CStrongHandle( const CStrongHandle< T > &src );
  29. ~CStrongHandle();
  30. // Init, shutdown
  31. void Init( ResourceHandleTyped_t nResourceId );
  32. void Init( ResourceId_t nResourceId );
  33. void Init( const char *pFileName, const char *pSubResourceName );
  34. void Init( const CStrongHandle< T > &src );
  35. void Shutdown();
  36. // Assignment
  37. CStrongHandle< T >& operator=( ResourceHandleTyped_t hResource );
  38. // Is the resource actually in memory
  39. bool IsCached() const;
  40. // Forces the resource to be brought into or out of memory
  41. void CacheResource();
  42. void UncacheResource();
  43. // Forces a reload of the resource
  44. void ReloadResource();
  45. // Cast operators
  46. operator const T*() const;
  47. const T* operator->() const;
  48. operator ResourceHandleTyped_t() const;
  49. operator ResourceHandle_t() const;
  50. const ResourceBinding_t< T > *GetHandle() const;
  51. // Comparison operators
  52. bool operator==( ResourceHandle_t hResource ) const;
  53. bool operator==( ResourceHandleTyped_t hResource ) const;
  54. bool operator==( const CStrongHandle< T > &hResource ) const;
  55. bool operator!=( ResourceHandle_t hResource ) const;
  56. bool operator!=( ResourceHandleTyped_t hResource ) const;
  57. bool operator!=( const CStrongHandle< T > &hResource ) const;
  58. // Marks the resource as being used this frame
  59. void MarkUsed();
  60. private:
  61. const ResourceBinding_t< T > *m_pBinding;
  62. };
  63. //-----------------------------------------------------------------------------
  64. // Constructor, destructor
  65. //-----------------------------------------------------------------------------
  66. template< class T >
  67. FORCEINLINE CStrongHandle< T >::CStrongHandle()
  68. {
  69. m_pBinding = NULL;
  70. }
  71. template< class T >
  72. FORCEINLINE CStrongHandle< T >::CStrongHandle( const char *pFileName, const char *pSubResourceName )
  73. {
  74. m_pBinding = NULL;
  75. Init( pFileName, pSubResourceName );
  76. }
  77. template< class T >
  78. FORCEINLINE CStrongHandle< T >::CStrongHandle( ResourceId_t nResourceId )
  79. {
  80. m_pBinding = NULL;
  81. Init( nResourceId );
  82. }
  83. // FIXME: Do I want typed resource handles?
  84. template< class T >
  85. FORCEINLINE CStrongHandle< T >::CStrongHandle( ResourceHandleTyped_t hResource )
  86. {
  87. m_pBinding = NULL;
  88. Init( hResource );
  89. }
  90. template< class T >
  91. CStrongHandle< T >::CStrongHandle( const CStrongHandle< T > &src )
  92. {
  93. m_pBinding = NULL;
  94. Init( src );
  95. }
  96. template< class T >
  97. CStrongHandle< T >::~CStrongHandle()
  98. {
  99. Shutdown();
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Init, shutdown
  103. //-----------------------------------------------------------------------------
  104. template< class T >
  105. void CStrongHandle< T >::Init( ResourceId_t nResourceId )
  106. {
  107. if ( m_pBinding )
  108. {
  109. Shutdown();
  110. }
  111. IResourceTypeManager *pMgr = g_pResourceSystem->GetResourceManagerForType< T >();
  112. m_pBinding = (ResourceHandleTyped_t)pMgr->FindResource( nResourceId );
  113. Assert( m_pBinding );
  114. if ( m_pBinding )
  115. {
  116. ++m_pBinding->m_nRefCount;
  117. }
  118. }
  119. template< class T >
  120. void CStrongHandle< T >::Init( const char *pFileName, const char *pSubResourceName )
  121. {
  122. if ( m_pBinding )
  123. {
  124. Shutdown();
  125. }
  126. IResourceTypeManager *pMgr = g_pResourceSystem->GetResourceManagerForType< T >();
  127. m_pBinding = (ResourceHandleTyped_t)pMgr->FindOrCreateResource( pFileName, pSubResourceName );
  128. Assert( m_pBinding );
  129. if ( m_pBinding )
  130. {
  131. ++m_pBinding->m_nRefCount;
  132. }
  133. }
  134. template< class T >
  135. void CStrongHandle< T >::Init( ResourceHandleTyped_t hResource )
  136. {
  137. if ( m_pBinding )
  138. {
  139. Shutdown();
  140. }
  141. m_pBinding = hResource;
  142. if ( m_pBinding )
  143. {
  144. ++m_pBinding->m_nRefCount;
  145. }
  146. }
  147. template< class T >
  148. void CStrongHandle< T >::Init( const CStrongHandle< T > &src )
  149. {
  150. if ( m_pBinding )
  151. {
  152. Shutdown();
  153. }
  154. m_pBinding = src.m_pBinding;
  155. if ( m_pBinding )
  156. {
  157. ++m_pBinding->m_nRefCount;
  158. }
  159. }
  160. template< class T >
  161. void CStrongHandle< T >::Shutdown()
  162. {
  163. if ( m_pBinding )
  164. {
  165. --m_pBinding->m_nRefCount;
  166. m_pBinding = NULL;
  167. }
  168. }
  169. //-----------------------------------------------------------------------------
  170. // Assignment
  171. //-----------------------------------------------------------------------------
  172. template< class T >
  173. FORCEINLINE CStrongHandle< T >& CStrongHandle< T >::operator=( ResourceHandleTyped_t hResource )
  174. {
  175. Init( hResource );
  176. return *this;
  177. }
  178. //-----------------------------------------------------------------------------
  179. // Cast operators
  180. //-----------------------------------------------------------------------------
  181. template< class T >
  182. FORCEINLINE const ResourceBinding_t< T > *CStrongHandle< T >::GetHandle() const
  183. {
  184. Assert( m_pBinding );
  185. m_pBinding->m_nLastBindFrame = g_nResourceFrameCount;
  186. return m_pBinding;
  187. }
  188. template< class T >
  189. FORCEINLINE CStrongHandle< T >::operator const T*() const
  190. {
  191. Assert( m_pBinding );
  192. m_pBinding->m_nLastBindFrame = g_nResourceFrameCount;
  193. return ( const T* )m_pBinding->m_pData;
  194. }
  195. template< class T >
  196. FORCEINLINE const T *CStrongHandle< T >::operator->() const
  197. {
  198. Assert( m_pBinding );
  199. m_pBinding->m_nLastBindFrame = g_nResourceFrameCount;
  200. return ( const T* )m_pBinding->m_pData;
  201. }
  202. template< class T >
  203. FORCEINLINE CStrongHandle< T >::operator ResourceHandleTyped_t() const
  204. {
  205. Assert( m_pBinding );
  206. m_pBinding->m_nLastBindFrame = g_nResourceFrameCount;
  207. return m_pBinding;
  208. }
  209. template< class T >
  210. FORCEINLINE CStrongHandle< T >::operator ResourceHandle_t() const
  211. {
  212. Assert( m_pBinding );
  213. m_pBinding->m_nLastBindFrame = g_nResourceFrameCount;
  214. return (ResourceHandle_t)m_pBinding;
  215. }
  216. //-----------------------------------------------------------------------------
  217. // Comparison operators
  218. //-----------------------------------------------------------------------------
  219. template< class T >
  220. inline bool CStrongHandle< T >::operator==( ResourceHandle_t hResource ) const
  221. {
  222. return m_pBinding == hResource;
  223. }
  224. template< class T >
  225. inline bool CStrongHandle< T >::operator==( ResourceHandleTyped_t hResource ) const
  226. {
  227. return m_pBinding == hResource;
  228. }
  229. template< class T >
  230. inline bool CStrongHandle< T >::operator==( const CStrongHandle< T > &hResource ) const
  231. {
  232. return m_pBinding == hResource.m_pBinding;
  233. }
  234. template< class T >
  235. inline bool CStrongHandle< T >::operator!=( ResourceHandle_t hResource ) const
  236. {
  237. return m_pBinding != hResource;
  238. }
  239. template< class T >
  240. inline bool CStrongHandle< T >::operator!=( ResourceHandleTyped_t hResource ) const
  241. {
  242. return m_pBinding != hResource;
  243. }
  244. template< class T >
  245. inline bool CStrongHandle< T >::operator!=( const CStrongHandle< T > &hResource ) const
  246. {
  247. return m_pBinding != hResource.m_pBinding;
  248. }
  249. //-----------------------------------------------------------------------------
  250. // Is the resource actually in memory
  251. //-----------------------------------------------------------------------------
  252. template< class T >
  253. inline bool CStrongHandle< T >::IsCached() const
  254. {
  255. Assert( m_pBinding );
  256. return ( m_pBinding->m_nFlags & RESOURCE_BINDING_CACHED ) != 0;
  257. }
  258. //-----------------------------------------------------------------------------
  259. // Marks the resource as being used this frame
  260. //-----------------------------------------------------------------------------
  261. template< class T >
  262. inline void CStrongHandle< T >::MarkUsed()
  263. {
  264. m_pBinding->m_nLastBindFrame = g_nResourceFrameCount;
  265. }
  266. //-----------------------------------------------------------------------------
  267. // Forces the resource to be brought into memory
  268. //-----------------------------------------------------------------------------
  269. template< class T >
  270. void CStrongHandle< T >::CacheResource()
  271. {
  272. Assert( m_pBinding );
  273. IResourceTypeManager *pMgr = g_pResourceSystem->GetResourceManagerForType< T >();
  274. pMgr->CacheResource( (ResourceHandle_t)m_pBinding );
  275. }
  276. template< class T >
  277. void CStrongHandle< T >::UncacheResource()
  278. {
  279. Assert( m_pBinding );
  280. IResourceTypeManager *pMgr = g_pResourceSystem->GetResourceManagerForType< T >();
  281. pMgr->UncacheResource( (ResourceHandle_t)m_pBinding );
  282. }
  283. //-----------------------------------------------------------------------------
  284. // Forces a reload of the resource
  285. //-----------------------------------------------------------------------------
  286. template< class T >
  287. void CStrongHandle< T >::ReloadResource()
  288. {
  289. Assert( m_pBinding );
  290. IResourceTypeManager *pMgr = g_pResourceSystem->GetResourceManagerForType< T >();
  291. pMgr->UncacheResource( (ResourceHandle_t)m_pBinding );
  292. pMgr->CacheResource( (ResourceHandle_t)m_pBinding );
  293. }
  294. #endif // STRONGHANDLE_H