home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-11-19 | 5.1 KB | 198 lines | [TEXT/MPS ] |
- /*
- File: ExampleClass.cp
-
- Contains: TExampleClass implementation
-
- Copyright: © 1991-1994 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
-
- #ifndef __GLOBALNEW__
- #include <GlobalNew.h>
- #endif
- #ifndef __EXAMPLECLASS__
- #include "ExampleClass.h"
- #endif
- #ifndef __LIBRARYMANAGERUTILITIES__
- #include <LibraryManagerUtilities.h>
- #endif
- #ifndef __STRING__
- #include <string.h>
- #endif
-
- // a global variable that has a getter and setter
- static long gMyGlobal;
-
- // sure you can statically initialize something
- // initialization happens each time a library is loaded.
- // (you can even have global instances of exported classes)
- static long gInitedGlobal = 98765432;
-
- // yes you can init a static member, this one
- // counts the number of instances of TExampleClass
- long TExampleClass::gExampleClassCount = 0;
-
- /**********************************************************************
- ** PUBLIC Constructor/Destructor
- ***********************************************************************/
-
- TExampleClass::TExampleClass()
- {
- fName = NULL;
- GetLocalLibraryManager()->RegisterDynamicObject((TDynamic*)this);
- gExampleClassCount++;
- }
-
- TExampleClass::~TExampleClass()
- {
- delete fName;
- GetLocalLibraryManager()->UnregisterDynamicObject((TDynamic*)this);
- gExampleClassCount--;
- }
-
- /**********************************************************************
- ** PRIVATE DoThisAndThat/DoThat
- ***********************************************************************/
-
- void TExampleClass::DoThisAndThat()
- {
- Trace("TExampleClass::DoThisAndThat - gMyGlobal %d\n", gMyGlobal, this);
- Trace("TExampleClass::DoThisAndThat - gInitedGlobal %d\n", gInitedGlobal, this);
- DoThat();
- }
-
- void TExampleClass::DoThat()
- {
- if (fName)
- Trace("TExampleClass::DoThat - Name %s, Address 0x%X\n", fName, this);
- }
-
- /**********************************************************************
- ** PRIVATE SetObjectName/GetObjectName
- ***********************************************************************/
-
- void TExampleClass::SetObjectName(char *theName)
- {
- TMemoryPool* thePool = GetLocalPool(); // get our pool so we can...
-
- delete fName;
-
- fName = (char*)thePool->Allocate(strlen(theName) + 1);
-
- if (fName)
- strcpy(fName, theName);
-
- Trace("TExampleClass::SetObjectName - Name %s, Address 0x%X\n", fName, this);
- }
-
- char *TExampleClass::GetObjectName() const
- {
- return fName;
- }
-
- /**********************************************************************
- ** PRIVATE SetGlobalInt/GetGlobalInt
- ** These functions demonstrate the use of getters and setters
- ** for global variables owned by the library.
- ***********************************************************************/
-
- void TExampleClass::SetGlobalInt(long theValue)
- {
- gMyGlobal = theValue;
- Trace("TExampleClass::SetGlobalInt - gMyGlobal %d\n", gMyGlobal, this);
- }
-
- long TExampleClass::GetGlobalInt()
- {
- long theValue = gMyGlobal;
- Trace("TExampleClass::GetGlobalInt - gMyGlobal %d\n", gMyGlobal, this);
-
- return theValue;
- }
-
- /*******************************************************************************
- ** GetGlobalRef
- ** This non-virtual member function is dynamically exported in the
- ** FunctionSet along with the C and Pascal examples below.
- ********************************************************************************/
-
- void TExampleClass::GetGlobalRef(long*& theGlobal)
- {
- theGlobal = &gMyGlobal;
- }
-
- /*******************************************************************************
- ** Test
- ** These two versions of Test demonstrate dynamically exported
- ** static member functions (overloaded too!).
- ********************************************************************************/
-
- Boolean TExampleClass::Test(ulong test)
- {
- return gExampleClassCount == test;
- }
-
- Boolean TExampleClass::Test(char* test)
- {
- ulong testval = 0;
- while (*test >= '0' && *test <= '9')
- testval = testval*10 + ((*test++) - '0');
- return Test(testval);
- }
-
- /*******************************************************************************
- ** Some dynamically exported C and Pascal functions:
- ** Hello, Goodbye, HelloPascal, GoodbyePascal
- ** The two versions of Hello to demonstrate exporting overloaded functions
- ** HelloC is declared extern "C" in the header so its name won't be mangled
- **
- ** None of these do anything too useful... call Hello and get TickCount,
- ** and call Goodbye and get the elapsed time since Hello was called.
- ********************************************************************************/
-
- ulong helloticks;
-
- char* _CDECL kHelloRef(ulong& theHelloTicks)
- {
- helloticks = TickCount();
- theHelloTicks = helloticks;
- return "Hello";
- }
-
- char* _CDECL kHelloPtr(ulong* theHelloTicks)
- {
- helloticks = TickCount();
- *theHelloTicks = helloticks;
- return "Hello";
- }
-
- char* HelloC(ulong* theHelloTicks)
- {
- helloticks = TickCount();
- *theHelloTicks = helloticks;
- return "Hello";
- }
-
- // this could of course be implemented in Pascal
- // compiled in the normal way and linked with the ExampleLibrary
- pascal Ptr HelloPascal(ulong& theHelloTicks)
- {
- helloticks = TickCount();
- theHelloTicks = helloticks;
- return "Hello";
- }
-
- ulong Goodbye()
- {
- return TickCount() - helloticks;
- }
-
- pascal ulong GoodbyePascal()
- {
- return TickCount() - helloticks;
- }
-