Humble Framework for SkyOS


Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

HMap.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 **  $Header: /SkyOS.root/HFramework/HMap.h 4     4/17/05 12:29p Lee Neuse $
00004 **
00005 ****************************************************************************/
00013 #ifndef HMAP_H
00014     #define HMAP_H
00015 
00024 template <class KEY, class T>
00025 class HMap :    public HObjNoCopy,
00026                 public std::map<KEY,T>
00027     {
00028     typedef HObjNoCopy  base_class;
00029     static StringPtr    ClassName(void) { return "HMap"; }
00030 
00031     typedef typename std::map<KEY, T>::iterator     HMapIterator;
00032     typedef typename std::map<KEY, T>::const_iterator   HMapConstIterator;
00033 /*  ----------------------------------------------------------------------
00034     CTOR / DTOR
00035     ----------------------------------------------------------------------  */
00036 public:
00037     HMap(void)
00038         { /* EMPTY CTOR */ }
00039     
00040     ~HMap(void) 
00041         { 
00042         if (!this->empty())
00043             {
00044             DEBUG_LOG("~HMap() => %u entries still in map\n", this->size());
00045             this->clear();
00046             }
00047         }
00048 /*  ----------------------------------------------------------------------
00049     METHODS
00050     ----------------------------------------------------------------------  */
00051 public:
00058     ErrCode
00059     Remove(const KEY & key)
00060         {
00061         ErrCode     ec = HError::NoError();
00062         
00063         try
00064             {
00065             HMapIterator    it;
00066             
00067             if ((it = this->find(key)) != this->end())
00068                 this->erase(it);
00069             else
00070                 ec = HError::SetSilent(ERR_NOT_FOUND, __FILE__, __LINE__);
00071             }
00072         catch (const std::exception & e)
00073             {
00074             DEBUG_LOG("HMap::Remove() => caught %s\n", e.what());
00075             if ((ec = ErrCode( HError::GetLastError() )) == NO_ERROR)
00076                 ec = HError::Set(ERR_STL_EXCEPTION, __FILE__, __LINE__);
00077             }
00078 
00079         return ec;
00080         }
00085     ErrCode
00086     RemoveAll(void)
00087         {
00088         (void) this->clear();
00089         return HError::NoError();
00090         }
00091 /*  ----------------------------------------------------------------------
00092     GETTERS & SETTERS
00093     ----------------------------------------------------------------------  */
00101     ErrCode             Get(const KEY & key, T & data) const
00102                             { 
00103                             ErrCode             ec = HError::NoError();
00104                             HMapConstIterator   it = this->find(key);
00105                                 
00106                             if (it != this->end())
00107                                 data = it->second;
00108                             else
00109                                 ec = HError::SetSilent(ERR_NOT_FOUND, __FILE__, __LINE__);
00110                                 
00111                             return ec;
00112                             }
00117     inline const T &    GetHead(void) const throw()
00118                             {
00119                             if (this->empty())
00120                                 HError::Throw(ERR_NOT_FOUND, __FILE__, __LINE__);
00121                             
00122                             return this->front();
00123                             }
00128     inline uint32       GetSize(void) const
00129                             { return this->size(); }
00136     ErrCode             Set(const KEY & key, const T & data)
00137                             { 
00138                             ErrCode     ec = HError::NoError();
00139 
00140                             try
00141                                 {
00142                                 HMapIterator    it = this->find(key);
00143                                 
00144                                 if (it == this->end())
00145                                     this->insert( std::make_pair(key, data) );  // add new value
00146                                 else
00147                                     it->second = data;  // update existing value
00148                                 }
00149                             catch (const std::exception & e)
00150                                 {
00151                                 DEBUG_LOG("HMap::Set() => caught %s\n", e.what());
00152                                 if ((ec = ErrCode( HError::GetLastError() )) == NO_ERROR)
00153                                     ec = HError::Set(ERR_STL_EXCEPTION, __FILE__, __LINE__);
00154                                 }
00155                                 
00156                             return ec;
00157                             }
00158 /*  ----------------------------------------------------------------------
00159     INLINES
00160     ----------------------------------------------------------------------  */
00161 public:
00167     inline bool     Contains(const KEY & key) const throw ()
00168                             { return (this->find(key) != this->end()); }
00173     inline bool     IsEmpty(void) const
00174                             { return this->empty(); }
00179     inline bool     IsNotEmpty(void) const
00180                             { return !this->empty(); }
00181     };
00182 
00183 #endif  // HMAP_H
00184 /****************************************************************************
00185 **
00186 **  $History: HMap.h $
00187  * 
00188  * *****************  Version 4  *****************
00189  * User: Lee Neuse    Date: 4/17/05    Time: 12:29p
00190  * Updated in $/SkyOS.root/HFramework
00191  * Development snapshot 050417
00192  * 
00193  * *****************  Version 3  *****************
00194  * User: Neusel       Date: 2/04/05    Time: 10:45a
00195  * Updated in $/SkyOS.root/pig/Humble
00196  * 
00197  * *****************  Version 2  *****************
00198  * User: Neusel       Date: 12/24/04   Time: 3:59p
00199  * Updated in $/SkyOS.root/pig/Humble
00200  * 
00201  * *****************  Version 1  *****************
00202  * User: Neusel       Date: 12/23/04   Time: 1:34p
00203  * Created in $/SkyOS.root/pig/Humble
00204  * Posted as HFramework-debug 20041223
00205 **
00206 **  -------------------------------------------------------------------------
00207 **
00208 **  End of HMap.h
00209 **
00210 ****************************************************************************/