diff options
| author | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
|---|---|---|
| committer | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
| commit | 3bf9df6b2785fa6d951086978a3e66f49427166a (patch) | |
| tree | 2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /public/datamodel/dmelementfactoryhelper.h | |
| download | archived-source-engine-2018-hl2-src-master.tar.xz archived-source-engine-2018-hl2-src-master.zip | |
Diffstat (limited to 'public/datamodel/dmelementfactoryhelper.h')
| -rw-r--r-- | public/datamodel/dmelementfactoryhelper.h | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/public/datamodel/dmelementfactoryhelper.h b/public/datamodel/dmelementfactoryhelper.h new file mode 100644 index 0000000..c4847cc --- /dev/null +++ b/public/datamodel/dmelementfactoryhelper.h @@ -0,0 +1,190 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +//============================================================================= + +#ifndef DMELEMENTFACTORYHELPER_H +#define DMELEMENTFACTORYHELPER_H +#ifdef _WIN32 +#pragma once +#endif + +#include "datamodel/idatamodel.h" +#include "datamodel/dmelement.h" +#include "datamodel/dmattribute.h" +#include "datamodel/dmattributevar.h" +#include "tier1/utlvector.h" +#include "tier1/utlsymbol.h" + + +//----------------------------------------------------------------------------- +// Internal interface for IDmElementFactory +//----------------------------------------------------------------------------- +class IDmElementFactoryInternal : public IDmElementFactory +{ +public: + virtual void SetElementTypeSymbol( CUtlSymbol sym ) = 0; + virtual bool IsAbstract() const = 0; +}; + + +//----------------------------------------------------------------------------- +// Class used to register factories into a global list +//----------------------------------------------------------------------------- +class CDmElementFactoryHelper +{ +public: + // Static list of helpers + static CDmElementFactoryHelper *s_pHelpers[2]; + + // Create all the hud elements + static void InstallFactories( ); + +public: + // Construction + CDmElementFactoryHelper( const char *pClassName, IDmElementFactoryInternal *pFactory, bool bIsStandardFactory ); + + // Accessors + CDmElementFactoryHelper *GetNext( void ); + + const char *GetClassname(); + IDmElementFactoryInternal *GetFactory(); + +private: + // Next factory in list + CDmElementFactoryHelper *m_pNext; + // Creation function to use for this technology + IDmElementFactoryInternal *m_pFactory; + const char *m_pszClassname; +}; + + +//----------------------------------------------------------------------------- +// Inline methods +//----------------------------------------------------------------------------- +inline const char *CDmElementFactoryHelper::GetClassname() +{ + return m_pszClassname; +} + +inline IDmElementFactoryInternal *CDmElementFactoryHelper::GetFactory() +{ + return m_pFactory; +} + + +//----------------------------------------------------------------------------- +// Helper Template factory for simple creation of factories +//----------------------------------------------------------------------------- +template <class T > +class CDmElementFactory : public IDmElementFactoryInternal +{ +public: + CDmElementFactory( const char *pLookupName ) : m_pLookupName( pLookupName ) {} + + // Creation, destruction + virtual CDmElement* Create( DmElementHandle_t handle, const char *pElementType, const char *pElementName, DmFileId_t fileid, const DmObjectId_t &id ) + { + return new T( handle, m_pLookupName, id, pElementName, fileid ); + } + + virtual void Destroy( DmElementHandle_t hElement ) + { + CDmElement *pElement = g_pDataModel->GetElement( hElement ); + if ( pElement ) + { + T *pActualElement = static_cast< T* >( pElement ); + delete pActualElement; + } + } + + // Sets the type symbol, used for "isa" implementation + virtual void SetElementTypeSymbol( CUtlSymbol sym ) + { + T::SetTypeSymbol( sym ); + } + + virtual bool IsAbstract() const { return false; } + +private: + const char *m_pLookupName; +}; + + +template < class T > +class CDmAbstractElementFactory : public IDmElementFactoryInternal +{ +public: + CDmAbstractElementFactory() {} + + // Creation, destruction + virtual CDmElement* Create( DmElementHandle_t handle, const char *pElementType, const char *pElementName, DmFileId_t fileid, const DmObjectId_t &id ) + { + return NULL; + } + + virtual void Destroy( DmElementHandle_t hElement ) + { + } + + // Sets the type symbol, used for "isa" implementation + virtual void SetElementTypeSymbol( CUtlSymbol sym ) + { + T::SetTypeSymbol( sym ); + } + + virtual bool IsAbstract() const { return true; } + +private: +}; + + +//----------------------------------------------------------------------------- +// Helper macro to create the class factory +//----------------------------------------------------------------------------- +#if defined( MOVIEOBJECTS_LIB ) || defined ( DATAMODEL_LIB ) || defined ( DMECONTROLS_LIB ) + +#define IMPLEMENT_ELEMENT_FACTORY( lookupName, className ) \ + IMPLEMENT_ELEMENT( className ) \ + CDmElementFactory< className > g_##className##_Factory( #lookupName ); \ + CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, true ); \ + className *g_##className##LinkerHack = NULL; + +#define IMPLEMENT_ABSTRACT_ELEMENT( lookupName, className ) \ + IMPLEMENT_ELEMENT( className ) \ + CDmAbstractElementFactory< className > g_##className##_Factory; \ + CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, true ); \ + className *g_##className##LinkerHack = NULL; + +#else + +#define IMPLEMENT_ELEMENT_FACTORY( lookupName, className ) \ + IMPLEMENT_ELEMENT( className ) \ + CDmElementFactory< className > g_##className##_Factory( #lookupName ); \ + CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, false ); \ + className *g_##className##LinkerHack = NULL; + +#define IMPLEMENT_ABSTRACT_ELEMENT( lookupName, className ) \ + IMPLEMENT_ELEMENT( className ) \ + CDmAbstractElementFactory< className > g_##className##_Factory; \ + CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, false ); \ + className *g_##className##LinkerHack = NULL; + +#endif + + +// Used by classes defined in movieobjects or scenedatabase that must be explicitly installed +#define IMPLEMENT_ELEMENT_FACTORY_INSTALL_EXPLICITLY( lookupName, className ) \ + IMPLEMENT_ELEMENT( className ) \ + CDmElementFactory< className > g_##className##_Factory( #lookupName ); \ + CDmElementFactoryHelper g_##className##_Helper( #lookupName, &g_##className##_Factory, false ); \ + className *g_##className##LinkerHack = NULL; + +//----------------------------------------------------------------------------- +// Installs dm element factories +//----------------------------------------------------------------------------- +void InstallDmElementFactories( ); + + +#endif // DMELEMENTFACTORYHELPER_H |