![]() |
VR-Forces 4.0.4 Class Documentation
|
00001 /******************************************************************************* 00002 ** Copyright (c) 1999 MAK Technologies, Inc. 00003 ** All rights reserved. 00004 *******************************************************************************/ 00005 /******************************************************************************* 00006 ** $RCSfile: rwTemplatedList.h,v $ $Revision: 1.23 $ $State: Exp $ 00007 *******************************************************************************/ 00008 00011 00012 #ifndef DtRwTemplatedList_H_ 00013 #define DtRwTemplatedList_H_ 00014 00015 #include "vrfutil/rwList.h" 00016 #include "vrfutil/iteratorBase.h" 00017 #include "vrfutil/bufferSerialize.h" 00018 00021 template <typename T> 00022 class DtRwTemplatedListPtr : public DtRwList 00023 { 00024 public: 00025 typedef DtIteratorBase<T> iterator; 00026 typedef DtIteratorBase<const T> const_iterator; 00027 00029 DtRwTemplatedListPtr(); 00030 00032 DtRwTemplatedListPtr(const DtString& name, DtReaderWriterRegistry* parentRegistry = NULL); 00033 DtRwTemplatedListPtr(const DtSymbolString& name, DtReaderWriterRegistry* parentRegistry = NULL); 00034 00037 DtRwTemplatedListPtr(const DtString& name, const DtRwTemplatedListPtr& orig, 00038 DtReaderWriterRegistry* parentRegistry = NULL); 00039 00040 DtRwTemplatedListPtr(const DtSymbolString& name, const DtRwTemplatedListPtr& orig, 00041 DtReaderWriterRegistry* parentRegistry = NULL); 00042 00044 DtRwTemplatedListPtr(const DtRwTemplatedListPtr& orig, 00045 DtReaderWriterRegistry* parentRegistry = NULL); 00046 00047 DtRwTemplatedListPtr& operator=(const DtRwTemplatedListPtr& orig); 00048 00049 virtual ~DtRwTemplatedListPtr(); 00050 00051 DtlObjPtr getData(DtlObjPtr args, const DtFilename& searchPath, 00052 const DtVariableBindingsList& variableBindings); 00053 00056 void removeAll(); 00057 void* remove(DtListItem* item); 00058 void* remove(T* item); 00059 00061 void removeAndDeleteAll(); 00062 00064 bool operator==(const DtRwTemplatedListPtr&) const; 00065 bool operator!=(const DtRwTemplatedListPtr&) const; 00066 00067 public: 00068 00072 void setAutoDelete(bool b) { myAutoDelete = b; }; 00073 bool autoDelete() const { return myAutoDelete; }; 00074 00075 const char* readerWriterType() const; 00076 00077 void copyList(const DtRwTemplatedListPtr& orig); 00078 00079 protected: 00080 bool myAutoDelete; 00081 }; 00082 00083 namespace DtBufferSerialize 00084 { 00087 template <typename T> 00088 int getSize(const DtRwTemplatedListPtr<T>& val); 00089 00093 template <typename T> 00094 char *encode(const DtRwTemplatedListPtr<T>& val, char *buffer); 00095 00100 template <typename T> 00101 const char *decode(DtRwTemplatedListPtr<T>& val, const char *buffer) ; 00102 }; 00103 00104 template <typename T> 00105 DtRwTemplatedListPtr<T>::DtRwTemplatedListPtr() : 00106 DtRwList(), 00107 myAutoDelete(false) 00108 { 00109 } 00110 00111 template <typename T> 00112 DtRwTemplatedListPtr<T>::DtRwTemplatedListPtr(const DtString& name, DtReaderWriterRegistry * parentRegistry) : 00113 DtRwList(name, parentRegistry), 00114 myAutoDelete(false) 00115 { 00116 } 00117 00118 template <typename T> 00119 DtRwTemplatedListPtr<T>::DtRwTemplatedListPtr(const DtSymbolString& name, DtReaderWriterRegistry * parentRegistry) : 00120 DtRwList(name, parentRegistry), 00121 myAutoDelete(false) 00122 { 00123 } 00124 00125 template <typename T> 00126 DtRwTemplatedListPtr<T>::DtRwTemplatedListPtr(const DtString& name, const DtRwTemplatedListPtr<T>& orig, 00127 DtReaderWriterRegistry * parentRegistry) : 00128 DtRwList(name, orig, parentRegistry), 00129 myAutoDelete(orig.myAutoDelete) 00130 { 00131 copyList(orig); 00132 } 00133 00134 template <typename T> 00135 DtRwTemplatedListPtr<T>::DtRwTemplatedListPtr(const DtSymbolString& name, const DtRwTemplatedListPtr<T>& orig, 00136 DtReaderWriterRegistry * parentRegistry) : 00137 DtRwList(name, orig, parentRegistry), 00138 myAutoDelete(orig.myAutoDelete) 00139 { 00140 copyList(orig); 00141 } 00142 00143 template <typename T> 00144 DtRwTemplatedListPtr<T>::DtRwTemplatedListPtr(const DtRwTemplatedListPtr<T>& orig, 00145 DtReaderWriterRegistry * parentRegistry) : 00146 DtRwList(orig.name(), orig, parentRegistry), 00147 myAutoDelete(orig.myAutoDelete) 00148 { 00149 copyList(orig); 00150 } 00151 00152 template <typename T> 00153 DtRwTemplatedListPtr<T>::~DtRwTemplatedListPtr() 00154 { 00155 if (myAutoDelete) 00156 { 00157 removeAndDeleteAll(); 00158 } 00159 } 00160 00161 template <typename T> 00162 DtRwTemplatedListPtr<T>& DtRwTemplatedListPtr<T>::operator=(const DtRwTemplatedListPtr<T>& orig) 00163 { 00164 if (this != &orig) 00165 { 00166 removeAll(); 00167 copyList(orig); 00168 myAutoDelete = orig.myAutoDelete; 00169 } 00170 00171 return *this; 00172 } 00173 00174 template <typename T> 00175 void * DtRwTemplatedListPtr<T>::remove(T* item) 00176 { 00177 DtListItem* listItem = itemLookup(item); 00178 00179 if (listItem) 00180 { 00181 return remove(listItem); 00182 } 00183 00184 return NULL; 00185 } 00186 00187 template <typename T> 00188 void* DtRwTemplatedListPtr<T>::remove(DtListItem* item) 00189 { 00190 T* d = (T*)DtRwList::remove(static_cast<DtReaderWriter*>(item->data()), item); 00191 if (myAutoDelete) 00192 { 00193 delete d; 00194 d = NULL; 00195 return NULL; 00196 } 00197 else 00198 { 00199 return d; 00200 } 00201 } 00202 00203 template <typename T> 00204 void DtRwTemplatedListPtr<T>::removeAndDeleteAll() 00205 { 00206 std::vector<T*> toDelete; 00207 00208 iterator iter(*this); 00209 00210 for (T* info = iter.first(); info; info = iter.next()) 00211 { 00212 toDelete.push_back(info); 00213 } 00214 00215 DtRwList::removeAll(); 00216 00217 int iLimit = toDelete.size(); 00218 00219 for (int i = 0; i < iLimit; i++) 00220 { 00221 delete toDelete[i]; 00222 } 00223 } 00224 00225 template <typename T> 00226 DtlObjPtr DtRwTemplatedListPtr<T>::getData(DtlObjPtr args, const DtFilename& searchPath, 00227 const DtVariableBindingsList& variableBindings) 00228 { 00229 DtlObjPtr nameObj = DtcCar(args); 00231 DtlObjPtr nextItem = DtcCdr(args); 00232 if(nextItem == DtlObjPtr::nil()) 00233 { 00234 return nextItem; 00235 } 00236 00237 T* value = new T(nameObj->pname()); 00238 value->getSelf(args, searchPath, variableBindings); 00239 00240 add(value); 00241 00242 return DtcCdr(DtcCdr(args)); 00243 } 00244 00245 template <typename T> 00246 void DtRwTemplatedListPtr<T>::removeAll() 00247 { 00248 if (myAutoDelete) 00249 { 00250 removeAndDeleteAll(); 00251 } 00252 else 00253 { 00254 DtRwList::removeAll(); 00255 } 00256 } 00257 00258 template <typename T> 00259 void DtRwTemplatedListPtr<T>::copyList(const DtRwTemplatedListPtr<T>& orig) 00260 { 00261 iterator iter(orig); 00262 00263 for (T* info = iter.first(); info; info = iter.next()) 00264 { 00265 add(new T(*info)); 00266 } 00267 } 00268 00269 template <typename T> 00270 const char* DtRwTemplatedListPtr<T>::readerWriterType() const 00271 { 00272 static T theDefaultType("default-name"); 00273 00274 return theDefaultType.readerWriterType(); 00275 } 00276 00277 template <typename T> 00278 bool DtRwTemplatedListPtr<T>::operator==(const DtRwTemplatedListPtr& rhs) const 00279 { 00280 if (count() == rhs.count()) 00281 { 00282 DtListItem* thisItem = first(); 00283 DtListItem* thatItem = rhs.first(); 00284 00285 while (thisItem && thatItem) 00286 { 00287 T* thisElement = static_cast<T*>(thisItem->data()); 00288 T* thatElement = static_cast<T*>(thatItem->data()); 00289 00290 if (*thisElement != *thatElement) 00291 { 00292 return false; 00293 } 00294 00295 thisItem = thisItem->next(); 00296 thatItem = thatItem->next(); 00297 } 00298 00299 return true; 00300 } 00301 00302 return false; 00303 } 00304 00305 template <typename T> 00306 bool DtRwTemplatedListPtr<T>::operator!=(const DtRwTemplatedListPtr& rhs) const 00307 { 00308 return !(*this == rhs); 00309 } 00310 00311 namespace DtBufferSerialize 00312 { 00313 template <typename T> 00314 int getSize(const DtRwTemplatedListPtr<T>& val) 00315 { 00316 DtU16 a; 00317 int size = getSize(a); 00318 DtListItem* item = val.first(); 00319 while(item) 00320 { 00321 const T* itemValue = static_cast<const T*>(item->data()); 00322 size += getSize(*itemValue); 00323 item = item->next(); 00324 } 00325 return size; 00326 } 00327 00328 template <typename T> 00329 char *encode(const DtRwTemplatedListPtr<T>& val, char *buffer) 00330 { 00331 DtU16 n = val.count(); 00332 buffer = encode(n, buffer); 00333 DtListItem* item = val.first(); 00334 while(item) 00335 { 00336 const T* itemValue = static_cast<const T*>(item->data()); 00337 buffer = encode(*itemValue, buffer); 00338 item = item->next(); 00339 } 00340 return buffer; 00341 } 00342 00343 template <typename T> 00344 const char *decode(DtRwTemplatedListPtr<T>& val, const char *buffer) 00345 { 00346 DtU16 n = 0; 00347 buffer = decode(n, buffer); 00348 val.removeAndDeleteAll(); 00349 for(int i = 0; i < n;++i) 00350 { 00351 T* itemValue = new T("item", &(val.registry())); 00352 buffer = decode(*itemValue, buffer); 00353 val.addToEnd(itemValue); 00354 } 00355 return buffer; 00356 } 00357 }; 00358 00359 #endif 00360