home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / Apple Shared Library Manager / ASLM Examples / Example Tools / Sources / TArrayExample.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  4.9 KB  |  144 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TArrayExample.cp
  3.  
  4.     Contains:    This module shows an example use of the TArray and TMatchObjects classes
  5.  
  6.     Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10. #include "TInitSLM.h"            // the TInitSLM class and SLM include files
  11. #include "TArrayExample.h"
  12.  
  13. ///————————————————————————————————————————————————————————————————————————————————————
  14. ///    CONSTANTS
  15. ///————————————————————————————————————————————————————————————————————————————————————
  16.  
  17. const unsigned kTotalData = 10;        // number of TData objects we'll add to the array
  18.  
  19. ///————————————————————————————————————————————————————————————————————————————————————
  20. ///    PROTOTYPES
  21. ///————————————————————————————————————————————————————————————————————————————————————
  22.  
  23. static void build_data_array(TArray* dataArray );
  24. static void disp_data(TArray* dataArray );
  25. static void remove_data(TArray* dataArray );
  26.  
  27. /*————————————————————————————————————————————————————————————————————————————————————
  28.     main 
  29.     
  30.     This example creates an array of TData objects by using the TArray class. It first
  31.     create instances of TData, adding them to the array as they are created.  Once
  32.     the objects have been added, it goes thru the array and displays the data associated
  33.     with each element.  Next, it removes two elements from the array, one using an
  34.     explicit index and another by using a match object. It then display the array
  35.     a second time. Finally, it removes each remaining element from the array.
  36.     —————————————————————————————————————————————————————————————————————*/
  37.  
  38. main() 
  39. {    
  40.     TInitSLM initLibraryManager;                // initialize the shared library manager
  41.     
  42.     if( initLibraryManager.Failed() )        // If we failed, let's go home
  43.         return 1;
  44.  
  45.     TArray *dataarray = new TArray(kTotalData);// allocate our TArray to add our data to
  46.  
  47.     if (dataarray && !dataarray->IsValid())    // make sure the TArray was constructed properly
  48.     {
  49.         delete dataarray;
  50.         return 0;
  51.     }
  52.                                             
  53.     if( dataarray ) {                        // Here we are going to create an array of data
  54.  
  55.         build_data_array( dataarray );        // build an array of data
  56.         
  57.         if( ! dataarray->IsEmpty() ) {        // make sure the array is not empty
  58.             
  59.             cout << "Total data in the array = " << dataarray->Count() << endl;
  60.             
  61.             disp_data( dataarray );            // go ahead and display each element
  62.                                         
  63.             // now we are going to remove the 3rd element of the array
  64.             TData* thedata = (TData*)(*dataarray)[2];
  65.             dataarray->Remove( thedata );
  66.             cout << "\nRemoved 3rd element from the array, data = " << thedata->GetData()
  67.                 << endl;
  68.             delete thedata;
  69.             
  70.             // now we are going to remove the element containing a 7
  71.             cout << "\nAbout to remove element with data = 7\n" << endl;
  72.             TDataMatchObject match(7);
  73.             thedata = (TData*)dataarray->Remove(match);
  74.             cout << "\nRemoved element, data = " << thedata->GetData() << endl;
  75.             delete thedata;
  76.  
  77.             cout << "\nNumber of elements in the array = " << dataarray->Count() << endl;
  78.             
  79.             // let's see now what we has happend to third location of array
  80.             disp_data( dataarray );            // go ahead and display each element
  81.             remove_data( dataarray );        // go ahead and clean up    
  82.         }
  83.         else
  84.             cout << "Array is empty.\n" << endl;
  85.         delete dataarray;                    // we are done with the array
  86.     }
  87.     
  88.     return 0;
  89. }
  90.  
  91. ///————————————————————————————————————————————————————————————————————————————————————
  92. ///    build_data_array
  93. ///
  94. ///     build an array containing TData objects
  95. ///————————————————————————————————————————————————————————————————————————————————————
  96.  
  97. static void build_data_array(TArray* dataArray )
  98. {
  99.     cout << "\nBuilding the array of TData objects\n" << endl;
  100.     
  101.     for( short i=0; i< kTotalData; i++ ) {
  102.         TData *tdata = new TData(i);     // i will also be the value stored in the TData 
  103.         dataArray->Add( tdata );
  104.     }
  105. }
  106.  
  107. ///————————————————————————————————————————————————————————————————————————————————————
  108. ///    disp_data
  109. ///
  110. ///     index thru the array and display each element of the array
  111. ///————————————————————————————————————————————————————————————————————————————————————
  112.  
  113. static void disp_data(TArray* dataArray )
  114. {
  115.     size_t i = 0;
  116.     TData     *thedata;
  117.  
  118.     // let's index through our array, and display each TData object
  119.     
  120.     cout << "\nIndexing thru the array and displaying each element\n" << endl;
  121.  
  122.     while( thedata = (TData *)(*dataArray)[i] )
  123.         cout << "array[" << i++ << "] = " << thedata->GetData() << endl;                    
  124. }
  125.  
  126. ///————————————————————————————————————————————————————————————————————————————————————
  127. ///    remove_data
  128. ///
  129. ///     index thru the array and remove each TData object
  130. ///————————————————————————————————————————————————————————————————————————————————————
  131.  
  132. static void remove_data(TArray* dataArray )
  133. {
  134.     // We'll just keep on removing the first element of the array until there
  135.     // are none left. If we didn't have to process each element as it was removed
  136.     // we could have just called RemoveAll or DeleteAll.
  137.     TData     *thedata;
  138.     while( thedata = (TData *)(*dataArray)[0] ) {
  139.         dataArray->Remove( thedata );
  140.         cout << "Removed element, data = " << thedata->GetData() << endl;
  141.         delete thedata;
  142.     }
  143. }
  144.