00001
00002
00003
00004
00005
00013 #ifndef HARRAY_H
00014 #define HARRAY_H
00015
00016
00017
00025 template <class T>
00026 class HArray : public HObj,
00027 public std::vector<T>
00028 {
00029 typedef HObjNoCopy base_class;
00030 static StringPtr ClassName(void) { return "HArray<T>"; }
00031 public:
00032 typedef typename std::vector<T>::iterator HArrayIterator;
00033
00034
00035
00036
00037
00038
00039
00040
00041 public:
00042 HArray(uint32 uSize = 0)
00043 {
00044 if (uSize)
00045 SetSize(uSize);
00046 }
00047
00048 ~HArray(void)
00049 {
00050 if (!IsNotEmpty())
00051 {
00052 DEBUG_LOG("~HArray() => %u elements still in array\n", this->size());
00053 this->clear();
00054 }
00055 }
00056
00057
00058
00059 public:
00066 ErrCode
00067 Append(const T & tData)
00068 {
00069 ErrCode ec = HError::NoError();
00070
00071 try
00072 {
00073 (void) this->push_back(tData);
00074 }
00075 catch (const std::exception & e)
00076 {
00077 DEBUG_LOG("HArray::Append() => caught %s.\n", e.what());
00078 ec = HError::Set(ERR_STL_EXCEPTION, __FILE__, __LINE__);
00079 }
00080
00081 return ec;
00082 }
00089 bool
00090 Contains(const T & tData) const throw ()
00091 {
00092 return (std::find(this->begin(), this->end(), tData) != this->end());
00093 }
00100 uint32
00101 Find(const T & tData) const throw ()
00102 {
00103 #if defined(HArrayIterator)
00104 HArrayIterator it;
00105
00106 it = std::find(this->begin(), this->end(), tData);
00107 if (it != this->end())
00108 return distance(this->begin(), it);
00109 #else
00110 for (uint32 udx = 0; udx < GetSize(); ++udx)
00111 if (this->at(udx) == tData)
00112 return udx;
00113 #endif
00114 return Humble::BAD_INDEX;
00115 }
00123 ErrCode
00124 InsertAt(uint32 udx, const T & tData, uint32 uCount = 1)
00125 {
00126 ErrCode ec = HError::NoError();
00127
00128 if (uCount > 0 && validIndex(udx))
00129 {
00130 (void) this->insert(this->begin() + udx, uCount, tData);
00131 }
00132 else
00133 ec = HError::Set(ERR_BAD_PARAMETER, __FILE__, __LINE__);
00134
00135 return ec;
00136 }
00142 ErrCode
00143 RemoveAt(uint32 udx)
00144 {
00145 ErrCode ec = HError::NoError();
00146
00147 if (validIndex(udx))
00148 {
00149 (void) this->erase( this->begin() + udx );
00150 }
00151 else
00152 ec = HError::Set(ERR_BAD_PARAMETER, __FILE__, __LINE__);
00153
00154 return ec;
00155 }
00156
00157
00158
00159 public:
00165 T GetAt(uint32 udx) const throw ()
00166 { return this->at(udx); }
00172 ErrCode SetAll(const T & tData) throw ()
00173 {
00174 ErrCode ec = HError::NoError();
00175
00176 try
00177 {
00178 std::fill(this->begin(), this->end(), tData);
00179 }
00180 catch (const std::exception & e)
00181 {
00182 DEBUG_LOG("HArray::SetAll() => caught %s.\n", e.what());
00183 ec = HError::Set(ERR_STL_EXCEPTION, __FILE__, __LINE__);
00184 }
00185
00186 return ec;
00187 }
00193 T * GetPtr(uint32 udx = 0) const
00194 { return validIndex(udx) ? &this[udx] : NULL; }
00199 uint32 GetSize(void) const
00200 { return this->size(); }
00207 ErrCode SetAt(uint32 udx, const T & tData) throw ()
00208 {
00209 ErrCode ec = HError::NoError();
00210
00211 if (validIndex(udx))
00212 this[udx] = tData;
00213 else
00214 ec = HError::Set(ERR_BAD_PARAMETER, __FILE__, __LINE__);
00215
00216 return ec;
00217 }
00228 ErrCode SetSize(uint32 uSize)
00229 {
00230 ErrCode ec = HError::NoError();
00231
00232 DEBUG_LOG("HArray[%p]::SetSize() => %lu\n", this, uSize);
00233
00234 try
00235 {
00236 if (uSize > 0)
00237 this->resize(uSize);
00238 else if (uSize == 0 && IsNotEmpty())
00239 this->clear();
00240 }
00241 catch (const std::exception & e)
00242 {
00243 DEBUG_LOG("HArray::SetSize() => caught %s.\n", e.what());
00244 ec = HError::Set(ERR_STL_EXCEPTION, __FILE__, __LINE__);
00245 }
00246
00247 return ec;
00248 }
00249
00250
00251
00254 inline bool IsEmpty(void) const
00255 { return this->empty(); }
00258 inline bool IsNotEmpty(void) const
00259 { return !(this->empty()); }
00262 inline ErrCode RemoveAll(void)
00263 { return SetSize(0); }
00264 protected:
00268 inline bool validIndex(uint32 udx) const
00269 { return (udx < this->size()); }
00270 };
00271
00272 #endif // HARRAY_H
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296