// ----------------------------------------------------------------------- // // // MODULE : Factory.h // // PURPOSE : Factory definition // // CREATED : 05.12.1999 // // ----------------------------------------------------------------------- // #ifndef __FACTORY_H__ #define __FACTORY_H__ // PORT: the original spelled the macro parameter `##fact##` throughout. A // leading/trailing ## with nothing to paste against was an MSVC tolerance; // the standard preprocessor rejects it (clang additionally misreads `<##` // as an editor placeholder). `fact` alone is what these always expanded to, // so this is semantics-preserving and remains valid on MSVC. #define FACTORY_NEW(fact)\ CFactory::Create(); #define FACTORY_DELETE(fact)\ fact->Destroy(); #define DEFINE_FACTORY_CLASS(fact)\ public CFactory #define DEFINE_ABSTRACT_FACTORY_METHODS(fact) \ public:\ virtual void Constructor();\ virtual void Destructor();\ virtual void Destroy() {}\ private: #define DEFINE_FACTORY_METHODS(fact) \ public:\ virtual void Constructor();\ virtual void Destructor();\ virtual void Destroy() { CFactory::Destroy((fact*)this); }\ private: // PORT: defining a static member of a class-template specialization for a // concrete type requires the `template<>` prefix. MSVC accepted these without // it; the standard does not. Adding it is semantics-preserving and is also the // correct spelling for MSVC. // ⚠️ The initialisers below are REQUIRED, not decoration. An explicit // specialization of a static data member WITHOUT an initialiser is only a // DECLARATION -- it defines no storage. The original code omitted `template<>` // entirely, which MSVC accepted as a definition; adding `template<>` alone made // it compile while silently emitting nothing, leaving 93 x 3 undefined symbols // at link time. `= 0` reproduces the zero-initialisation the original got. #define IMPLEMENT_FACTORY(fact, size)\ template<> CFactory::CCleaner CFactory::s_Cleaner = CFactory::CCleaner();\ template<> int CFactory::s_cTYPE = 0;\ template<> int CFactory::s_iCursor = 0;\ template<> fact** CFactory::s_aPTYPE = CFactory::Initialize(size); template class CFactory { typedef TYPE* PTYPE; // For clarity public : // Public methods // Methods on the shared factory data static inline TYPE* Create(); // Methods per instance of a factory instantiated class virtual void Constructor() = 0; virtual void Destructor() = 0; static void Destroy(TYPE* pTYPE); private : // Private methods inline int GetFactoryIndex() { return m_iPTYPE; } inline void SetFactoryIndex(int iPTYPE) { m_iPTYPE = iPTYPE; } static inline bool Resize(int cTYPE); static inline PTYPE* Initialize(int cTYPE); static inline void AssertValid(); private : // Private inner classes class CCleaner { public : inline ~CCleaner() { if ( m_aPTYPE ) { // Delete all the allocated TYPEs and the TYPE* array for ( int iPTYPE = 0 ; iPTYPE < m_cTYPE ; iPTYPE++ ) { debug_delete(m_aPTYPE[iPTYPE]); m_aPTYPE[iPTYPE] = NULL; } debug_deletea(m_aPTYPE); m_aPTYPE = NULL; m_cTYPE = 0; } } PTYPE* m_aPTYPE; int m_cTYPE; }; private : // Private member variables static PTYPE* s_aPTYPE; static int s_cTYPE; static int s_iCursor; static CCleaner s_Cleaner; int m_iPTYPE; }; template TYPE* CFactory::Create() { if ( 0 == s_cTYPE ) { // We started with 0 types, so allocate one if ( !Resize(1) ) { // Couldn't grow the array, allocation failed. return NULL; } } else if ( s_iCursor >= s_cTYPE ) { // We ran out of TYPE*s, and need to grow the array (we grow by 2x) if ( !Resize(s_cTYPE*2) ) { // Couldn't grow the array, allocation failed. return NULL; } } // Clear it out before we return it ((CFactory*)s_aPTYPE[s_iCursor])->Constructor(); // Return the next available TYPE* #ifdef _DEBUG AssertValid(); #endif return s_aPTYPE[s_iCursor++]; } template void CFactory::Destroy(TYPE* pTYPE) { if ( !s_aPTYPE ) return; int iPTYPE = ((CFactory*)pTYPE)->GetFactoryIndex(); _ASSERT(s_iCursor > 0); _ASSERT(iPTYPE >= 0 && iPTYPE <= s_cTYPE); TYPE* pTemp = s_aPTYPE[--s_iCursor]; s_aPTYPE[s_iCursor] = s_aPTYPE[iPTYPE]; ((CFactory*)s_aPTYPE[s_iCursor])->SetFactoryIndex(s_iCursor); s_aPTYPE[iPTYPE] = pTemp; ((CFactory*)s_aPTYPE[iPTYPE])->SetFactoryIndex(iPTYPE); ((CFactory*)pTYPE)->Destructor(); #ifdef _DEBUG AssertValid(); #endif return; } template TYPE** CFactory::Initialize(int cTYPE) { typedef TYPE* PTYPE; // For clarity // If we were already initted then either someone explicitly Init'd us (bad) // or two classes are sharing the same factory (WORSE). _ASSERT(!s_aPTYPE); if ( s_aPTYPE ) return s_aPTYPE; PTYPE* aPTYPE; // Allocate a TYPE* array and creat the TYPEs if a valid size is specified if ( cTYPE > 0 ) { aPTYPE = debug_newa(PTYPE, cTYPE); for ( int iPTYPE = 0 ; iPTYPE < cTYPE ; iPTYPE++ ) { aPTYPE[iPTYPE] = debug_new(TYPE); ((CFactory*)aPTYPE[iPTYPE])->SetFactoryIndex(iPTYPE); } } else { aPTYPE = NULL; cTYPE = 0; } // Set the Cleaner information s_Cleaner.m_aPTYPE = aPTYPE; s_Cleaner.m_cTYPE = cTYPE; // Set our internal information s_aPTYPE = aPTYPE; s_cTYPE = cTYPE; s_iCursor = 0; return aPTYPE; } template bool CFactory::Resize(int cTYPE) { // We can't go smaller than our current amount of allocated TYPE*s if ( cTYPE < s_iCursor ) return false; // If we're changing to the same size, it's trivial. if ( cTYPE == s_cTYPE ) return true; typedef TYPE* PTYPE; // For clarity // Allocate a new TYPE* array of the appropriate size PTYPE* aPTYPE = debug_newa(PTYPE, cTYPE); // If we're growing... if ( cTYPE > s_cTYPE ) { // Copy all the existing TYPE* array into the new one int iPTYPE; for ( iPTYPE = 0 ; iPTYPE < s_cTYPE ; iPTYPE++ ) { aPTYPE[iPTYPE] = s_aPTYPE[iPTYPE]; ((CFactory*)aPTYPE[iPTYPE])->SetFactoryIndex(iPTYPE); } // Allocate all the additional TYPEs in the new, larger array for ( iPTYPE = s_cTYPE ; iPTYPE < cTYPE ; iPTYPE++ ) { aPTYPE[iPTYPE] = debug_new(TYPE); ((CFactory*)aPTYPE[iPTYPE])->SetFactoryIndex(iPTYPE); } } else { // Copy the number of TYPE*s into the new array as requested int iPTYPE; for ( iPTYPE = 0 ; iPTYPE < cTYPE ; iPTYPE++ ) { aPTYPE[iPTYPE] = s_aPTYPE[iPTYPE]; ((CFactory*)aPTYPE[iPTYPE])->SetFactoryIndex(iPTYPE); } // Delete all the old TYPEs past the end of the new TYPE* array for ( iPTYPE = s_cTYPE ; iPTYPE < cTYPE ; iPTYPE++ ) { debug_delete(aPTYPE[iPTYPE]); aPTYPE[iPTYPE] = NULL; } } // Delete our old TYPE* array debug_deletea(s_aPTYPE); // Set the new Cleaner information s_Cleaner.m_aPTYPE = aPTYPE; s_Cleaner.m_cTYPE = cTYPE; // Set our new internal information. The cursor will not change. s_aPTYPE = aPTYPE; s_cTYPE = cTYPE; return true; } template void CFactory::AssertValid() { for ( int iPTYPE = 0 ; iPTYPE < s_cTYPE ; iPTYPE++ ) { _ASSERT(((CFactory*)s_aPTYPE[iPTYPE])->GetFactoryIndex() == iPTYPE); } } #endif