sourcehook/sh_listcat.h
author Pavol Marko <pavolmarko@gmail.com>
Sun Oct 07 22:45:22 2007 +0000 (2007-10-07)
changeset 384 264cab8ff2cc
parent 324 257f0837a885
permissions -rw-r--r--
correct version now... i guess i'm just tired :)
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2007 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 // Used for VP-Hooks
    12 
    13 #ifndef _INCLUDE_SMM_LISTCAT_H
    14 #define _INCLUDE_SMM_LISTCAT_H
    15 
    16 #include "sh_list.h"
    17 
    18 namespace SourceHook
    19 {
    20 
    21 	// Only a very special-case forward iterator!
    22 	template <typename T>
    23 	class ListCatIterator
    24 	{
    25 		List<T> *m_pListLeft;
    26 		List<T> *m_pListRight;
    27 		typename List<T>::iterator m_Iter;
    28 
    29 		void CheckLeftEmptyOnBegin()
    30 		{
    31 			// If the left list is empty and right is valid, GoToBegin sets m_Iter to m_Left
    32 			// End() checks for equality to m_Right.end() so it returns false
    33 			// then Next() corrupts by incrementing!
    34 
    35 			// To avoid this, we skip left if it's empty.
    36 			if (m_pListLeft && m_pListLeft->empty() && m_pListRight)
    37 			{
    38 				m_Iter = m_pListRight->begin();
    39 			}
    40 		}
    41 	public:
    42 		// At least one list has to be non-null!
    43 		ListCatIterator(List<T> *pListLeft, List<T> *pListRight) : m_pListLeft(pListLeft), m_pListRight(pListRight),
    44 			m_Iter(pListLeft ? pListLeft->begin() : pListRight->begin())
    45 		{
    46 			CheckLeftEmptyOnBegin();
    47 		}
    48 
    49 		void GoToBegin()
    50 		{
    51 			m_Iter = m_pListLeft ? m_pListLeft->begin() : m_pListRight->begin();
    52 			CheckLeftEmptyOnBegin();
    53 		}
    54 
    55 		bool End()
    56 		{
    57 			return m_pListRight ? (m_Iter == m_pListRight->end())
    58 				: (m_Iter == m_pListLeft->end());
    59 		}
    60 
    61 		//pre increment
    62 		ListCatIterator & operator++()
    63 		{
    64 			++m_Iter;
    65 			if (m_pListLeft && m_Iter == m_pListLeft->end())
    66 			{
    67 				if (m_pListRight)
    68 					m_Iter = m_pListRight->begin();
    69 			}
    70 			return *this;
    71 		}
    72 		//post increment
    73 		ListCatIterator operator++(int)
    74 		{
    75 			ListCatIterator old(*this);
    76 			
    77 			++m_Iter;
    78 			if (m_pListLeft && m_Iter == m_pListLeft->end())
    79 			{
    80 				if (m_pListRight)
    81 					m_Iter = m_pListRight->begin();
    82 			}
    83 
    84 			return old;
    85 		}
    86 
    87 		const T & operator * () const
    88 		{
    89 			return *m_Iter;
    90 		}
    91 		T & operator * ()
    92 		{
    93 			return *m_Iter;
    94 		}
    95 
    96 		T * operator -> ()
    97 		{
    98 			return &(*m_Iter);
    99 		}
   100 		const T * operator -> () const
   101 		{
   102 			return &(*m_Iter);
   103 		}
   104 
   105 		bool operator != (const typename List<T>::iterator &where) const
   106 		{
   107 			return (m_Iter != where);
   108 		}
   109 		bool operator ==(const typename List<T>::iterator &where) const
   110 		{
   111 			return (m_Iter == where);
   112 		}
   113 
   114 		ListCatIterator & operator = (const typename List<T>::iterator &where)
   115 		{
   116 			m_Iter = where;
   117 
   118 			if (m_pListLeft && m_Iter == m_pListLeft->end())
   119 			{
   120 				if (m_pListRight)
   121 					m_Iter = m_pListRight->begin();
   122 
   123 				// :HACK HACK: RemoveHookById is not aware of ListCatIterator (yet? :TODO: Change it!)
   124 				// So we have to do this here... (look for the "Move all iterators pointing at this" section)
   125 				--m_Iter;
   126 			}
   127 
   128 			return *this;
   129 		}
   130 
   131 		ListCatIterator & operator = (const ListCatIterator<T> &other)
   132 		{
   133 			m_Iter = other.m_Iter;
   134 			m_pListLeft = other.m_pListLeft;
   135 			m_pListRight = other.m_pListRight;
   136 			return *this;
   137 		}
   138 
   139 		void SetListLeft(List<T> *pList)
   140 		{
   141 			m_pListLeft = pList;
   142 		}
   143 	};
   144 }
   145 
   146 #endif
   147