VR-Forces 4.1 Class Documentation
rwTemplatedList.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 1999 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 
8 
9 #pragma once
10 
11 #include "vrfutil/rwList.h"
12 #include "vrfutil/iteratorBase.h"
14 
17 template <typename T>
19 {
20 public:
23 
26 
28  DtRwTemplatedListPtr(const DtString& name, DtReaderWriterRegistry* parentRegistry = NULL);
29  DtRwTemplatedListPtr(const DtSymbolString& name, DtReaderWriterRegistry* parentRegistry = NULL);
30 
34  DtReaderWriterRegistry* parentRegistry = NULL);
35 
37  DtReaderWriterRegistry* parentRegistry = NULL);
38 
41  DtReaderWriterRegistry* parentRegistry = NULL);
42 
44 
45  virtual ~DtRwTemplatedListPtr();
46 
47  DtlObjPtr getData(DtlObjPtr args, const DtFilename& searchPath,
48  const DtVariableBindingsList& variableBindings);
49 
52  void removeAll();
53  void* remove(DtListItem* item);
54  void* remove(T* item);
55 
57  void removeAndDeleteAll();
58 
60  bool operator==(const DtRwTemplatedListPtr&) const;
61  bool operator!=(const DtRwTemplatedListPtr&) const;
62 
63 public:
64 
68  void setAutoDelete(bool b) { myAutoDelete = b; };
69  bool autoDelete() const { return myAutoDelete; };
70 
71  const char* readerWriterType() const;
72 
73  void copyList(const DtRwTemplatedListPtr& orig);
74 
75 protected:
77 };
78 
79 namespace DtBufferSerialize
80 {
83  template <typename T>
84  int getSize(const DtRwTemplatedListPtr<T>& val);
85 
89  template <typename T>
90  char *encode(const DtRwTemplatedListPtr<T>& val, char *buffer);
91 
96  template <typename T>
97  const char *decode(DtRwTemplatedListPtr<T>& val, const char *buffer) ;
98 };
99 
100 template <typename T>
102  DtRwList(),
103 myAutoDelete(false)
104 {
105 }
106 
107 template <typename T>
109  DtRwList(name, parentRegistry),
110 myAutoDelete(false)
111 {
112 }
113 
114 template <typename T>
116  DtRwList(name, parentRegistry),
117 myAutoDelete(false)
118 {
119 }
120 
121 template <typename T>
123  DtReaderWriterRegistry * parentRegistry) :
124 DtRwList(name, orig, parentRegistry),
125 myAutoDelete(orig.myAutoDelete)
126 {
127  copyList(orig);
128 }
129 
130 template <typename T>
132  DtReaderWriterRegistry * parentRegistry) :
133 DtRwList(name, orig, parentRegistry),
134 myAutoDelete(orig.myAutoDelete)
135 {
136  copyList(orig);
137 }
138 
139 template <typename T>
141  DtReaderWriterRegistry * parentRegistry) :
142 DtRwList(orig.name(), orig, parentRegistry),
143 myAutoDelete(orig.myAutoDelete)
144 {
145  copyList(orig);
146 }
147 
148 template <typename T>
150 {
151  if (myAutoDelete)
152  {
153  removeAndDeleteAll();
154  }
155 }
156 
157 template <typename T>
159 {
160  if (this != &orig)
161  {
162  removeAll();
163  copyList(orig);
164  myAutoDelete = orig.myAutoDelete;
165  }
166 
167  return *this;
168 }
169 
170 template <typename T>
172 {
173  DtListItem* listItem = itemLookup(item);
174 
175  if (listItem)
176  {
177  return remove(listItem);
178  }
179 
180  return NULL;
181 }
182 
183 template <typename T>
184 void* DtRwTemplatedListPtr<T>::remove(DtListItem* item)
185 {
186  T* d = (T*)DtRwList::remove(static_cast<DtReaderWriter*>(item->data()), item);
187  if (myAutoDelete)
188  {
189  delete d;
190  d = NULL;
191  return NULL;
192  }
193  else
194  {
195  return d;
196  }
197 }
198 
199 template <typename T>
201 {
202  std::vector<T*> toDelete;
203 
204  iterator iter(*this);
205 
206  for (T* info = iter.first(); info; info = iter.next())
207  {
208  toDelete.push_back(info);
209  }
210 
212 
213  int iLimit = toDelete.size();
214 
215  for (int i = 0; i < iLimit; i++)
216  {
217  delete toDelete[i];
218  }
219 }
220 
221 template <typename T>
222 DtlObjPtr DtRwTemplatedListPtr<T>::getData(DtlObjPtr args, const DtFilename& searchPath,
223  const DtVariableBindingsList& variableBindings)
224 {
225  DtlObjPtr nameObj = DtcCar(args);
227  DtlObjPtr nextItem = DtcCdr(args);
228  if(nextItem == DtlObjPtr::nil())
229  {
230  return nextItem;
231  }
232 
233  T* value = new T(nameObj->pname());
234  value->getSelf(args, searchPath, variableBindings);
235 
236  add(value);
237 
238  return DtcCdr(DtcCdr(args));
239 }
240 
241 template <typename T>
243 {
244  if (myAutoDelete)
245  {
246  removeAndDeleteAll();
247  }
248  else
249  {
251  }
252 }
253 
254 template <typename T>
256 {
257  iterator iter(orig);
258 
259  for (T* info = iter.first(); info; info = iter.next())
260  {
261  add(new T(*info));
262  }
263 }
264 
265 template <typename T>
267 {
268  static T theDefaultType("default-name");
269 
270  return theDefaultType.readerWriterType();
271 }
272 
273 template <typename T>
275 {
276  if (count() == rhs.count())
277  {
278  DtListItem* thisItem = first();
279  DtListItem* thatItem = rhs.first();
280 
281  while (thisItem && thatItem)
282  {
283  T* thisElement = static_cast<T*>(thisItem->data());
284  T* thatElement = static_cast<T*>(thatItem->data());
285 
286  if (*thisElement != *thatElement)
287  {
288  return false;
289  }
290 
291  thisItem = thisItem->next();
292  thatItem = thatItem->next();
293  }
294 
295  return true;
296  }
297 
298  return false;
299 }
300 
301 template <typename T>
303 {
304  return !(*this == rhs);
305 }
306 
307 namespace DtBufferSerialize
308 {
309  template <typename T>
311  {
312  DtU16 a;
313  int size = getSize(a);
314  DtListItem* item = val.first();
315  while(item)
316  {
317  const T* itemValue = static_cast<const T*>(item->data());
318  size += getSize(*itemValue);
319  item = item->next();
320  }
321  return size;
322  }
323 
324  template <typename T>
325  char *encode(const DtRwTemplatedListPtr<T>& val, char *buffer)
326  {
327  DtU16 n = val.count();
328  buffer = encode(n, buffer);
329  DtListItem* item = val.first();
330  while(item)
331  {
332  const T* itemValue = static_cast<const T*>(item->data());
333  buffer = encode(*itemValue, buffer);
334  item = item->next();
335  }
336  return buffer;
337  }
338 
339  template <typename T>
340  const char *decode(DtRwTemplatedListPtr<T>& val, const char *buffer)
341  {
342  DtU16 n = 0;
343  buffer = decode(n, buffer);
344  val.removeAndDeleteAll();
345  for(int i = 0; i < n;++i)
346  {
347  T* itemValue = new T("item", &(val.registry()));
348  buffer = decode(*itemValue, buffer);
349  val.addToEnd(itemValue);
350  }
351  return buffer;
352  }
353 };
354 

Document ID: Generated on Tue Jan 29 18:21:16 EST 2013 from SVN revision 123193
Copyright © 2005-2013 VT MÄK Inc. All Rights Reserved (www.mak.com)