glinnercircle.h

00001 /*
00002 Copyright (c) 2000-2007 Lee Thomason (www.grinninglizard.com)
00003 Grinning Lizard Utilities.
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any 
00010 purpose, including commercial applications, and to alter it and 
00011 redistribute it freely, subject to the following restrictions:
00012 
00013 1. The origin of this software must not be misrepresented; you must 
00014 not claim that you wrote the original software. If you use this 
00015 software in a product, an acknowledgment in the product documentation 
00016 would be appreciated but is not required.
00017 
00018 2. Altered source versions must be plainly marked as such, and 
00019 must not be misrepresented as being the original software.
00020 
00021 3. This notice may not be removed or altered from any source 
00022 distribution.
00023 */
00024 
00025 #ifndef GRINLIZ_INNER_CIRCLE_INCLUDED
00026 #define GRINLIZ_INNER_CIRCLE_INCLUDED
00027 
00028 namespace grinliz
00029 {
00030 
00031 template<class T>
00032 class InnerCircle
00033 {
00034   public:
00035         enum Sentinel
00036         {
00037                 SENTINEL
00038         };
00039 
00040         InnerCircle( Sentinel ) {
00041                 next = prev = this;
00042                 container = 0;
00043         } 
00044 
00045         InnerCircle( T* _container )    { 
00046                 GLASSERT( _container );
00047                 this->container = _container;
00048                 next = prev = 0;
00049         }
00050 
00051         ~InnerCircle() {
00052                 if ( !Sentinel() )
00053                         Remove();
00054                 container = 0;  // silly, but useful for debugging
00055         }
00056 
00057         void Add( InnerCircle* addThis )
00058         {
00059                 GLASSERT( addThis->next == 0 );
00060                 GLASSERT( addThis->prev == 0 );
00061                 GLASSERT( addThis->Sentinel() == false );
00062 
00063                 if ( next )
00064                 {
00065                         next->prev = addThis;
00066                         addThis->next = next;
00067                 }
00068                 next = addThis;
00069                 addThis->prev = this;
00070         }
00071 
00072         void Remove() {
00073                 GLASSERT( ( prev && next ) || (!prev && !next ) );
00074                 GLASSERT( !Sentinel() );
00075                 if ( prev ) {
00076                         prev->next = next;
00077                         next->prev = prev;
00078                         prev = next = 0;
00079                 }
00080         }
00081 
00082         void RemoveAndDelete() {
00083                 Remove();
00084                 delete container;
00085         }
00086 
00087         bool InList() {
00088                 GLASSERT( !Sentinel() );
00089                 GLASSERT( ( prev && next ) || (!prev && !next ) );
00090                 return prev != 0;
00091         }
00092 
00093         T* Container()                  { return container; }
00094         InnerCircle<T>* Next()  { return next; }
00095         InnerCircle<T>* Prev()  { return prev; }
00096         bool Sentinel()                 { return !container; }
00097 
00098   private:
00099         InnerCircle *next, *prev;
00100         T* container;
00101 };
00102 
00103 
00104 };      // namespace grinliz
00105 
00106 #endif

Generated on Fri Mar 23 19:36:21 2007 for Lilith3D by  doxygen 1.5.1-p1