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.

248 lines
8.1 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //http://www.nfl.com/gamecenter/2010010900/2009/POST18/eagles@cowboys#tab:watch
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef DMELEMENTFACTORYHELPER_H
  7. #define DMELEMENTFACTORYHELPER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "datamodel/idatamodel.h"
  12. #include "datamodel/dmelement.h"
  13. #include "datamodel/dmattribute.h"
  14. #include "datamodel/dmattributevar.h"
  15. #include "tier1/utlvector.h"
  16. #include "tier1/utlsymbollarge.h"
  17. //-----------------------------------------------------------------------------
  18. // Internal interface for IDmElementFactory
  19. //-----------------------------------------------------------------------------
  20. class CDmElementFactoryInternal : public IDmElementFactory
  21. {
  22. public:
  23. virtual void SetElementTypeSymbol( CUtlSymbolLarge sym ) = 0;
  24. virtual bool IsAbstract() const = 0;
  25. virtual CUtlSymbolLarge GetElementTypeSymbol() const = 0;
  26. virtual CUtlSymbolLarge GetParentElementTypeSymbol() const = 0;
  27. virtual void AddOnElementCreatedCallback( IDmeElementCreated *pCallback );
  28. virtual void RemoveOnElementCreatedCallback( IDmeElementCreated *pCallback );
  29. virtual void OnElementCreated( CDmElement* pElement );
  30. private:
  31. CUtlVector< IDmeElementCreated* > m_CallBackList;
  32. };
  33. //-----------------------------------------------------------------------------
  34. // Class used to register factories into a global list
  35. //-----------------------------------------------------------------------------
  36. class CDmElementFactoryHelper
  37. {
  38. public:
  39. // Static list of helpers
  40. static CDmElementFactoryHelper *s_pHelpers[2];
  41. // Create all the hud elements
  42. static void InstallFactories( );
  43. public:
  44. // Construction
  45. CDmElementFactoryHelper( const char *pClassName, CDmElementFactoryInternal *pFactory, bool bIsStandardFactory );
  46. // Accessors
  47. CDmElementFactoryHelper *GetNext () { return m_pNext; }
  48. CDmElementFactoryHelper *GetParent () { return m_pParent; }
  49. CDmElementFactoryHelper *GetChild () { return m_pChild; }
  50. CDmElementFactoryHelper *GetSibling() { return m_pSibling; }
  51. const char *GetClassname();
  52. CDmElementFactoryInternal *GetFactory();
  53. private:
  54. CDmElementFactoryHelper() {}
  55. // Next factory in list
  56. CDmElementFactoryHelper *m_pNext;
  57. // class hierarchy links
  58. CDmElementFactoryHelper *m_pParent;
  59. CDmElementFactoryHelper *m_pChild;
  60. CDmElementFactoryHelper *m_pSibling;
  61. // Creation function to use for this technology
  62. CDmElementFactoryInternal *m_pFactory;
  63. const char *m_pszClassname;
  64. };
  65. //-----------------------------------------------------------------------------
  66. // Inline methods
  67. //-----------------------------------------------------------------------------
  68. inline const char *CDmElementFactoryHelper::GetClassname()
  69. {
  70. return m_pszClassname;
  71. }
  72. inline CDmElementFactoryInternal *CDmElementFactoryHelper::GetFactory()
  73. {
  74. return m_pFactory;
  75. }
  76. //-----------------------------------------------------------------------------
  77. // Helper Template factory for simple creation of factories
  78. //-----------------------------------------------------------------------------
  79. template < class T >
  80. class CDmElementFactory : public CDmElementFactoryInternal
  81. {
  82. public:
  83. CDmElementFactory( const char *pLookupName ) : m_pLookupName( pLookupName ) {}
  84. // Creation, destruction
  85. virtual CDmElement* Create( DmElementHandle_t handle, const char *pElementType, const char *pElementName, DmFileId_t fileid, const DmObjectId_t &id )
  86. {
  87. return new T( handle, m_pLookupName, id, pElementName, fileid );
  88. }
  89. virtual void Destroy( DmElementHandle_t hElement )
  90. {
  91. CDmElement *pElement = g_pDataModel->GetElement( hElement );
  92. if ( pElement )
  93. {
  94. T *pActualElement = static_cast< T* >( pElement );
  95. delete pActualElement;
  96. }
  97. }
  98. // Sets the type symbol, used for "isa" implementation
  99. virtual void SetElementTypeSymbol( CUtlSymbolLarge sym )
  100. {
  101. T::SetTypeSymbol( sym );
  102. }
  103. virtual bool IsAbstract() const { return false; }
  104. virtual CUtlSymbolLarge GetElementTypeSymbol() const
  105. {
  106. return T::GetStaticTypeSymbol();
  107. }
  108. virtual CUtlSymbolLarge GetParentElementTypeSymbol() const
  109. {
  110. CUtlSymbolLarge baseClassSym = T::BaseClass::GetStaticTypeSymbol();
  111. if ( baseClassSym == T::GetStaticTypeSymbol() )
  112. return UTL_INVAL_SYMBOL_LARGE; // only CDmElement has itself as it's BaseClass - this lets us know we're at the top of the hierarchy
  113. return baseClassSym;
  114. }
  115. private:
  116. const char *m_pLookupName;
  117. };
  118. template < class T >
  119. class CDmAbstractElementFactory : public CDmElementFactoryInternal
  120. {
  121. public:
  122. CDmAbstractElementFactory() {}
  123. // Creation, destruction
  124. virtual CDmElement* Create( DmElementHandle_t handle, const char *pElementType, const char *pElementName, DmFileId_t fileid, const DmObjectId_t &id )
  125. {
  126. return NULL;
  127. }
  128. virtual void Destroy( DmElementHandle_t hElement )
  129. {
  130. }
  131. // Sets the type symbol, used for "isa" implementation
  132. virtual void SetElementTypeSymbol( CUtlSymbolLarge sym )
  133. {
  134. T::SetTypeSymbol( sym );
  135. }
  136. virtual bool IsAbstract() const { return true; }
  137. virtual CUtlSymbolLarge GetElementTypeSymbol() const
  138. {
  139. return T::GetStaticTypeSymbol();
  140. }
  141. virtual CUtlSymbolLarge GetParentElementTypeSymbol() const
  142. {
  143. CUtlSymbolLarge baseClassSym = T::BaseClass::GetStaticTypeSymbol();
  144. if ( baseClassSym == T::GetStaticTypeSymbol() )
  145. return UTL_INVAL_SYMBOL_LARGE; // only CDmElement has itself as it's BaseClass - this lets us know we're at the top of the hierarchy
  146. return baseClassSym;
  147. }
  148. private:
  149. };
  150. //-----------------------------------------------------------------------------
  151. // Helper macro to create the class factory
  152. //-----------------------------------------------------------------------------
  153. #if defined( MOVIEOBJECTS_LIB ) || defined ( DATAMODEL_LIB ) || defined ( DMECONTROLS_LIB ) || defined ( MDLOBJECTS_LIB )
  154. #define IMPLEMENT_ELEMENT_FACTORY( lookupName, className ) \
  155. IMPLEMENT_ELEMENT( className ) \
  156. CDmElementFactory< className > g_##className##_Factory( #lookupName ); \
  157. CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, true ); \
  158. className *g_##className##LinkerHack = NULL;
  159. #define IMPLEMENT_ABSTRACT_ELEMENT( lookupName, className ) \
  160. IMPLEMENT_ELEMENT( className ) \
  161. CDmAbstractElementFactory< className > g_##className##_Factory; \
  162. CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, true ); \
  163. className *g_##className##LinkerHack = NULL;
  164. #else
  165. #define IMPLEMENT_ELEMENT_FACTORY( lookupName, className ) \
  166. IMPLEMENT_ELEMENT( className ) \
  167. CDmElementFactory< className > g_##className##_Factory( #lookupName ); \
  168. CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, false ); \
  169. className *g_##className##LinkerHack = NULL;
  170. #define IMPLEMENT_ABSTRACT_ELEMENT( lookupName, className ) \
  171. IMPLEMENT_ELEMENT( className ) \
  172. CDmAbstractElementFactory< className > g_##className##_Factory; \
  173. CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, false ); \
  174. className *g_##className##LinkerHack = NULL;
  175. #endif
  176. // Used by classes defined in movieobjects or scenedatabase that must be explicitly installed
  177. #define IMPLEMENT_ELEMENT_FACTORY_INSTALL_EXPLICITLY( lookupName, className ) \
  178. IMPLEMENT_ELEMENT( className ) \
  179. CDmElementFactory< className > g_##className##_Factory( #lookupName ); \
  180. CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, false ); \
  181. className *g_##className##LinkerHack = NULL;
  182. //-----------------------------------------------------------------------------
  183. // Used to instantiate classes in libs from dlls/exes
  184. //-----------------------------------------------------------------------------
  185. #define USING_ELEMENT_FACTORY( className ) \
  186. extern C##className *g_##C##className##LinkerHack; \
  187. C##className *g_##C##className##PullInModule = g_##C##className##LinkerHack;
  188. //-----------------------------------------------------------------------------
  189. // Installs dm element factories
  190. //-----------------------------------------------------------------------------
  191. void InstallDmElementFactories( );
  192. #endif // DMELEMENTFACTORYHELPER_H