From fb105bd338d843f557b23d04ebe7f7e5defd7d58 Mon Sep 17 00:00:00 2001 From: elasota Date: Sun, 29 Dec 2019 23:14:37 -0500 Subject: [PATCH] Handle refactor --- GpApp/About.cpp | 9 +- GpApp/AnimCursor.cpp | 24 ++- GpApp/DialogUtils.cpp | 202 +++++++++--------- GpApp/GliderStructs.h | 9 +- GpApp/House.cpp | 4 +- GpApp/HouseIO.cpp | 22 +- GpApp/HouseLegal.cpp | 4 +- GpApp/InterfaceInit.cpp | 2 +- GpApp/Map.cpp | 4 +- GpApp/Music.cpp | 2 +- GpApp/ObjectDraw2.cpp | 6 +- GpApp/Play.cpp | 4 +- GpApp/Render.cpp | 13 +- GpApp/Room.cpp | 10 +- GpApp/RoomGraphics.cpp | 6 +- GpApp/RoomInfo.cpp | 10 +- GpApp/Sound.cpp | 6 +- GpApp/StructuresInit.cpp | 2 +- GpApp/StructuresInit2.cpp | 2 +- GpApp/Utilities.cpp | 4 +- PortabilityLayer/MenuManager.cpp | 4 +- PortabilityLayer/PLControlDefinitions.h | 3 +- PortabilityLayer/PLCore.cpp | 26 +-- PortabilityLayer/PLCore.h | 16 +- PortabilityLayer/PLDialogs.cpp | 8 +- PortabilityLayer/PLDialogs.h | 12 +- PortabilityLayer/PLHandle.cpp | 9 + PortabilityLayer/PLHandle.h | 133 ++++++++++++ PortabilityLayer/PLMenus.cpp | 2 +- PortabilityLayer/PLQDOffscreen.cpp | 4 +- PortabilityLayer/PLQDOffscreen.h | 2 +- PortabilityLayer/PLQDraw.cpp | 4 +- PortabilityLayer/PLResourceManager.cpp | 6 +- PortabilityLayer/PLResources.cpp | 20 +- PortabilityLayer/PortabilityLayer.vcxproj | 2 + .../PortabilityLayer.vcxproj.filters | 6 + PortabilityLayer/ResourceManager.h | 3 +- 37 files changed, 365 insertions(+), 240 deletions(-) create mode 100644 PortabilityLayer/PLHandle.cpp create mode 100644 PortabilityLayer/PLHandle.h diff --git a/GpApp/About.cpp b/GpApp/About.cpp index a5b4158..a4ee828 100644 --- a/GpApp/About.cpp +++ b/GpApp/About.cpp @@ -5,6 +5,7 @@ //============================================================================ #include "PLKeyEncoding.h" +#include "PLControlDefinitions.h" #include "PLNumberFormatting.h" #include "PLResources.h" #include "PLSound.h" @@ -42,7 +43,7 @@ void DoAbout (void) Str255 longVersion; StringPtr messagePtr; VersRecHndl version; - Handle itemHandle; + ControlHandle itemHandle; short itemType, hit, wasResFile; ModalFilterUPP aboutFilterUPP; @@ -55,7 +56,7 @@ void DoAbout (void) // if (aboutDialog == nil) // RedAlert(kErrDialogDidntLoad); - version = (VersRecHndl)GetResource('vers', 1); + version = GetResource('vers', 1).StaticCast(); if (version != nil) { messagePtr = (**version).shortVersion + 1 + (**version).shortVersion[0]; @@ -109,7 +110,7 @@ static void HiLiteOkayButton (void) if (thePict != nil) { DrawPicture(thePict, &okayButtonBounds); - DisposeHandle((Handle)thePict); + thePict.Dispose(); okayButtIsHiLit = true; } @@ -131,7 +132,7 @@ static void UnHiLiteOkayButton (void) if (thePict != nil) { DrawPicture(thePict, &okayButtonBounds); - DisposeHandle((Handle)thePict); + thePict.Dispose(); okayButtIsHiLit = false; } diff --git a/GpApp/AnimCursor.cpp b/GpApp/AnimCursor.cpp index a29084d..b350a7d 100644 --- a/GpApp/AnimCursor.cpp +++ b/GpApp/AnimCursor.cpp @@ -25,15 +25,19 @@ typedef struct BEInt16_t resID; BEInt16_t reserved; } frame[1]; -} acurRec, *acurPtr, **acurHandle; +} acurRec; + +typedef THandle acurHandle; typedef struct { struct { - Handle cursorHdl; + THandle cursorHdl; } frame[1]; -} compiledAcurRec, *compiledAcurPtr, **compiledAcurHandle; +} compiledAcurRec; + +typedef THandle compiledAcurHandle; Boolean GetColorCursors (acurHandle, compiledAcurHandle); @@ -73,7 +77,7 @@ Boolean GetColorCursors (acurHandle ballCursH, compiledAcurHandle compiledBallCu } else // But, if the cursor loaded ok { // add it to our list or cursor handles - (*compiledBallCursH)->frame[i].cursorHdl = (Handle)cursHdl; + (*compiledBallCursH)->frame[i].cursorHdl = cursHdl; SetCCursor((CCrsrHandle)(*compiledBallCursH)->frame[i].cursorHdl); } } @@ -93,10 +97,10 @@ void InitAnimatedCursor (acurHandle ballCursH) useColor = thisMac.hasColor; if (ballCursH == nil) - ballCursH = reinterpret_cast(GetResource('acur', 128)); + ballCursH = GetResource('acur', 128).StaticCast(); if (ballCursH && ballCursH != animCursorH) { - compiledBallCursorH = (compiledAcurHandle)NewHandle(sizeof(compiledAcurRec) * (*ballCursH)->n); + compiledBallCursorH = NewHandle(sizeof(compiledAcurRec) * (*ballCursH)->n).StaticCast(); if (!compiledBallCursorH) RedAlert(kErrFailedResourceLoad); @@ -120,7 +124,7 @@ void InitAnimatedCursor (acurHandle ballCursH) void LoadCursors (void) { - InitAnimatedCursor((acurHandle)GetResource('acur', rAcurID)); + InitAnimatedCursor(GetResource('acur', rAcurID).StaticCast()); } //-------------------------------------------------------------- DisposCursors @@ -147,16 +151,16 @@ void DisposCursors (void) for (i = 0; i < j; i++) { if ((*compiledAnimCursorH)->frame[i].cursorHdl != nil) - DisposeHandle((Handle)(*compiledAnimCursorH)->frame[i].cursorHdl); + (*compiledAnimCursorH)->frame[i].cursorHdl.Dispose(); } } - DisposeHandle((Handle)compiledAnimCursorH); + compiledAnimCursorH.Dispose(); compiledAnimCursorH = nil; } if (animCursorH != nil) { - DisposeHandle((Handle)animCursorH); + animCursorH.Dispose(); animCursorH = nil; } } diff --git a/GpApp/DialogUtils.cpp b/GpApp/DialogUtils.cpp index dbcee20..32666bc 100644 --- a/GpApp/DialogUtils.cpp +++ b/GpApp/DialogUtils.cpp @@ -140,7 +140,7 @@ void GetDialogRect (Rect *bounds, short dialogID) DialogTHndl dlogHandle; Byte wasState; - dlogHandle = (DialogTHndl)GetResource('DLOG', dialogID); + dlogHandle = GetResource('DLOG', dialogID).StaticCast(); if (dlogHandle != nil) { *bounds = (**dlogHandle).boundsRect; @@ -331,15 +331,15 @@ void ZoomOutAlertRect (short alertID) void FlashDialogButton (DialogPtr theDialog, short itemNumber) { - Rect itemRect; - Handle itemHandle; - UInt32 dummyLong; - short itemType; + Rect itemRect; + ControlHandle itemHandle; + UInt32 dummyLong; + short itemType; GetDialogItem(theDialog, itemNumber, &itemType, &itemHandle, &itemRect); - HiliteControl((ControlHandle)itemHandle, kControlButtonPart); + HiliteControl(itemHandle, kControlButtonPart); Delay(8, &dummyLong); - HiliteControl((ControlHandle)itemHandle, 0); + HiliteControl(itemHandle, 0); } //-------------------------------------------------------------- DrawDefaultButton @@ -348,9 +348,9 @@ void FlashDialogButton (DialogPtr theDialog, short itemNumber) void DrawDefaultButton (DialogPtr theDialog) { - Rect itemRect; - Handle itemHandle; - short itemType; + Rect itemRect; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, 1, &itemType, &itemHandle, &itemRect); InsetRect(&itemRect, -4, -4); @@ -364,9 +364,9 @@ void DrawDefaultButton (DialogPtr theDialog) void GetDialogString (DialogPtr theDialog, short item, StringPtr theString) { - Rect itemRect; - Handle itemHandle; - short itemType; + Rect itemRect; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, item, &itemType, &itemHandle, &itemRect); GetDialogItemText(itemHandle, theString); @@ -377,9 +377,9 @@ void GetDialogString (DialogPtr theDialog, short item, StringPtr theString) void SetDialogString (DialogPtr theDialog, short item, const PLPasStr &theString) { - Rect itemRect; - Handle itemHandle; - short itemType; + Rect itemRect; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, item, &itemType, &itemHandle, &itemRect); SetDialogItemText(itemHandle, theString); @@ -390,10 +390,10 @@ void SetDialogString (DialogPtr theDialog, short item, const PLPasStr &theString short GetDialogStringLen (DialogPtr theDialog, short item) { - Rect itemRect; - Str255 theString; - Handle itemHandle; - short itemType; + Rect itemRect; + Str255 theString; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, item, &itemType, &itemHandle, &itemRect); GetDialogItemText(itemHandle, theString); @@ -406,12 +406,12 @@ short GetDialogStringLen (DialogPtr theDialog, short item) void GetDialogItemValue (DialogPtr theDialog, short item, short *theState) { - Rect itemRect; - Handle itemHandle; - short itemType; + Rect itemRect; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, item, &itemType, &itemHandle, &itemRect); - *theState = GetControlValue((ControlHandle)itemHandle); + *theState = GetControlValue(itemHandle); } //-------------------------------------------------------------- SetDialogItemValue @@ -420,12 +420,12 @@ void GetDialogItemValue (DialogPtr theDialog, short item, short *theState) void SetDialogItemValue (DialogPtr theDialog, short item, short theState) { - Rect itemRect; - Handle itemHandle; - short itemType; + Rect itemRect; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, item, &itemType, &itemHandle, &itemRect); - SetControlValue((ControlHandle)itemHandle, theState); + SetControlValue(itemHandle, theState); } //-------------------------------------------------------------- ToggleDialogItemValue @@ -433,17 +433,17 @@ void SetDialogItemValue (DialogPtr theDialog, short item, short theState) void ToggleDialogItemValue (DialogPtr theDialog, short item) { - Rect itemRect; - Handle itemHandle; - short itemType, theState; + Rect itemRect; + ControlHandle itemHandle; + short itemType, theState; GetDialogItem(theDialog, item, &itemType, &itemHandle, &itemRect); - theState = GetControlValue((ControlHandle)itemHandle); + theState = GetControlValue(itemHandle); if (theState == 0) theState = 1; else theState = 0; - SetControlValue((ControlHandle)itemHandle, theState); + SetControlValue(itemHandle, theState); } //-------------------------------------------------------------- SetDialogNumToStr @@ -452,10 +452,10 @@ void ToggleDialogItemValue (DialogPtr theDialog, short item) void SetDialogNumToStr (DialogPtr theDialog, short item, long theNumber) { - Str255 theString; - Rect itemRect; - Handle itemHandle; - short itemType; + Str255 theString; + Rect itemRect; + ControlHandle itemHandle; + short itemType; NumToString(theNumber, theString); GetDialogItem(theDialog, item, &itemType, &itemHandle, &itemRect); @@ -468,10 +468,10 @@ void SetDialogNumToStr (DialogPtr theDialog, short item, long theNumber) void GetDialogNumFromStr (DialogPtr theDialog, short item, long *theNumber) { - Str255 theString; - Rect itemRect; - Handle itemHandle; - short itemType; + Str255 theString; + Rect itemRect; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, item, &itemType, &itemHandle, &itemRect); GetDialogItemText(itemHandle, theString); @@ -483,8 +483,8 @@ void GetDialogNumFromStr (DialogPtr theDialog, short item, long *theNumber) void GetDialogItemRect (DialogPtr theDialog, short item, Rect *theRect) { - Handle itemHandle; - short itemType; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, item, &itemType, &itemHandle, theRect); } @@ -495,9 +495,9 @@ void GetDialogItemRect (DialogPtr theDialog, short item, Rect *theRect) void SetDialogItemRect (DialogPtr theDialog, short item, Rect *theRect) { - Rect oldRect; - Handle itemHandle; - short itemType; + Rect oldRect; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, item, &itemType, &itemHandle, &oldRect); OffsetRect(&oldRect, theRect->left - oldRect.left, theRect->top - oldRect.top); @@ -509,9 +509,9 @@ void SetDialogItemRect (DialogPtr theDialog, short item, Rect *theRect) void OffsetDialogItemRect (DialogPtr theDialog, short item, short h, short v) { - Rect oldRect; - Handle itemHandle; - short itemType; + Rect oldRect; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, item, &itemType, &itemHandle, &oldRect); OffsetRect(&oldRect, h, v); @@ -525,18 +525,18 @@ void OffsetDialogItemRect (DialogPtr theDialog, short item, short h, short v) void SelectFromRadioGroup (DialogPtr dial, short which, short first, short last) { - Rect iRect; - Handle iHandle; - short iType, i; + Rect iRect; + ControlHandle iHandle; + short iType, i; for (i = first; i <= last; i++) { GetDialogItem(dial, i, &iType, &iHandle, &iRect); - SetControlValue((ControlHandle)iHandle, (short)false); + SetControlValue(iHandle, (short)false); } GetDialogItem(dial, which, &iType, &iHandle, &iRect); - SetControlValue((ControlHandle)iHandle, (short)true); + SetControlValue(iHandle, (short)true); } //-------------------------------------------------------------- AddMenuToPopUp @@ -558,12 +558,12 @@ void AddMenuToPopUp (DialogPtr theDialog, short whichItem, MenuHandle theMenu) void GetPopUpMenuValue (DialogPtr theDialog, short whichItem, short *value) { - Rect iRect; - Handle iHandle; - short iType; + Rect iRect; + ControlHandle iHandle; + short iType; GetDialogItem(theDialog, whichItem, &iType, &iHandle, &iRect); - *value = GetControlValue((ControlHandle)iHandle); + *value = GetControlValue(iHandle); } //-------------------------------------------------------------- SetPopUpMenuValue @@ -571,12 +571,12 @@ void GetPopUpMenuValue (DialogPtr theDialog, short whichItem, short *value) void SetPopUpMenuValue (DialogPtr theDialog, short whichItem, short value) { - Rect iRect; - Handle iHandle; - short iType; + Rect iRect; + ControlHandle iHandle; + short iType; GetDialogItem(theDialog, whichItem, &iType, &iHandle, &iRect); - SetControlValue((ControlHandle)iHandle, value); + SetControlValue(iHandle, value); } //-------------------------------------------------------------- MyEnableControl @@ -584,12 +584,12 @@ void SetPopUpMenuValue (DialogPtr theDialog, short whichItem, short value) void MyEnableControl (DialogPtr theDialog, short whichItem) { - Rect iRect; - Handle iHandle; - short iType; + Rect iRect; + ControlHandle iHandle; + short iType; GetDialogItem(theDialog, whichItem, &iType, &iHandle, &iRect); - HiliteControl((ControlHandle)iHandle, kActive); + HiliteControl(iHandle, kActive); } //-------------------------------------------------------------- MyDisableControl @@ -597,12 +597,12 @@ void MyEnableControl (DialogPtr theDialog, short whichItem) void MyDisableControl (DialogPtr theDialog, short whichItem) { - Rect iRect; - Handle iHandle; - short iType; + Rect iRect; + ControlHandle iHandle; + short iType; GetDialogItem(theDialog, whichItem, &iType, &iHandle, &iRect); - HiliteControl((ControlHandle)iHandle, kInactive); + HiliteControl(iHandle, kInactive); } //-------------------------------------------------------------- DrawDialogUserText @@ -612,10 +612,10 @@ void MyDisableControl (DialogPtr theDialog, short whichItem) void DrawDialogUserText (DialogPtr dial, short item, StringPtr text, Boolean invert) { - Rect iRect; - Handle iHandle; - Str255 newString, stringCopy; - short iType, textLong, i, inset; + Rect iRect; + ControlHandle iHandle; + Str255 newString, stringCopy; + short iType, textLong, i, inset; TextFont(applFont); TextSize(9); @@ -651,10 +651,10 @@ void DrawDialogUserText (DialogPtr dial, short item, StringPtr text, Boolean inv void DrawDialogUserText2 (DialogPtr dial, short item, StringPtr text) { - Rect iRect; - Handle iHandle; - Str255 stringCopy; - short iType; + Rect iRect; + ControlHandle iHandle; + Str255 stringCopy; + short iType; TextFont(applFont); TextSize(9); @@ -673,10 +673,10 @@ void DrawDialogUserText2 (DialogPtr dial, short item, StringPtr text) void LoadDialogPICT (DialogPtr theDialog, short item, short theID) { - Rect iRect; - Handle iHandle; - PicHandle thePict; - short iType; + Rect iRect; + ControlHandle iHandle; + PicHandle thePict; + short iType; GetDialogItem(theDialog, item, &iType, &iHandle, &iRect); thePict = GetPicture(theID); @@ -689,9 +689,9 @@ void LoadDialogPICT (DialogPtr theDialog, short item, short theID) void FrameDialogItem (DialogPtr theDialog, short item) { - Rect itemRect; - Handle itemHandle; - short itemType; + Rect itemRect; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, item, &itemType, &itemHandle, &itemRect); FrameRect(&itemRect); @@ -702,10 +702,10 @@ void FrameDialogItem (DialogPtr theDialog, short item) void FrameDialogItemC (DialogPtr theDialog, short item, long color) { - RGBColor theRGBColor, wasColor; - Rect itemRect; - Handle itemHandle; - short itemType; + RGBColor theRGBColor, wasColor; + Rect itemRect; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, item, &itemType, &itemHandle, &itemRect); GetForeColor(&wasColor); @@ -720,9 +720,9 @@ void FrameDialogItemC (DialogPtr theDialog, short item, long color) void FrameOvalDialogItem (DialogPtr theDialog, short item) { - Rect itemRect; - Handle itemHandle; - short itemType; + Rect itemRect; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, item, &itemType, &itemHandle, &itemRect); FrameOval(&itemRect); @@ -734,9 +734,9 @@ void FrameOvalDialogItem (DialogPtr theDialog, short item) void BorderDialogItem (DialogPtr theDialog, short item, short sides) { - Rect itemRect; - Handle itemHandle; - short itemType; + Rect itemRect; + ControlHandle itemHandle; + short itemType; // 1 = left // 2 = top @@ -775,9 +775,9 @@ void BorderDialogItem (DialogPtr theDialog, short item, short sides) void ShadowDialogItem (DialogPtr theDialog, short item, short thickness) { - Rect itemRect; - Handle itemHandle; - short itemType; + Rect itemRect; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, item, &itemType, &itemHandle, &itemRect); PenSize(thickness, thickness); @@ -793,9 +793,9 @@ void ShadowDialogItem (DialogPtr theDialog, short item, short thickness) void EraseDialogItem (DialogPtr theDialog, short item) { - Rect itemRect; - Handle itemHandle; - short itemType; + Rect itemRect; + ControlHandle itemHandle; + short itemType; GetDialogItem(theDialog, item, &itemType, &itemHandle, &itemRect); EraseRect(&itemRect); diff --git a/GpApp/GliderStructs.h b/GpApp/GliderStructs.h index 7b6b132..63c9c63 100644 --- a/GpApp/GliderStructs.h +++ b/GpApp/GliderStructs.h @@ -199,7 +199,9 @@ typedef struct roomType rooms[1]; // 348 * nRooms static const size_t kBinaryDataSize = 866; -} houseType, *housePtr, **houseHand; // total = 866 + +} houseType, *housePtr; // total = 866 + + +typedef THandle houseHand; typedef struct { @@ -277,7 +279,10 @@ typedef struct Boolean top; Boolean right; Boolean bottom; -} boundsType, *boundsPtr, **boundsHand; +} boundsType, *boundsPtr; + +typedef THandle boundsHand; + typedef struct { diff --git a/GpApp/House.cpp b/GpApp/House.cpp index 37b697c..ebb455c 100644 --- a/GpApp/House.cpp +++ b/GpApp/House.cpp @@ -120,9 +120,9 @@ Boolean InitializeEmptyHouse (void) Str255 tempStr; if (thisHouse != nil) - DisposeHandle((Handle)thisHouse); + thisHouse.Dispose(); - thisHouse = (houseHand)NewHandle(sizeof(houseType)); + thisHouse = NewHandle(sizeof(houseType)).StaticCast(); if (thisHouse == nil) { diff --git a/GpApp/HouseIO.cpp b/GpApp/HouseIO.cpp index 414c194..948534c 100644 --- a/GpApp/HouseIO.cpp +++ b/GpApp/HouseIO.cpp @@ -51,19 +51,19 @@ extern Boolean phoneBitSet, bannerStarCountOn; void LoopMovie (void) { - Handle theLoop; - UserData theUserData; - short theCount; + THandle theLoop; + UserData theUserData; + short theCount; - theLoop = NewHandle(sizeof(long)); - (** (long **) theLoop) = 0; + theLoop = NewHandle(sizeof(long)).StaticCast(); + (**theLoop) = 0; theUserData = GetMovieUserData(theMovie); theCount = CountUserDataType(theUserData, 'LOOP'); while (theCount--) { RemoveUserData(theUserData, 'LOOP', 1); } - AddUserData(theUserData, theLoop, 'LOOP'); + AddUserData(theUserData, theLoop.StaticCast(), 'LOOP'); } //-------------------------------------------------------------- OpenHouseMovie @@ -119,11 +119,11 @@ void OpenHouseMovie (void) if (theErr != PLErrors::kNone) { YellowAlert(kYellowQTMovieNotLoaded, theErr); - DisposeHandle(spaceSaver); + spaceSaver.Dispose(); CloseHouseMovie(); return; } - DisposeHandle(spaceSaver); + spaceSaver.Dispose(); theErr = PrerollMovie(theMovie, 0, 0x000F0000); if (theErr != PLErrors::kNone) @@ -642,12 +642,12 @@ Boolean ReadHouse (void) #endif if (thisHouse != nil) - DisposeHandle((Handle)thisHouse); + thisHouse.Dispose(); // GP: Correct for padding const size_t alignmentPadding = sizeof(houseType) - sizeof(roomType) - houseType::kBinaryDataSize; - thisHouse = (houseHand)NewHandle(byteCount + alignmentPadding); + thisHouse = NewHandle(byteCount + alignmentPadding).StaticCast(); if (thisHouse == nil) { YellowAlert(kYellowNoMemory, 10); @@ -767,7 +767,7 @@ Boolean WriteHouse (Boolean checkIt) if (checkIt) CheckHouseForProblems(); - byteCount = GetHandleSize((Handle)thisHouse); + byteCount = GetHandleSize(thisHouse.StaticCast()); if (fileDirty) { diff --git a/GpApp/HouseLegal.cpp b/GpApp/HouseLegal.cpp index 402e040..65af944 100644 --- a/GpApp/HouseLegal.cpp +++ b/GpApp/HouseLegal.cpp @@ -616,7 +616,7 @@ void ValidateNumberOfRooms (void) char wasState; reportsRooms = (long)(*thisHouse)->nRooms; - countedRooms = (GetHandleSize((Handle)thisHouse) - + countedRooms = (GetHandleSize(thisHouse.StaticCast()) - sizeof(houseType)) / sizeof(roomType); if (reportsRooms != countedRooms) { @@ -737,7 +737,7 @@ void LopOffExtraRooms (void) { r = (*thisHouse)->nRooms - count; newSize = sizeof(houseType) + (sizeof(roomType) * (long)r); - if (SetHandleSize((Handle)thisHouse, newSize) != PLErrors::kNone) // resize house handle (shrink) + if (SetHandleSize(thisHouse.StaticCast(), newSize) != PLErrors::kNone) // resize house handle (shrink) { ForeColor(redColor); GetLocalizedString(16, message); diff --git a/GpApp/InterfaceInit.cpp b/GpApp/InterfaceInit.cpp index ad0ed24..7c49eb9 100644 --- a/GpApp/InterfaceInit.cpp +++ b/GpApp/InterfaceInit.cpp @@ -20,7 +20,7 @@ #define kDiagCursorID 131 -extern Rect **mirrorRects; +extern THandle mirrorRects; extern WindowPtr mapWindow, toolsWindow, linkWindow; extern WindowPtr menuWindow; extern Rect shieldRect, boardSrcRect, localRoomsDest[]; diff --git a/GpApp/Map.cpp b/GpApp/Map.cpp index b228769..cacc22d 100644 --- a/GpApp/Map.cpp +++ b/GpApp/Map.cpp @@ -165,14 +165,14 @@ void LoadGraphicPlus (short resID, Rect *theRect) thePicture = GetPicture(resID); if (thePicture == nil) { - thePicture = (PicHandle)GetResource('Date', resID); + thePicture = GetResource('Date', resID).StaticCast(); if (thePicture == nil) { return; } } DrawPicture(thePicture, theRect); - DisposeHandle((Handle)thePicture); + thePicture.Dispose(); } //-------------------------------------------------------------- RedrawMapContents diff --git a/GpApp/Music.cpp b/GpApp/Music.cpp index 782b6dc..f95848f 100644 --- a/GpApp/Music.cpp +++ b/GpApp/Music.cpp @@ -261,7 +261,7 @@ PLError_t LoadMusicSounds (void) return PLErrors::kOutOfMemory; BlockMove((Ptr)(static_cast(*theSound) + 20L), theMusicData[i], soundDataSize); - DisposeHandle(theSound); + theSound.Dispose(); } return (theErr); } diff --git a/GpApp/ObjectDraw2.cpp b/GpApp/ObjectDraw2.cpp index 4386888..83e6a7b 100644 --- a/GpApp/ObjectDraw2.cpp +++ b/GpApp/ObjectDraw2.cpp @@ -1158,7 +1158,7 @@ void DrawCalendar (Rect *theRect) QOffsetRect(&bounds, -bounds.left, -bounds.top); QOffsetRect(&bounds, theRect->left, theRect->top); DrawPicture(thePicture, &bounds); - DisposeHandle((Handle)thePicture); + thePicture.Dispose(); SetPort((GrafPtr)backSrcMap); TextFace(bold); @@ -1191,7 +1191,7 @@ void DrawBulletin (Rect *theRect) QOffsetRect(&bounds, -bounds.left, -bounds.top); QOffsetRect(&bounds, theRect->left, theRect->top); DrawPicture(thePicture, &bounds); - DisposeHandle((Handle)thePicture); + thePicture.Dispose(); SetGraphicsPort(wasCPort); } @@ -1246,7 +1246,7 @@ void DrawPictObject (short what, Rect *theRect) bounds = srcRects[what]; QOffsetRect(&bounds, theRect->left, theRect->top); DrawPicture(thePicture, &bounds); - DisposeHandle((Handle)thePicture); + thePicture.Dispose(); SetGraphicsPort(wasCPort); } diff --git a/GpApp/Play.cpp b/GpApp/Play.cpp index b20c90e..0ea2894 100644 --- a/GpApp/Play.cpp +++ b/GpApp/Play.cpp @@ -526,7 +526,7 @@ void PlayGame (void) QOffsetRect(&bounds, -bounds.left, -bounds.top); QOffsetRect(&bounds, hOffset, 0); DrawPicture(thePicture, &bounds); - DisposeHandle((Handle)thePicture); + thePicture.Dispose(); } #else // ShowMenuBarOld(); // TEMP @@ -576,7 +576,7 @@ void PlayGame (void) QOffsetRect(&bounds, -bounds.left, -bounds.top); QOffsetRect(&bounds, hOffset, 0); DrawPicture(thePicture, &bounds); - DisposeHandle((Handle)thePicture); + thePicture.Dispose(); SetGraphicsPort(wasCPort); } diff --git a/GpApp/Render.cpp b/GpApp/Render.cpp index 3cad704..34ad5f8 100644 --- a/GpApp/Render.cpp +++ b/GpApp/Render.cpp @@ -29,13 +29,12 @@ void RenderStars (void); void RenderBands (void); void RenderShreds (void); void CopyRectsQD (void); -void CopyRectsAssm (void); Rect work2MainRects[kMaxGarbageRects]; Rect back2WorkRects[kMaxGarbageRects]; Rect shieldRect; -Rect **mirrorRects; +THandle mirrorRects; Point shieldPt; long nextFrame; short numWork2Main, numBack2Work; @@ -151,7 +150,7 @@ void DrawReflection (gliderPtr thisGlider, Boolean oneOrTwo) SetPort((GrafPtr)workSrcMap); - long numMirrorRects = GetHandleSize(reinterpret_cast(mirrorRects)) / sizeof(Rect); + long numMirrorRects = GetHandleSize(mirrorRects.StaticCast()) / sizeof(Rect); for (long i = 0; i < numMirrorRects; i++) { @@ -743,16 +742,16 @@ void AddToMirrorRegion (Rect *theRect) { if (mirrorRects == nil) { - mirrorRects = reinterpret_cast(NewHandle(sizeof(Rect))); + mirrorRects = NewHandle(sizeof(Rect)).StaticCast(); if (mirrorRects != nil) **mirrorRects = *theRect; } else { - const long oldSize = GetHandleSize(reinterpret_cast(mirrorRects)); + const long oldSize = GetHandleSize(mirrorRects.StaticCast()); const long newSize = oldSize + sizeof(Rect); - if (SetHandleSize(reinterpret_cast(mirrorRects), newSize) == PLErrors::kNone) + if (SetHandleSize(mirrorRects.StaticCast(), newSize) == PLErrors::kNone) (*mirrorRects)[oldSize / sizeof(Rect)] = *theRect; } hasMirror = true; @@ -763,7 +762,7 @@ void AddToMirrorRegion (Rect *theRect) void ZeroMirrorRegion (void) { if (mirrorRects != nil) - DisposeHandle(reinterpret_cast(mirrorRects)); + mirrorRects.Dispose(); mirrorRects = nil; hasMirror = false; } diff --git a/GpApp/Room.cpp b/GpApp/Room.cpp index fa59fc6..9709c00 100644 --- a/GpApp/Room.cpp +++ b/GpApp/Room.cpp @@ -194,7 +194,7 @@ Boolean CreateNewRoom (short h, short v) if (availableRoom == -1) // found no available rooms { howMuch = sizeof(roomType); // add new room to end of house - theErr = PtrAndHand((Ptr)thisRoom, (Handle)thisHouse, howMuch); + theErr = PtrAndHand((Ptr)thisRoom, thisHouse.StaticCast(), howMuch); if (theErr != PLErrors::kNone) { YellowAlert(kYellowUnaccounted, theErr); @@ -260,7 +260,7 @@ void ReadyBackground (short theID, short *theTiles) thePicture = GetPicture(theID); if (thePicture == nil) { - thePicture = (PicHandle)GetResource('Date', theID); + thePicture = GetResource('Date', theID).StaticCast(); if (thePicture == nil) { YellowAlert(kYellowNoBackground, 0); @@ -271,7 +271,7 @@ void ReadyBackground (short theID, short *theTiles) dest = (*thePicture)->picFrame.ToRect(); QOffsetRect(&dest, -dest.left, -dest.top); DrawPicture(thePicture, &dest); - DisposeHandle((Handle)thePicture); + thePicture.Dispose(); QSetRect(&src, 0, 0, kTileWide, kTileHigh); QSetRect(&dest, 0, 0, kTileWide, kTileHigh); @@ -895,7 +895,7 @@ short GetOriginalBounding (short theID) boundsHand boundsRes; short boundCode; - boundsRes = (boundsHand)GetResource('bnds', theID); + boundsRes = GetResource('bnds', theID).StaticCast(); if (boundsRes == nil) { if (PictIDExists(theID)) @@ -913,7 +913,7 @@ short GetOriginalBounding (short theID) boundCode += 4; if ((*boundsRes)->bottom) boundCode += 8; - DisposeHandle((Handle)boundsRes); + boundsRes.Dispose(); } return (boundCode); diff --git a/GpApp/RoomGraphics.cpp b/GpApp/RoomGraphics.cpp index 34eeaff..46324c8 100644 --- a/GpApp/RoomGraphics.cpp +++ b/GpApp/RoomGraphics.cpp @@ -134,7 +134,7 @@ void LoadGraphicSpecial (short resID) thePicture = GetPicture(resID); if (thePicture == nil) { - thePicture = (PicHandle)GetResource('Date', resID); + thePicture = GetResource('Date', resID).StaticCast(); if (thePicture == nil) { thePicture = GetPicture(2000); @@ -146,8 +146,8 @@ void LoadGraphicSpecial (short resID) bounds = (*thePicture)->picFrame.ToRect(); OffsetRect(&bounds, -bounds.left, -bounds.top); DrawPicture(thePicture, &bounds); - - DisposeHandle((Handle)thePicture); + + thePicture.Dispose(); } //-------------------------------------------------------------- DrawRoomBackground diff --git a/GpApp/RoomInfo.cpp b/GpApp/RoomInfo.cpp index ed21284..d62ac4e 100644 --- a/GpApp/RoomInfo.cpp +++ b/GpApp/RoomInfo.cpp @@ -839,16 +839,16 @@ Boolean PictIDExists (short theID) thePicture = GetPicture(theID); if (thePicture == nil) { - thePicture = (PicHandle)GetResource('Date', theID); + thePicture = GetResource('Date', theID).StaticCast(); if (thePicture == nil) { foundIt = false; } else - DisposeHandle((Handle)thePicture); + thePicture.Dispose(); } else - DisposeHandle((Handle)thePicture); + thePicture.Dispose(); // foundIt = false; // numPicts = Count1Resources('PICT'); @@ -880,8 +880,8 @@ short GetFirstPICT (void) resHandle = Get1IndResource('PICT', 1); if (resHandle != nil) { - const PortabilityLayer::ResourceCompiledRef *resRef = PortabilityLayer::ResourceManager::GetInstance()->ResourceForHandle(reinterpret_cast(resHandle)); - DisposeHandle(resHandle); + const PortabilityLayer::ResourceCompiledRef *resRef = PortabilityLayer::ResourceManager::GetInstance()->ResourceForHandle(resHandle.MMBlock()); + resHandle.Dispose(); return resRef->m_resID; } else diff --git a/GpApp/Sound.cpp b/GpApp/Sound.cpp index e6737e9..e1108e1 100644 --- a/GpApp/Sound.cpp +++ b/GpApp/Sound.cpp @@ -228,13 +228,13 @@ PLError_t LoadTriggerSound (short soundID) theSoundData[kMaxSounds - 1] = NewPtr(soundDataSize); if (theSoundData[kMaxSounds - 1] == nil) { - DisposeHandle(theSound); + theSound.Dispose(); theErr = PLErrors::kOutOfMemory; } else { BlockMove((Ptr)((Byte*)(*theSound) + 20L), theSoundData[kMaxSounds - 1], soundDataSize); - DisposeHandle(theSound); + theSound.Dispose(); } } } @@ -275,7 +275,7 @@ PLError_t LoadBufferSounds (void) return (PLErrors::kOutOfMemory); BlockMove((Ptr)((Byte*)(*theSound) + 20L), theSoundData[i], soundDataSize); - DisposeHandle(theSound); + theSound.Dispose(); } theSoundData[kMaxSounds - 1] = nil; diff --git a/GpApp/StructuresInit.cpp b/GpApp/StructuresInit.cpp index 8613050..32de6d8 100644 --- a/GpApp/StructuresInit.cpp +++ b/GpApp/StructuresInit.cpp @@ -84,7 +84,7 @@ void InitScoreboardMap (void) QOffsetRect(&bounds, -bounds.left, -bounds.top); QOffsetRect(&bounds, hOffset, 0); DrawPicture(thePicture, &bounds); - DisposeHandle((Handle)thePicture); + thePicture.Dispose(); QSetRect(&badgeSrcRect, 0, 0, 32, 66); // 2144 pixels theErr = CreateOffScreenGWorld(&badgeSrcMap, &badgeSrcRect, kPreferredPixelFormat); diff --git a/GpApp/StructuresInit2.cpp b/GpApp/StructuresInit2.cpp index 1bc7c7d..2d034db 100644 --- a/GpApp/StructuresInit2.cpp +++ b/GpApp/StructuresInit2.cpp @@ -289,7 +289,7 @@ void CreatePointers (void) else { BlockMove(*tempHandle, demoData, kDemoLength); - DisposeHandle(tempHandle); + tempHandle.Dispose(); } #endif } diff --git a/GpApp/Utilities.cpp b/GpApp/Utilities.cpp index d72b0f5..b21ead2 100644 --- a/GpApp/Utilities.cpp +++ b/GpApp/Utilities.cpp @@ -288,7 +288,7 @@ void LoadGraphic (short resID) OffsetRect(&bounds, -bounds.left, -bounds.top); DrawPicture(thePicture, &bounds); - DisposeHandle((Handle)thePicture); + thePicture.Dispose(); } //-------------------------------------------------------------- LoadScaledGraphic @@ -304,7 +304,7 @@ void LoadScaledGraphic (short resID, Rect *theRect) if (thePicture == nil) RedAlert(kErrFailedGraphicLoad); DrawPicture(thePicture, theRect); - DisposeHandle((Handle)thePicture); + thePicture.Dispose(); } //-------------------------------------------------------------- LargeIconPlot diff --git a/PortabilityLayer/MenuManager.cpp b/PortabilityLayer/MenuManager.cpp index 4a1106c..f6578fb 100644 --- a/PortabilityLayer/MenuManager.cpp +++ b/PortabilityLayer/MenuManager.cpp @@ -533,10 +533,10 @@ namespace PortabilityLayer } if (icsHandle) - DisposeHandle(icsHandle); + icsHandle.Dispose(); if (ics8Handle) - DisposeHandle(ics8Handle); + ics8Handle.Dispose(); m_haveIcon = true; } diff --git a/PortabilityLayer/PLControlDefinitions.h b/PortabilityLayer/PLControlDefinitions.h index 1101733..0256a4a 100644 --- a/PortabilityLayer/PLControlDefinitions.h +++ b/PortabilityLayer/PLControlDefinitions.h @@ -8,8 +8,7 @@ struct Control { }; -typedef Control *ControlPtr; -typedef ControlPtr *ControlHandle; +typedef THandle ControlHandle; typedef void(*ControlActionProc)(ControlHandle control, short part); typedef ControlActionProc ControlActionUPP; diff --git a/PortabilityLayer/PLCore.cpp b/PortabilityLayer/PLCore.cpp index 9a326cc..9984f0b 100644 --- a/PortabilityLayer/PLCore.cpp +++ b/PortabilityLayer/PLCore.cpp @@ -222,7 +222,7 @@ Rect BERect::ToRect() const CursHandle GetCursor(int cursorID) { - return reinterpret_cast(GetResource('CURS', cursorID)); + return GetResource('CURS', cursorID).ReinterpretCast(); } CCrsrHandle GetCCursor(int cursorID) @@ -271,7 +271,7 @@ void DisposeCCursor(CCrsrHandle handle) { (*handle)->hwCursor->Destroy(); - PortabilityLayer::MemoryManager::GetInstance()->ReleaseHandle(reinterpret_cast(handle)); + PortabilityLayer::MemoryManager::GetInstance()->ReleaseHandle(handle.MMBlock()); } void Delay(int ticks, UInt32 *endTickCount) @@ -298,11 +298,7 @@ short Alert(int dialogID, void *unknown) Handle GetResource(int32_t resType, int id) { - PortabilityLayer::MMHandleBlock *block = PortabilityLayer::ResourceManager::GetInstance()->GetResource(PortabilityLayer::ResTypeID(resType), id); - if (!block) - return nullptr; - - return &block->m_contents; + return PortabilityLayer::ResourceManager::GetInstance()->GetResource(PortabilityLayer::ResTypeID(resType), id); } Handle GetResource(const char(&resTypeLiteral)[5], int id) @@ -637,10 +633,10 @@ void GetIndString(unsigned char *str, int stringsID, int fnameIndex) if (fnameIndex < 1) return; - PortabilityLayer::MMHandleBlock *istrRes = PortabilityLayer::ResourceManager::GetInstance()->GetResource('STR#', stringsID); - if (istrRes && istrRes->m_contents) + THandle istrRes = PortabilityLayer::ResourceManager::GetInstance()->GetResource('STR#', stringsID).StaticCast(); + if (istrRes && *istrRes) { - const uint8_t *contentsBytes = static_cast(istrRes->m_contents); + const uint8_t *contentsBytes = *istrRes; const BEUInt16_t *pArraySize = reinterpret_cast(contentsBytes); const uint16_t arraySize = *pArraySize; @@ -922,18 +918,12 @@ Handle NewHandle(Size size) return &hBlock->m_contents; } -void DisposeHandle(Handle handle) -{ - PortabilityLayer::MemoryManager::GetInstance()->ReleaseHandle(reinterpret_cast(handle)); -} - long GetHandleSize(Handle handle) { if (!handle) return 0; - PortabilityLayer::MMHandleBlock *block = reinterpret_cast(handle); - return static_cast(block->m_size); + return handle.MMBlock()->m_size; } PLError_t PtrAndHand(const void *data, Handle handle, Size size) @@ -945,7 +935,7 @@ PLError_t PtrAndHand(const void *data, Handle handle, Size size) PLError_t SetHandleSize(Handle hdl, Size newSize) { PortabilityLayer::MemoryManager *mm = PortabilityLayer::MemoryManager::GetInstance(); - if (!mm->ResizeHandle(reinterpret_cast(hdl), newSize)) + if (!mm->ResizeHandle(hdl.MMBlock(), newSize)) return PLErrors::kOutOfMemory; return PLErrors::kNone; diff --git a/PortabilityLayer/PLCore.h b/PortabilityLayer/PLCore.h index 451177b..a41568a 100644 --- a/PortabilityLayer/PLCore.h +++ b/PortabilityLayer/PLCore.h @@ -1,6 +1,4 @@ #pragma once -#ifndef __PL_CORE_H__ -#define __PL_CORE_H__ #include "DataTypes.h" #include "PLErrorCodes.h" @@ -9,6 +7,7 @@ #include "QDGraf.h" #include "ResTypeID.h" #include "VirtualDirectory.h" +#include "PLHandle.h" #ifdef _MSC_VER #pragma warning(error:4311) // Pointer truncation to int @@ -44,7 +43,6 @@ struct CGraf; struct Menu; typedef void *Ptr; -typedef Ptr *Handle; #define PL_DEAD(n) ((void)0) @@ -172,10 +170,10 @@ typedef Menu *MenuPtr; typedef CInfoPBRec *CInfoPBPtr; typedef VersionRecord *VersRecPtr; -typedef CursPtr *CursHandle; -typedef CCrsrPtr *CCrsrHandle; -typedef MenuPtr *MenuHandle; -typedef VersRecPtr *VersRecHndl; +typedef THandle CursHandle; +typedef THandle CCrsrHandle; +typedef THandle MenuHandle; +typedef THandle VersRecHndl; typedef WindowPtr WindowRef; // wtf? @@ -332,7 +330,6 @@ void ExitToShell(); void InvalWindowRect(WindowPtr window, const Rect *rect); Handle NewHandle(Size size); -void DisposeHandle(Handle handle); long GetHandleSize(Handle handle); PLError_t PtrAndHand(const void *data, Handle handle, Size size); // Appends data to the end of a handle @@ -362,6 +359,3 @@ void PL_NotYetImplemented(); void PL_NotYetImplemented_Minor(); void PL_NotYetImplemented_TODO(const char *category); void PL_Init(); - - -#endif diff --git a/PortabilityLayer/PLDialogs.cpp b/PortabilityLayer/PLDialogs.cpp index 6d2d374..b9c378f 100644 --- a/PortabilityLayer/PLDialogs.cpp +++ b/PortabilityLayer/PLDialogs.cpp @@ -23,22 +23,22 @@ CGrafPtr GetDialogPort(DialogPtr dialog) return nullptr; } -void GetDialogItem(DialogPtr dialog, int index, short *itemType, Handle *itemHandle, Rect *itemRect) +void GetDialogItem(DialogPtr dialog, int index, short *itemType, THandle *itemHandle, Rect *itemRect) { PL_NotYetImplemented(); } -void GetDialogItemText(Handle handle, StringPtr str) +void GetDialogItemText(THandle handle, StringPtr str) { PL_NotYetImplemented(); } -void SetDialogItem(DialogPtr dialog, int index, short itemType, Handle itemHandle, const Rect *itemRect) +void SetDialogItem(DialogPtr dialog, int index, short itemType, THandle itemHandle, const Rect *itemRect) { PL_NotYetImplemented(); } -void SetDialogItemText(Handle handle, const PLPasStr &str) +void SetDialogItemText(THandle handle, const PLPasStr &str) { PL_NotYetImplemented(); } diff --git a/PortabilityLayer/PLDialogs.h b/PortabilityLayer/PLDialogs.h index 0716cda..3c484cc 100644 --- a/PortabilityLayer/PLDialogs.h +++ b/PortabilityLayer/PLDialogs.h @@ -5,6 +5,7 @@ #include "PLCore.h" class PLPasStr; +struct Control; struct Dialog : public PortabilityLayer::QDPort { @@ -23,8 +24,7 @@ enum TEMode typedef Dialog *DialogPtr; -typedef DialogTemplate *DialogTPtr; -typedef DialogTPtr *DialogTHndl; +typedef THandle DialogTHndl; typedef Boolean(*ModalFilterUPP)(DialogPtr dial, EventRecord *event, short *item); @@ -34,11 +34,11 @@ WindowPtr GetDialogWindow(DialogPtr dialog); DialogPtr GetNewDialog(int resID, void *unknown, WindowPtr behind); CGrafPtr GetDialogPort(DialogPtr dialog); -void GetDialogItem(DialogPtr dialog, int index, short *itemType, Handle *itemHandle, Rect *itemRect); -void GetDialogItemText(Handle handle, StringPtr str); +void GetDialogItem(DialogPtr dialog, int index, short *itemType, THandle *itemHandle, Rect *itemRect); +void GetDialogItemText(THandle handle, StringPtr str); -void SetDialogItem(DialogPtr dialog, int index, short itemType, Handle itemHandle, const Rect *itemRect); -void SetDialogItemText(Handle handle, const PLPasStr &str); +void SetDialogItem(DialogPtr dialog, int index, short itemType, THandle itemHandle, const Rect *itemRect); +void SetDialogItemText(THandle handle, const PLPasStr &str); void SelectDialogItemText(DialogPtr dialog, int item, int firstSelChar, int lastSelCharExclusive); diff --git a/PortabilityLayer/PLHandle.cpp b/PortabilityLayer/PLHandle.cpp new file mode 100644 index 0000000..8d25b5e --- /dev/null +++ b/PortabilityLayer/PLHandle.cpp @@ -0,0 +1,9 @@ +#include "PLHandle.h" + +#include "MemoryManager.h" + +void THandleBase::Dispose() +{ + if (m_hdl) + PortabilityLayer::MemoryManager::GetInstance()->ReleaseHandle(m_hdl); +} diff --git a/PortabilityLayer/PLHandle.h b/PortabilityLayer/PLHandle.h new file mode 100644 index 0000000..b4db377 --- /dev/null +++ b/PortabilityLayer/PLHandle.h @@ -0,0 +1,133 @@ +#pragma once + +namespace PortabilityLayer +{ + struct MMHandleBlock; +} + +class THandleBase +{ +public: + explicit THandleBase(PortabilityLayer::MMHandleBlock *hdl); + THandleBase(const THandleBase &other); + + PortabilityLayer::MMHandleBlock *MMBlock() const; + + void Dispose(); + +protected: + PortabilityLayer::MMHandleBlock *m_hdl; +}; + +template +class THandle final : public THandleBase +{ +public: + THandle(); + THandle(T **hdl); + explicit THandle(PortabilityLayer::MMHandleBlock *hdl); + THandle(const THandle &other); + + operator T **() const; + + template + THandle StaticCast() const; + + template + THandle ReinterpretCast() const; + + bool operator==(const THandle &other) const; + bool operator!=(const THandle &other) const; + + bool operator==(T** other) const; + bool operator!=(T** other) const; +}; + +typedef THandle Handle; + +#include "MMHandleBlock.h" + +inline THandleBase::THandleBase(PortabilityLayer::MMHandleBlock *hdl) + : m_hdl(hdl) +{ +} + +inline THandleBase::THandleBase(const THandleBase &other) + : m_hdl(other.m_hdl) +{ +} + +inline PortabilityLayer::MMHandleBlock *THandleBase::MMBlock() const +{ + return m_hdl; +} + +template +inline THandle::THandle() + : THandleBase(nullptr) +{ +} + +template +inline THandle::THandle(T **hdl) + : THandleBase(reinterpret_cast(hdl)) +{ +} + +template +inline THandle::THandle(PortabilityLayer::MMHandleBlock *hdl) + : THandleBase(hdl) +{ +} + +template +inline THandle::THandle(const THandle &other) + : THandleBase(other.m_hdl) +{ +} + +template +bool THandle::operator==(const THandle &other) const +{ + return m_hdl == other.m_hdl; +} + +template +bool THandle::operator!=(const THandle &other) const +{ + return m_hdl != other.m_hdl; +} + +template +bool THandle::operator==(T** other) const +{ + return static_cast(&m_hdl->m_contents) == static_cast(other); +} + +template +bool THandle::operator!=(T** other) const +{ + return static_cast(&m_hdl->m_contents) != static_cast(other); +} + +template +inline THandle::operator T**() const +{ + return reinterpret_cast(&m_hdl->m_contents); +} + +template +template +THandle THandle::StaticCast() const +{ + (void)(static_cast(static_cast(nullptr))); + return THandle(m_hdl); +} + +template +template +THandle THandle::ReinterpretCast() const +{ + (void)(reinterpret_cast(static_cast(nullptr))); + return THandle(m_hdl); +} diff --git a/PortabilityLayer/PLMenus.cpp b/PortabilityLayer/PLMenus.cpp index 0eb18f0..d79c75b 100644 --- a/PortabilityLayer/PLMenus.cpp +++ b/PortabilityLayer/PLMenus.cpp @@ -27,7 +27,7 @@ MenuHandle GetMenu(int resID) return nullptr; const MenuHandle menu = PortabilityLayer::MenuManager::GetInstance()->DeserializeMenu(*menuRes); - DisposeHandle(menuRes); + menuRes.Dispose(); return menu; } diff --git a/PortabilityLayer/PLQDOffscreen.cpp b/PortabilityLayer/PLQDOffscreen.cpp index d699dc3..ab92b73 100644 --- a/PortabilityLayer/PLQDOffscreen.cpp +++ b/PortabilityLayer/PLQDOffscreen.cpp @@ -252,7 +252,7 @@ PixMapHandle GetGWorldPixMap(GWorldPtr gworld) PicHandle GetPicture(short resID) { - return reinterpret_cast(PortabilityLayer::ResourceManager::GetInstance()->GetResource('PICT', resID)); + return PortabilityLayer::ResourceManager::GetInstance()->GetResource('PICT', resID).StaticCast(); } void OffsetRect(Rect *rect, int right, int down) @@ -290,7 +290,7 @@ void DrawPicture(PicHandle pict, Rect *bounds) PortabilityLayer::PixMapImpl *pixMap = static_cast(*port->GetPixMap()); - long handleSize = GetHandleSize(reinterpret_cast(pict)); + long handleSize = pict.MMBlock()->m_size; PortabilityLayer::MemReaderStream stream(picPtr, handleSize); // Adjust draw origin diff --git a/PortabilityLayer/PLQDOffscreen.h b/PortabilityLayer/PLQDOffscreen.h index b1488f1..e9d55af 100644 --- a/PortabilityLayer/PLQDOffscreen.h +++ b/PortabilityLayer/PLQDOffscreen.h @@ -18,7 +18,7 @@ typedef PixMap *PixMapPtr; typedef PixMapPtr *PixMapHandle; typedef Picture *PicPtr; -typedef PicPtr *PicHandle; +typedef THandle PicHandle; enum QDFlags { diff --git a/PortabilityLayer/PLQDraw.cpp b/PortabilityLayer/PLQDraw.cpp index 82547f7..77598af 100644 --- a/PortabilityLayer/PLQDraw.cpp +++ b/PortabilityLayer/PLQDraw.cpp @@ -889,8 +889,8 @@ void GetIndPattern(Pattern *pattern, int patListID, int index) if (index < 1) return; - PortabilityLayer::MMHandleBlock *patternList = PortabilityLayer::ResourceManager::GetInstance()->GetResource('PAT#', patListID); - const uint8_t *patternRes = static_cast(patternList->m_contents); + THandle patternList = PortabilityLayer::ResourceManager::GetInstance()->GetResource('PAT#', patListID).StaticCast(); + const uint8_t *patternRes = *patternList; int numPatterns = (patternRes[0] << 8) | patternRes[1]; if (index > numPatterns) diff --git a/PortabilityLayer/PLResourceManager.cpp b/PortabilityLayer/PLResourceManager.cpp index 5a2f516..eb9952b 100644 --- a/PortabilityLayer/PLResourceManager.cpp +++ b/PortabilityLayer/PLResourceManager.cpp @@ -33,7 +33,7 @@ namespace PortabilityLayer short OpenResFork(VirtualDirectory_t virtualDir, const PLPasStr &filename) override; void CloseResFile(short ref) override; - MMHandleBlock *GetResource(const ResTypeID &resType, int id) override; + THandle GetResource(const ResTypeID &resType, int id) override; short GetCurrentResFile() const override; void SetCurrentResFile(short ref) override; @@ -224,7 +224,7 @@ namespace PortabilityLayer m_currentResFile = m_lastResFile; } - MMHandleBlock *ResourceManagerImpl::GetResource(const ResTypeID &resType, int id) + THandle ResourceManagerImpl::GetResource(const ResTypeID &resType, int id) { short searchIndex = m_currentResFile; while (searchIndex >= 0) @@ -233,7 +233,7 @@ namespace PortabilityLayer assert(slot.m_resourceFile); if (MMHandleBlock *block = slot.m_resourceFile->GetResource(resType, id, m_load)) - return block; + return THandle(block); searchIndex = slot.m_prevFile; } diff --git a/PortabilityLayer/PLResources.cpp b/PortabilityLayer/PLResources.cpp index 4dde44b..4303998 100644 --- a/PortabilityLayer/PLResources.cpp +++ b/PortabilityLayer/PLResources.cpp @@ -17,24 +17,6 @@ struct PLOpenedResFile static const unsigned int kPLMaxOpenedResFiles = 64; static PLOpenedResFile gs_resFiles[kPLMaxOpenedResFiles]; -void DetachResource(Handle hdl) -{ - if (!hdl) - return; - - PortabilityLayer::MMHandleBlock *block = reinterpret_cast(hdl); - assert(block->m_rmSelfRef); - assert(block->m_rmSelfRef->m_handle == block); - block->m_rmSelfRef->m_handle = nullptr; - block->m_rmSelfRef = nullptr; -} - -void ReleaseResource(Handle hdl) -{ - DetachResource(hdl); - DisposeHandle(hdl); -} - short CurResFile() { return PortabilityLayer::ResourceManager::GetInstance()->GetCurrentResFile(); @@ -76,7 +58,7 @@ void SetResLoad(Boolean load) long GetMaxResourceSize(Handle res) { - const PortabilityLayer::MMHandleBlock *hBlock = reinterpret_cast(res); + const PortabilityLayer::MMHandleBlock *hBlock = res.MMBlock(); const PortabilityLayer::ResourceCompiledRef *resRef = hBlock->m_rmSelfRef; return resRef->GetSize(); } diff --git a/PortabilityLayer/PortabilityLayer.vcxproj b/PortabilityLayer/PortabilityLayer.vcxproj index 50c7e9b..3785632 100644 --- a/PortabilityLayer/PortabilityLayer.vcxproj +++ b/PortabilityLayer/PortabilityLayer.vcxproj @@ -193,6 +193,7 @@ + @@ -317,6 +318,7 @@ + diff --git a/PortabilityLayer/PortabilityLayer.vcxproj.filters b/PortabilityLayer/PortabilityLayer.vcxproj.filters index 98ce994..f138d7b 100644 --- a/PortabilityLayer/PortabilityLayer.vcxproj.filters +++ b/PortabilityLayer/PortabilityLayer.vcxproj.filters @@ -390,6 +390,9 @@ Header Files + + Header Files + @@ -590,5 +593,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/PortabilityLayer/ResourceManager.h b/PortabilityLayer/ResourceManager.h index 9533ebf..d35c1eb 100644 --- a/PortabilityLayer/ResourceManager.h +++ b/PortabilityLayer/ResourceManager.h @@ -1,6 +1,7 @@ #pragma once #include "VirtualDirectory.h" +#include "PLHandle.h" class PLPasStr; @@ -21,7 +22,7 @@ namespace PortabilityLayer virtual short OpenResFork(VirtualDirectory_t virtualDir, const PLPasStr &filename) = 0; virtual void CloseResFile(short ref) = 0; - virtual MMHandleBlock *GetResource(const ResTypeID &resType, int id) = 0; + virtual THandle GetResource(const ResTypeID &resType, int id) = 0; virtual short GetCurrentResFile() const = 0; virtual void SetCurrentResFile(short ref) = 0;