• MacTech Network:
  • Tech Support
  • |
  • MacForge.net
  • |
  • Apple News
  • |
  • Register Domains
  • |
  • SSL Certificates
  • |
  • iPod Deals
  • |
  • Mac Deals
  • |
  • Mac Book Shelf

MAC TECH

  • Home
  • Magazine
    • About MacTech in Print
    • Issue Table of Contents
    • Subscribe
    • Risk Free Sample
    • Back Issues
    • MacTech DVD
  • Archives
    • MacTech Print Archives
    • MacMod
    • MacTutor
    • FrameWorks
    • develop
  • Forums
  • News
    • MacTech News
    • MacTech Blog
    • MacTech Reviews and KoolTools
    • Whitepapers, Screencasts, Videos and Books
    • News Scanner
    • Rumors Scanner
    • Documentation Scanner
    • Submit News or PR
    • MacTech News List
  • Store
  • Apple Expo
    • by Category
    • by Company
    • by Product
  • Job Board
  • Editorial
    • Submit News or PR
    • Writer's Kit
    • Editorial Staff
    • Editorial Calendar
  • Advertising
    • Benefits of MacTech
    • Mechanicals and Submission
    • Dates and Deadlines
    • Submit Apple Expo Entry
  • User
    • Register for Ongoing Raffles
    • Register new user
    • Edit User Settings
    • Logout
  • Contact
    • Customer Service
    • Webmaster Feedback
    • Submit News or PR
    • Suggest an article
  • Connect Tools
    • MacTech Live Podcast
    • RSS Feeds
    • Twitter

ADVERTISEMENT

CHLockSaver

Michael D. Rossetti

Here's a great gift idea for that special person in your life: you! I'll bet that everyone out in that reading audience can use this terrific labor saving device right this very evening: automatically unlocking handles and resources! That's right-you'll never have to worry about balancing your HLocks with HUnlocks ever again! And if you read on you'll see how you can get two additional special gifts for no extra charge: Magic Object Lockers and Charmed Resource Unpurgers.

Using secret C++ technology [constructors and destructors] our scientists [me] have labored for many years [not!] to develop the following class:

class CHLockSaver
{
public:
	CHLockSaver(Handle aHandle)
		: fLockedHandle(aHandle)
		{	fOldState = HGetState(aHandle);
			HLock(aHandle); }
	~CHLockSaver()
		{ HSetState(fLockedHandle, fOldState); }	
protected:
	Handle				fLockedHandle;
	SignedByte			fOldState;
};

To lock a handle simply use a statement like:

CHLockSaver myLocker(myHandle);

(Note: CHLock is pronounced like schlock [so if you use this stuff you can justifiably call yourself a CHLockMeister].)

Like magic you handle gets locked [it's all in the constructor], your countertops shine like new, and…presto…when "myLocker" goes out of scope "myHandle" is restored magically [okay, it's all in the destructor] to its original state.

As promised, here's a couple of free classes which work just like the CHLockSaver. Here's one which will lock down a derivative of TObject:

class COLockSaver
{
public:
	COLockSaver(TObject* anObject)
		: fLockedObject(anObject)
		{ fOldState = anObject->Lock(TRUE); }
	~COLockSaver()
		{ fLockedObject->Lock(fOldState); }
	
protected:
	TObject*		fLockedObject;
	Boolean			fOldState;
};

Here's another class which makes a resource unpurgeable:

class CHPurgeSaver
{
public:
	CHPurgeSaver(Handle aResource)
		: fUnpurgedHandle(aResource)
		{	fOldState = HGetState(aResource);
			HNoPurge(aResource); }
	~CHPurgeSaver()
		{ HSetState(fUnpurgedHandle, fOldState); }
	
protected:
	Handle				fUnpurgedHandle;
	SignedByte			fOldState;
};

Caveat Emptor: CHLockSaver and CHPurgeSaver completely restore the handle's state to its original settings-any state changes which you make before CHLockSaver and CHPurgeSaver go out of scope will be lost.

 
MacTech Only Search:
Community Search:

 
 
 

 
 
 
 
 
  • SPREAD THE WORD:
  • Slashdot
  • Digg
  • Del.icio.us
  • Reddit
  • Newsvine
  • Generate a short URL for this page:



MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797
MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.
All contents are Copyright 1984-2010 by Xplain Corporation. All rights reserved. Theme designed by Icreon.
 
Nov. 20: Take Control of Syncing Data in Sow Leopard' released
Nov. 19: Cocktail 4.5 (Leopard Edition) released
Nov. 19: macProVideo offers new Cubase tutorials
Nov. 18: S Stardom anounces Safe Capsule, a companion piece for Apple's
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live