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.

243 lines
5.0 KiB

  1. //========= Copyright Valve Corporation, 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. #include "datamodel/dmattribute.h"
  20. #include "datamodel/dmattributevar.h"
  21. //-----------------------------------------------------------------------------
  22. // Purpose: CDmeHandle is a templatized wrapper around DmElementHandle_t
  23. //-----------------------------------------------------------------------------
  24. template< class DmeType, bool Counted = false >
  25. class CDmeHandle : public CDmeElementRefHelper
  26. {
  27. public:
  28. CDmeHandle() : m_handle( DMELEMENT_HANDLE_INVALID )
  29. {
  30. }
  31. explicit CDmeHandle( CDmElement *pObject ) : m_handle( DMELEMENT_HANDLE_INVALID )
  32. {
  33. Set( pObject );
  34. }
  35. CDmeHandle( DmElementHandle_t h ) : m_handle( DMELEMENT_HANDLE_INVALID )
  36. {
  37. Set( h );
  38. }
  39. CDmeHandle( const CDmeHandle< DmeType, Counted > &handle ) : m_handle( DMELEMENT_HANDLE_INVALID )
  40. {
  41. Set( handle.m_handle );
  42. }
  43. template < class T, bool B >
  44. CDmeHandle( const CDmeHandle< T, B > &handle ) : m_handle( DMELEMENT_HANDLE_INVALID )
  45. {
  46. DmeType *p = ( T* )NULL; // triggers compiler error if converting from invalid handle type
  47. NOTE_UNUSED( p );
  48. Set( handle.GetHandle() );
  49. }
  50. ~CDmeHandle()
  51. {
  52. if ( !g_pDataModel )
  53. return; // some handles are static, and don't get destroyed until program termination
  54. Unref( m_handle, Counted );
  55. }
  56. template < class T, bool B >
  57. CDmeHandle& operator=( const CDmeHandle< T, B > &handle )
  58. {
  59. DmeType *p = ( T* )NULL; // triggers compiler error if converting from invalid handle type
  60. NOTE_UNUSED( p );
  61. Set( handle.GetHandle() );
  62. return *this;
  63. }
  64. DmeType *Get()
  65. {
  66. return static_cast< DmeType* >( g_pDataModel->GetElement( m_handle ) );
  67. }
  68. const DmeType *Get() const
  69. {
  70. return static_cast< DmeType* >( g_pDataModel->GetElement( m_handle ) );
  71. }
  72. DmElementHandle_t GetHandle() const
  73. {
  74. return m_handle;
  75. }
  76. void Set( CDmElement *pObject )
  77. {
  78. Set( pObject ? pObject->GetHandle() : DMELEMENT_HANDLE_INVALID );
  79. }
  80. void Set( DmElementHandle_t h )
  81. {
  82. if ( h == m_handle )
  83. return;
  84. Unref( m_handle, Counted );
  85. m_handle = h;
  86. if ( h != DMELEMENT_HANDLE_INVALID )
  87. {
  88. CDmElement *pElement = g_pDataModel->GetElement( m_handle );
  89. Assert( pElement );
  90. if ( pElement && !pElement->IsA( DmeType::GetStaticTypeSymbol() ) )
  91. {
  92. m_handle = DMELEMENT_HANDLE_INVALID;
  93. }
  94. }
  95. Ref( m_handle, Counted );
  96. }
  97. operator DmeType*()
  98. {
  99. return Get();
  100. }
  101. operator const DmeType*() const
  102. {
  103. return Get();
  104. }
  105. operator DmElementHandle_t() const
  106. {
  107. return m_handle;
  108. }
  109. DmeType* operator->()
  110. {
  111. return Get();
  112. }
  113. const DmeType* operator->() const
  114. {
  115. return Get();
  116. }
  117. CDmeHandle& operator=( DmElementHandle_t h )
  118. {
  119. Set( h );
  120. return *this;
  121. }
  122. CDmeHandle& operator=( CDmElement *pObject )
  123. {
  124. Set( pObject );
  125. return *this;
  126. }
  127. bool operator==( const CDmeHandle< DmeType > &h ) const
  128. {
  129. return m_handle == h.m_handle;
  130. }
  131. bool operator!=( const CDmeHandle< DmeType > &h ) const
  132. {
  133. return !operator==( h );
  134. }
  135. bool operator<( const CDmeHandle< DmeType > &h ) const
  136. {
  137. return m_handle < h.m_handle;
  138. }
  139. bool operator==( DmeType *pObject ) const
  140. {
  141. DmElementHandle_t h = pObject ? pObject->GetHandle() : DMELEMENT_HANDLE_INVALID;
  142. return m_handle == h;
  143. }
  144. bool operator!=( DmeType *pObject ) const
  145. {
  146. return !operator==( pObject );
  147. }
  148. bool operator==( DmElementHandle_t h ) const
  149. {
  150. return ( m_handle == h );
  151. }
  152. bool operator!=( DmElementHandle_t h ) const
  153. {
  154. return ( m_handle != h );
  155. }
  156. operator bool() const
  157. {
  158. return ( Get() != NULL );
  159. }
  160. bool operator!() const
  161. {
  162. return ( Get() == NULL );
  163. }
  164. private:
  165. DmElementHandle_t m_handle;
  166. };
  167. typedef CDmeHandle< CDmElement, true > CDmeCountedHandle;
  168. //-----------------------------------------------------------------------------
  169. // Vector of element handles
  170. //-----------------------------------------------------------------------------
  171. typedef CUtlVector< CDmeHandle<CDmElement> > DmeHandleVec_t;
  172. //-----------------------------------------------------------------------------
  173. // helper class for undo classes to allow them to hold onto refcounted element handles
  174. //-----------------------------------------------------------------------------
  175. template< typename T >
  176. class CDmAttributeUndoStorageType
  177. {
  178. public:
  179. typedef T UndoStorageType;
  180. };
  181. template<>
  182. class CDmAttributeUndoStorageType< DmElementHandle_t >
  183. {
  184. public:
  185. typedef CDmeCountedHandle UndoStorageType;
  186. };
  187. template<>
  188. class CDmAttributeUndoStorageType< CUtlVector< DmElementHandle_t > >
  189. {
  190. public:
  191. typedef CUtlVector< CDmeCountedHandle > UndoStorageType;
  192. };
  193. #endif // DMEHANDLE_H