#include "StHandleUnlocker.h" #include "CThrownResult.h" // =========================================================================== // ¥ StHandleUnlocker Class // =========================================================================== // Constructor Locks the Handle // Destructor restores the Handle's original locked/unlocked state StHandleUnlocker::StHandleUnlocker( Handle inHandle) { mHandle = nil; mWasLocked = false; Adopt(inHandle); } StHandleUnlocker::~StHandleUnlocker() { RestoreLockState(); } void StHandleUnlocker::RestoreLockState() { if (mWasLocked && (mHandle != nil)) { ::HLock(mHandle); } } void StHandleUnlocker::Adopt( // Take control over locking Handle inHandle) // and unlocking this Handle { RestoreLockState(); // Restore state of current Handle mHandle = inHandle; // Store new Handle mWasLocked = false; if (inHandle != nil) { // Lock new Handle SInt8 theState = ::HGetState(inHandle); CThrownOSErr err = ::MemError(); // Bit 7 of state is set if Handle is locked. We want to // know if the Handle is currently locked or unlocked. // If it is locked, we unlock it now and lock it the // destructor. If it is already un locked, we do nothing // here and nothing in the destructor. mWasLocked = (theState & 0x80) != 0; // Locked if bit 7 is set if (mWasLocked) { ::HUnlock(inHandle); } } } Handle // Relinquish control of Handle StHandleUnlocker::Release() { RestoreLockState(); Handle outHandle = mHandle; mHandle = nil; // We now have no Handle mWasLocked = false; return outHandle; }