home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-05-24 | 5.1 KB | 157 lines | [TEXT/CWIE] |
- //Copyright (c) 1997 Aidan Cully
- //All rights reserved
-
- #include <Dialogs.h>
- #include "CLCleanup.h"
-
- // TCleanup::RegisterDirtyAction()
- // uses:
- // Use this procedure to place a procedure in the procedure list which will be called
- // after two conditions are met:
- // 1: that an action is pending in the "Dirty" list (normally this occurs through
- // calls to "NotifyDirty", which must be called after "RegisterDirtyAction"
- // 2: that Clean be called. This can be called from anywhere, but will most likely
- // be called (depending on the variable you use for cleanup) every time through the
- // the event loop.
- // in:
- // UInt32 procID: a unique ID number which is forever bound to the second parameter
- // CleanupProc theProc: a procedure which takes a void* and a procID as parameters, and
- // performs some action or other based on these datas.
- // return value:
- // UInt8: an error code. Current possibilities are:
- // kSuccess == No Error
- // kDuplicateActionErr == the procID variable is already in use
- // kCleanupMemErr == out of memory
- UInt8 TCleanup::RegisterDirtyAction( UInt32 procID, CleanupProc theProc ) {
- TProcEl *theEl;
-
- //if nothing is in the procedure list as yet, then insert the first element.
- if( !mProcs.MoveFirst() ) {
- //create and initialize the element structure. We need to have an element structure
- //because of the *extremely* rudimentary way in which TList operates.
- theEl = new TProcEl;
- if( !theEl )
- return( kCleanupMemErr );
- theEl->mProcID = procID;
- theEl->mProc = theProc;
- if( !mProcs.AddNext( theEl ) )
- return( kCleanupMemErr );
- return( kSuccess );
- } else { //otherwise, we have to check that the procID variable is actually unique before
- //inserting.
- //the if block above called upon entry "procs.MoveFirst()," so we don't have to bother
- //here.
- UInt32 temp;
- do { //loop through every element in the list to find possibly duplicate procID's.
- mProcs.GetData( theEl );
- if( theEl->mProcID == procID ) //Found one?
- return( kDuplicateActionErr ); //then return the error code.
- } while( mProcs.MoveNext() );
- //create the new variables (in the TList, and in the ProcType*) checking for memory
- //allocation errors.
- theEl = new TProcEl;
- if( !theEl )
- return( kCleanupMemErr );
- theEl->mProcID = procID;
- theEl->mProc = theProc;
- if( !mProcs.AddNext( theEl ) )
- return( kCleanupMemErr );
- return( kSuccess );
- }
- }
-
- // TCleanup::NotifyDirty()
- // uses:
- // Use NotifyDirty to have a function called with certain data the next time Clean() is
- // called for this particular instance of TCleanup.
- // in:
- // UInt32 procID: unique ID number identifying which procedure to call
- // void *procData: data to be used locally by said procedure
- // return value:
- // UInt8: possible return values are at present:
- // kSuccess == everything's groovin.
- // kActionNotFoundErr == the procID specified is invalid.
- // kCleanupMemErr == out of memory.
- UInt8 TCleanup::NotifyDirty( UInt32 procID, void *procData ) {
- TDataEl *theEl;
- TProcEl *procEl;
- Boolean found = false;
-
- //Verify that a procedure IS identified by procID.
- if( !mProcs.MoveFirst() )
- return( kActionNotFoundErr );
- UInt32 temp;
- do {
- mProcs.GetData( procEl );
- if( procEl->mProcID == procID )
- found = true;
- } while( !found && mProcs.MoveNext() );
- if( !found )
- return( kActionNotFoundErr );
- //The procedure does exist, now add an element to the stack signifying that this procedure
- //must be called with this particular data.
- theEl = new TDataEl;
- if( !theEl )
- return( kCleanupMemErr );
- theEl->mProcID = procID;
- theEl->mData = procData;
- if( !mQueue.Enqueue( theEl ) )
- return( kCleanupMemErr );
- //Wow, everything went fine!
- return( kSuccess );
- }
-
- // TCleanup::Clean()
- // uses:
- // Use this procedure to call all other procedures that the TCleanup object has been told
- // to call (usually by use of the NotifyDirty() procedure).
- // in: none.
- // return value:
- // UInt8: the return value can be (at present) one of the following:
- // kSuccess == Far Out in a Happening Kind of Way (™)
- // kActionNotFound == one of the elements in the list had no appropriate action.
- // kActionLocalErr == the action procedure called had some kind of problem with it.
- UInt8 TCleanup::Clean() {
- TDataEl *theEl;
- TProcEl *theProc;
- UInt32 temp;
- Boolean found;
-
- while( mQueue.Dequeue( theEl ) ) {
- if( !mProcs.MoveFirst() )
- return( kActionNotFoundErr );
- found = false;
- do {
- mProcs.GetData( theProc );
- if( theProc->mProcID == theEl->mProcID )
- found = true;
- else if( !mProcs.MoveNext() ) {
- delete theEl;
- return( kActionNotFoundErr );
- }
- } while( !found );
- if( !theProc->mProc( theEl->mProcID, theEl->mData ) ) {
- delete theEl;
- return( kActionLocalErr );
- }
- delete theEl;
- }
- return( kSuccess );
- }
-
- // TCleanup::~TCleanup()
- // Destroys all variables associated with TCleanup. DOES NOT call the action procedures (at
- // this point, that could even be dangerous).
- TCleanup::~TCleanup() {
- TProcEl *theProc;
- TDataEl *theData;
-
- if( mProcs.MoveFirst() )
- do {
- mProcs.GetData( theProc );
- delete theProc;
- } while( mProcs.Remove() );
- while( mQueue.Dequeue( theData ) ) {
- delete theData;
- }
- }