00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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 typedef typename std::map<I,T*,C>::iterator theIter;
00058 typename std::pair<theIter,bool> aMapResult;
00059 aMapResult = mItems.insert(std::pair<I,T*>(index,(T*)item));
00060 return aMapResult.second;
00061 }
00062
00069 T* FindItem(const I &index)
00070 {
00071 typename std::map<I,T*,C>::iterator found;
00072
00073 found = mItems.find(index);
00074
00075 if (found != mItems.end())
00076 {
00077 st = found;
00078 mDirection = kUndefined;
00079 return (*found).second;
00080 }
00081 return 0;
00082 }
00083
00089 T* FindItemNoIterator(const I &index)
00090 {
00091 typename std::map<I,T*,C>::iterator found;
00092
00093 found = mItems.find(index);
00094
00095 if (found != mItems.end())
00096 {
00097 return (*found).second;
00098 }
00099 return 0;
00100 }
00101
00107 bool RemoveItem(const I &index)
00108 {
00109 typename std::map<I,T*,C>::iterator found;
00110
00111 found = mItems.find(index);
00112
00113 if (found != mItems.end())
00114 {
00115 if (mDirection != kUndefined)
00116 {
00117 if (found == st)
00118 {
00119 if (mDirection == kForward)
00120 {
00121 st++;
00122 mItems.erase(found);
00123 return true;
00124 }
00125
00126 if (mDirection == kBackward)
00127 {
00128 st--;
00129 mItems.erase(found);
00130 st++;
00131 return true;
00132 }
00133 }
00134 }
00135 mItems.erase(found);
00136 return true;
00137 }
00138 return false;
00139 }
00140
00144 void RemoveItem(void)
00145 {
00146 switch(mDirection)
00147 {
00148 default:
00149 {
00150 assert(false && "Undefined direction state");
00151 break;
00152 }
00153
00154 case kUndefined:
00155 {
00156
00157 mItems.erase(st);
00158 st = mItems.end();
00159 break;
00160 }
00161
00162 case kForward:
00163 {
00164 typename std::map<I,T*,C>::iterator tst;
00165 st--;
00166 tst = st;
00167 st++;
00168 mItems.erase(tst);
00169 break;
00170 }
00171
00172 case kBackward:
00173 {
00174 typename std::map<I,T*,C>::iterator tst;
00175 tst = st;
00176 st++;
00177 mItems.erase(tst);
00178 break;
00179 }
00180 }
00181 }
00182
00186 void BeginIterate(void)
00187 {
00188 mDirection = kForward;
00189 st = mItems.begin();
00190 }
00191
00195 void EndIterate(void)
00196 {
00197 mDirection = kBackward;
00198 st = mItems.end();
00199 }
00200
00206 T *Iterate(I *index = 0)
00207 {
00208 assert(mDirection != kUndefined && "RegistryManagerList::Iterate() used when BeginIterate() or EndIterate() not used or Iterate() reached the end of the list");
00209
00210 if (mDirection == kForward)
00211 {
00212 if (st != mItems.end())
00213 {
00214 T *tclass = (*st).second;
00215 if (index)
00216 {
00217 *index = (*st).first;
00218 }
00219 st++;
00220 return tclass;
00221 }
00222 }
00223
00224 if (mDirection == kBackward)
00225 {
00226 if (st != mItems.begin())
00227 {
00228 st--;
00229 T *tclass = (*st).second;
00230 if (index)
00231 {
00232 *index = (*st).first;
00233 }
00234 return tclass;
00235 }
00236 }
00237
00238 #ifdef _DEBUG
00239 mDirection = kUndefined;
00240 #endif
00241 return 0;
00242 }
00243
00247 bool IsEmpty(void)
00248 {
00249 return mItems.empty();
00250 }
00251
00252 bool IsEmpty(void) const
00253 {
00254 return mItems.empty();
00255 }
00256
00257 private:
00258
00259 std::map<I,T*,C> mItems;
00260 typename std::map<I,T*,C>::iterator st;
00261 Direction mDirection;
00262 };
00263
00264 }
00265
00266 #endif