Blender V2.61 - r43446

SG_QList.h

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00032 #ifndef __SG_QLIST
00033 #define __SG_QLIST
00034 
00035 #include "SG_DList.h"
00036 
00041 class SG_QList : public SG_DList
00042 {
00043 protected :
00044     SG_QList* m_fqlink;
00045     SG_QList* m_bqlink;
00046 
00047 public:
00048     template<typename T> class iterator
00049     {
00050     private:
00051         SG_QList&   m_head;
00052         T*          m_current;
00053     public:
00054         typedef iterator<T> _myT;
00055         iterator(SG_QList& head, SG_QList* current=NULL) : m_head(head) { m_current = (T*)current; }
00056         ~iterator() {}
00057 
00058         void begin()
00059         {
00060             m_current = (T*)m_head.QPeek();
00061         }
00062         void back()
00063         {
00064             m_current = (T*)m_head.QBack();
00065         }
00066         bool end()
00067         {
00068             return (m_current == (T*)m_head.Self());
00069         }
00070         bool add_back(T* item)
00071         {
00072             return m_current->QAddBack(item);
00073         }
00074         T* operator*()
00075         {
00076             return m_current;
00077         }
00078         _myT& operator++()
00079         {
00080             m_current = (T*)m_current->QPeek();
00081             return *this;
00082         }
00083         _myT& operator--()
00084         {
00085             // no check on NULL! make sure you don't try to increment beyond end
00086             m_current = (T*)m_current->QBack();
00087             return *this;
00088         }
00089     };
00090 
00091     SG_QList() : SG_DList()
00092     {
00093         m_fqlink = m_bqlink = this;
00094     }
00095     SG_QList(const SG_QList& other) : SG_DList()
00096     {
00097         m_fqlink = m_bqlink = this;
00098     }
00099     virtual ~SG_QList()
00100     {
00101         QDelink();
00102     }
00103 
00104     inline bool QEmpty()               // Check for empty queue
00105     {
00106         return ( m_fqlink == this );
00107     }
00108     bool QAddBack( SG_QList *item )  // Add to the back
00109     {
00110         if (!item->QEmpty())
00111             return false;
00112         item->m_bqlink = m_bqlink;
00113         item->m_fqlink = this;
00114         m_bqlink->m_fqlink = item;
00115         m_bqlink = item;
00116         return true;
00117     }
00118     bool QAddFront( SG_QList *item )  // Add to the back
00119     {
00120         if (!item->Empty())
00121             return false;
00122         item->m_fqlink = m_fqlink;
00123         item->m_bqlink = this;
00124         m_fqlink->m_bqlink = item;
00125         m_fqlink = item;
00126         return true;
00127     }
00128     SG_QList *QRemove()           // Remove from the front
00129     {
00130         if (QEmpty())
00131         {
00132             return NULL;
00133         }
00134         SG_QList* item = m_fqlink;
00135         m_fqlink = item->m_fqlink;
00136         m_fqlink->m_bqlink = this;
00137         item->m_fqlink = item->m_bqlink = item;
00138         return item;
00139     }
00140     bool QDelink()             // Remove from the middle
00141     {
00142         if (QEmpty())
00143             return false;
00144         m_bqlink->m_fqlink = m_fqlink;
00145         m_fqlink->m_bqlink = m_bqlink;
00146         m_fqlink = m_bqlink = this;
00147         return true;
00148     }
00149     inline SG_QList *QPeek()            // Look at front without removing
00150     {
00151         return m_fqlink;
00152     }
00153     inline SG_QList *QBack()            // Look at front without removing
00154     {
00155         return m_bqlink;
00156     }
00157 
00158 
00159 #ifdef WITH_CXX_GUARDEDALLOC
00160 public:
00161     void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:SG_QList"); }
00162     void operator delete( void *mem ) { MEM_freeN(mem); }
00163 #endif
00164 };
00165 
00166 #endif //__SG_QLIST
00167