00001 /* 00002 00003 ************************************************************************* 00004 00005 ArmageTron -- Just another Tron Lightcycle Game in 3D. 00006 Copyright (C) 2000 Manuel Moos (manuel@moosnet.de) 00007 00008 ************************************************************************** 00009 00010 This program is free software; you can redistribute it and/or 00011 modify it under the terms of the GNU General Public License 00012 as published by the Free Software Foundation; either version 2 00013 of the License, or (at your option) any later version. 00014 00015 This program is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU General Public License for more details. 00019 00020 You should have received a copy of the GNU General Public License 00021 along with this program; if not, write to the Free Software 00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00023 00024 *************************************************************************** 00025 00026 */ 00027 00028 #ifndef ArmageTron_tLinkedList_H 00029 #define ArmageTron_tLinkedList_H 00030 00031 #include "tError.h" 00032 00033 #include <stdlib.h> 00034 00035 class tListItemBase{ 00036 protected: 00037 tListItemBase *next; 00038 tListItemBase **anchor; 00039 00040 public: 00041 typedef int (Comparator)( const tListItemBase* a, const tListItemBase* b ); 00042 00043 void Remove(); 00044 void Insert(tListItemBase *&a); 00045 00046 tListItemBase():next(NULL),anchor(NULL) {} 00047 tListItemBase(tListItemBase *&a):next(NULL),anchor(NULL){Insert(a);} 00048 virtual ~tListItemBase() {Remove();} 00049 00050 tListItemBase *Next() {return next;} 00051 00052 int Len(); 00053 void Sort( Comparator* comparator ); 00054 }; 00055 00056 template < typename T, typename comparator > 00057 int st_Compare( const tListItemBase* a, const tListItemBase* b ) 00058 { 00059 const T* A = static_cast<const T*>( a ); 00060 const T* B = static_cast<const T*>( b ); 00061 00062 return comparator::Compare( A, B ); 00063 } 00064 00065 00066 template <class T> class tListItem:public tListItemBase{ 00067 public: 00068 tListItem():tListItemBase() 00069 { 00070 // this class only works under this condition: 00071 tASSERT( static_cast< tListItemBase * >( ( T * )(NULL) ) == NULL ); 00072 }; 00073 tListItem(T *&a):tListItemBase(reinterpret_cast<tListItemBase*&>(a)){}; 00074 T *Next(){return reinterpret_cast<T*>(next);} 00075 00076 template< typename comparator > 00077 void Sort( ) 00078 { 00079 tListItemBase::Sort( &st_Compare< T, comparator> ); 00080 } 00081 00082 void Insert(tListItem *&a) 00083 { 00084 tListItemBase::Insert( reinterpret_cast<tListItemBase*&>(a) ); 00085 } 00086 00087 void Insert(T *&a) 00088 { 00089 tListItemBase::Insert( reinterpret_cast<tListItemBase*&>(a) ); 00090 } 00091 00092 void Insert(tListItemBase *&a) 00093 { 00094 tListItemBase::Insert( a ); 00095 } 00096 }; 00097 00098 #endif