Handle refactor

This commit is contained in:
elasota
2019-12-29 23:14:37 -05:00
parent 9cf539eb52
commit fb105bd338
37 changed files with 365 additions and 240 deletions

View File

@@ -533,10 +533,10 @@ namespace PortabilityLayer
}
if (icsHandle)
DisposeHandle(icsHandle);
icsHandle.Dispose();
if (ics8Handle)
DisposeHandle(ics8Handle);
ics8Handle.Dispose();
m_haveIcon = true;
}

View File

@@ -8,8 +8,7 @@ struct Control
{
};
typedef Control *ControlPtr;
typedef ControlPtr *ControlHandle;
typedef THandle<Control> ControlHandle;
typedef void(*ControlActionProc)(ControlHandle control, short part);
typedef ControlActionProc ControlActionUPP;

View File

@@ -222,7 +222,7 @@ Rect BERect::ToRect() const
CursHandle GetCursor(int cursorID)
{
return reinterpret_cast<CursHandle>(GetResource('CURS', cursorID));
return GetResource('CURS', cursorID).ReinterpretCast<Cursor>();
}
CCrsrHandle GetCCursor(int cursorID)
@@ -271,7 +271,7 @@ void DisposeCCursor(CCrsrHandle handle)
{
(*handle)->hwCursor->Destroy();
PortabilityLayer::MemoryManager::GetInstance()->ReleaseHandle(reinterpret_cast<PortabilityLayer::MMHandleBlock*>(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<uint8_t> istrRes = PortabilityLayer::ResourceManager::GetInstance()->GetResource('STR#', stringsID).StaticCast<uint8_t>();
if (istrRes && *istrRes)
{
const uint8_t *contentsBytes = static_cast<const uint8_t *>(istrRes->m_contents);
const uint8_t *contentsBytes = *istrRes;
const BEUInt16_t *pArraySize = reinterpret_cast<const BEUInt16_t*>(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<PortabilityLayer::MMHandleBlock*>(handle));
}
long GetHandleSize(Handle handle)
{
if (!handle)
return 0;
PortabilityLayer::MMHandleBlock *block = reinterpret_cast<PortabilityLayer::MMHandleBlock*>(handle);
return static_cast<long>(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<PortabilityLayer::MMHandleBlock*>(hdl), newSize))
if (!mm->ResizeHandle(hdl.MMBlock(), newSize))
return PLErrors::kOutOfMemory;
return PLErrors::kNone;

View File

@@ -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<Cursor> CursHandle;
typedef THandle<CCursor> CCrsrHandle;
typedef THandle<Menu> MenuHandle;
typedef THandle<VersionRecord> 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

View File

@@ -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<Control> *itemHandle, Rect *itemRect)
{
PL_NotYetImplemented();
}
void GetDialogItemText(Handle handle, StringPtr str)
void GetDialogItemText(THandle<Control> 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<Control> itemHandle, const Rect *itemRect)
{
PL_NotYetImplemented();
}
void SetDialogItemText(Handle handle, const PLPasStr &str)
void SetDialogItemText(THandle<Control> handle, const PLPasStr &str)
{
PL_NotYetImplemented();
}

View File

@@ -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<DialogTemplate> 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<Control> *itemHandle, Rect *itemRect);
void GetDialogItemText(THandle<Control> 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<Control> itemHandle, const Rect *itemRect);
void SetDialogItemText(THandle<Control> handle, const PLPasStr &str);
void SelectDialogItemText(DialogPtr dialog, int item, int firstSelChar, int lastSelCharExclusive);

View File

@@ -0,0 +1,9 @@
#include "PLHandle.h"
#include "MemoryManager.h"
void THandleBase::Dispose()
{
if (m_hdl)
PortabilityLayer::MemoryManager::GetInstance()->ReleaseHandle(m_hdl);
}

133
PortabilityLayer/PLHandle.h Normal file
View File

@@ -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 T>
class THandle final : public THandleBase
{
public:
THandle();
THandle(T **hdl);
explicit THandle(PortabilityLayer::MMHandleBlock *hdl);
THandle(const THandle<T> &other);
operator T **() const;
template<class TOther>
THandle<TOther> StaticCast() const;
template<class TOther>
THandle<TOther> ReinterpretCast() const;
bool operator==(const THandle<T> &other) const;
bool operator!=(const THandle<T> &other) const;
bool operator==(T** other) const;
bool operator!=(T** other) const;
};
typedef THandle<void> 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<class T>
inline THandle<T>::THandle()
: THandleBase(nullptr)
{
}
template<class T>
inline THandle<T>::THandle(T **hdl)
: THandleBase(reinterpret_cast<PortabilityLayer::MMHandleBlock*>(hdl))
{
}
template<class T>
inline THandle<T>::THandle(PortabilityLayer::MMHandleBlock *hdl)
: THandleBase(hdl)
{
}
template<class T>
inline THandle<T>::THandle(const THandle<T> &other)
: THandleBase(other.m_hdl)
{
}
template<class T>
bool THandle<T>::operator==(const THandle<T> &other) const
{
return m_hdl == other.m_hdl;
}
template<class T>
bool THandle<T>::operator!=(const THandle<T> &other) const
{
return m_hdl != other.m_hdl;
}
template<class T>
bool THandle<T>::operator==(T** other) const
{
return static_cast<void*>(&m_hdl->m_contents) == static_cast<void*>(other);
}
template<class T>
bool THandle<T>::operator!=(T** other) const
{
return static_cast<void*>(&m_hdl->m_contents) != static_cast<void*>(other);
}
template<class T>
inline THandle<T>::operator T**() const
{
return reinterpret_cast<T**>(&m_hdl->m_contents);
}
template<class T>
template<class TOther>
THandle<TOther> THandle<T>::StaticCast() const
{
(void)(static_cast<TOther*>(static_cast<T*>(nullptr)));
return THandle<TOther>(m_hdl);
}
template<class T>
template<class TOther>
THandle<TOther> THandle<T>::ReinterpretCast() const
{
(void)(reinterpret_cast<TOther*>(static_cast<T*>(nullptr)));
return THandle<TOther>(m_hdl);
}

View File

@@ -27,7 +27,7 @@ MenuHandle GetMenu(int resID)
return nullptr;
const MenuHandle menu = PortabilityLayer::MenuManager::GetInstance()->DeserializeMenu(*menuRes);
DisposeHandle(menuRes);
menuRes.Dispose();
return menu;
}

