sourcehook/sh_stack.h
author Pavol Marko <pavolmarko@gmail.com>
Wed Oct 12 20:25:15 2005 +0000 (2005-10-12)
changeset 128 63f95eed16b6
child 129 1e021006dae7
permissions -rw-r--r--
Untested reentrancy support; TODO: Test sh_stack and reentrancy extensively!
     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 #include <assert.h>
    17 
    18 namespace SourceHook
    19 {
    20 	// Vector
    21 	template <class T> class CStack
    22 	{
    23 		T *m_Elements;
    24 		size_t m_AllocatedSize;
    25 		size_t m_UsedSize;
    26 	public:
    27 		friend class iterator;
    28 		class iterator
    29 		{
    30 			CStack<T> *m_pParent;
    31 			size_t m_Index;
    32 		public:
    33 			iterator(CStack<T> *pParent, size_t id) : m_pParent(pParent), m_Index(id)
    34 			{
    35 			}
    36 
    37 			iterator(CStack<T> *pParent) : m_pParent(pParent), m_Index(0)
    38 			{
    39 			}
    40 			
    41 			iterator() : m_pParent(NULL), m_Index(0)
    42 			{
    43 			}
    44 
    45 			T &operator *()
    46 			{
    47 				return m_pParent->m_Elements[m_Index];
    48 			}
    49 			const T &operator *() const
    50 			{
    51 				return m_pParent->m_Elements[m_Index];
    52 			}
    53 			
    54 			T * operator->()
    55 			{
    56 				return m_pParent->m_Elements + m_Index;
    57 			}
    58 
    59 			const T * operator->() const
    60 			{
    61 				return m_pParent->m_Elements + m_Index;
    62 			}
    63 
    64 			iterator & operator++()		// preincrement
    65 			{
    66 				++m_Index;
    67 				return (*this);
    68 			}
    69 
    70 			iterator operator++(int)	// postincrement
    71 			{
    72 				iterator tmp = *this;
    73 				++m_Ptr;
    74 				return tmp;
    75 			}
    76 
    77 			iterator & operator--()		// predecrement
    78 			{
    79 				--m_Index;
    80 				return (*this);
    81 			}
    82 
    83 			iterator operator--(int)	// postdecrememnt
    84 			{
    85 				iterator tmp = *this;
    86 				--m_Ptr;
    87 				return tmp;
    88 			}
    89 
    90 			bool operator==(const iterator & right) const
    91 			{
    92 				return (m_pParent == right.m_pParent && m_Index == right.m_Index);
    93 			}
    94 
    95 			bool operator!=(const iterator & right) const
    96 			{
    97 				return !(*this == right);
    98 			}
    99 		};
   100 		CStack() : m_Elements(new T[SH_STACK_DEFAULT_SIZE]),
   101 			m_AllocatedSize(SH_STACK_DEFAULT_SIZE),
   102 			m_UsedSize(0)
   103 		{
   104 			assert(m_Elements);
   105 		}
   106 		CStack(size_t size) : m_Elements(new T[size]),
   107 			m_AllocatedSize(size),
   108 			m_UsedSize(0)
   109 		{
   110 			assert(m_Elements);
   111 		}
   112 
   113 		~CStack()
   114 		{
   115 			if (m_Elements)
   116 				delete [] m_Elements;
   117 		}
   118 		
   119 		bool push(const T &val)
   120 		{
   121 			if (m_UsedSize + 1 == m_AllocatedSize)
   122 			{
   123 				// zOHNOES! REALLOCATE!
   124 				m_AllocatedSize *= 2;
   125 				T *newElements = new T[m_AllocatedSize];
   126 				if (!newElements)
   127 				{
   128 					m_AllocatedSize /= 2;
   129 					return false;
   130 				}
   131 				for (size_t i = 0; i < m_UsedSize; ++i)
   132 					newElements[i] = m_Elements[i];
   133 				delete [] m_Elements;
   134 				m_Elements = newElements;
   135 			}
   136 			m_Elements[m_UsedSize++] = val;
   137 			return true;
   138 		}
   139 		void pop()
   140 		{
   141 			--m_UsedSize;
   142 		}
   143 
   144 		T &front()
   145 		{
   146 			return m_Elements[m_UsedSize - 1];
   147 		}
   148 
   149 		const T &front() const
   150 		{
   151 			return m_Elements[m_UsedSize - 1];
   152 		}
   153 
   154 		iterator begin()
   155 		{
   156 			return iterator(this, 0);
   157 		}
   158 		iterator end()
   159 		{
   160 			return iterator(this, m_UsedSize);
   161 		}
   162 	};
   163 };	//namespace SourceHook
   164 
   165 #endif