home *** CD-ROM | disk | FTP | other *** search
- // ===========================================================================
- // File: UResourcePlus.h
- // Version: 1.0 - Feb 1, 1996
- // Author: Mike Shields (mshields@inconnect.com)
- //
- // Copyright ©1996 Mike Shields. All rights reserved.
- // ===========================================================================
- //
-
- #pragma once
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- template <class T>
- class TResource
- {
- public:
- TResource()
- : mResourceH(nil)
- {
- };
-
- TResource(ResType inResType, ResIDT inResID, Boolean inThrowFail = true,
- Boolean inCurrResOnly = false)
- : mResourceH(nil)
- {
- Load(inResType, inResID, inThrowFail, inCurrResOnly);
- };
-
- TResource(ResType inResType, ConstStr255Param inResName,
- Boolean inThrowFail = true, Boolean inCurrResOnly = false)
- : mResourceH(nil)
- {
- Load(inResType, inResName, inThrowFail, inCurrResOnly);
- };
-
- ~TResource()
- {
- if ( mResourceH != nil )
- {
- ::DisposeHandle((Handle)mResourceH);
- }
- };
-
- void Load(ResType inResType, ConstStr255Param inResName,
- Boolean inThrowFail = true, Boolean inCurrResOnly = false)
- {
- if ( inCurrResOnly )
- {
- mResourceH = (T)::Get1NamedResource(inResType, inResName);
- }
- else
- {
- mResourceH = (T)::GetNamedResource(inResType, inResName);
- }
-
- if ( inThrowFail )
- {
- ThrowIfResFail_(mResourceH);
- }
-
- ::HNoPurge((Handle)mResourceH);
- if ( inThrowFail )
- {
- ThrowIfResError_();
- }
-
- ::DetachResource((Handle)mResourceH);
- if ( inThrowFail )
- {
- ThrowIfResError_();
- }
- };
-
- void Load(ResType inResType, ResIDT inResID, Boolean inThrowFail = true,
- Boolean inCurrResOnly = false)
- {
- if ( inCurrResOnly )
- {
- mResourceH = (T)::Get1Resource(inResType, inResID);
- }
- else
- {
- mResourceH = (T)::GetResource(inResType, inResID);
- }
-
- if ( inThrowFail )
- {
- ThrowIfResFail_(mResourceH);
- }
-
- ::HNoPurge((Handle)mResourceH);
- if ( inThrowFail )
- {
- ThrowIfResError_();
- }
-
- ::DetachResource((Handle)mResourceH);
- if ( inThrowFail )
- {
- ThrowIfResError_();
- }
- };
-
- operator Handle() { return (Handle)mResourceH; }
- operator T() { return mResourceH; }
-
- private:
- T mResourceH;
- };
-