// -------------------------------------------------
// CHashtable.cpp
// -------------------------------------------------
// (c) 2003, BeamVision
// Alle Rechte vorbehalten
// -------------------------------------------------
#include "../common/CHashtable.h"
///////////////////////////////////////////////////////////////
// TEMPLATE T: class to use as a class for the Hashtable
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
// constructs the element
//
template <class T> CHashtable<T>::CHashtable() {
this->data = new CArray();
}
///////////////////////////////////////////////////////////////
// every element entered in the hashtable is deleted
// when it is removed
//
template <class T> CHashtable<T>::~CHashtable() {
for(unsigned int i = 0; i < this->data->countItems(); i++) {
if(this->data->GetItem(i) != NULL) {
delete this->data->GetItem(i);
}
}
delete this->data;
}
/////////////////////////////////////////////////////////////
// retrived a entry of the hashtable
// @param: the name of the assiciated object
// @return: Template type entry associated with the name
//
template <class T> T* CHashtable<T>::get(char* name) {
CHashtableEntry<T> *tmp1;
for(unsigned int i = 0; i < this->data->countItems(); i++) {
if(this->data->GetItem(i) != NULL) {
tmp1 = (CHashtableEntry<T>*)this->data->GetItem(i);
if(_stricmp(tmp1->name, name) == NULL) {
return (T*)((CHashtableEntry*)this->data->GetItem(i))->object;
}
}
}
return NULL;
}
/////////////////////////////////////////////////////////////
// adds a new element to the Hashtable
// @param: name of the object, is duplicated afterwards
// @param: pointer to the object to insert, should be
// allocated on the heap
//
template <class T> void CHashtable<T>::insert(char* name, T* obj) {
this->data->AddItem(new CHashtableEntry<T>(obj, strdup(name)));
}
/////////////////////////////////////////////////////////////
// removes and deletes a specified item from the
// hashtable
// @param: name of the element
//
template <class T> void CHashtable<T>::remove(char* name) {
for(unsigned int i = 0; i < this->data->countItems(); i++) {
if(this->data->GetItem(i) != NULL) {
if(_stricmp(((CHashtableEntry*)this->data->GetItem(i))->name, name) == NULL) {
delete ((CHashtableEntry*)this->data->GetItem(i));
this->data->DeleteItem(i);
}
}
}
}
////////////////////////////////////////////////////////////////
// contructs a new Hashtable entry, should not be
// used by any other element then CHashtable and childs
// @param: object to insert
// @param: name to be duplicated and inserted
//
template <class T> CHashtableEntry<T>::CHashtableEntry(T* obj, char* name) {
this->object = obj;
this->name = _strdup(name);
}
////////////////////////////////////////////////////////////////
// this ctor is only for use in exceptional cases
// it should not be used regularly
//
template <class T> CHashtableEntry<T>::CHashtableEntry() {
this->name = NULL;
this->object = NULL;
}
////////////////////////////////////////////////////////////////
// destructs the element, and deletes any associated values
template <class T> CHashtableEntry<T>::~CHashtableEntry() {
if(this->name) {
delete this->name;
}
if(this->object) {
delete this->object;
}
}
/////////////////////////////////////////////////////////////////
// Create Virtual instance of CHashtable
// this must be done 'cause we are working inside
// a library.
template class CHashtable<char>;
template class CHashtable<_CObject>;
template class CHashtableEntry<char>;
template class CHashtableEntry<_CObject>;