Index: reactos/sdk/lib/atl/atlsimpcoll.h =================================================================== --- reactos/sdk/lib/atl/atlsimpcoll.h (nonexistent) +++ reactos/sdk/lib/atl/atlsimpcoll.h (working copy) @@ -0,0 +1,192 @@ +// PROJECT: ReactOS ATL Simple Collection +// LICENSE: Public Domain +// PURPOSE: Provides compatibility to Microsoft ATL +// PROGRAMMERS: Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com) + +#ifndef __ATLSIMPCOLL_H__ +#define __ATLSIMPCOLL_H__ + +#pragma once + +#include "atlcore.h" // for ATL Core +#include // for placement new + +namespace ATL +{ + +template +class CSimpleArrayEqualHelper +{ +public: + static bool IsEqual(const T& t1, const T& t2) + { + return t1 == t2; + } +}; + +template +class CSimpleArrayEqualHelperFalse +{ +public: + static bool IsEqual(const T& t1, const T& t2) + { + return false; + } +}; + +template > +class CSimpleArray +{ +public: + CSimpleArray() : m_pData(NULL), m_nCount(0) + { + } + + CSimpleArray(const CSimpleArray& src) : m_pData(NULL), m_nCount(0) + { + *this = src; + } + + ~CSimpleArray() + { + RemoveAll(); + } + + BOOL Add(const T& t) + { + T *new_data = (T *)realloc(m_pData, (m_nCount + 1) * sizeof(T)); + if (new_data == NULL) + return FALSE; + + m_pData = new_data; + + // call constructor + new(reinterpret_cast(&m_pData[m_nCount])) T; + + m_pData[m_nCount] = t; + ++m_nCount; + return TRUE; + } + + int Find(const T& t) const + { + for (int nIndex = 0; nIndex < m_nCount; ++nIndex) + { + if (TEqual::IsEqual(m_pData[nIndex], t)) + { + return nIndex; + } + } + return -1; + } + + T* GetData() + { + return m_pData; + } + + const T* GetData() const + { + return m_pData; + } + + int GetSize() const + { + return m_nCount; + } + + BOOL Remove(const T& t) + { + BOOL ret = FALSE; + for (;;) + { + int nIndex = Find(t); + if (nIndex < 0) + break; + + ret = RemoveAt(nIndex); + if (!ret) + break; + } + return ret; + } + + void RemoveAll() + { + if (m_pData) + { + // call destructor + const int count = m_nCount; + for (int i = 0; i < count; ++i) + { + (m_pData + i)->~T(); + } + + free(m_pData); + m_pData = NULL; + } + m_nCount = 0; + } + + BOOL RemoveAt(int nIndex) + { + if (nIndex < 0 || m_nCount <= nIndex) + return FALSE; + + // call destructor + (m_pData + nIndex)->~T(); + + int right = m_nCount - (nIndex + 1); + memmove(&m_pData[nIndex], &m_pData[nIndex + 1], right * sizeof(T)); + --m_nCount; + return TRUE; + } + + BOOL SetAtIndex(int nIndex, const T& t) + { + m_pData[nIndex] = t; + return TRUE; + } + + T& operator[](int nIndex) + { + return m_pData[nIndex]; + } + + const T& operator[](int nIndex) const + { + return m_pData[nIndex]; + } + + CSimpleArray& operator=(const CSimpleArray& src) + { + if (this == &src) + { + return *this; + } + + const int count = src.GetSize(); + T *new_data = (T *)malloc(count * sizeof(T)); + if (new_data == NULL) + return *this; + + m_pData = new_data; + m_nCount = count; + for (int i = 0; i < count; ++i) + { + new(reinterpret_cast(&m_pData[m_nCount])) T; + m_pData[i] = src[i]; + } + return *this; + } + +protected: + T * m_pData; // malloc'ed + int m_nCount; +}; + +} + +// TODO: Add CSimpleMap + +#endif