sourcehook/sh_stack.h
author Scott Ehlert <ds@alliedmods.net>
Sun Aug 13 11:34:30 2006 +0000 (2006-08-13)
changeset 238 c27105949293
parent 162 a95db73792e5
child 252 080f0438e0cb
permissions -rw-r--r--
Wow, lots of stuff here
1) Bumped version to 1.3
2) Fixed MSVC project files to compile and link against latest SDK
3) Fixed Linux compilation of stub_mm and sample_mm against new SDK (added -msse to makefiles)
4) Removed -fpermissive and -Wno-deprecated from Linux makefiles
5) Added -Wall -Wno-non-virtual-dtor -Werror to Linux makefiles
6) Stub_mm and sample_mm no longer dynamically link to libstdc++
7) SourceMM, stub_mm, and sample_mm compile cleanly (no warnings) with GCC 3.4.x and 4.x
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2005 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): Pavol "PM OnoTo" Marko
     8 * ============================
     9 */
    10 
    11 #ifndef __SH_STACK_H__
    12 #define __SH_STACK_H__
    13 
    14 #define SH_STACK_DEFAULT_SIZE 4
    15 
    16 namespace SourceHook
    17 {
    18 	// Vector
    19 	template <class T> class CStack
    20 	{
    21 		T *m_Elements;
    22 		size_t m_AllocatedSize;
    23 		size_t m_UsedSize;
    24 	public:
    25 		friend class iterator;
    26 		class iterator
    27 		{
    28 			CStack<T> *m_pParent;
    29 			size_t m_Index;
    30 		public:
    31 			iterator(CStack<T> *pParent, size_t id) : m_pParent(pParent), m_Index(id)
    32 			{
    33 			}
    34 
    35 			iterator(CStack<T> *pParent) : m_pParent(pParent), m_Index(0)
    36 			{
    37 			}
    38 			
    39 			iterator() : m_pParent(NULL), m_Index(0)
    40 			{
    41 			}
    42 
    43 			T &operator *()
    44 			{
    45 				return m_pParent->m_Elements[m_Index];
    46 			}
    47 			const T &operator *() const
    48 			{
    49 				return m_pParent->m_Elements[m_Index];
    50 			}
    51 			
    52 			T * operator->()
    53 			{
    54 				return m_pParent->m_Elements + m_Index;
    55 			}
    56 
    57 			const T * operator->() const
    58 			{
    59 				return m_pParent->m_Elements + m_Index;
    60 			}
    61 
    62 			iterator & operator++()		// preincrement
    63 			{
    64 				++m_Index;
    65 				return (*this);
    66 			}
    67 
    68 			iterator operator++(int)	// postincrement
    69 			{
    70 				iterator tmp = *this;
    71 				++m_Index;
    72 				return tmp;
    73 			}
    74 
    75 			iterator & operator--()		// predecrement
    76 			{
    77 				--m_Index;
    78 				return (*this);
    79 			}
    80 
    81 			iterator operator--(int)	// postdecrememnt
    82 			{
    83 				iterator tmp = *this;
    84 				--m_Index;
    85 				return tmp;
    86 			}
    87 
    88 			bool operator==(const iterator & right) const
    89 			{
    90 				return (m_pParent == right.m_pParent && m_Index == right.m_Index);
    91 			}
    92 
    93 			bool operator!=(const iterator & right) const
    94 			{
    95 				return !(*this == right);
    96 			}
    97 		};
    98 		CStack() : m_Elements(new T[SH_STACK_DEFAULT_SIZE]),
    99 			m_AllocatedSize(SH_STACK_DEFAULT_SIZE),
   100 			m_UsedSize(0)
   101 		{
   102 		}
   103 		CStack(size_t size) : m_Elements(new T[size]),
   104 			m_AllocatedSize(size),
   105 			m_UsedSize(0)
   106 		{
   107 		}
   108 
   109 		CStack(const CStack &other) : m_Elements(NULL),
   110 			m_AllocatedSize(0),
   111 			m_UsedSize(0)
   112 		{
   113 			reserve(other.m_AllocatedSize);
   114 			m_UsedSize = other.m_UsedSize;
   115 			for (size_t i = 0; i < m_UsedSize; ++i)
   116 				m_Elements[i] = other.m_Elements[i];
   117 		}
   118 
   119 		~CStack()
   120 		{
   121 			if (m_Elements)
   122 				delete [] m_Elements;
   123 		}
   124 		
   125 		void operator=(const CStack &other)
   126 		{
   127 			if (m_AllocatedSize < other.m_AllocatedSize)
   128 			{
   129 				if (m_Elements)
   130 					delete [] m_Elements;
   131 				m_Elements = new T[other.m_AllocatedSize];
   132 				m_AllocatedSize = other.m_AllocatedSize;
   133 			}
   134 			m_UsedSize = other.m_UsedSize;
   135 			for (size_t i = 0; i < m_UsedSize; ++i)
   136 				m_Elements[i] = other.m_Elements[i];
   137 		}
   138 
   139 		bool push(const T &val)
   140 		{
   141 			if (m_UsedSize + 1 == m_AllocatedSize)
   142 			{
   143 				// zOHNOES! REALLOCATE!
   144 				m_AllocatedSize *= 2;
   145 				T *newElements = new T[m_AllocatedSize];
   146 				if (!newElements)
   147 				{
   148 					m_AllocatedSize /= 2;
   149 					return false;
   150 				}
   151 				if (m_Elements)
   152 				{
   153 					for (size_t i = 0; i < m_UsedSize; ++i)
   154 						newElements[i] = m_Elements[i];
   155 					delete [] m_Elements;
   156 				}
   157 				m_Elements = newElements;
   158 			}
   159 			m_Elements[m_UsedSize++] = val;
   160 			return true;
   161 		}
   162 		void pop()
   163 		{
   164 			--m_UsedSize;
   165 		}
   166 
   167 		T &front()
   168 		{
   169 			return m_Elements[m_UsedSize - 1];
   170 		}
   171 
   172 		const T &front() const
   173 		{
   174 			return m_Elements[m_UsedSize - 1];
   175 		}
   176 
   177 		T &second()
   178 		{
   179 			return m_Elements[m_UsedSize - 2];
   180 		}
   181 
   182 		const T &second() const
   183 		{
   184 			return m_Elements[m_UsedSize - 2];
   185 		}
   186 
   187 		iterator begin()
   188 		{
   189 			return iterator(this, 0);
   190 		}
   191 		iterator end()
   192 		{
   193 			return iterator(this, m_UsedSize);
   194 		}
   195 
   196 		size_t size()
   197 		{
   198 			return m_UsedSize;
   199 		}
   200 		size_t capacity()
   201 		{
   202 			return m_AllocatedSize;
   203 		}
   204 		bool empty()
   205 		{
   206 			return m_UsedSize == 0 ? true : false;
   207 		}
   208 		bool reserve(size_t size)
   209 		{
   210 			if (size > m_AllocatedSize)
   211 			{
   212 				T *newElements = new T[size];
   213 				if (!newElements)
   214 					return false;
   215 				if (m_Elements)
   216 				{
   217 					for (size_t i = 0; i < m_UsedSize; ++i)
   218 						newElements[i] = m_Elements[i];
   219 					delete [] m_Elements;
   220 				}
   221 				m_Elements = newElements;
   222 				m_AllocatedSize = size;
   223 			}
   224 			return true;
   225 		}
   226 	};
   227 };	//namespace SourceHook
   228 
   229 #endif