core/sourcehook/sh_stack.h
author Scott Ehlert <ds@alliedmods.net>
Thu Dec 10 00:59:50 2009 -0600 (2009-12-10)
changeset 689 4065ba5e9155
parent 539 db914129ae01
child 733 2365240861a2
permissions -rw-r--r--
Rewrote user message caching code to always use IServerGameDLL::GetUserMessageInfo (bug 4117, r=dvander).

Games where the above function can crash are handled by detouring tier0's Error() function and jumping back to our own code. So in theory this should work on all games now.
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2008 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 #include <stddef.h>
    15 #include "sh_vector.h"
    16 
    17 namespace SourceHook
    18 {
    19 	// May _never_ reallocate used memory!
    20 
    21 	template <class T> class CStack
    22 	{
    23 		static const size_t SECTOR_SIZE = 16;
    24 
    25 		CVector<T*> m_Sectors;				// Stores sectors
    26 
    27 		size_t m_UsedSize;
    28 
    29 		void clear()
    30 		{
    31 			typename CVector<T*>::iterator sect_iter;
    32 			for (sect_iter = m_Sectors.begin(); sect_iter != m_Sectors.end(); ++sect_iter)
    33 			{
    34 				delete [] *sect_iter;
    35 			}
    36 			m_Sectors.clear();
    37 		}
    38 	public:
    39 		friend class iterator;
    40 		class iterator
    41 		{
    42 			CStack<T> *m_pParent;
    43 			size_t m_Index;
    44 		public:
    45 			iterator(CStack<T> *pParent, size_t id) : m_pParent(pParent), m_Index(id)
    46 			{
    47 			}
    48 
    49 			iterator(CStack<T> *pParent) : m_pParent(pParent), m_Index(0)
    50 			{
    51 			}
    52 			
    53 			iterator() : m_pParent(NULL), m_Index(0)
    54 			{
    55 			}
    56 
    57 			T &operator *()
    58 			{
    59 				return m_pParent->at(m_Index);
    60 			}
    61 			const T &operator *() const
    62 			{
    63 				return m_pParent->at(m_Index);
    64 			}
    65 			
    66 			T * operator->()
    67 			{
    68 				return &(m_pParent->at(m_Index));
    69 			}
    70 
    71 			const T * operator->() const
    72 			{
    73 				return &(m_pParent->at(m_Index));
    74 			}
    75 
    76 			iterator & operator++()		// preincrement
    77 			{
    78 				++m_Index;
    79 				return (*this);
    80 			}
    81 
    82 			iterator operator++(int)	// postincrement
    83 			{
    84 				iterator tmp = *this;
    85 				++m_Index;
    86 				return tmp;
    87 			}
    88 
    89 			iterator & operator--()		// predecrement
    90 			{
    91 				--m_Index;
    92 				return (*this);
    93 			}
    94 
    95 			iterator operator--(int)	// postdecrememnt
    96 			{
    97 				iterator tmp = *this;
    98 				--m_Index;
    99 				return tmp;
   100 			}
   101 
   102 			bool operator==(const iterator & right) const
   103 			{
   104 				return (m_pParent == right.m_pParent && m_Index == right.m_Index);
   105 			}
   106 
   107 			bool operator!=(const iterator & right) const
   108 			{
   109 				return !(*this == right);
   110 			}
   111 		};
   112 		CStack() : m_UsedSize(0)
   113 		{
   114 		}
   115 
   116 		CStack(const CStack &other)
   117 		{
   118 			for (typename CVector<T*>::iterator sect_iter = other.m_Sectors.begin();
   119 				sect_iter != other.m_Sectors.end(); ++sect_iter)
   120 			{
   121 				m_Sectors.push_back(new T[SECTOR_SIZE]);
   122 				for (size_t i = 0; i < SECTOR_SIZE; ++i)
   123 					m_Sectors.back()[i] = (*sect_iter)[i];
   124 			}
   125 			m_UsedSize = other.m_UsedSize;
   126 		}
   127 
   128 		CStack & operator =(const CStack &other)
   129 		{
   130 			clear();
   131 			for (typename CVector<T*>::iterator sect_iter = other.m_Sectors.begin();
   132 				sect_iter != other.m_Sectors.end(); ++sect_iter)
   133 			{
   134 				m_Sectors.push_back(new T[SECTOR_SIZE]);
   135 				for (size_t i = 0; i < SECTOR_SIZE; ++i)
   136 					m_Sectors.back()[i] = (*sect_iter)[i];
   137 			}
   138 			m_UsedSize = other.m_UsedSize;
   139 			return *this;
   140 		}
   141 
   142 		~CStack()
   143 		{
   144 			clear();
   145 		}
   146 
   147 		T &at(size_t x)
   148 		{
   149 			return m_Sectors[x / SECTOR_SIZE][x % SECTOR_SIZE];
   150 		}
   151 
   152 		const T &at(size_t x) const
   153 		{
   154 			return m_Sectors[x / SECTOR_SIZE][x % SECTOR_SIZE];
   155 		}
   156 
   157 		bool push(const T &val)
   158 		{
   159 			if ((m_UsedSize / SECTOR_SIZE) >= m_Sectors.size())
   160 			{
   161 				// New sector
   162 				m_Sectors.push_back(new T[SECTOR_SIZE]);
   163 			}
   164 
   165 			at(m_UsedSize) = val;
   166 
   167 			++m_UsedSize;
   168 			return true;
   169 		}
   170 
   171 		T *make_next()
   172 		{
   173 			if ((m_UsedSize / SECTOR_SIZE) >= m_Sectors.size())
   174 			{
   175 				// New sector
   176 				m_Sectors.push_back(new T[SECTOR_SIZE]);
   177 			}
   178 
   179 			return &(at(m_UsedSize++));
   180 		}
   181 
   182 		void pop()
   183 		{
   184 			--m_UsedSize;
   185 		}
   186 
   187 		void popall()
   188 		{
   189 			m_UsedSize = 0;
   190 		}
   191 
   192 		T &front()
   193 		{
   194 			return at(m_UsedSize-1);
   195 		}
   196 
   197 		const T &front() const
   198 		{
   199 			return at(m_UsedSize-1);
   200 		}
   201 
   202 		T &second()
   203 		{
   204 			return at(m_UsedSize-2);
   205 		}
   206 
   207 		const T &second() const
   208 		{
   209 			return at(m_UsedSize-2);
   210 		}
   211 
   212 		iterator begin()
   213 		{
   214 			return iterator(this, 0);
   215 		}
   216 		iterator end()
   217 		{
   218 			return iterator(this, m_UsedSize);
   219 		}
   220 
   221 		size_t size()
   222 		{
   223 			return m_UsedSize;
   224 		}
   225 		bool empty()
   226 		{
   227 			return m_UsedSize == 0 ? true : false;
   228 		}
   229 	};
   230 };	//namespace SourceHook
   231 
   232 #endif