From ead774778f5c244ebc52abd52a7abd6806ffeede Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 11 Sep 2005 23:24:18 +0000 Subject: [PATCH] std::vector -> SourceHook::CVector --HG-- extra : convert_revision : svn%3Ac2935e3e-5518-0410-8daf-afa5dab7d4e3/trunk%40100 --- sourcehook/sh_list.h | 3 +- sourcehook/sh_string.h | 1 - sourcehook/sh_vector.h | 474 ++++++++++++++++++++++++++++++++++++ sourcehook/smm_vector.h | 97 -------- sourcehook/sourcehook.cpp | 4 +- sourcehook/sourcehook.h | 4 +- sourcehook/test/main.cpp | 1 + sourcehook/test/test.vcproj | 8 +- 8 files changed, 488 insertions(+), 104 deletions(-) create mode 100755 sourcehook/sh_vector.h delete mode 100644 sourcehook/smm_vector.h diff --git a/sourcehook/sh_list.h b/sourcehook/sh_list.h index c06b1ef..004f208 100644 --- a/sourcehook/sh_list.h +++ b/sourcehook/sh_list.h @@ -13,8 +13,7 @@ namespace SourceHook { -//This class is from CSDM_amxx by BAILOPAN -// +//This class is from CSDM for AMX Mod X template class List { diff --git a/sourcehook/sh_string.h b/sourcehook/sh_string.h index 97bda37..ad0cf72 100755 --- a/sourcehook/sh_string.h +++ b/sourcehook/sh_string.h @@ -11,7 +11,6 @@ /* AMX Mod X * * by the AMX Mod X Development Team - * originally developed by OLO */ #ifndef _INCLUDE_CSTRING_H diff --git a/sourcehook/sh_vector.h b/sourcehook/sh_vector.h new file mode 100755 index 0000000..8a23a4f --- /dev/null +++ b/sourcehook/sh_vector.h @@ -0,0 +1,474 @@ +/* ======== SourceMM ======== +* Copyright (C) 2004-2005 Metamod:Source Development Team +* No warranties of any kind +* +* License: zlib/libpng +* +* Author(s): Pavol "PM OnoTo" Marko +* ============================ +*/ + +#ifndef __CVECTOR_H__ +#define __CVECTOR_H__ + +#include + +//This file originally from AMX Mod X +namespace SourceHook +{ +// Vector +template class CVector +{ + bool Grow() + { + // automatic grow + size_t newSize = m_Size * 2; + if (newSize == 0) + newSize = 8; // a good init value + T *newData = new T[newSize]; + if (!newData) + return false; + if (m_Data) + { + for (size_t i=0; i= m_Size) + return Grow(); + else + return true; + } + + bool ChangeSize(size_t size) + { + // change size + if (size == m_Size) + return true; + + if (!size) + { + if (m_Data) + { + delete [] m_Data; + m_Data = NULL; + m_Size = 0; + } + return true; + } + + T *newData = new T[size]; + if (!newData) + return false; + if (m_Data) + { + size_t end = (m_CurrentUsedSize < size) ? (m_CurrentUsedSize) : size; + for (size_t i=0; i m_Size) + m_CurrentUsedSize = m_Size; + + return true; + } + + void FreeMemIfPossible() + { + if (!m_Data) + return; + + if (!m_CurrentUsedSize) + { + ChangeSize(0); + return; + } + + size_t newSize = m_Size; + while (m_CurrentUsedSize <= newSize / 2) + newSize /= 2; + + if (newSize != m_Size) + ChangeSize(newSize); + } +protected: + T *m_Data; + size_t m_Size; + size_t m_CurrentUsedSize; +public: + class iterator + { + protected: + T *m_Ptr; + public: + // constructors / destructors + iterator() + { + m_Ptr = NULL; + } + + iterator(T * ptr) + { + m_Ptr = ptr; + } + + // member functions + T * base() + { + return m_Ptr; + } + + const T * base() const + { + return m_Ptr; + } + + // operators + T & operator*() + { + return *m_Ptr; + } + + T * operator->() + { + return m_Ptr; + } + + iterator & operator++() // preincrement + { + ++m_Ptr; + return (*this); + } + + iterator operator++(int) // postincrement + { + iterator tmp = *this; + ++m_Ptr; + return tmp; + } + + iterator & operator--() // predecrement + { + --m_Ptr; + return (*this); + } + + iterator operator--(int) // postdecrememnt + { + iterator tmp = *this; + --m_Ptr; + return tmp; + } + + bool operator==(T * right) const + { + return (m_Ptr == right); + } + + bool operator==(const iterator & right) const + { + return (m_Ptr == right.m_Ptr); + } + + bool operator!=(T * right) const + { + return (m_Ptr != right); + } + + bool operator!=(const iterator & right) const + { + return (m_Ptr != right.m_Ptr); + } + + iterator & operator+=(size_t offset) + { + m_Ptr += offset; + return (*this); + } + + iterator & operator-=(size_t offset) + { + m_Ptr -= offset; + return (*this); + } + + iterator operator+(size_t offset) const + { + iterator tmp(*this); + tmp.m_Ptr += offset; + return tmp; + } + + iterator operator-(size_t offset) const + { + iterator tmp(*this); + tmp.m_Ptr -= offset; + return tmp; + } + + T & operator[](size_t offset) + { + return (*(*this + offset)); + } + + const T & operator[](size_t offset) const + { + return (*(*this + offset)); + } + + bool operator<(const iterator & right) const + { + return m_Ptr < right.m_Ptr; + } + + bool operator>(const iterator & right) const + { + return m_Ptr > right.m_Ptr; + } + + bool operator<=(const iterator & right) const + { + return m_Ptr <= right.m_Ptr; + } + + bool operator>=(const iterator & right) const + { + return m_Ptr >= right.m_Ptr; + } + + size_t operator-(const iterator & right) const + { + return m_Ptr - right.m_Ptr; + } + }; + + // constructors / destructors + CVector() + { + m_Size = 0; + m_CurrentUsedSize = 0; + m_Data = NULL; + } + + CVector(const CVector & other) + { + // copy data + m_Data = new T [other.m_CurrentUsedSize]; + m_Size = other.m_CurrentUsedSize; + m_CurrentUsedSize = other.m_CurrentUsedSize; + for (size_t i=0; i() + { + clear(); + } + + // interface + size_t size() const + { + return m_CurrentUsedSize; + } + + size_t capacity() const + { + return m_Size; + } + + iterator begin() const + { + return iterator(m_Data); + } + + iterator end() const + { + return iterator(m_Data + m_CurrentUsedSize); + } + + iterator iterAt(size_t pos) + { + if (pos > m_CurrentUsedSize) + assert(0); + return iterator(m_Data + pos); + } + + bool reserve(size_t newSize) + { + if (newSize > m_Size) + return ChangeSize(newSize); + return true; + } + + bool push_back(const T & elem) + { + ++m_CurrentUsedSize; + if (!GrowIfNeeded()) + { + --m_CurrentUsedSize; + return false; + } + + m_Data[m_CurrentUsedSize - 1] = elem; + return true; + } + + void pop_back() + { + --m_CurrentUsedSize; + if (m_CurrentUsedSize < 0) + m_CurrentUsedSize = 0; + + FreeMemIfPossible(); + } + + bool resize(size_t newSize) + { + if (!ChangeSize(newSize)) + return false; + m_CurrentUsedSize = newSize; + return true; + } + + bool empty() const + { + return (m_CurrentUsedSize == 0); + } + + T & at(size_t pos) + { + if (pos > m_CurrentUsedSize) + { + assert(0); + } + return m_Data[pos]; + } + + const T & at(size_t pos) const + { + if (pos > m_CurrentUsedSize) + { + assert(0); + } + return m_Data[pos]; + } + + T & operator[](size_t pos) + { + return at(pos); + } + + const T & operator[](size_t pos) const + { + return at(pos); + } + + T & front() + { + if (m_CurrentUsedSize < 1) + { + assert(0); + } + return m_Data[0]; + } + + const T & front() const + { + if (m_CurrentUsedSize < 1) + { + assert(0); + } + return m_Data[0]; + } + + T & back() + { + if (m_CurrentUsedSize < 1) + { + assert(0); + } + return m_Data[m_CurrentUsedSize - 1]; + } + + const T & back() const + { + if (m_CurrentUsedSize < 1) + { + assert(0); + } + return m_Data[m_CurrentUsedSize - 1]; + } + + iterator insert(iterator where, const T & value) + { + // validate iter + if (where < m_Data || where > (m_Data + m_CurrentUsedSize)) + return iterator(0); + + size_t ofs = where - begin(); + + ++m_CurrentUsedSize; + if (!GrowIfNeeded()) + { + --m_CurrentUsedSize; + return false; + } + + where = begin() + ofs; + + // Move subsequent entries + for (T *ptr = m_Data + m_CurrentUsedSize - 2; ptr >= where.base(); --ptr) + *(ptr + 1) = *ptr; + + *where.base() = value; + + return where; + } + + iterator erase(iterator where) + { + // validate iter + if (where < m_Data || where >= (m_Data + m_CurrentUsedSize)) + return iterator(0); + + size_t ofs = where - begin(); + + if (m_CurrentUsedSize > 1) + { + // move + T *theend = m_Data + m_CurrentUsedSize; + for (T *ptr = where.base() + 1; ptr < theend; ++ptr) + *(ptr - 1) = *ptr; + } + + --m_CurrentUsedSize; + + FreeMemIfPossible(); + + return begin() + ofs; + } + + void clear() + { + m_Size = 0; + m_CurrentUsedSize = 0; + if (m_Data) + { + delete [] m_Data; + m_Data = NULL; + } + } +}; +}; //namespace SourceHook + +#endif // __CVECTOR_H__ + diff --git a/sourcehook/smm_vector.h b/sourcehook/smm_vector.h deleted file mode 100644 index bd97fd8..0000000 --- a/sourcehook/smm_vector.h +++ /dev/null @@ -1,97 +0,0 @@ -/* ======== SourceMM ======== -* Copyright (C) 2004-2005 Metamod:Source Development Team -* No warranties of any kind -* -* License: zlib/libpng -* -* Author(s): David "BAILOPAN" Anderson -* ============================ -*/ - -#ifndef _INCLUDE_SMMVECTOR_H -#define _INCLUDE_SMMVECTOR_H - -template -class DynVector -{ -public: - DynVector() - { - m_Data = NULL; - m_CurrentSize = 0; - m_UsedSize = 0; - } - ~DynVector() - { - clear(); - } - void clear() - { - if (m_Data) - { - delete [] m_Data; - m_Data = NULL; - } - m_UsedSize = 0; - m_CurrentSize = 0; - } - void resize(size_t size) - { - if (!size) - clear(); - else - Grow(size); - m_UsedSize = size; - } - DynVector & operator =(DynVector &src) - { - Grow(src.m_UsedSize + 1); - for (size_t i=0; i size) ? size : m_UsedSize; - for (size_t i=0; i m_CurrentSize) - Grow((m_UsedSize+1) * 2); - m_UsedSize++; - } -private: - T * m_Data; - size_t m_CurrentSize; - size_t m_UsedSize; -}; - -#endif //_INCLUDE_SMMVECTOR_H diff --git a/sourcehook/sourcehook.cpp b/sourcehook/sourcehook.cpp index 9da6e12..44e29ba 100644 --- a/sourcehook/sourcehook.cpp +++ b/sourcehook/sourcehook.cpp @@ -458,6 +458,8 @@ namespace SourceHook { iter->second[vtbl_idx] = 0; // Free some memory if possible + // :TODO: add this back in! + /* OrigFuncs::reverse_iterator riter; for (riter = iter->second.rbegin(); riter != iter->second.rend(); ++riter) { @@ -466,7 +468,7 @@ namespace SourceHook } iter->second.resize(iter->second.size() - (riter - iter->second.rbegin())); if (!iter->second.size()) - cc.cc.vt.erase(iter); + cc.cc.vt.erase(iter);*/ } } } diff --git a/sourcehook/sourcehook.h b/sourcehook/sourcehook.h index e328e90..a487ff0 100644 --- a/sourcehook/sourcehook.h +++ b/sourcehook/sourcehook.h @@ -69,7 +69,7 @@ #include "sh_memfuncinfo.h" #include "sh_memory.h" #include "sh_list.h" -#include +#include "sh_vector.h" #include // Good old metamod! @@ -218,7 +218,7 @@ namespace SourceHook VfnPtrList vfnptrs; //!< List of hooked interfaces }; - typedef std::vector OrigFuncs; + typedef SourceHook::CVector OrigFuncs; typedef std::map OrigVTables; template struct CallClass diff --git a/sourcehook/test/main.cpp b/sourcehook/test/main.cpp index ecd8114..6d10d0c 100644 --- a/sourcehook/test/main.cpp +++ b/sourcehook/test/main.cpp @@ -1,4 +1,5 @@ // Hello BAIL! +// hello pm how are you // This is a test file #include diff --git a/sourcehook/test/test.vcproj b/sourcehook/test/test.vcproj index 9b79a98..5bca81d 100644 --- a/sourcehook/test/test.vcproj +++ b/sourcehook/test/test.vcproj @@ -269,7 +269,13 @@ Name="Header Files" Filter="h;hpp;hxx;hm;inl;inc"> + RelativePath="..\sh_list.h"> + + + +