VR-Forces 4.2 Class Documentation
rwTemplatedList.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2013 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 
20 template <typename T, bool T_autoDelete = false>
22 {
23 public:
26 
29 
31  DtRwTemplatedListPtr(const DtString& name, DtReaderWriterRegistry* parentRegistry = NULL);
32  DtRwTemplatedListPtr(const DtSymbolString& name, DtReaderWriterRegistry* parentRegistry = NULL);
33 
37  DtReaderWriterRegistry* parentRegistry = NULL);
38 
39  DtRwTemplatedListPtr(const DtSymbolString& name, const DtRwTemplatedListPtr& orig,
40  DtReaderWriterRegistry* parentRegistry = NULL);
41 
44  DtReaderWriterRegistry* parentRegistry = NULL);
45 
47 
48  virtual ~DtRwTemplatedListPtr();
49 
50  DtlObjPtr getData(DtlObjPtr args, const DtFilename& searchPath,
51  const DtVariableBindingsList& variableBindings);
52 
55  virtual DtListItem* add(T* data) {return DtRwList::add(data);};
56 
59  void removeAll();
60  void* remove(DtListItem* item);
61  void* remove(T* item);
62 
64  void removeAndDeleteAll();
65 
67  bool operator==(const DtRwTemplatedListPtr&) const;
68  bool operator!=(const DtRwTemplatedListPtr&) const;
69 
70 public:
71 
75  void setAutoDelete(bool b) { myAutoDelete = b; };
76  bool autoDelete() const { return myAutoDelete; };
77 
78  const char* readerWriterType() const;
79 
80  static const char* ListReaderWriterType();
81 
82  void copyList(const DtRwTemplatedListPtr& orig);
83 
84 protected:
86 };
87 
88 namespace DtBufferSerialize
89 {
92  template <typename T, bool T_autoDelete>
94 
98  template <typename T, bool T_autoDelete>
99  char *encode(const DtRwTemplatedListPtr<T, T_autoDelete>& val, char *buffer);
100 
105  template <typename T, bool T_autoDelete>
106  const char *decode(DtRwTemplatedListPtr<T, T_autoDelete>& val, const char *buffer) ;
107 };
108 
109 template <typename T, bool T_autoDelete>
111  DtRwList(),
112 myAutoDelete(T_autoDelete)
113 {
114 }
115 
116 template <typename T, bool T_autoDelete>
118  DtRwList(name, parentRegistry),
119 myAutoDelete(T_autoDelete)
120 {
121 }
122 
123 template <typename T, bool T_autoDelete>
125  DtRwList(name, parentRegistry),
126 myAutoDelete(T_autoDelete)
127 {
128 }
129 
130 template <typename T, bool T_autoDelete>
132  DtReaderWriterRegistry * parentRegistry) :
133 DtRwList(name, orig, parentRegistry),
134 myAutoDelete(orig.myAutoDelete)
135 {
136  copyList(orig);
137 }
138 
139 template <typename T, bool T_autoDelete>
141  DtReaderWriterRegistry * parentRegistry) :
142 DtRwList(name, orig, parentRegistry),
143 myAutoDelete(orig.myAutoDelete)
144 {
145  copyList(orig);
146 }
147 
148 template <typename T, bool T_autoDelete>
150  DtReaderWriterRegistry * parentRegistry) :
151 DtRwList(orig.name(), orig, parentRegistry),
152 myAutoDelete(orig.myAutoDelete)
153 {
154  copyList(orig);
155 }
156 
157 template <typename T, bool T_autoDelete>
159 {
160  if (myAutoDelete)
161  {
162  removeAndDeleteAll();
163  }
164 }
165 
166 template <typename T, bool T_autoDelete>
168 {
169  if (this != &orig)
170  {
171  DtRwList::operator=(orig);
172 
173  removeAll();
174  copyList(orig);
175  myAutoDelete = orig.myAutoDelete;
176  }
177 
178  return *this;
179 }
180 
181 template <typename T, bool T_autoDelete>
183 {
184  DtListItem* listItem = itemLookup(item);
185 
186  if (listItem)
187  {
188  return remove(listItem);
189  }
190 
191  return NULL;
192 }
193 
194 template <typename T, bool T_autoDelete>
196 {
197  T* d = (T*)DtRwList::remove(static_cast<DtReaderWriter*>(item->data()), item);
198  if (myAutoDelete)
199  {
200  delete d;
201  d = NULL;
202  return NULL;
203  }
204  else
205  {
206  return d;
207  }
208 }
209 
210 template <typename T, bool T_autoDelete>
212 {
213  std::vector<T*> toDelete;
214 
215  iterator iter(*this);
216 
217  for (T* info = iter.first(); info; info = iter.next())
218  {
219  toDelete.push_back(info);
220  }
221 
223 
224  int iLimit = toDelete.size();
225 
226  for (int i = 0; i < iLimit; i++)
227  {
228  delete toDelete[i];
229  }
230 }
231 
232 template <typename T, bool T_autoDelete>
233 DtlObjPtr DtRwTemplatedListPtr<T, T_autoDelete>::getData(DtlObjPtr args, const DtFilename& searchPath,
234  const DtVariableBindingsList& variableBindings)
235 {
236  DtlObjPtr nameObj = DtcCar(args);
238  DtlObjPtr nextItem = DtcCdr(args);
239  if(nextItem == DtlObjPtr::nil())
240  {
241  return nextItem;
242  }
243 
244  T* value = new T(nameObj->pname());
245  value->getSelf(args, searchPath, variableBindings);
246 
247  add(value);
248 
249  return DtcCdr(DtcCdr(args));
250 }
251 
252 template <typename T, bool T_autoDelete>
254 {
255  if (myAutoDelete)
256  {
257  removeAndDeleteAll();
258  }
259  else
260  {
262  }
263 }
264 
265 template <typename T, bool T_autoDelete>
267 {
268  iterator iter(orig);
269 
270  for (T* info = iter.first(); info; info = iter.next())
271  {
272  add(new T(*info));
273  }
274 }
275 
276 template <typename T, bool T_autoDelete>
278 {
279  return ListReaderWriterType();
280 };
281 
282 template <typename T, bool T_autoDelete>
284 {
285  static T theDefaultType("default-name");
286  static std::string* listType = new std::string(
287  std::string(theDefaultType.readerWriterType()) + "-templated-list");
288  return listType->c_str();
289 }
290 
291 template <typename T, bool T_autoDelete>
293 {
294  if (count() == rhs.count())
295  {
296  DtListItem* thisItem = first();
297  DtListItem* thatItem = rhs.first();
298 
299  while (thisItem && thatItem)
300  {
301  T* thisElement = static_cast<T*>(thisItem->data());
302  T* thatElement = static_cast<T*>(thatItem->data());
303 
304  if (*thisElement != *thatElement)
305  {
306  return false;
307  }
308 
309  thisItem = thisItem->next();
310  thatItem = thatItem->next();
311  }
312 
313  return true;
314  }
315 
316  return false;
317 }
318 
319 template <typename T, bool T_autoDelete>
321 {
322  return !(*this == rhs);
323 }
324 
325 namespace DtBufferSerialize
326 {
327  template <typename T, bool T_autoDelete>
329  {
330  DtU16 a;
331  int size = getSize(a);
332  DtListItem* item = val.first();
333  while(item)
334  {
335  const T* itemValue = static_cast<const T*>(item->data());
336  size += getSize(*itemValue);
337  item = item->next();
338  }
339  return size;
340  }
341 
342  template <typename T, bool T_autoDelete>
343  char *encode(const DtRwTemplatedListPtr<T, T_autoDelete>& val, char *buffer)
344  {
345  DtU16 n = val.count();
346  buffer = encode(n, buffer);
347  DtListItem* item = val.first();
348  while(item)
349  {
350  const T* itemValue = static_cast<const T*>(item->data());
351  buffer = encode(*itemValue, buffer);
352  item = item->next();
353  }
354  return buffer;
355  }
356 
357  template <typename T, bool T_autoDelete>
358  const char *decode(DtRwTemplatedListPtr<T, T_autoDelete>& val, const char *buffer)
359  {
360  DtU16 n = 0;
361  buffer = decode(n, buffer);
362  val.removeAndDeleteAll();
363  for(int i = 0; i < n;++i)
364  {
365  T* itemValue = new T("item", &(val.registry()));
366  buffer = decode(*itemValue, buffer);
367  val.addToEnd(itemValue);
368  }
369  return buffer;
370  }
371 };
372 

Document ID: Generated on Sun Nov 24 19:49:21 EST 2013 from SVN revision 133924
Copyright © 2005-2013 VT MÄK. All Rights Reserved (www.mak.com)