View File

@@ -252,7 +252,7 @@ PixMapHandle GetGWorldPixMap(GWorldPtr gworld)
PicHandle GetPicture(short resID)
{
return reinterpret_cast<PicHandle>(PortabilityLayer::ResourceManager::GetInstance()->GetResource('PICT', resID));
return PortabilityLayer::ResourceManager::GetInstance()->GetResource('PICT', resID).StaticCast<Picture>();
}
void OffsetRect(Rect *rect, int right, int down)
@@ -290,7 +290,7 @@ void DrawPicture(PicHandle pict, Rect *bounds)
PortabilityLayer::PixMapImpl *pixMap = static_cast<PortabilityLayer::PixMapImpl*>(*port->GetPixMap());
long handleSize = GetHandleSize(reinterpret_cast<Handle>(pict));
long handleSize = pict.MMBlock()->m_size;
PortabilityLayer::MemReaderStream stream(picPtr, handleSize);
// Adjust draw origin

View File

@@ -18,7 +18,7 @@ typedef PixMap *PixMapPtr;
typedef PixMapPtr *PixMapHandle;
typedef Picture *PicPtr;
typedef PicPtr *PicHandle;
typedef THandle<Picture> PicHandle;
enum QDFlags
{

View File

@@ -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<const uint8_t*>(patternList->m_contents);
THandle<uint8_t> patternList = PortabilityLayer::ResourceManager::GetInstance()->GetResource('PAT#', patListID).StaticCast<uint8_t>();
const uint8_t *patternRes = *patternList;
int numPatterns = (patternRes[0] << 8) | patternRes[1];
if (index > numPatterns)

View File

@@ -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<void> 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<void> 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<void>(block);
searchIndex = slot.m_prevFile;
}

View File

@@ -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<PortabilityLayer::MMHandleBlock*>(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<PortabilityLayer::MMHandleBlock*>(res);
const PortabilityLayer::MMHandleBlock *hBlock = res.MMBlock();
const PortabilityLayer::ResourceCompiledRef *resRef = hBlock->m_rmSelfRef;
return resRef->GetSize();
}

View File

@@ -193,6 +193,7 @@
<ClInclude Include="PLEventQueue.h" />
<ClInclude Include="PLFolders.h" />
<ClInclude Include="PLHacks.h" />
<ClInclude Include="PLHandle.h" />
<ClInclude Include="PLKeyEncoding.h" />
<ClInclude Include="PLLowMem.h" />
<ClInclude Include="PLMacTypes.h" />
@@ -317,6 +318,7 @@
<ClCompile Include="ResourceFile.cpp" />
<ClCompile Include="ScanlineMaskIterator.cpp" />
<ClCompile Include="SimpleGraphic.cpp" />
<ClCompile Include="PLHandle.cpp" />
<ClCompile Include="WindowDef.cpp" />
<ClCompile Include="WindowManager.cpp" />
<ClCompile Include="XModemCRC.cpp" />

View File

@@ -390,6 +390,9 @@
<ClInclude Include="EllipsePlotter.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="PLHandle.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CFileStream.cpp">
@@ -590,5 +593,8 @@
<ClCompile Include="EllipsePlotter.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="PLHandle.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@@ -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<void> GetResource(const ResTypeID &resType, int id) = 0;
virtual short GetCurrentResFile() const = 0;
virtual void SetCurrentResFile(short ref) = 0;