List.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _SUPPORT_LIST_H
00014 #define _SUPPORT_LIST_H
00015 
00025 #include <support/SupportDefs.h>
00026 #include <support/TypeFuncs.h>
00027 
00028 #if _SUPPORTS_NAMESPACE
00029 namespace palmos {
00030 namespace support {
00031 #endif
00032 
00037 // *************************************************************************
00039 
00047 class SAbstractList
00048 {
00049 public:
00050                                 SAbstractList(const size_t itemSize);
00051                                 SAbstractList(const SAbstractList& o);
00052     virtual                     ~SAbstractList(void);
00053     
00055             SAbstractList&      Duplicate(const SAbstractList& o);
00056     
00058             size_t              ItemSize(void) const;
00060             size_t              CountItems(void) const;
00062             bool                IsEmpty(void) const;
00063             
00064 struct _ListNode;
00065 class AbstractIterator;
00066     
00068             _ListNode*          Add(const void* newItem);
00070             _ListNode*          AddAt(const void* newItem, const AbstractIterator& i);
00073             _ListNode*          Splice(SAbstractList& subList);
00076             _ListNode*          SpliceAt(SAbstractList& subList, const AbstractIterator& i);
00077     
00079             AbstractIterator    Remove(AbstractIterator& i);
00080     
00082             AbstractIterator    Begin(void) const;
00084             AbstractIterator    End(void) const;
00085     
00087             void                MakeEmpty(void);
00088     
00089 protected:
00090     virtual void                PerformConstruct(void* base, size_t count) const = 0;
00091     virtual void                PerformCopy(void* to, const void* from, size_t count) const = 0;
00092     virtual void                PerformDestroy(void* base, size_t count) const = 0;
00093     virtual void                PerformAssign(void* to, const void* from, size_t count) const = 0;
00094     
00095 private:
00097             void                Insert(_ListNode* node, _ListNode* before = NULL);
00102             void                SpliceNodes(_ListNode* sublist_head, _ListNode* sublist_tail = NULL, _ListNode* before = NULL);
00104             void                Extract(_ListNode* node);
00105 
00106     virtual status_t            _ReservedAbstractList1();
00107     virtual status_t            _ReservedAbstractList2();
00108     virtual status_t            _ReservedAbstractList3();
00109     virtual status_t            _ReservedAbstractList4();
00110     virtual status_t            _ReservedAbstractList5();
00111     virtual status_t            _ReservedAbstractList6();
00112     virtual status_t            _ReservedAbstractList7();
00113     virtual status_t            _ReservedAbstractList8();
00114     virtual status_t            _ReservedAbstractList9();
00115     virtual status_t            _ReservedAbstractList10();
00116     
00117             size_t              m_itemSize;
00118             size_t              m_numItems;
00119 
00120             _ListNode*          m_end; // sentinel -- does not contain a valid list item
00121 };
00122 
00123 // *************************************************************************
00125 
00130 class SAbstractList::AbstractIterator 
00131 {
00132 public:
00133                                 AbstractIterator(SAbstractList const * domain);
00134                                 AbstractIterator(SAbstractList const * domain, _ListNode* node);
00135                                 AbstractIterator(const AbstractIterator& o);
00136     virtual                     ~AbstractIterator(void);
00137     
00139             _ListNode*          operator*(void) const;
00140 
00143             AbstractIterator&   operator++(void); // prefix
00146             AbstractIterator    operator++(int); // postfix
00149             AbstractIterator&   operator--(void); // prefix
00152             AbstractIterator    operator--(int); // postfix
00153 
00155             AbstractIterator&   operator=(const AbstractIterator& o);
00156 
00160             bool                operator==(const AbstractIterator& o) const;
00161             bool                operator!=(const AbstractIterator& o) const;
00163     
00165             const void*         Data(void) const;
00167             void*               EditData(void) const;
00168             
00170             SAbstractList const * Domain() const;
00171     
00172 private:
00173             _ListNode*          m_current;
00174             SAbstractList const * m_domain;
00175 };
00176 
00177 // *************************************************************************
00178 
00179 template<class TYPE>
00180 class _Iterator;
00181 
00183 
00188 template<class TYPE>
00189 class SList : public SAbstractList
00190 {
00191 public:
00192             typedef TYPE    value_type;
00193 
00194 public:
00195                                 SList(void);
00196                                 SList(const SList<TYPE>& o);
00197     virtual                     ~SList(void);
00198     
00200             SList<TYPE>&        operator=(const SList<TYPE>& o);
00201             
00202 typedef class _Iterator<TYPE> const iterator;
00203 typedef class _Iterator<TYPE> edit_iterator;
00204     
00205 //typedef typename const _Iterator<TYPE> iterator;
00206 //typedef typename _Iterator<TYPE> edit_iterator;
00208             typename SList::edit_iterator       IndexOf(const TYPE& item);
00209     
00211             typename SList::edit_iterator       AddItem(const TYPE& item);
00213             typename SList::edit_iterator       AddItemAt(const TYPE& item, const edit_iterator& i);
00214 
00217             typename SList::edit_iterator       SpliceList(SList<TYPE> &sublist);
00221             typename SList::edit_iterator       SpliceListAt(SList<TYPE> &sublist, const iterator& i);
00222     
00225             typename SList::edit_iterator       RemoveItemFor(const TYPE& item);
00228             typename SList::edit_iterator       RemoveAllItemsFor(const TYPE& item);
00229 
00232             typename SList::edit_iterator       RemoveItemAt(edit_iterator& i);
00233             
00235             typename SList::edit_iterator       Begin(void);
00240             typename SList::edit_iterator       End(void);
00242             typename SList::iterator            Begin(void) const;
00244             typename SList::iterator            End(void) const;
00245 
00247             void                MakeEmpty(void);
00248 
00249 protected:
00250     virtual void                PerformConstruct(void* base, size_t count) const;
00251     virtual void                PerformCopy(void* to, const void* from, size_t count) const;
00252     virtual void                PerformDestroy(void* base, size_t count) const;
00253     virtual void                PerformAssign(void* to, const void* from, size_t count) const;
00254 };
00255 
00257 
00262 template<class TYPE>
00263 class _Iterator : public SAbstractList::AbstractIterator
00264 {
00265 public:
00266                                 _Iterator(SAbstractList const * domain);
00267                                 _Iterator(SAbstractList const * domain, SAbstractList::_ListNode* node);
00268 
00269 typedef _Iterator<TYPE> self;
00270 
00272             TYPE&               operator*(void);
00273             const TYPE&         operator*(void) const;
00275             TYPE*               operator->(void);
00276             const TYPE*         operator->(void) const; // not super useful.
00277 
00280             self&               operator++(void); // prefix
00281             self                operator++(int); // postfix
00282             self&               operator--(void); // prefix
00283             self                operator--(int); // postfix
00285 };
00286 
00289 /* No user-serviceable parts below ****************************************/
00290 
00291 template<class TYPE>
00292 inline _Iterator<TYPE>::_Iterator(SAbstractList const * domain)
00293     : SAbstractList::AbstractIterator(domain)
00294 {
00295 }
00296 
00297 template<class TYPE>
00298 inline _Iterator<TYPE>::_Iterator(SAbstractList const * domain, SAbstractList::_ListNode* node)
00299     : SAbstractList::AbstractIterator(domain, node)
00300 {
00301 }
00302 
00303 template<class TYPE>
00304 inline TYPE& _Iterator<TYPE>::operator*(void)
00305 {
00306     return *static_cast<TYPE*>(EditData());
00307 }
00308 
00309 template<class TYPE>
00310 inline const TYPE& _Iterator<TYPE>::operator*(void) const
00311 {
00312     return *static_cast<TYPE*>(Data());
00313 }
00314 
00315 template<class TYPE>
00316 inline TYPE* _Iterator<TYPE>::operator->(void)
00317 {
00318     return static_cast<TYPE*>(EditData());
00319 }
00320 
00321 template<class TYPE>
00322 inline const TYPE* _Iterator<TYPE>::operator->(void) const
00323 {
00324     return static_cast<TYPE*>(Data());
00325 }
00326 
00327 template<class TYPE>
00328 inline _Iterator<TYPE>& _Iterator<TYPE>::operator++(void)
00329 {
00330     SAbstractList::AbstractIterator::operator++();
00331     
00332     return *this;
00333 }
00334 
00335 template<class TYPE>
00336 inline _Iterator<TYPE> _Iterator<TYPE>::operator++(int o)
00337 {
00338     return _Iterator<TYPE>(Domain(), *SAbstractList::AbstractIterator::operator++(o));
00339 }
00340 
00341 template<class TYPE>
00342 inline _Iterator<TYPE>& _Iterator<TYPE>::operator--(void)
00343 {
00344     SAbstractList::AbstractIterator::operator--();
00345     
00346     return *this;
00347 }
00348 
00349 template<class TYPE>
00350 inline _Iterator<TYPE> _Iterator<TYPE>::operator--(int o)
00351 {
00352     return _Iterator<TYPE>(Domain(), *SAbstractList::AbstractIterator::operator--(o));
00353 }
00354 
00355 template<class TYPE>
00356 inline SList<TYPE>::SList(void)
00357     : SAbstractList(sizeof (TYPE[2])/2)
00358 {
00359 }
00360 
00361 template<class TYPE>
00362 inline SList<TYPE>::SList(const SList<TYPE>& o)
00363     : SAbstractList(o.ItemSize())
00364 {
00365     Duplicate(o);
00366 }   
00367 
00368 template<class TYPE>
00369 inline SList<TYPE>::~SList(void)
00370 {
00371     MakeEmpty();
00372 }
00373 
00374 template<class TYPE>
00375 inline SList<TYPE>& SList<TYPE>::operator=(const SList<TYPE>& o)
00376 {
00377     Duplicate(o);
00378     
00379     return *this;
00380 }
00381 
00382 template<class TYPE>
00383 typename SList<TYPE>::edit_iterator SList<TYPE>::IndexOf(const TYPE& item)
00384 {
00385     typename SList<TYPE>::edit_iterator i = Begin();
00386     for (; i != End(); i++) {
00387         if (*i == item) return i;
00388     }
00389     
00390     return End();
00391 }
00392 
00393 template<class TYPE>
00394 inline typename SList<TYPE>::edit_iterator SList<TYPE>::AddItem(const TYPE& item)
00395 {
00396     return edit_iterator(this, Add(&item));
00397 }
00398 
00399 template<class TYPE>
00400 inline typename SList<TYPE>::edit_iterator SList<TYPE>::AddItemAt(const TYPE& item,
00401     const edit_iterator& i)
00402 {
00403     edit_iterator r = edit_iterator(this, AddAt(&item, i));
00404     return r;
00405 }
00406 
00407 template<class TYPE>
00408 inline typename SList<TYPE>::edit_iterator 
00409 SList<TYPE>::SpliceList(SList<TYPE> &sublist)
00410 {
00411     return edit_iterator(this, Splice(sublist));
00412 }
00413 
00414 template<class TYPE>
00415 inline typename SList<TYPE>::edit_iterator 
00416 SList<TYPE>::SpliceListAt(SList<TYPE> &sublist, const iterator &i)
00417 {
00418     return edit_iterator(this, SpliceAt(sublist, i));
00419 }
00420 
00421 template<class TYPE>
00422 typename SList<TYPE>::edit_iterator SList<TYPE>::RemoveItemFor(const TYPE& item)
00423 {
00424     typename SList<TYPE>::edit_iterator i = Begin();
00425     for (; i != End(); i++) {
00426         if (*i == item) return edit_iterator(this, *Remove(i));
00427     }
00428     
00429     // otherwise, return End()
00430     return edit_iterator(End());
00431 }
00432 
00433 template<class TYPE>
00434 typename SList<TYPE>::edit_iterator SList<TYPE>::RemoveAllItemsFor(const TYPE& item)
00435 {
00436     typename SList<TYPE>::edit_iterator i = Begin();
00437     for (; i != End();) {
00438         if (*i == item) i = edit_iterator(this, *Remove(i));
00439         else i++;
00440     }
00441     
00442     return i;
00443 }
00444 
00445 template<class TYPE>
00446 inline typename SList<TYPE>::edit_iterator SList<TYPE>::RemoveItemAt(edit_iterator& i)
00447 {
00448     return edit_iterator(this, *Remove(i));
00449 }
00450 
00451 template<class TYPE>
00452 inline typename SList<TYPE>::edit_iterator SList<TYPE>::Begin(void)
00453 {
00454     return edit_iterator(this, *SAbstractList::Begin());
00455 }
00456 
00457 template<class TYPE>
00458 inline typename SList<TYPE>::edit_iterator SList<TYPE>::End(void)
00459 {
00460     return edit_iterator(this, *SAbstractList::End());
00461 }
00462 
00463 template<class TYPE>
00464 inline typename SList<TYPE>::iterator SList<TYPE>::Begin(void) const
00465 {
00466     return iterator(this, *SAbstractList::Begin());
00467 }
00468 
00469 template<class TYPE>
00470 inline typename SList<TYPE>::iterator SList<TYPE>::End(void) const
00471 {
00472     return iterator(this, *SAbstractList::End());
00473 }
00474 
00475 template<class TYPE> inline
00476 void SList<TYPE>::MakeEmpty(void)
00477 {
00478     SAbstractList::MakeEmpty();
00479 }
00480 
00481 template<class TYPE>
00482 void SList<TYPE>::PerformConstruct(void* base, size_t count) const
00483 {
00484     BConstruct(static_cast<TYPE*>(base), count);
00485 }
00486 
00487 template<class TYPE>
00488 void SList<TYPE>::PerformCopy(void* to, const void* from, size_t count) const
00489 {
00490     BCopy(static_cast<TYPE*>(to), static_cast<const TYPE*>(from), count);
00491 }
00492 
00493 template<class TYPE>
00494 void SList<TYPE>::PerformDestroy(void* base, size_t count) const
00495 {
00496     BDestroy(static_cast<TYPE*>(base), count);
00497 }
00498 
00499 template<class TYPE>
00500 void SList<TYPE>::PerformAssign(void* to, const void* from, size_t count) const
00501 {
00502     BAssign(static_cast<TYPE*>(to), static_cast<const TYPE*>(from), count);
00503 }
00504 
00505 struct SAbstractList::_ListNode {
00506     _ListNode(void);
00507     ~_ListNode(void);
00508 
00509     _ListNode* _next;
00510     _ListNode* _prev;
00511 
00512 // This was _data[0] but the ADS compiler dose not seem to like it.
00513 // Someone needs to verify that this has not changed the behavihor
00514 // of List XXX--jparks
00515     uint8_t _data[1];
00516 };
00517 
00518 #if _SUPPORTS_NAMESPACE
00519 } } // namespace palmos::support
00520 #endif
00521 
00522 #endif // #ifndef _SUPPORT_LIST_H