1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #ifndef CSV_SWELIST_HXX 25 #define CSV_SWELIST_HXX 26 27 // USED SERVICES 28 // BASE CLASSES 29 // COMPONENTS 30 // PARAMETERS 31 #include <cosv/tpl/dyn.hxx> 32 33 34 namespace csv 35 { 36 37 38 template <class XX> 39 class SweListElement 40 { 41 public: 42 typedef SweListElement<XX> self; 43 SweListElement(const XX & in_aObj)44 SweListElement( 45 const XX & in_aObj ) 46 : aObj(in_aObj), pNext(0) {} 47 Obj() const48 const XX & Obj() const { return aObj; } Obj()49 XX & Obj() { return aObj; } Next() const50 self * Next() const { return pNext; } 51 SetNext(self * i_pNext)52 void SetNext( 53 self * i_pNext ) 54 { pNext = i_pNext; } 55 private: 56 XX aObj; 57 self * pNext; 58 }; 59 60 61 62 template <class XX> class SweListIterator; 63 template <class XX> class SweListCIterator; 64 65 66 template <class XX> 67 class SweList 68 { 69 public: 70 // TYPES 71 typedef SweList<XX> self; 72 typedef XX value_type; 73 typedef SweListIterator<XX> iterator; 74 typedef SweListCIterator<XX> const_iterator; 75 private: 76 typedef SweListElement<XX> elem; 77 78 public: 79 // LIFECYCLE SweList()80 SweList() : pTop(0), pTail(0) {} ~SweList()81 ~SweList() { erase_all(); } 82 // OPERATIONS 83 void push_front( 84 const XX & i_aObj ); 85 void pop_front(); 86 void push_back( 87 const XX & i_aObj ); 88 void erase_all(); 89 90 // INQUIRY begin() const91 const_iterator begin() const { return pTop; } begin()92 iterator begin() { return pTop; } end() const93 const_iterator end() const { return (elem*)0; } end()94 iterator end() { return (elem*)0; } front() const95 const XX & front() const { return pTop->Obj(); } front()96 XX & front() { return pTop->Obj(); } back() const97 const XX & back() const { return pTail->Obj(); } back()98 XX & back() { return pTail->Obj(); } 99 empty() const100 bool empty() const { return pTop == 0; } 101 uintt size() const; 102 103 104 private: 105 // Forbiddden methods. 106 SweList( 107 const self & i_rList ); 108 self & operator=( 109 const self & i_rList ); 110 111 // DATA 112 DYN elem * pTop; 113 elem * pTail; 114 }; 115 116 template <class XX> 117 class SweList_dyn 118 { 119 public: 120 // TYPES 121 typedef SweList_dyn<XX> self; 122 typedef SweListElement< XX* > elem; 123 typedef SweListIterator< XX* > iterator; 124 125 // LIFECYCLE SweList_dyn()126 SweList_dyn() : pTop(0), pTail(0) {} ~SweList_dyn()127 ~SweList_dyn() { erase_all(); } 128 // OPERATIONS 129 void push_front( 130 XX * i_pObj ); 131 void push_back( 132 XX * i_pObj ); 133 void pop_front(); 134 void erase_all(); 135 136 // INQUIRY begin() const137 iterator begin() const { return pTop; } end() const138 iterator end() const { return (elem*)0; } front() const139 XX * front() const { return pTop->Obj(); } back() const140 XX * back() const { return pTail->Obj(); } 141 empty() const142 bool empty() const { return pTop == 0; } 143 uintt size() const; 144 145 private: 146 // Forbiddden methods. 147 SweList_dyn( 148 const self & i_rList ); 149 self & operator=( 150 const self & i_rList ); 151 152 DYN elem * pTop; 153 elem * pTail; 154 }; 155 156 157 158 159 template<class XX> 160 class SweListIterator 161 { 162 public: 163 typedef SweListIterator<XX> self; 164 typedef SweListElement<XX> elem; 165 SweListIterator(elem * i_pElem=0)166 SweListIterator( 167 elem * i_pElem = 0) 168 : pElem(i_pElem) { } 169 170 // OPERATORS operator *() const171 XX & operator*() const { return pElem->Obj(); } operator ++()172 self & operator++() { if (pElem != 0) pElem = pElem->Next(); 173 return *this; } operator ==(const self & i_rIter) const174 bool operator==( 175 const self & i_rIter ) const 176 { return pElem == i_rIter.pElem; } operator !=(const self & i_rIter) const177 bool operator!=( 178 const self & i_rIter ) const 179 { return pElem != i_rIter.pElem; } 180 private: 181 friend class SweListCIterator<XX>; 182 183 elem * pElem; 184 }; 185 186 template<class XX> 187 class SweListCIterator 188 { 189 public: 190 typedef SweListCIterator<XX> self; 191 typedef SweListElement<XX> elem; 192 SweListCIterator(const elem * i_pElem=0)193 SweListCIterator( 194 const elem * i_pElem = 0) 195 : pElem(i_pElem) { } 196 197 // OPERATORS operator =(const SweListIterator<XX> & i_rIter)198 self & operator=( 199 const SweListIterator<XX> & 200 i_rIter ) 201 { pElem = i_rIter.pElem; return *this; } 202 operator *() const203 const XX & operator*() const { return pElem->Obj(); } operator ++()204 self & operator++() { if (pElem != 0) pElem = pElem->Next(); 205 return *this; } operator ==(const self & i_rIter) const206 bool operator==( 207 const self & i_rIter ) const 208 { return pElem == i_rIter.pElem; } operator !=(const self & i_rIter) const209 bool operator!=( 210 const self & i_rIter ) const 211 { return pElem != i_rIter.pElem; } 212 private: 213 const elem * pElem; 214 }; 215 216 // Implementierung 217 218 template <class XX> 219 void push_front(const XX & i_aObj)220SweList<XX>::push_front( const XX & i_aObj ) 221 { 222 DYN elem * dpNew = new elem(i_aObj); 223 dpNew->SetNext(pTop); 224 pTop = dpNew; 225 if (pTail == 0) 226 pTail = pTop; 227 } 228 229 template <class XX> 230 void push_back(const XX & i_aObj)231SweList<XX>::push_back( const XX & i_aObj) 232 { 233 if (pTail != 0) 234 { 235 pTail->SetNext(new elem(i_aObj)); 236 pTail = pTail->Next(); 237 } 238 else 239 { 240 pTop = pTail = new elem(i_aObj); 241 } 242 } 243 244 template <class XX> 245 void pop_front()246SweList<XX>::pop_front() 247 { 248 if (pTop != 0) 249 { 250 elem * pDel = pTop; 251 pTop = pTop->Next(); 252 delete pDel; 253 if (pTop == 0) 254 pTail = 0; 255 } 256 } 257 258 template <class XX> 259 uintt size() const260SweList<XX>::size() const 261 { 262 uintt ret = 0; 263 for ( const_iterator iter = begin(); 264 iter != end(); 265 ++iter ) 266 { 267 ++ret; 268 } 269 return ret; 270 } 271 272 273 template <class XX> 274 void erase_all()275SweList<XX>::erase_all() 276 { 277 for (pTail = pTop ; pTop != 0; pTail = pTop) 278 { 279 pTop = pTop->Next(); 280 delete pTail; 281 } 282 pTop = pTail = 0; 283 } 284 285 286 template <class XX> 287 void push_front(XX * i_pObj)288SweList_dyn<XX>::push_front( XX * i_pObj ) 289 { 290 DYN elem * dpNew = new elem(i_pObj); 291 dpNew->SetNext(pTop); 292 pTop = dpNew; 293 if (pTail == 0) 294 pTail = pTop; 295 } 296 297 template <class XX> 298 void push_back(XX * i_pObj)299SweList_dyn<XX>::push_back( XX * i_pObj ) 300 { 301 if (pTail != 0) 302 { 303 pTail->SetNext(new elem(i_pObj)); 304 pTail = pTail->Next(); 305 } 306 else 307 { 308 pTop = pTail = new elem(i_pObj); 309 } 310 } 311 312 template <class XX> 313 void pop_front()314SweList_dyn<XX>::pop_front() 315 { 316 if (pTop != 0) 317 { 318 elem * pDel = pTop; 319 pTop = pTop->Next(); 320 if (pDel->Obj() != 0) 321 Delete_dyn(pDel->Obj()); 322 delete pDel; 323 if (pTop == 0) 324 pTail = 0; 325 } 326 } 327 328 329 template <class XX> 330 void erase_all()331SweList_dyn<XX>::erase_all() 332 { 333 for (pTail = pTop ; pTop != 0; pTail = pTop) 334 { 335 pTop = pTop->Next(); 336 if (pTail->Obj() != 0) 337 { 338 delete pTail->Obj(); 339 } 340 delete pTail; 341 } 342 pTop = pTail = 0; 343 } 344 345 template <class XX> 346 uintt size() const347SweList_dyn<XX>::size() const 348 { 349 uintt ret = 0; 350 for ( iterator iter = begin(); 351 iter != end(); 352 ++iter ) 353 { 354 ++ret; 355 } 356 return ret; 357 } 358 359 360 } // namespace csv 361 362 363 #endif 364 365 366