#ifndef CSimpleArrayH #define CSimpleArrayH #include //--------------------------------------------------------------------------- inline void __declspec(noreturn) _AtlRaiseException( DWORD dwExceptionCode, DWORD dwExceptionFlags = EXCEPTION_NONCONTINUABLE ) { RaiseException( dwExceptionCode, dwExceptionFlags, 0, NULL ); } // Simple Struct Array class (WTLÀÇ CSimpleArray¸¦ ¼öÁ¤ °³¼±ÇÔ) template class CSArray { #define ATLASSERT assert public: CSArray() : m_aT(NULL), m_nSize(0), m_nAllocSize(0) { } ~CSArray() { RemoveAll(); } CSArray(const CSArray& src) : m_aT(NULL), m_nSize(0), m_nAllocSize(0) { m_aT = (T*)malloc(src.GetSize() * sizeof(T)); if (m_aT != NULL) { m_nAllocSize = src.GetSize(); for (int i=0; i& operator=(const CSArray& src) { if (GetSize() != src.GetSize()) { RemoveAll(); m_aT = (T*)malloc(src.GetSize() * sizeof(T)); if (m_aT != NULL) m_nAllocSize = src.GetSize(); } else { for (int i = GetSize(); i > 0; i--) RemoveAt(i - 1); } for (int i=0; i= 0 && nIndex < m_nSize); if (nIndex < 0 || nIndex >= m_nSize) return false; m_aT[nIndex].~T(); if(nIndex != (m_nSize - 1)) memmove((void*)(m_aT + nIndex), (void*)(m_aT + nIndex + 1), (m_nSize - (nIndex + 1)) * sizeof(T)); m_nSize--; return true; } void RemoveAll() { if(m_aT != NULL) { for(int i = 0; i < m_nSize; i++) m_aT[i].~T(); free(m_aT); m_aT = NULL; } m_nSize = 0; m_nAllocSize = 0; } const T& operator[] (int nIndex) const { ATLASSERT(nIndex >= 0 && nIndex < m_nSize); if(nIndex < 0 || nIndex >= m_nSize) { _AtlRaiseException(EXCEPTION_ARRAY_BOUNDS_EXCEEDED); } return m_aT[nIndex]; } T& operator[] (int nIndex) { ATLASSERT(nIndex >= 0 && nIndex < m_nSize); if(nIndex < 0 || nIndex >= m_nSize) { _AtlRaiseException(EXCEPTION_ARRAY_BOUNDS_EXCEEDED); } return m_aT[nIndex]; } T* GetData() const { return m_aT; } int Find(const T& t) const { for(int i = 0; i < m_nSize; i++) { if(TEqual::IsEqual(m_aT[i], t)) return i; } return -1; // not found } BOOL SetAtIndex(int nIndex, const T& t) { if (nIndex < 0 || nIndex >= m_nSize) return FALSE; InternalSetAtIndex(nIndex, t); return TRUE; } // Implementation class Wrapper { public: Wrapper(const T& _t) : t(_t) { } template void * __cdecl operator new(size_t, _Ty* p) { return p; } template void __cdecl operator delete(void* /* pv */, _Ty* /* p */) { } T t; }; // Implementation void InternalSetAtIndex(int nIndex, const T& t) { new(m_aT + nIndex) Wrapper(t); } //typedef T _ArrayElementType; T* m_aT; int m_nSize; int m_nAllocSize; }; //--------------------------------------------------------------------------- #endif