00001
00032 #ifndef _HCOLLECTION_
00033 #define _HCOLLECTION_
00034
00035 #include "main.h"
00036
00037 template<class T>
00038 class CCollection {
00039
00040 public:
00041
00042 CCollection();
00043 virtual ~CCollection();
00044
00045 void add(T* item);
00046 void add(T** item, int count);
00047 bool remove(int index);
00048 virtual void clean();
00049 void resize();
00050 int getLastInsertID(){ return m_size - 1; };
00051
00052 void lock(int position) {
00053 m_lockAt = position;
00054 }
00055
00056 int getLockPos() {
00057 return m_lockAt;
00058 }
00059
00060 void unlock() {
00061 m_lockAt = -1;
00062 }
00063
00064 bool isLocked() const {
00065 return m_lockAt >= 0 ? true : false;
00066 }
00067
00068 int size() const {
00069 return m_size;
00070 }
00071
00072 T* get(int index);
00073
00074 protected:
00075
00076 int m_capacity;
00077 int m_size;
00078 int m_lockAt;
00079
00080 T** m_data;
00081
00082 };
00083
00084 template<class T>
00085 CCollection<T>::CCollection() {
00086
00087 cout << "Create collection" << endl;
00088
00089 m_lockAt = -1;
00090 m_capacity = 32;
00091 m_size = 0;
00092
00093 m_data = new T*[m_capacity];
00094
00095 for (int i = 0; i < m_capacity; i++) {
00096 this->m_data[i] = NULL;
00097 }
00098
00099 }
00100
00101 template<class T>
00102 CCollection<T>::~CCollection() {
00103 unlock();
00104 clean();
00105
00106 if(m_data != NULL){
00107 delete [] m_data;
00108 m_data = NULL;
00109 }
00110
00111 }
00112
00113 template<class T>
00114 void CCollection<T>::clean() {
00115
00116 cout << "Clean Up Collection " << endl;
00117
00118 if (m_data != NULL) {
00119
00120 int _size = m_size;
00121 for (int i = 0; i < _size; i++) {
00122 if(m_data[i] != NULL){
00123 delete m_data[i];
00124 m_data[i] = NULL;
00125 }
00126 m_size--;
00127 }
00128 }
00129
00130 }
00131
00132 template<class T>
00133 void CCollection<T>::resize() {
00134
00135 cout << "TOGLE JA ZVETSUJI" << endl;
00136
00137 m_capacity *= 2;
00138 T** newData = new T*[m_capacity];
00139
00140 for (int i = 0; i < m_size; i++) {
00141 newData[i] = m_data[i];
00142 }
00143
00144 for (int i = m_size; i < m_capacity; i++) {
00145 newData[i] = NULL;
00146 }
00147
00148 delete[] m_data;
00149
00150 m_data = newData;
00151 }
00152
00153 template<class T>
00154 bool CCollection<T>::remove(int index) {
00155
00156 if (index >= m_size) {
00157 return false;
00158 }
00159
00160 if (m_data[index] == NULL) {
00161 return false;
00162 } else {
00163 delete m_data[index];
00164 }
00165
00166 m_data[index] = NULL;
00167
00168 return true;
00169 }
00170
00171 template<class T>
00172 void CCollection<T>::add(T* item) {
00173
00174 if (item == NULL) {
00175 return;
00176 }
00177
00178 if (m_size + 1 >= m_capacity) {
00179 resize();
00180 }
00181
00182 m_data[m_size++] = item;
00183
00184 }
00185
00186 template<class T>
00187 void CCollection<T>::add(T** item, int count) {
00188
00189 for (int i = 0; i < count; i++) {
00190 if (item[i] != NULL) {
00191 add(item[i]);
00192 }
00193 }
00194
00195 }
00196
00197 template<class T>
00198 T* CCollection<T>::get(int index) {
00199
00200 if (index >= m_size || index < 0 || m_data == NULL) {
00201 return NULL;
00202 }
00203
00204 if (m_data[index] == NULL) {
00205 return NULL;
00206 }
00207
00208 return m_data[index];
00209 }
00210
00211 #endif
00212