Main Page | Class Hierarchy | Class List | Directories | File List | Class Members

RegistryManagerMap.h

00001 /* START_LICENSE_HEADER
00002 
00003 Copyright (C) 2000 Martin Piper, original design and program code
00004 Copyright (C) 2001-2005 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 __REGISTRYMANAGERMAP_H__
00012 #define __REGISTRYMANAGERMAP_H__
00013 #include "RNPlatform/Inc/DLLExportAPI.h"
00014 
00015 #include <assert.h>
00016 
00017 #include <map>
00018 
00019 namespace RNReplicaNet
00020 {
00021 
00025 template <class I,class T,class C=std::less<I> > class RegistryManagerMap
00026 {
00027 public:
00028     enum Direction
00029     {
00030         kForward=0,
00031         kBackward,
00032         kUndefined
00033     };
00034 
00038     RegistryManagerMap()
00039     {
00040         mDirection = kUndefined;
00041         st = mItems.end();
00042     }
00043 
00047     virtual ~RegistryManagerMap()
00048     {
00049     }
00050 
00055     bool AddItem(const I &index,const T *item)
00056     {
00057         std::pair<std::map<I,T*,C>::iterator,bool> aMapResult;
00058         aMapResult = mItems.insert(std::pair<I,T*>(index,(T*)item));
00059         return aMapResult.second;
00060     }
00061 
00068     T* FindItem(const I &index)
00069     {
00070         std::map<I,T*,C>::iterator found;
00071 
00072         found = mItems.find(index);
00073 
00074         if (found != mItems.end())
00075         {
00076             st = found;
00077             mDirection = kUndefined;
00078             return (*found).second;
00079         }
00080         return 0;
00081     }
00082 
00088     bool RemoveItem(const I &index)
00089     {
00090         std::map<I,T*,C>::iterator found;
00091 
00092         found = mItems.find(index);
00093 
00094         if (found != mItems.end())
00095         {
00096             if (mDirection != kUndefined)
00097             {
00098                 if (found == st)
00099                 {
00100                     if (mDirection == kForward)
00101                     {
00102                         st++;
00103                         mItems.erase(found);
00104                         return true;
00105                     }
00106 
00107                     if (mDirection == kBackward)
00108                     {
00109                         st--;
00110                         mItems.erase(found);
00111                         st++;
00112                         return true;
00113                     }
00114                 }
00115             }
00116             mItems.erase(found);
00117             return true;
00118         }
00119         return false;
00120     }
00121 
00125     void RemoveItem(void)
00126     {
00127         switch(mDirection)
00128         {
00129             default:
00130             {
00131                 assert(false && "Undefined direction state");
00132                 break;
00133             }
00134 
00135             case kUndefined:
00136             {
00137                 // No direction so it was probably a find so remove the iterator where it is at.
00138                 mItems.erase(st);
00139                 st = mItems.end();
00140                 break;
00141             }
00142 
00143             case kForward:
00144             {
00145                 std::map<I,T*,C>::iterator tst;
00146                 st--;
00147                 tst = st;
00148                 st++;
00149                 mItems.erase(tst);
00150                 break;
00151             }
00152 
00153             case kBackward:
00154             {
00155                 std::map<I,T*,C>::iterator tst;
00156                 tst = st;
00157                 st++;
00158                 mItems.erase(tst);
00159                 break;
00160             }
00161         }
00162     }
00163 
00167     void BeginIterate(void)
00168     {
00169         mDirection = kForward;
00170         st = mItems.begin();
00171     }
00172 
00176     void EndIterate(void)
00177     {
00178         mDirection = kBackward;
00179         st = mItems.end();
00180     }
00181 
00186     T *Iterate(void)
00187     {
00188         assert(mDirection != kUndefined && "RegistryManagerList::Iterate() used when BeginIterate() or EndIterate() not used or Iterate() reached the ned of the list");
00189 
00190         if (mDirection == kForward)
00191         {
00192             if (st != mItems.end())
00193             {
00194                 T *tclass = (*st).second;
00195                 st++;
00196                 return tclass;
00197             }
00198         }
00199 
00200         if (mDirection == kBackward)
00201         {
00202             if (st != mItems.begin())
00203             {
00204                 st--;
00205                 T *tclass = (*st).second;
00206                 return tclass;
00207             }
00208         }
00209 
00210 #ifdef _DEBUG
00211         mDirection = kUndefined;
00212 #endif
00213         return 0;
00214     }
00215 
00216 private:
00217 
00218     std::map<I,T*,C> mItems;                    
00219     typename std::map<I,T*,C>::iterator st;     
00220     Direction mDirection;
00221 };
00222 
00223 } // namespace RNReplicaNet
00224 
00225 #endif

Generated on Sun Oct 30 01:12:31 2005 for Platform by  doxygen 1.4.1