1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcehook/sh_listcat.h Mon May 07 18:35:59 2007 +0000
1.3 @@ -0,0 +1,147 @@
1.4 +/* ======== SourceMM ========
1.5 +* Copyright (C) 2004-2007 Metamod:Source Development Team
1.6 +* No warranties of any kind
1.7 +*
1.8 +* License: zlib/libpng
1.9 +*
1.10 +* Author(s): Pavol "PM OnoTo" Marko
1.11 +* ============================
1.12 +*/
1.13 +
1.14 +// Used for VP-Hooks
1.15 +
1.16 +#ifndef _INCLUDE_SMM_LISTCAT_H
1.17 +#define _INCLUDE_SMM_LISTCAT_H
1.18 +
1.19 +#include "sh_list.h"
1.20 +
1.21 +namespace SourceHook
1.22 +{
1.23 +
1.24 + // Only a very special-case forward iterator!
1.25 + template <typename T>
1.26 + class ListCatIterator
1.27 + {
1.28 + List<T> *m_pListLeft;
1.29 + List<T> *m_pListRight;
1.30 + typename List<T>::iterator m_Iter;
1.31 +
1.32 + void CheckLeftEmptyOnBegin()
1.33 + {
1.34 + // If the left list is empty and right is valid, GoToBegin sets m_Iter to m_Left
1.35 + // End() checks for equality to m_Right.end() so it returns false
1.36 + // then Next() corrupts by incrementing!
1.37 +
1.38 + // To avoid this, we skip left if it's empty.
1.39 + if (m_pListLeft && m_pListLeft->empty() && m_pListRight)
1.40 + {
1.41 + m_Iter = m_pListRight->begin();
1.42 + }
1.43 + }
1.44 + public:
1.45 + // At least one list has to be non-null!
1.46 + ListCatIterator(List<T> *pListLeft, List<T> *pListRight) : m_pListLeft(pListLeft), m_pListRight(pListRight),
1.47 + m_Iter(pListLeft ? pListLeft->begin() : pListRight->begin())
1.48 + {
1.49 + CheckLeftEmptyOnBegin();
1.50 + }
1.51 +
1.52 + void GoToBegin()
1.53 + {
1.54 + m_Iter = m_pListLeft ? m_pListLeft->begin() : m_pListRight->begin();
1.55 + CheckLeftEmptyOnBegin();
1.56 + }
1.57 +
1.58 + bool End()
1.59 + {
1.60 + return m_pListRight ? (m_Iter == m_pListRight->end())
1.61 + : (m_Iter == m_pListLeft->end());
1.62 + }
1.63 +
1.64 + //pre increment
1.65 + ListCatIterator & operator++()
1.66 + {
1.67 + ++m_Iter;
1.68 + if (m_pListLeft && m_Iter == m_pListLeft->end())
1.69 + {
1.70 + if (m_pListRight)
1.71 + m_Iter = m_pListRight->begin();
1.72 + }
1.73 + return *this;
1.74 + }
1.75 + //post increment
1.76 + ListCatIterator operator++(int)
1.77 + {
1.78 + ListCatIterator old(*this);
1.79 +
1.80 + ++m_Iter;
1.81 + if (m_pListLeft && m_Iter == m_pListLeft->end())
1.82 + {
1.83 + if (m_pListRight)
1.84 + m_Iter = m_pListRight->begin();
1.85 + }
1.86 +
1.87 + return old;
1.88 + }
1.89 +
1.90 + const T & operator * () const
1.91 + {
1.92 + return *m_Iter;
1.93 + }
1.94 + T & operator * ()
1.95 + {
1.96 + return *m_Iter;
1.97 + }
1.98 +
1.99 + T * operator -> ()
1.100 + {
1.101 + return &(*m_Iter);
1.102 + }
1.103 + const T * operator -> () const
1.104 + {
1.105 + return &(*m_Iter);
1.106 + }
1.107 +
1.108 + bool operator != (const typename List<T>::iterator &where) const
1.109 + {
1.110 + return (m_Iter != where);
1.111 + }
1.112 + bool operator ==(const typename List<T>::iterator &where) const
1.113 + {
1.114 + return (m_Iter == where);
1.115 + }
1.116 +
1.117 + ListCatIterator & operator = (const typename List<T>::iterator &where)
1.118 + {
1.119 + m_Iter = where;
1.120 +
1.121 + if (m_pListLeft && m_Iter == m_pListLeft->end())
1.122 + {
1.123 + if (m_pListRight)
1.124 + m_Iter = m_pListRight->begin();
1.125 +
1.126 + // :HACK HACK: RemoveHookById is not aware of ListCatIterator (yet? :TODO: Change it!)
1.127 + // So we have to do this here... (look for the "Move all iterators pointing at this" section)
1.128 + --m_Iter;
1.129 + }
1.130 +
1.131 + return *this;
1.132 + }
1.133 +
1.134 + ListCatIterator & operator = (const ListCatIterator<T> &other)
1.135 + {
1.136 + m_Iter = other.m_Iter;
1.137 + m_pListLeft = other.m_pListLeft;
1.138 + m_pListRight = other.m_pListRight;
1.139 + return *this;
1.140 + }
1.141 +
1.142 + void SetListLeft(List<T> *pList)
1.143 + {
1.144 + m_pListLeft = pList;
1.145 + }
1.146 + };
1.147 +}
1.148 +
1.149 +#endif
1.150 +