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.

263 lines
5.6 KiB

  1. //========= Copyright � 1996-2005, Valve LLC, All rights reserved. ============
  2. //
  3. // The copyright to the contents herein is the property of Valve, L.L.C.
  4. // The contents may be used and/or copied only with the written permission of
  5. // Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  6. // the agreement/contract under which the contents have been supplied.
  7. //
  8. // Purpose:
  9. //
  10. // $NoKeywords: $
  11. //=============================================================================
  12. #ifndef DMEHANDLE_H
  13. #define DMEHANDLE_H
  14. #ifdef _WIN32
  15. #pragma once
  16. #endif
  17. #include "datamodel/idatamodel.h"
  18. #include "datamodel/dmelement.h"
  19. //-----------------------------------------------------------------------------
  20. // Purpose: CDmeHandle is a templatized wrapper around DmElementHandle_t
  21. //-----------------------------------------------------------------------------
  22. template< class DmeType, HandleType_t HandleType = HT_WEAK >
  23. class CDmeHandle : public CDmeElementRefHelper
  24. {
  25. public:
  26. CDmeHandle() : m_handle( DMELEMENT_HANDLE_INVALID )
  27. {
  28. }
  29. explicit CDmeHandle( CDmElement *pObject ) : m_handle( DMELEMENT_HANDLE_INVALID )
  30. {
  31. Set( pObject );
  32. }
  33. CDmeHandle( DmElementHandle_t h ) : m_handle( DMELEMENT_HANDLE_INVALID )
  34. {
  35. Set( h );
  36. }
  37. CDmeHandle( const CDmeHandle< DmeType, HandleType > &handle ) : m_handle( DMELEMENT_HANDLE_INVALID )
  38. {
  39. Set( handle.m_handle );
  40. }
  41. template < class T, HandleType_t HT >
  42. CDmeHandle( const CDmeHandle< T, HT > &handle ) : m_handle( DMELEMENT_HANDLE_INVALID )
  43. {
  44. DmeType *p = ( T* )NULL; // triggers compiler error if converting from invalid handle type
  45. NOTE_UNUSED( p );
  46. Set( handle.GetHandle() );
  47. }
  48. ~CDmeHandle()
  49. {
  50. if ( !g_pDataModel )
  51. return; // some handles are static, and don't get destroyed until program termination
  52. Unref( m_handle, HandleType );
  53. }
  54. template < class T, HandleType_t HT >
  55. CDmeHandle& operator=( const CDmeHandle< T, HT > &handle )
  56. {
  57. DmeType *p = ( T* )NULL; // triggers compiler error if converting from invalid handle type
  58. NOTE_UNUSED( p );
  59. Set( handle.GetHandle() );
  60. return *this;
  61. }
  62. DmeType *Get()
  63. {
  64. return static_cast< DmeType* >( g_pDataModel->GetElement( m_handle ) );
  65. }
  66. const DmeType *Get() const
  67. {
  68. return static_cast< DmeType* >( g_pDataModel->GetElement( m_handle ) );
  69. }
  70. DmElementHandle_t GetHandle() const
  71. {
  72. return m_handle;
  73. }
  74. void Set( CDmElement *pObject )
  75. {
  76. Set( pObject ? pObject->GetHandle() : DMELEMENT_HANDLE_INVALID );
  77. }
  78. void Set( DmElementHandle_t h )
  79. {
  80. if ( h == m_handle )
  81. return;
  82. Unref( m_handle, HandleType );
  83. m_handle = h;
  84. if ( h != DMELEMENT_HANDLE_INVALID )
  85. {
  86. CDmElement *pElement = g_pDataModel->GetElement( m_handle );
  87. Assert( pElement );
  88. if ( pElement && !pElement->IsA( DmeType::GetStaticTypeSymbol() ) )
  89. {
  90. m_handle = DMELEMENT_HANDLE_INVALID;
  91. }
  92. }
  93. Ref( m_handle, HandleType );
  94. }
  95. operator DmeType*()
  96. {
  97. return Get();
  98. }
  99. operator const DmeType*() const
  100. {
  101. return Get();
  102. }
  103. operator DmElementHandle_t() const
  104. {
  105. return m_handle;
  106. }
  107. DmeType* operator->()
  108. {
  109. return Get();
  110. }
  111. const DmeType* operator->() const
  112. {
  113. return Get();
  114. }
  115. CDmeHandle& operator=( DmElementHandle_t h )
  116. {
  117. Set( h );
  118. return *this;
  119. }
  120. CDmeHandle& operator=( CDmElement *pObject )
  121. {
  122. Set( pObject );
  123. return *this;
  124. }
  125. bool operator==( const CDmeHandle< DmeType > &h ) const
  126. {
  127. return m_handle == h.m_handle;
  128. }
  129. bool operator!=( const CDmeHandle< DmeType > &h ) const
  130. {
  131. return !operator==( h );
  132. }
  133. bool operator<( const CDmeHandle< DmeType > &h ) const
  134. {
  135. return m_handle < h.m_handle;
  136. }
  137. bool operator==( DmeType *pObject ) const
  138. {
  139. DmElementHandle_t h = pObject ? pObject->GetHandle() : DMELEMENT_HANDLE_INVALID;
  140. return m_handle == h;
  141. }
  142. bool operator!=( DmeType *pObject ) const
  143. {
  144. return !operator==( pObject );
  145. }
  146. bool operator==( DmElementHandle_t h ) const
  147. {
  148. return ( m_handle == h );
  149. }
  150. bool operator!=( DmElementHandle_t h ) const
  151. {
  152. return ( m_handle != h );
  153. }
  154. operator bool() const
  155. {
  156. return ( Get() != NULL );
  157. }
  158. bool operator!() const
  159. {
  160. return ( Get() == NULL );
  161. }
  162. private:
  163. DmElementHandle_t m_handle;
  164. };
  165. typedef CDmeHandle< CDmElement, HT_STRONG > CDmeCountedHandle;
  166. typedef CDmeHandle< CDmElement, HT_UNDO > CDmeUndoHandle;
  167. //-----------------------------------------------------------------------------
  168. // Vector of element handles
  169. //-----------------------------------------------------------------------------
  170. typedef CUtlVector< CDmeHandle<CDmElement> > DmeHandleVec_t;
  171. // NOTE: these methods only append, so if there is already data in the list, it will remain
  172. template< typename T >
  173. inline void ConvertHandleToPtrVector( const CUtlVector< CDmeHandle< T > > &in, CUtlVector< T * > &out )
  174. {
  175. int c = in.Count();
  176. for ( int i = 0; i < c; ++i )
  177. {
  178. out.AddToTail( in[ i ].Get() );
  179. }
  180. }
  181. template< typename T >
  182. inline void ConvertPtrToHandleVector( const CUtlVector< T * > &in, CUtlVector< CDmeHandle< T > > &out )
  183. {
  184. int c = in.Count();
  185. for ( int i = 0; i < c; ++i )
  186. {
  187. out.AddToTail( CDmeHandle< T >( in[ i ] ) );
  188. }
  189. }
  190. //-----------------------------------------------------------------------------
  191. // helper class for undo classes to allow them to hold onto refcounted element handles
  192. //-----------------------------------------------------------------------------
  193. template< typename T >
  194. class CDmAttributeUndoStorageType
  195. {
  196. public:
  197. typedef T UndoStorageType;
  198. };
  199. template<>
  200. class CDmAttributeUndoStorageType< DmElementHandle_t >
  201. {
  202. public:
  203. typedef CDmeUndoHandle UndoStorageType;
  204. };
  205. template<>
  206. class CDmAttributeUndoStorageType< CUtlVector< DmElementHandle_t > >
  207. {
  208. public:
  209. typedef CUtlVector< CDmeUndoHandle > UndoStorageType;
  210. };
  211. #endif // DMEHANDLE_H