//----------------------------------------------------------------------------- // Torque Game Engine // Quake GL DirectX wrapper //----------------------------------------------------------------------------- #ifndef _GLLIST_H_ #define _GLLIST_H_ //#define USE_IOSTREAM 1 #if defined(USE_IOSTREAM) #include #endif // forward class declarations template class GLListIter; template class GLListManip; template class GLLink { // Private members Type d_data; GLLink *d_next_p; // Links cannot be copied or assigned GLLink(const GLLink&); GLLink& operator=(const GLLink&); // Public members public: // Construct & Destroy inline GLLink(GLLink **addLinkPtr, const Type &data); ~GLLink() {} // Modifiers inline void setData(const Type &data); inline void setNext(GLLink *link); inline GLLink*& getNextRef(); // generally a bad practice // Accessors inline Type& getData(); inline GLLink* getNext() const; }; template class GLList { // Private members int d_length; GLLink *d_head_p; // Friends friend class GLListIter; friend class GLListManip; // Public members public: // Construct & Destroy GLList(); GLList(const GLList&); ~GLList(); // Modifiers GLList& operator=(const GLList &list); GLList& operator+=(const Type &i); GLList& operator+=(const GLList &list); GLList& prepend(const Type &i); GLList& prepend(const GLList &list); // Accessors int length() const; }; //template ostream& operator<<(ostream& o, const GLList& list); template class GLListIter { // Private Data GLLink *d_current_p; // Public Members public: // Construct and Destroy inline GLListIter(const GLList &list); inline GLListIter(const GLListIter &iter); ~GLListIter() {} // Modifiers inline GLListIter& operator=(const GLListIter &iter); inline void operator++(); // Accessors inline operator const void* () const; inline Type& operator()() const; }; template class GLListManip { // Private Data GLList *d_list_p; GLLink **d_current_p; // Links cannot be copied or assigned GLListManip(const GLListManip &manip); GLListManip& operator=(const GLListManip &manip); // Public Members public: // Construct and Destroy inline GLListManip(GLList *list); ~GLListManip() {} // Modifiers inline void operator++(); inline void insert (const Type &data); inline void remove (); // Accessors inline operator const void *() const; inline Type& operator()() const; }; template GLLink::GLLink(GLLink **addLinkPtr, const Type &data) : d_next_p(*addLinkPtr), d_data(data) { *addLinkPtr = this; } template void GLLink::setData(const Type &data) { d_data = data; } template void GLLink::setNext(GLLink *link) { d_next_p = link; } template GLLink*& GLLink::getNextRef() { return d_next_p; } template Type& GLLink::getData() { return d_data; } template GLLink* GLLink::getNext() const { return d_next_p; } template GLList::GLList() : d_head_p(0), d_length(0) { } template GLList::GLList(const GLList& list) : d_head_p(0) { GLListManip m(this); GLListIter l(list); while(l) { m.insert(l()); ++l; ++m; } } template GLList::~GLList() { GLListManip m(this); while(m != 0) m.remove(); } template GLList& GLList::operator=(const GLList& list) { GLListManip m(this); GLListIter l(list); if(this != &list) { while(m) m.remove(); while(l) { m.insert(l()); ++l; ++m; } } return *this; } template GLList& GLList::operator+=(const Type &i) { GLListManip m(this); while(m) ++m; m.insert(i); return *this; } template GLList& GLList::operator+=(const GLList& list) { unsigned i, s; GLListIter l(list); GLListManip m(this); while(m) ++m; s = list.d_length; for(i = 0; i < s; ++i) { m.insert(l()); ++m; ++l; } return *this; } template GLList& GLList::prepend(const Type &i) { GLListManip m(this); m.insert(i); return *this; } template GLList& GLList::prepend(const GLList &list) { GLListIter l(list); GLListManip m(this); while(l) { m.insert(l()); ++m; ++l; } return *this; } template int GLList::length() const { return d_length; } #if defined(USE_IOSTREAM) template ostream& operator<<(ostream &o, const GLList& list) { GLListIter l(list); o << "[ "; while(l != 0) { o << l(); o << " "; ++l; } return o << "]"; } #endif template GLListIter::GLListIter(const GLList &list) : d_current_p(list.d_head_p) { } template GLListIter::GLListIter(const GLListIter &iter) : d_current_p(iter.d_current_p) { } template GLListIter& GLListIter::operator=(const GLListIter &iter) { d_current_p = iter.d_current_p; return *this; } template void GLListIter::operator++() { d_current_p = d_current_p -> getNext(); } template Type& GLListIter::operator()() const { return d_current_p -> getData(); } template GLListIter::operator const void* () const { return d_current_p; } template GLListManip::GLListManip(GLList *list) : d_current_p(&(list -> d_head_p)), d_list_p(list) { } template void GLListManip::operator++() { d_current_p = &((*d_current_p) -> getNextRef()); } template void GLListManip::insert(const Type &data) { new GLLink(d_current_p, data); ++(d_list_p -> d_length); } template void GLListManip::remove() { GLLink *t = *d_current_p; *d_current_p = (*d_current_p) -> getNext(); delete t; --(d_list_p -> d_length); } template GLListManip::operator const void* () const { return *d_current_p; } template Type& GLListManip::operator()() const { return (*d_current_p) -> getData(); } #endif