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 return FDO_SAFE_ADDREF(m_list[index]);
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 virtual void SetItem(FdoInt32 index, OBJ* value)
00091 {
00092 if (index < m_size && index >= 0)
00093 {
00094 FDO_SAFE_RELEASE(m_list[index]);
00095 m_list[index] = FDO_SAFE_ADDREF(value);
00096 }
00097 else
00098 throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)));
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 virtual FdoInt32 Add(OBJ* value)
00111 {
00112 if (m_size == m_capacity)
00113 resize();
00114
00115 m_list[m_size] = FDO_SAFE_ADDREF(value);
00116 return m_size++;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 virtual void Insert(FdoInt32 index, OBJ* value)
00133 {
00134 FdoInt32 i;
00135 if (m_size == m_capacity)
00136 resize();
00137 if (index <= m_size && index >= 0)
00138 {
00139 for (i = m_size; i > index; i--)
00140 m_list[i] = m_list[i-1];
00141
00142 m_list[index] = FDO_SAFE_ADDREF(value);
00143 m_size++;
00144 }
00145 else
00146 throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)));
00147 }
00148
00149
00150
00151
00152
00153
00154
00155 virtual void Clear()
00156 {
00157 FdoInt32 i;
00158 for (i = 0; i < m_size; i++)
00159 {
00160 FDO_SAFE_RELEASE(m_list[i]);
00161 m_list[i] = NULL;
00162 }
00163
00164 m_size = 0;
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 virtual void Remove(const OBJ* value)
00177 {
00178 FdoInt32 i;
00179 for (i = 0; i < m_size; i++)
00180 {
00181 if (m_list[i] == value)
00182 break;
00183 }
00184
00185 FDO_SAFE_RELEASE(m_list[i]);
00186
00187 if (i == m_size)
00188 throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_6_OBJECTNOTFOUND)));
00189
00190 while (i < m_size - 1)
00191 {
00192 m_list[i] = m_list[i+1];
00193 i++;
00194 }
00195
00196 m_list[--m_size] = NULL;
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 virtual void RemoveAt(FdoInt32 index)
00209 {
00210 if (index < m_size && index >= 0)
00211 {
00212 FdoInt32 i;
00213
00214 FDO_SAFE_RELEASE(m_list[index]);
00215
00216 for (i = index; i < m_size-1; i++)
00217 m_list[i] = m_list[i+1];
00218
00219 m_list[--m_size] = NULL;
00220 }
00221 else
00222 throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)));
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 virtual bool Contains(const OBJ* value) const
00235 {
00236 FdoInt32 i;
00237 bool ret = false;
00238 for (i = 0; i < m_size; i++) {
00239 if (m_list[i] == value)
00240 {
00241 ret = true;
00242 break;
00243 }
00244 }
00245 return ret;
00246 }
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 virtual FdoInt32 IndexOf(const OBJ* value) const
00258 {
00259 FdoInt32 i, index = -1;
00260 for (i = 0; i < m_size; i++) {
00261 if (m_list[i] == value)
00262 {
00263 index = i;
00264 break;
00265 }
00266 }
00267 return index;
00268 }
00269
00270 private:
00271
00272 void resize()
00273 {
00274 FdoInt32 i, old_capacity = m_capacity;
00275 m_capacity = (FdoInt32)(m_capacity*(1.4));
00276 OBJ** newArray = new OBJ*[m_capacity];
00277 for (i = 0; i < old_capacity; i++) {
00278 newArray[i] = m_list[i];
00279 }
00280 delete[] m_list;
00281 m_list = newArray;
00282 }
00283
00284 private:
00285 OBJ** m_list;
00286 FdoInt32 m_capacity;
00287 FdoInt32 m_size;
00288 };
00289 #endif
00290
00291