00001
00002
00003
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
00035
00036 public:
00037 HMap(void)
00038 { }
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
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
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) );
00146 else
00147 it->second = data;
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
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
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210