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 / ExampleLibrary / Sources / ExampleClass.cp next >
Encoding:
Text File  |  1996-11-19  |  5.1 KB  |  198 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ExampleClass.cp
  3.  
  4.     Contains:    TExampleClass implementation
  5.  
  6.     Copyright:    © 1991-1994 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10. #ifndef __EVENTS__
  11. #include <Events.h>
  12. #endif
  13.  
  14. #ifndef __GLOBALNEW__
  15. #include <GlobalNew.h>
  16. #endif
  17. #ifndef __EXAMPLECLASS__
  18. #include "ExampleClass.h"
  19. #endif
  20. #ifndef __LIBRARYMANAGERUTILITIES__
  21. #include <LibraryManagerUtilities.h>
  22. #endif
  23. #ifndef __STRING__
  24. #include <string.h>
  25. #endif
  26.  
  27. // a global variable that has a getter and setter
  28. static long gMyGlobal;    
  29.  
  30. // sure you can statically initialize something
  31. // initialization happens each time a library is loaded.
  32. // (you can even have global instances of exported classes)
  33. static long gInitedGlobal = 98765432;
  34.  
  35. // yes you can init a static member, this one
  36. // counts the number of instances of TExampleClass
  37. long TExampleClass::gExampleClassCount = 0;
  38.  
  39. /**********************************************************************
  40. ** PUBLIC Constructor/Destructor
  41. ***********************************************************************/
  42.  
  43. TExampleClass::TExampleClass()
  44. {
  45.     fName = NULL;
  46.     GetLocalLibraryManager()->RegisterDynamicObject((TDynamic*)this);
  47.     gExampleClassCount++;
  48. }
  49.  
  50. TExampleClass::~TExampleClass()
  51. {
  52.     delete fName;
  53.     GetLocalLibraryManager()->UnregisterDynamicObject((TDynamic*)this);
  54.     gExampleClassCount--;
  55. }
  56.  
  57. /**********************************************************************
  58. ** PRIVATE DoThisAndThat/DoThat
  59. ***********************************************************************/
  60.  
  61. void TExampleClass::DoThisAndThat()
  62. {
  63.     Trace("TExampleClass::DoThisAndThat - gMyGlobal %d\n", gMyGlobal, this);
  64.     Trace("TExampleClass::DoThisAndThat - gInitedGlobal %d\n", gInitedGlobal, this);
  65.     DoThat();
  66. }
  67.  
  68. void TExampleClass::DoThat()
  69. {
  70.     if (fName)
  71.         Trace("TExampleClass::DoThat - Name %s, Address 0x%X\n", fName, this);
  72. }
  73.  
  74. /**********************************************************************
  75. ** PRIVATE SetObjectName/GetObjectName
  76. ***********************************************************************/
  77.  
  78. void TExampleClass::SetObjectName(char *theName)
  79. {
  80.     TMemoryPool* thePool = GetLocalPool();        // get our pool so we can...
  81.     
  82.     delete fName;
  83.     
  84.     fName = (char*)thePool->Allocate(strlen(theName) + 1);
  85.     
  86.     if (fName)
  87.         strcpy(fName, theName);
  88.  
  89.     Trace("TExampleClass::SetObjectName - Name %s, Address 0x%X\n", fName, this);
  90. }
  91.  
  92. char *TExampleClass::GetObjectName() const
  93. {
  94.     return fName;
  95. }
  96.  
  97. /**********************************************************************
  98. ** PRIVATE SetGlobalInt/GetGlobalInt
  99. **         These functions demonstrate the use of getters and setters
  100. **      for global variables owned by the library.
  101. ***********************************************************************/
  102.  
  103. void TExampleClass::SetGlobalInt(long theValue)
  104. {
  105.     gMyGlobal = theValue;
  106.     Trace("TExampleClass::SetGlobalInt - gMyGlobal %d\n", gMyGlobal, this);
  107. }
  108.  
  109. long TExampleClass::GetGlobalInt()
  110. {
  111.     long theValue = gMyGlobal;
  112.     Trace("TExampleClass::GetGlobalInt - gMyGlobal %d\n", gMyGlobal, this);
  113.     
  114.     return theValue;
  115. }
  116.  
  117. /*******************************************************************************
  118. ** GetGlobalRef
  119. **         This non-virtual member function is dynamically exported in the 
  120. **        FunctionSet along with the C and Pascal examples below.
  121. ********************************************************************************/
  122.  
  123. void TExampleClass::GetGlobalRef(long*& theGlobal)
  124. {
  125.     theGlobal = &gMyGlobal;
  126. }
  127.  
  128. /*******************************************************************************
  129. ** Test
  130. **         These two versions of Test demonstrate dynamically exported
  131. **        static member functions (overloaded too!).
  132. ********************************************************************************/
  133.  
  134. Boolean TExampleClass::Test(ulong test)
  135. {
  136.     return gExampleClassCount == test;
  137. }
  138.  
  139. Boolean TExampleClass::Test(char* test)
  140. {
  141.     ulong testval = 0;
  142.     while (*test >= '0' && *test <= '9')
  143.         testval = testval*10 + ((*test++) - '0');
  144.     return Test(testval);
  145. }
  146.  
  147. /*******************************************************************************
  148. ** Some dynamically exported C and Pascal functions: 
  149. ** Hello, Goodbye, HelloPascal, GoodbyePascal
  150. ** The two versions of Hello to demonstrate exporting overloaded functions
  151. ** HelloC is declared extern "C" in the header so its name won't be mangled
  152. **
  153. ** None of these do anything too useful... call Hello and get TickCount,
  154. ** and call Goodbye and get the elapsed time since Hello was called.
  155. ********************************************************************************/
  156.  
  157. ulong helloticks;
  158.  
  159. char* _CDECL kHelloRef(ulong& theHelloTicks)
  160. {
  161.     helloticks = TickCount();
  162.     theHelloTicks = helloticks;
  163.     return "Hello";
  164. }
  165.  
  166. char* _CDECL kHelloPtr(ulong* theHelloTicks)
  167. {
  168.     helloticks = TickCount();
  169.     *theHelloTicks = helloticks;
  170.     return "Hello";
  171. }
  172.  
  173. char* HelloC(ulong* theHelloTicks)
  174. {
  175.     helloticks = TickCount();
  176.     *theHelloTicks = helloticks;
  177.     return "Hello";
  178. }
  179.  
  180. // this could of course be implemented in Pascal
  181. // compiled in the normal way and linked with the ExampleLibrary
  182. pascal Ptr HelloPascal(ulong& theHelloTicks)
  183. {
  184.     helloticks = TickCount();
  185.     theHelloTicks = helloticks;
  186.     return "Hello";
  187. }
  188.  
  189. ulong Goodbye()
  190. {
  191.     return TickCount() - helloticks;
  192. }
  193.  
  194. pascal ulong GoodbyePascal()
  195. {
  196.     return TickCount() - helloticks;
  197. }
  198.