RegistryManagerList.h

00001 /* START_LICENSE_HEADER
00002 
00003 Copyright (C) 2000 Martin Piper, original design and program code
00004 Copyright (C) 2001 Replica Software
00005 
00006 This program file is copyright (C) Replica Software and can only be used under license.
00007 For more information visit: http://www.replicanet.com/
00008 Or email: info@replicanet.com
00009 
00010 END_LICENSE_HEADER */
00011 #ifndef __REGISTRYMANAGERLIST_H__
00012 #define __REGISTRYMANAGERLIST_H__
00013 #include "RNPlatform/Inc/DLLExportAPI.h"
00014 
00015 #include <assert.h>
00016 
00017 #include <list>
00018 
00019 namespace RNReplicaNet
00020 {
00021 
00025 template <class T> class RegistryManagerList
00026 {
00027 public:
00028     enum Direction
00029     {
00030         kForward=0,
00031         kBackward,
00032         kUndefined
00033     };
00034 
00038     RegistryManagerList()
00039     {
00040         mDirection = kUndefined;
00041     }
00042 
00046     virtual ~RegistryManagerList()
00047     {
00048     }
00049 
00054     void AddItem(T *item)
00055     {
00056         mItems.push_back(item);
00057     }
00058 
00063     void AddItemHead(T *item)
00064     {
00065         mItems.push_front(item);
00066     }
00067 
00072     void RemoveItem(T *item)
00073     {
00074         typename std::list<T *>::iterator tst,ten;
00075 
00076         tst = mItems.begin();
00077         ten = mItems.end();
00078 
00079         while(tst != ten)
00080         {
00081             T *titem = *tst;
00082             if (titem == item)
00083             {
00084                 if (mDirection != kUndefined)
00085                 {
00086                     if (tst == st)
00087                     {
00088                         if (mDirection == kForward)
00089                         {
00090                             st++;
00091                             mItems.erase(tst);
00092                             return;
00093                         }
00094 
00095                         if (mDirection == kBackward)
00096                         {
00097                             st--;
00098                             mItems.erase(tst);
00099                             st++;
00100                             return;
00101                         }
00102                     }
00103                 }
00104                 mItems.erase(tst);
00105                 return;
00106             }
00107             tst++;
00108         }
00109     }
00110 
00114     void RemoveItem(void)
00115     {
00116         assert(mDirection != kUndefined && "RegistryManagerList::RemoveItem() used when BeginIterate() or EndIterate() not used or Iterate() reached the ned of the list");
00117 
00118         typename std::list<T *>::iterator tst;
00119 
00120         if (mDirection == kForward)
00121         {
00122             st--;
00123             tst = st;
00124             st++;
00125             mItems.erase(tst);
00126             return;
00127         }
00128 
00129         if (mDirection == kBackward)
00130         {
00131             tst = st;
00132             st++;
00133             mItems.erase(tst);
00134         }
00135     }
00136 
00140     void BeginIterate(void)
00141     {
00142         mDirection = kForward;
00143         st = mItems.begin();
00144     }
00145 
00149     void EndIterate(void)
00150     {
00151         mDirection = kBackward;
00152         st = mItems.end();
00153     }
00154 
00159     T *Iterate(void)
00160     {
00161         assert(mDirection != kUndefined && "RegistryManagerList::Iterate() used when BeginIterate() or EndIterate() not used or Iterate() reached the end of the list");
00162 
00163         if (mDirection == kForward)
00164         {
00165             if (st != mItems.end())
00166             {
00167                 T *tclass = *st;
00168                 st++;
00169                 return tclass;
00170             }
00171         }
00172 
00173         if (mDirection == kBackward)
00174         {
00175             if (st != mItems.begin())
00176             {
00177                 st--;
00178                 T *tclass = *st;
00179                 return tclass;
00180             }
00181         }
00182 
00183 #ifdef _DEBUG
00184         mDirection = kUndefined;
00185 #endif
00186         return 0;
00187     }
00188 
00189     bool IsEmpty(void) const
00190     {
00191         return mItems.empty();
00192     }
00193 
00194 private:
00195 
00196     std::list<T *> mItems;          
00197     typename std::list<T *>::iterator st;   
00198     Direction mDirection;
00199 };
00200 
00201 } // namespace RNReplicaNet
00202 
00203 #endif

Generated on Sat Jun 28 22:02:16 2008 for ReplicaNet and RNLobby by  doxygen 1.5.3