00001 #ifndef _COLLECTION_H_
00002 #define _COLLECTION_H_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifdef _WIN32
00023 #pragma once
00024 #endif
00025
00026
00027
00028
00029 template <class OBJ, class EXC> class FdoCollection : public FdoIDisposable
00030 {
00031 protected:
00032 static const FdoInt32 INIT_CAPACITY = 10;
00033
00034 FdoCollection()
00035 {
00036 m_capacity = INIT_CAPACITY;
00037 m_size = 0;
00038 m_list = new OBJ*[m_capacity];
00039 }
00040
00041 virtual ~FdoCollection()
00042 {
00043 for(FdoInt32 i = 0; i < m_size; i++)
00044 {
00045 FDO_SAFE_RELEASE(m_list[i]);
00046 }
00047
00048 delete[] m_list;
00049 }
00050
00051 public:
00052
00053
00054
00055
00056
00057
00058 virtual FdoInt32 GetCount() const
00059 {
00060 return m_size;
00061 }
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 virtual OBJ* GetItem(FdoInt32 index) const
00073 {
00074 if (index >= m_size || index < 0)
00075 throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)));
00076 #ifdef _DEBUG
00077 if (NULL != m_list[index] && m_list[index]->GetRefCount() <= 0)
00078 throw FdoException::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_1_MEMORY_DEALLOCATION_ERROR),
00079 L"FdoCollection::GetItem",
00080 L"FDO Object"));
00081 #endif
00082 return FDO_SAFE_ADDREF(m_list[index]);
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 virtual void SetItem(FdoInt32 index, OBJ* value)
00097 {
00098 if (index < m_size && index >= 0)
00099 {
00100 FDO_SAFE_RELEASE(m_list[index]);
00101 m_list[index] = FDO_SAFE_ADDREF(value);
00102 }
00103 else
00104 throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)));
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 virtual FdoInt32 Add(OBJ* value)
00117 {
00118 if (m_size == m_capacity)
00119 resize();
00120
00121 m_list[m_size] = FDO_SAFE_ADDREF(value);
00122 return m_size++;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 virtual void Insert(FdoInt32 index, OBJ* value)
00139 {
00140 FdoInt32 i;
00141 if (m_size == m_capacity)
00142 resize();
00143 if (index <= m_size && index >= 0)
00144 {
00145 for (i = m_size; i > index; i--)
00146 m_list[i] = m_list[i-1];
00147
00148 m_list[index] = FDO_SAFE_ADDREF(value);
00149 m_size++;
00150 }
00151 else
00152 throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)));
00153 }
00154
00155
00156
00157
00158
00159
00160
00161 virtual void Clear()
00162 {
00163 FdoInt32 i;
00164 for (i = 0; i < m_size; i++)
00165 {
00166 FDO_SAFE_RELEASE(m_list[i]);
00167 m_list[i] = NULL;
00168 }
00169
00170 m_size = 0;
00171 }
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 virtual void Remove(const OBJ* value)
00183 {
00184 FdoInt32 i;
00185 for (i = 0; i < m_size; i++)
00186 {
00187 if (m_list[i] == value)
00188 break;
00189 }
00190
00191 FDO_SAFE_RELEASE(m_list[i]);
00192
00193 if (i == m_size)
00194 throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_6_OBJECTNOTFOUND)));
00195
00196 while (i < m_size - 1)
00197 {
00198 m_list[i] = m_list[i+1];
00199 i++;
00200 }
00201
00202 m_list[--m_size] = NULL;
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 virtual void RemoveAt(FdoInt32 index)
00215 {
00216 if (index < m_size && index >= 0)
00217 {
00218 FdoInt32 i;
00219
00220 FDO_SAFE_RELEASE(m_list[index]);
00221
00222 for (i = index; i < m_size-1; i++)
00223 m_list[i] = m_list[i+1];
00224
00225 m_list[--m_size] = NULL;
00226 }
00227 else
00228 throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)));
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 virtual bool Contains(const OBJ* value) const
00241 {
00242 FdoInt32 i;
00243 bool ret = false;
00244 for (i = 0; i < m_size; i++) {
00245 if (m_list[i] == value)
00246 {
00247 ret = true;
00248 break;
00249 }
00250 }
00251 return ret;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 virtual FdoInt32 IndexOf(const OBJ* value) const
00264 {
00265 FdoInt32 i, index = -1;
00266 for (i = 0; i < m_size; i++) {
00267 if (m_list[i] == value)
00268 {
00269 index = i;
00270 break;
00271 }
00272 }
00273 return index;
00274 }
00275
00276 private:
00277
00278 void resize()
00279 {
00280 FdoInt32 i, old_capacity = m_capacity;
00281 m_capacity = (FdoInt32)(m_capacity*(1.4));
00282 OBJ** newArray = new OBJ*[m_capacity];
00283 for (i = 0; i < old_capacity; i++) {
00284 newArray[i] = m_list[i];
00285 }
00286 delete[] m_list;
00287 m_list = newArray;
00288 }
00289
00290 private:
00291 OBJ** m_list;
00292 FdoInt32 m_capacity;
00293 FdoInt32 m_size;
00294 };
00295 #endif
00296
00297