ReplicaNet and RNLobby  1
RegistryManagerMap.h
1 /* START_LICENSE_HEADER
2 
3 Copyright (C) 2000 Martin Piper, original design and program code
4 Copyright (C) 2001 Replica Software
5 
6 This program file is copyright (C) Replica Software and can only be used under license.
7 For more information visit: http://www.replicanet.com/
8 Or email: info@replicanet.com
9 
10 END_LICENSE_HEADER */
11 #include "RNPlatform/Inc/MemoryTracking.h"
12 #ifndef __REGISTRYMANAGERMAP_H__
13 #define __REGISTRYMANAGERMAP_H__
14 
15 #include <assert.h>
16 
17 #include <map>
18 
19 namespace RNReplicaNet
20 {
21 
25 template <class I,class T,class C=std::less<I> > class RegistryManagerMap
26 {
27 public:
28  enum Direction
29  {
30  kForward=0,
31  kBackward,
32  kUndefined
33  };
34 
39  {
40  mDirection = kUndefined;
41  st = mItems.end();
42  }
43 
48  {
49  }
50 
55  bool AddItem(const I &index,const T *item)
56  {
57  typedef typename std::map<I,T*,C>::iterator theIter;
58  typename std::pair<theIter,bool> aMapResult;
59  aMapResult = mItems.insert(std::pair<I,T*>(index,(T*)item));
60  return aMapResult.second;
61  }
62 
69  T* FindItem(const I &index)
70  {
71  typename std::map<I,T*,C>::iterator found;
72 
73  found = mItems.find(index);
74 
75  if (found != mItems.end())
76  {
77  st = found;
78  mDirection = kUndefined;
79  return (*found).second;
80  }
81  return 0;
82  }
83 
89  T* FindItemNoIterator(const I &index)
90  {
91  typename std::map<I,T*,C>::iterator found;
92 
93  found = mItems.find(index);
94 
95  if (found != mItems.end())
96  {
97  return (*found).second;
98  }
99  return 0;
100  }
101 
107  bool RemoveItem(const I &index)
108  {
109  typename std::map<I,T*,C>::iterator found;
110 
111  found = mItems.find(index);
112 
113  if (found != mItems.end())
114  {
115  if (mDirection != kUndefined)
116  {
117  if (found == st)
118  {
119  if (mDirection == kForward)
120  {
121  st++;
122  mItems.erase(found);
123  return true;
124  }
125 
126  if (mDirection == kBackward)
127  {
128  st--;
129  mItems.erase(found);
130  st++;
131  return true;
132  }
133  }
134  }
135  mItems.erase(found);
136  return true;
137  }
138  return false;
139  }
140 
144  void RemoveItem(void)
145  {
146  switch(mDirection)
147  {
148  default:
149  {
150  assert(false && "Undefined direction state");
151  break;
152  }
153 
154  case kUndefined:
155  {
156  // No direction so it was probably a find so remove the iterator where it is at.
157  mItems.erase(st);
158  st = mItems.end();
159  break;
160  }
161 
162  case kForward:
163  {
164  typename std::map<I,T*,C>::iterator tst;
165  st--;
166  tst = st;
167  st++;
168  mItems.erase(tst);
169  break;
170  }
171 
172  case kBackward:
173  {
174  typename std::map<I,T*,C>::iterator tst;
175  tst = st;
176  st++;
177  mItems.erase(tst);
178  break;
179  }
180  }
181  }
182 
186  void BeginIterate(void)
187  {
188  mDirection = kForward;
189  st = mItems.begin();
190  }
191 
195  void EndIterate(void)
196  {
197  mDirection = kBackward;
198  st = mItems.end();
199  }
200 
206  T *Iterate(I *index = 0)
207  {
208  assert(mDirection != kUndefined && "RegistryManagerList::Iterate() used when BeginIterate() or EndIterate() not used or Iterate() reached the end of the list");
209 
210  if (mDirection == kForward)
211  {
212  if (st != mItems.end())
213  {
214  T *tclass = (*st).second;
215  if (index)
216  {
217  *index = (*st).first;
218  }
219  st++;
220  return tclass;
221  }
222  }
223 
224  if (mDirection == kBackward)
225  {
226  if (st != mItems.begin())
227  {
228  st--;
229  T *tclass = (*st).second;
230  if (index)
231  {
232  *index = (*st).first;
233  }
234  return tclass;
235  }
236  }
237 
238 #ifdef _DEBUG
239  mDirection = kUndefined;
240 #endif
241  return 0;
242  }
243 
247  bool IsEmpty(void)
248  {
249  return mItems.empty();
250  }
251 
252  bool IsEmpty(void) const
253  {
254  return mItems.empty();
255  }
256 
260  void Clear(void)
261  {
262  mItems.clear();
263  st = mItems.end();
264  }
265 
266 private:
267 
268  std::map<I,T*,C> mItems;
269  typename std::map<I,T*,C>::iterator st;
270  Direction mDirection;
271 };
272 
273 } // namespace RNReplicaNet
274 
275 #endif
void EndIterate(void)
Definition: RegistryManagerMap.h:195
T * FindItem(const I &index)
Definition: RegistryManagerMap.h:69
void RemoveItem(void)
Definition: RegistryManagerMap.h:144
bool RemoveItem(const I &index)
Definition: RegistryManagerMap.h:107
bool IsEmpty(void)
Definition: RegistryManagerMap.h:247
T * FindItemNoIterator(const I &index)
Definition: RegistryManagerMap.h:89
T * Iterate(I *index=0)
Definition: RegistryManagerMap.h:206
RegistryManagerMap()
Definition: RegistryManagerMap.h:38
Definition: RegistryManagerMap.h:25
void BeginIterate(void)
Definition: RegistryManagerMap.h:186
virtual ~RegistryManagerMap()
Definition: RegistryManagerMap.h:47
void Clear(void)
Definition: RegistryManagerMap.h:260
bool AddItem(const I &index, const T *item)
Definition: RegistryManagerMap.h:55