home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / Example Tools / Sources / TArbitratorExample2.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  2.4 KB  |  71 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TArbitratorExample2.cp
  3.  
  4.     Contains:    This module show an example use of the TArbitrator class. It is the
  5.                 same as TArbitratorExample1.cp except it uses the GlobalArbitrator.
  6.  
  7.     Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  8.  
  9. */
  10.  
  11. #include "TInitSLM.h"            // the TInitSLM class and SLM include files
  12.  
  13. ///————————————————————————————————————————————————————————————————————————————————————
  14. ///    CONSTANTS
  15. ///————————————————————————————————————————————————————————————————————————————————————
  16.  
  17. // id of the object we register with the arbitrator
  18. #define kLinkObjectID    "slm:exam$link1"
  19.  
  20. /*————————————————————————————————————————————————————————————————————————————————————
  21.     main 
  22.     
  23.     This is a simple demonstration of how to use the TArbitrator class. Here we are
  24.     going to register our object with the global arbitrator.  This make it easier
  25.     for other clients to have access to our object since they automatically have
  26.     access to the global arbitrator. Off course they still need to know the object's ID.
  27. ————————————————————————————————————————————————————————————————————————————————————*/
  28.  
  29. main() 
  30. {    
  31.     TInitSLM initLibraryManager;                // initialize the shared library manager
  32.     
  33.     if( initLibraryManager.Failed() )        // If we failed, let's go home
  34.         return 1;
  35.         
  36.     TArbitrator *gArbitrator;
  37.     
  38.     if( gArbitrator = GetGlobalArbitrator() ) {    // get a pointer to Global Arbitrator
  39.         
  40.         cout << "Got a pointer to the global arbitrator" << endl;
  41.         
  42.         TLink *link;
  43.                 
  44.         if( link = new TLink ) {             // This is the object we are arbitrating
  45.             
  46.             cout <<"Allocating and registering an object…" << endl;
  47.             
  48.             // Register the object so other clients can look it up
  49.             OSErr err = gArbitrator->RegisterObject( kLinkObjectID, (void *)link );
  50.             if( err != kNoError)
  51.                 cout << "ERROR ==> {RegisterObject} = " << err << endl;
  52.             else {
  53.                 // see if we can find the object by ID
  54.                 TLink *object = (TLink *)gArbitrator->LookupObject( kLinkObjectID );
  55.                 
  56.                 if( object != link )        // if object returned != our object
  57.                     cout << "Something fishy, we got a wrong object" << endl;
  58.                 else
  59.                     cout << "LookupObject returned our Registered object" << endl;
  60.  
  61.                 // since we are the only one using this object we can go ahead
  62.                 // unregister and then delete the object
  63.                 if( object = (TLink *)gArbitrator->UnregisterObject( kLinkObjectID ) )
  64.                     cout << "Unregistered the object OK" << endl;
  65.             }
  66.             delete link;                    // we are done w/ the object
  67.         }
  68.     }
  69.     return 0;
  70. }
  71.