RegistryManagerSet.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 __REGISTRYMANAGERSET_H__
00012 #define __REGISTRYMANAGERSET_H__
00013 #include "RNPlatform/Inc/DLLExportAPI.h"
00014 
00015 #include <assert.h>
00016 
00017 #include <set>
00018 
00019 namespace RNReplicaNet
00020 {
00021 
00025 template <class T,class C = std::less<T*> > class RegistryManagerSet
00026 {
00027 public:
00028     enum Direction
00029     {
00030         kForward=0,
00031         kBackward,
00032         kUndefined
00033     };
00034 
00038     RegistryManagerSet()
00039     {
00040         mDirection = kUndefined;
00041     }
00042 
00046     virtual ~RegistryManagerSet()
00047     {
00048     }
00049 
00054     void AddItem(T *item)
00055     {
00056         mItems.insert(item);
00057     }
00058 
00063     void RemoveItem(T *item)
00064     {
00065         typename std::set<T *,C>::iterator found = mItems.find(item);
00066 
00067         if (found != mItems.end())
00068         {
00069             if (mDirection != kUndefined)
00070             {
00071                 if (found == st)
00072                 {
00073                     if (mDirection == kForward)
00074                     {
00075                         st++;
00076                         mItems.erase(found);
00077                         return;
00078                     }
00079 
00080                     if (mDirection == kBackward)
00081                     {
00082                         st--;
00083                         mItems.erase(found);
00084                         st++;
00085                         return;
00086                     }
00087                 }
00088             }
00089             mItems.erase(found);
00090             return;
00091         }
00092     }
00093 
00097     void RemoveItem(void)
00098     {
00099         assert(mDirection != kUndefined && "RegistryManagerSet::RemoveItem() used when BeginIterate() or EndIterate() not used or Iterate() reached the end of the list");
00100 
00101         typename std::set<T *,C>::iterator tst;
00102 
00103         if (mDirection == kForward)
00104         {
00105             st--;
00106             tst = st;
00107             st++;
00108             mItems.erase(tst);
00109             return;
00110         }
00111 
00112         if (mDirection == kBackward)
00113         {
00114             tst = st;
00115             st++;
00116             mItems.erase(tst);
00117         }
00118     }
00119 
00123     void BeginIterate(void)
00124     {
00125         mDirection = kForward;
00126         st = mItems.begin();
00127     }
00128 
00132     void EndIterate(void)
00133     {
00134         mDirection = kBackward;
00135         st = mItems.end();
00136     }
00137 
00142     T *Iterate(void)
00143     {
00144         assert(mDirection != kUndefined && "RegistryManagerSet::Iterate() used when BeginIterate() or EndIterate() not used or Iterate() reached the ned of the list");
00145 
00146         if (mDirection == kForward)
00147         {
00148             if (st != mItems.end())
00149             {
00150                 T *tclass = *st;
00151                 st++;
00152                 return tclass;
00153             }
00154         }
00155 
00156         if (mDirection == kBackward)
00157         {
00158             if (st != mItems.begin())
00159             {
00160                 st--;
00161                 T *tclass = *st;
00162                 return tclass;
00163             }
00164         }
00165 
00166 #ifdef _DEBUG
00167         mDirection = kUndefined;
00168 #endif
00169         return 0;
00170     }
00171 
00172     bool IsEmpty(void) const
00173     {
00174         return mItems.empty();
00175     }
00176 
00177 private:
00178 
00179     std::set<T *,C> mItems;         
00180     typename std::set<T *,C>::iterator st;  
00181     Direction mDirection;
00182 };
00183 
00184 } // namespace RNReplicaNet
00185 
00186 #endif

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