// (c) 1997-2000 Monolith Productions, Inc. All Rights Reserved #ifndef __ANIMATIONSTD_H__ #define __ANIMATIONSTD_H__ // Strings namespace Animation { class CStr { public : // Ctors/Dtors/etc CStr() { m_szString[0] = 0; } CStr(const char* szString) { strcpy(m_szString, szString); } // Casting // // Note: casting into varargs must be made explicit (ie, printf, etc) // Not doing so will crash the c runtime. operator const char*() const { return m_szString; } operator char*() { return m_szString; } // Concatenation void operator+=(const CStr& sStr) { strcat(m_szString, sStr); } CStr operator+(const CStr& sStr) const { CStr sStrNew; sprintf(sStrNew, "%s%s", m_szString, (const char*)sStr); return sStrNew; } CStr operator+(const char* szStr) const { CStr sStrNew; sprintf(sStrNew, "%s%s", m_szString, szStr); return sStrNew; } // Case sensitive compare int operator==(const CStr& sStr) const { return !strcmp(m_szString, sStr.m_szString); } int operator==(const char* szStr) const { return !strcmp(m_szString, szStr); } // Case insensitive compare int operator^=(const CStr& sStr) const { return !stricmp(m_szString, sStr.m_szString); } int operator^=(const char* szStr) const { return !stricmp(m_szString, szStr); } private : friend class CStr; char m_szString[64]; }; // Stack template class CStack { public : inline CStack(); inline void Push(TYPE* pTYPE); inline TYPE* Pop(); inline TYPE* GetTop(); inline void Clear() { m_iTYPE = 0; } inline LTBOOL IsEmpty() { return m_iTYPE == 0; } inline int32 GetSize() { return m_iTYPE; } protected : int32 m_iTYPE; // The next free TYPE TYPE* m_aTYPEs[SIZE]; // The array of types }; // Methods template inline CStack::CStack() { m_iTYPE = 0; } template inline void CStack::Push(TYPE* pTYPE) { m_aTYPEs[m_iTYPE++] = pTYPE; } template inline TYPE* CStack::Pop() { return m_aTYPEs[--m_iTYPE]; } // Map template struct FnForEach { virtual void operator()(const t_ClsKey& Key, t_ClsValue Value) = 0; }; template class CMap { public : // Functors struct FnHash { virtual uint32 Hash(const t_ClsKey& Key) const = 0; }; struct FnCompare { virtual LTBOOL Compare(const t_ClsKey& Key1, const t_ClsKey& Key2) const = 0; }; public : // Ctors/Dtors CMap(); ~CMap(); // Methods virtual const FnHash& GetFnHash() const = 0; virtual const FnCompare& GetFnCompare() const = 0; t_ClsValue* Add(const t_ClsKey& clsKey, const t_ClsValue& clsValue); void Remove(const t_ClsKey& clsKey); LTBOOL Find(const t_ClsKey& clsKey, t_ClsValue** ppclsValue = LTNULL) const; void Clear(); void ForEach(FnForEach& fnForEach); protected : class CMapEntry { public : CMapEntry* m_pNext; t_ClsValue m_Value; t_ClsKey m_Key; CMapEntry() { m_pNext = LTNULL; } }; protected : CMapEntry* m_apEntries[t_nSize]; }; template CMap::CMap() { memset(m_apEntries, LTNULL, sizeof(CMapEntry*)*t_nSize); } template CMap::~CMap() { for ( uint32 iEntry = 0 ; iEntry < t_nSize ; iEntry++ ) { CMapEntry* pEntry = m_apEntries[iEntry]; while ( pEntry ) { CMapEntry* pEntryNext = pEntry->m_pNext; debug_delete(pEntry); pEntry = pEntryNext; } } } template t_ClsValue* CMap::Add(const t_ClsKey& clsKey, const t_ClsValue& clsValue) { uint32 iEntry = GetFnHash().Hash(clsKey)%t_nSize; CMapEntry* pEntry = debug_new(CMapEntry); pEntry->m_pNext = m_apEntries[iEntry]; pEntry->m_Key = clsKey; pEntry->m_Value = clsValue; m_apEntries[iEntry] = pEntry; return &pEntry->m_Value; } template void CMap::Remove(const t_ClsKey& clsKey) { uint32 iEntry = GetFnHash().Hash(clsKey)%t_nSize; CMapEntry* pEntry = m_apEntries[iEntry]; CMapEntry* pEntryPrev = LTNULL; while ( pEntry ) { if ( GetFnCompare().Compare(pEntry->m_Key, clsKey) ) { if ( pEntryPrev ) { pEntryPrev->m_pNext = pEntry->m_pNext; } else { m_apEntries[iEntry] = pEntry->m_pNext; } debug_delete(pEntry); return; } pEntryPrev = pEntry; pEntry = pEntry->m_pNext; } } template void CMap::ForEach(FnForEach& fnForEach) { for ( uint32 iEntry = 0 ; iEntry < t_nSize ; iEntry++ ) { CMapEntry* pEntry = m_apEntries[iEntry]; while ( pEntry ) { fnForEach(pEntry->m_Key, pEntry->m_Value); pEntry = pEntry->m_pNext; } } } template LTBOOL CMap::Find(const t_ClsKey& clsKey, t_ClsValue** ppclsValue /* = LTNULL */) const { uint32 iEntry = GetFnHash().Hash(clsKey)%t_nSize; CMapEntry* pEntry = m_apEntries[iEntry]; while ( pEntry ) { if ( GetFnCompare().Compare(pEntry->m_Key, clsKey) ) { if ( ppclsValue ) { *ppclsValue = &pEntry->m_Value; } return LTTRUE; } pEntry = pEntry->m_pNext; } return LTFALSE; } template void CMap::Clear() { for ( uint32 iEntry = 0 ; iEntry < t_nSize ; iEntry++ ) { CMapEntry* pEntry = m_apEntries[iEntry]; while ( pEntry ) { CMapEntry* pEntryNext = pEntry->m_pNext; debug_delete(pEntry); pEntry = pEntryNext; } m_apEntries[iEntry] = LTNULL; } } template class CMapStrStr : public CMap { private : // Functors struct FnHashCStr : public CMap::FnHash { uint32 Hash(CStr const& sString) const { return t_bCaseInsensitive ? toupper(sString[0]) : sString[0]; } }; struct FnCompareCStr : public CMap::FnCompare { LTBOOL Compare(CStr const& sString1, CStr const& sString2) const { return !t_bCaseInsensitive ? !strcmp(sString1, sString2) : !stricmp(sString1, sString2); } }; public : // Methods const CMap::FnHash& GetFnHash() const { static FnHashCStr fnHashCStr; return fnHashCStr; } const CMap::FnCompare& GetFnCompare() const { static FnCompareCStr fnCompareCStr; return fnCompareCStr; } }; template class CMapStrCls : public CMap { private : // Functors struct FnHashCStr : public CMap::FnHash { uint32 Hash(CStr const& sString) const { LPCTSTR temp = sString; return t_bCaseInsensitive ? toupper(temp[0]) : temp[0]; } }; struct FnCompareCStr : public CMap::FnCompare { LTBOOL Compare(CStr const& sString1, CStr const& sString2) const { return !t_bCaseInsensitive ? !strcmp(sString1, sString2) : !stricmp(sString1, sString2); } }; public : // Methods const CMap::FnHash& GetFnHash() const { static FnHashCStr fnHashCStr; return fnHashCStr; } const CMap::FnCompare& GetFnCompare() const { static FnCompareCStr fnCompareCStr; return fnCompareCStr; } }; }; #endif