glinnercircle.h

00001 #ifndef GRINLIZ_INNER_CIRCLE_INCLUDED
00002 #define GRINLIZ_INNER_CIRCLE_INCLUDED
00003 
00004 namespace grinliz
00005 {
00006 
00007 template<class T>
00008 class InnerCircle
00009 {
00010   public:
00011         enum Sentinel
00012         {
00013                 SENTINEL
00014         };
00015 
00016         InnerCircle( Sentinel ) {
00017                 next = prev = this;
00018                 container = 0;
00019         } 
00020 
00021         InnerCircle( T* container )     { 
00022                 GLASSERT( container );
00023                 this->container = container;
00024                 next = prev = 0;
00025         }
00026 
00027         ~InnerCircle() {
00028                 if ( !Sentinel() )
00029                         Remove();
00030                 container = 0;  // silly, but useful for debugging
00031         }
00032 
00033         void Add( InnerCircle* addThis )
00034         {
00035                 GLASSERT( addThis->next == 0 );
00036                 GLASSERT( addThis->prev == 0 );
00037                 GLASSERT( addThis->Sentinel() == false );
00038 
00039                 if ( next )
00040                 {
00041                         next->prev = addThis;
00042                         addThis->next = next;
00043                 }
00044                 next = addThis;
00045                 addThis->prev = this;
00046         }
00047 
00048         void Remove() {
00049                 GLASSERT( ( prev && next ) || (!prev && !next ) );
00050                 GLASSERT( !Sentinel() );
00051                 if ( prev ) {
00052                         prev->next = next;
00053                         next->prev = prev;
00054                         prev = next = 0;
00055                 }
00056         }
00057 
00058         void RemoveAndDelete() {
00059                 Remove();
00060                 delete container;
00061         }
00062 
00063         bool InList() {
00064                 GLASSERT( !Sentinel() );
00065                 GLASSERT( ( prev && next ) || (!prev && !next ) );
00066                 return prev != 0;
00067         }
00068 
00069         T* Container()                  { return container; }
00070         InnerCircle<T>* Next()  { return next; }
00071         InnerCircle<T>* Prev()  { return prev; }
00072         bool Sentinel()                 { return !container; }
00073 
00074   private:
00075         InnerCircle *next, *prev;
00076         T* container;
00077 };
00078 
00079 
00080 };      // namespace grinliz
00081 
00082 #endif

Generated on Thu Jul 20 20:45:31 2006 for Kyra by  doxygen 1.4.7