home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Frameworks / MacZoop 1.6.5 / Basic Classes / Z Sources / ZObjectArray.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-18  |  6.8 KB  |  257 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            ObjectMacZapp        -- a standard Mac OOP application template
  5. *
  6. *
  7. *
  8. *            ZObjectArray.cpp    -- a container for objects (template class)
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *            © 1996, Graham Cox
  15. *
  16. *
  17. *
  18. *
  19. *************************************************************************************************/
  20.  
  21.  
  22. #include    "ZObjectArray.h"
  23. #include    "ZErrors.h"
  24. #include    "ZDefines.h"
  25.  
  26.  
  27. /*---------------------------------***  CONSTRUCTOR  ***---------------------------------*/
  28.  
  29. template <class T> ZObjectArray<T>::ZObjectArray()
  30.     : ZArray( sizeof(T*) )
  31. {
  32.     // we manipulate our handle via <o> rather than <a> so we can take advantage
  33.     // of direct register copies, etc for maximum performance, which is what this
  34.     // class is all about (it avoids BlockMove).
  35.     
  36.     o = (T***) a;
  37. }
  38.  
  39. /*----------------------------------***  GETOBJECT  ***----------------------------------*/
  40. /*    
  41. return the object at <index>
  42. -----------------------------------------------------------------------------------------*/
  43.  
  44. template <class T> T*    ZObjectArray<T>::GetObject( const long index )
  45. {
  46.     // gets the item at position <index> in the array. Index is one-based.
  47.     
  48.     if ((index < 1) || (index > numElements))
  49.         FailOSErr( kIndexOutOfRangeErr );
  50.         
  51.     return ((*o)[index - 1]);
  52. }
  53.  
  54.  
  55. /*--------------------------------***  DELETEOBJECT  ***---------------------------------*/
  56. /*    
  57. delete the object at <index> from the array. Does not dispose of the object itself
  58. -----------------------------------------------------------------------------------------*/
  59.  
  60. template <class T> void    ZObjectArray<T>::DeleteObject(T* item)
  61. {
  62.     long    index;
  63.     
  64.     index = FindIndex( item );
  65.     
  66.     if (index > 0)
  67.         DeleteItem ( index );
  68. }
  69.  
  70.  
  71. /*---------------------------------***  DISPOSEALL  ***----------------------------------*/
  72. /*    
  73. disposes of all of the objects in the array
  74. -----------------------------------------------------------------------------------------*/
  75.  
  76. template <class T> void    ZObjectArray<T>::DisposeAll()
  77. {
  78.     // calls ForgetObject for every object in the array, then forces the array empty. Since
  79.     // it is possible for ForgetObject to modify this array, we iterate in reverse order.
  80.     
  81.     long    i = numElements;
  82.     T*        p;
  83.     
  84.     while(i--)
  85.     {
  86.         p = (*o)[i];
  87.         
  88.         if (p)
  89.             ForgetObject(p);
  90.     }
  91.  
  92.     SetHandleSize( a, 0 );
  93.     numElements = 0;
  94. }
  95.  
  96.  
  97. /*----------------------------------***  DOFOREACH  ***----------------------------------*/
  98. /*    
  99. calls the grovelling function for every object in the array
  100. -----------------------------------------------------------------------------------------*/
  101.  
  102. template <class T> void    ZObjectArray<T>::DoForEach( IteratorProcPtr aProc, const long ref )
  103. {
  104.     // calls the grovelling function for every object in the array.
  105.     
  106.     if (aProc && (numElements > 0))
  107.     {
  108.         long    i = numElements;
  109.         
  110.         while(i)
  111.             (*aProc)((*o)[--i] , ref);
  112.     }
  113. }
  114.  
  115.  
  116. /*-----------------------------------***  CONTAINS  ***----------------------------------*/
  117. /*    
  118. returns TRUE if the object can be found in the array
  119. -----------------------------------------------------------------------------------------*/
  120.  
  121. template <class T> Boolean    ZObjectArray<T>::Contains(T* item)
  122. {
  123.     return (FindIndex( item ) > 0);
  124. }
  125.  
  126.  
  127. /*--------------------------------***  SETARRAYITEM  ***---------------------------------*/
  128. /*    
  129. overrides the inherited method to store objects in the list avoiding BlockMove().
  130. -----------------------------------------------------------------------------------------*/
  131.  
  132. template <class T> void    ZObjectArray<T>::SetArrayItem( void* item, const long index )
  133. {
  134.     if ((index < 1) || (index > numElements))
  135.         FailOSErr( kIndexOutOfRangeErr );
  136.         
  137.     (*o)[index - 1] = (T*) item;
  138. }
  139.  
  140.  
  141. /*--------------------------------***  GETARRAYITEM  ***---------------------------------*/
  142. /*    
  143. overrides the inherited method to obtain objects in the list avoiding BlockMove().
  144. -----------------------------------------------------------------------------------------*/
  145.  
  146. template <class T> void    ZObjectArray<T>::GetArrayItem( void* item, const long index )
  147. {
  148.     if ((index < 1) || (index > numElements))
  149.         FailOSErr( kIndexOutOfRangeErr );
  150.         
  151.     *(T**)item = ((*o)[index - 1]);
  152. }
  153.  
  154.  
  155.  
  156. /*----------------------------------***  FINDINDEX  ***----------------------------------*/
  157. /*    
  158. return the index of the object, or zero if not found.
  159. -----------------------------------------------------------------------------------------*/
  160.  
  161. template <class T> long    ZObjectArray<T>::FindIndex(T* item)
  162. {
  163.     if ( numElements < 1 )
  164.         return 0;
  165.         
  166.     long        i = 0;
  167.     Boolean        found = FALSE;
  168.     
  169.     do
  170.     {
  171.         if ((*o)[i] == item)
  172.         {
  173.             found = TRUE;
  174.             break;
  175.         }    
  176.     }
  177.     while( ++i < numElements );
  178.  
  179.     return ( found? i + 1 : 0 );
  180. }
  181.  
  182.  
  183. /*-------------------------------------***  SWAP  ***------------------------------------*/
  184. /*    
  185. swaps the items at indices a and b- can be used within sorts, etc
  186. -----------------------------------------------------------------------------------------*/
  187.  
  188. template <class T> void    ZObjectArray<T>::Swap( const long itema, const long itemb )
  189. {
  190.     register T* temp;
  191.     
  192.     if ((itema < 1) ||
  193.         (itema > numElements) ||
  194.         (itemb < 1) ||
  195.         (itemb > numElements))
  196.         FailOSErr( kIndexOutOfRangeErr );
  197.         
  198.     temp = GetObject( itema );
  199.     
  200.     (*o)[itema - 1] = (*o)[itemb - 1];
  201.     (*o)[itemb - 1] = temp;
  202. }
  203.  
  204.  
  205. /*-----------------------------------***  MOVEITEM  ***----------------------------------*/
  206. /*    
  207. moves the object at index <curIndex> to the index <newIndex>, displacing other items
  208. as needed. Completely overrides the ZArray method for better efficiency and to eliminate
  209. "pointer to pointer to object" dereferencing difficulties.
  210. -----------------------------------------------------------------------------------------*/
  211.  
  212. template <class T> void    ZObjectArray<T>::MoveItem( const long curIndex, const long newIndex )
  213. {
  214.     register T*    temp;
  215.     
  216.     if ((curIndex < 1)                 ||
  217.         (curIndex > numElements)     ||
  218.         (newIndex < 1)                 ||
  219.         (newIndex > numElements))
  220.         FailOSErr( kIndexOutOfRangeErr );
  221.     
  222.     // don't do anything if indices the same (not an error)
  223.     
  224.     if ( curIndex != newIndex )
  225.     {    
  226.         temp = GetObject( curIndex );
  227.         
  228.         DeleteItem( curIndex );
  229.         InsertItem( temp, newIndex );
  230.         
  231.         SendMessage( msgArrayItemMoved, (void*) newIndex );
  232.     }
  233. }
  234.  
  235. /*---------------------------------***  MOVETOFRONT  ***---------------------------------*/
  236. /*    
  237. moves the object at index <curIndex> to the front of the list (1)
  238. -----------------------------------------------------------------------------------------*/
  239.  
  240. template <class T> void    ZObjectArray<T>::MoveToFront( const long index )
  241. {
  242.     MoveItem( index, 1 );
  243. }
  244.  
  245. /*----------------------------------***  MOVETOBACK  ***---------------------------------*/
  246. /*    
  247. moves the object at index <curIndex> to the back of the list (n)
  248. -----------------------------------------------------------------------------------------*/
  249.  
  250. template <class T> void    ZObjectArray<T>::MoveToBack( const long index )
  251. {
  252.     MoveItem( index, CountItems());
  253. }
  254.  
  255.  
  256.  
  257.