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.

190 lines
6.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  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/utlsymbol.h"
  17. //-----------------------------------------------------------------------------
  18. // Internal interface for IDmElementFactory
  19. //-----------------------------------------------------------------------------
  20. class IDmElementFactoryInternal : public IDmElementFactory
  21. {
  22. public:
  23. virtual void SetElementTypeSymbol( CUtlSymbol sym ) = 0;
  24. virtual bool IsAbstract() const = 0;
  25. };
  26. //-----------------------------------------------------------------------------
  27. // Class used to register factories into a global list
  28. //-----------------------------------------------------------------------------
  29. class CDmElementFactoryHelper
  30. {
  31. public:
  32. // Static list of helpers
  33. static CDmElementFactoryHelper *s_pHelpers[2];
  34. // Create all the hud elements
  35. static void InstallFactories( );
  36. public:
  37. // Construction
  38. CDmElementFactoryHelper( const char *pClassName, IDmElementFactoryInternal *pFactory, bool bIsStandardFactory );
  39. // Accessors
  40. CDmElementFactoryHelper *GetNext( void );
  41. const char *GetClassname();
  42. IDmElementFactoryInternal *GetFactory();
  43. private:
  44. // Next factory in list
  45. CDmElementFactoryHelper *m_pNext;
  46. // Creation function to use for this technology
  47. IDmElementFactoryInternal *m_pFactory;
  48. const char *m_pszClassname;
  49. };
  50. //-----------------------------------------------------------------------------
  51. // Inline methods
  52. //-----------------------------------------------------------------------------
  53. inline const char *CDmElementFactoryHelper::GetClassname()
  54. {
  55. return m_pszClassname;
  56. }
  57. inline IDmElementFactoryInternal *CDmElementFactoryHelper::GetFactory()
  58. {
  59. return m_pFactory;
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Helper Template factory for simple creation of factories
  63. //-----------------------------------------------------------------------------
  64. template <class T >
  65. class CDmElementFactory : public IDmElementFactoryInternal
  66. {
  67. public:
  68. CDmElementFactory( const char *pLookupName ) : m_pLookupName( pLookupName ) {}
  69. // Creation, destruction
  70. virtual CDmElement* Create( DmElementHandle_t handle, const char *pElementType, const char *pElementName, DmFileId_t fileid, const DmObjectId_t &id )
  71. {
  72. return new T( handle, m_pLookupName, id, pElementName, fileid );
  73. }
  74. virtual void Destroy( DmElementHandle_t hElement )
  75. {
  76. CDmElement *pElement = g_pDataModel->GetElement( hElement );
  77. if ( pElement )
  78. {
  79. T *pActualElement = static_cast< T* >( pElement );
  80. delete pActualElement;
  81. }
  82. }
  83. // Sets the type symbol, used for "isa" implementation
  84. virtual void SetElementTypeSymbol( CUtlSymbol sym )
  85. {
  86. T::SetTypeSymbol( sym );
  87. }
  88. virtual bool IsAbstract() const { return false; }
  89. private:
  90. const char *m_pLookupName;
  91. };
  92. template < class T >
  93. class CDmAbstractElementFactory : public IDmElementFactoryInternal
  94. {
  95. public:
  96. CDmAbstractElementFactory() {}
  97. // Creation, destruction
  98. virtual CDmElement* Create( DmElementHandle_t handle, const char *pElementType, const char *pElementName, DmFileId_t fileid, const DmObjectId_t &id )
  99. {
  100. return NULL;
  101. }
  102. virtual void Destroy( DmElementHandle_t hElement )
  103. {
  104. }
  105. // Sets the type symbol, used for "isa" implementation
  106. virtual void SetElementTypeSymbol( CUtlSymbol sym )
  107. {
  108. T::SetTypeSymbol( sym );
  109. }
  110. virtual bool IsAbstract() const { return true; }
  111. private:
  112. };
  113. //-----------------------------------------------------------------------------
  114. // Helper macro to create the class factory
  115. //-----------------------------------------------------------------------------
  116. #if defined( MOVIEOBJECTS_LIB ) || defined ( DATAMODEL_LIB ) || defined ( DMECONTROLS_LIB )
  117. #define IMPLEMENT_ELEMENT_FACTORY( lookupName, className ) \
  118. IMPLEMENT_ELEMENT( className ) \
  119. CDmElementFactory< className > g_##className##_Factory( #lookupName ); \
  120. CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, true ); \
  121. className *g_##className##LinkerHack = NULL;
  122. #define IMPLEMENT_ABSTRACT_ELEMENT( lookupName, className ) \
  123. IMPLEMENT_ELEMENT( className ) \
  124. CDmAbstractElementFactory< className > g_##className##_Factory; \
  125. CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, true ); \
  126. className *g_##className##LinkerHack = NULL;
  127. #else
  128. #define IMPLEMENT_ELEMENT_FACTORY( lookupName, className ) \
  129. IMPLEMENT_ELEMENT( className ) \
  130. CDmElementFactory< className > g_##className##_Factory( #lookupName ); \
  131. CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, false ); \
  132. className *g_##className##LinkerHack = NULL;
  133. #define IMPLEMENT_ABSTRACT_ELEMENT( lookupName, className ) \
  134. IMPLEMENT_ELEMENT( className ) \
  135. CDmAbstractElementFactory< className > g_##className##_Factory; \
  136. CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, false ); \
  137. className *g_##className##LinkerHack = NULL;
  138. #endif
  139. // Used by classes defined in movieobjects or scenedatabase that must be explicitly installed
  140. #define IMPLEMENT_ELEMENT_FACTORY_INSTALL_EXPLICITLY( lookupName, className ) \
  141. IMPLEMENT_ELEMENT( className ) \
  142. CDmElementFactory< className > g_##className##_Factory( #lookupName ); \
  143. CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, false ); \
  144. className *g_##className##LinkerHack = NULL;
  145. //-----------------------------------------------------------------------------
  146. // Installs dm element factories
  147. //-----------------------------------------------------------------------------
  148. void InstallDmElementFactories( );
  149. #endif // DMELEMENTFACTORYHELPER_H