D:/Papagan/490.2006/490.2006/korsan/Papagan/490.2006/korsan/Papagan/CEGUIHashMapTemplates.h

Go to the documentation of this file.
00001 #ifndef _CEGUI_HASHMAP_TEMPLATES_H_
00002 #define _CEGUI_HASHMAP_TEMPLATES_H_
00003 #include <hash_map>
00004 
00005 #if defined(_MSC_VER)
00006 #       pragma warning(push)
00007 #       pragma warning(disable : 4786)
00008 #endif
00009 
00010 using namespace std;
00011 using namespace stdext;
00012 
00013 namespace CEGUI
00014 {
00015         /*********************************************************************************************
00016         Method      : getEntryFromHashMap
00017         Description : The following template methods provide generic methods, such as adding,
00018                       searching and deleting entries to/from a HashMap. The HashMap is a hash_map
00019                       with attributes of type Z as an identifying key and attributes of class T as
00020                                   the object to be stored.
00021                       This template method is used to search an object in the HashMap, using the id
00022                                   as search key.
00023         Parameters  : NA
00024         Returnvalue : NA
00025         **********************************************************************************************/
00033         template<typename Z, class T>
00034         T getEntryFromHashMap (Z id, hash_map<Z, T>* hashMap)
00035         {
00036                 // Search the entry in the hashMap
00037                 if (hashMap)
00038                 {
00039                         hash_map<Z, T>::iterator hashMapIterator;
00040                         hashMapIterator = hashMap->find (id);
00041                         if (hashMapIterator != hashMap->end())
00042                         {
00043                                 // Found it
00044                                 return hashMapIterator->second;
00045                         }
00046                 }
00047 
00048                 return NULL;
00049         };
00050 
00051         /*********************************************************************************************
00052         Method      : addEntryToHashMap
00053         Description : This template method is used to add an entry to the hashMap. If an entry
00054                       with the same key is already present, the stored object is deleted!
00055         Parameters  : NA
00056         Returnvalue : NA
00057         **********************************************************************************************/
00058         template<typename Z, class T>
00059         void addEntryToHashMap (Z id, T object, hash_map<Z, T>* hashMap)
00060         {
00061                 if (hashMap)
00062                 {
00063                         T existingObject = getEntryFromHashMap (id, hashMap);
00064                         if (existingObject == NULL)
00065                         {
00066                                 // Not found; insert it
00067                                 hashMap->insert (make_pair(id, object));
00068                         }
00069                         else
00070                         {
00071                                 if (object != existingObject)
00072                                 {
00073                                         // The key exists, but the object differs
00074                                         hashMap->erase (id); // Remove the entry from the hash_map
00075                                         delete existingObject; // Delete the object
00076                                         hashMap->insert (make_pair(id, object));
00077                                 }
00078                         }
00079                 }
00080         };
00081 
00082         /*********************************************************************************************
00083         Method      : eraseEntryFromHashMap
00084         Description : This template method is used to remove one entry from the hashMap. The entry itself
00085                       will not be deleted!
00086         Parameters  : NA
00087         Returnvalue : NA
00088         **********************************************************************************************/
00089         template<typename Z, class T>
00090         void eraseEntryFromHashMap (Z id, hash_map<Z, T>* hashMap)
00091         {
00092                 // Remove one entry; it wil not be deleted
00093                 if (hashMap)
00094                 {
00095                         hash_map<Z, T>::iterator hashMapIterator;
00096                         hashMapIterator = hashMap->find (id);
00097                         if (hashMapIterator != hashMap->end())
00098                         {
00099                                 // Found it
00100                                 hashMap->erase (hashMapIterator);
00101                         }
00102                 }
00103         };
00104 
00105         /*********************************************************************************************
00106         Method      : deleteEntryFromHashMap
00107         Description : This template method is used to remove one entry from the hashMap and delete it.
00108         Parameters  : NA
00109         Returnvalue : NA
00110         **********************************************************************************************/
00111         template<typename Z, class T>
00112         void deleteEntryFromHashMap (Z id, hash_map<Z, T>* hashMap)
00113         {
00114                 // Remove one entry; it wil not be deleted
00115                 if (hashMap)
00116                 {
00117                         hash_map<Z, T>::iterator hashMapIterator;
00118                         hashMapIterator = hashMap->find (id);
00119                         if (hashMapIterator != hashMap->end())
00120                         {
00121                                 // Found it
00122                                 hashMap->erase (hashMapIterator);
00123                                 delete hashMapIterator->second;
00124                         }
00125                 }
00126         };
00127 
00128         /*********************************************************************************************
00129         Method      : deleteAllEntriesFromHashMap
00130         Description : This template method is used to delete all entries from the hashMap.
00131         Parameters  : NA
00132         Returnvalue : NA
00133         **********************************************************************************************/
00134         template<typename Z, class T>
00135         void deleteAllEntriesFromHashMap (hash_map<Z, T>* hashMap)
00136         {
00137                 // Delete all hashMap entries
00138                 if (hashMap)
00139                 {
00140                         hash_map<Z, T>::iterator hashMapIterator;
00141                         for (hashMapIterator = hashMap->begin(); hashMapIterator != hashMap->end(); hashMapIterator++)
00142                         {
00143                                 if (hashMapIterator->second)
00144                                         delete hashMapIterator->second;
00145                         }
00146                         hashMap->clear();
00147                 }
00148         };
00149 };
00150 #endif

Generated on Mon May 29 01:10:33 2006 for Papagan by  doxygen 1.4.6-NO