1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-12-25 14:11:28 +01:00

Redo resource RAII without the dubious casts

This commit is contained in:
Paul Licameli
2018-11-15 10:35:36 -05:00
parent cc34abea61
commit 7c1216edc6
2 changed files with 28 additions and 18 deletions

View File

@@ -1114,12 +1114,12 @@ void VSTEffect::BundleDeleter::operator() (void* p) const
CFRelease(static_cast<CFBundleRef>(p)); CFRelease(static_cast<CFBundleRef>(p));
} }
void VSTEffect::ResourceDeleter::operator() (void *p) const void VSTEffect::ResourceHandle::reset()
{ {
if (mpHandle) { if (mpHandle)
int resource = (int)p; CFBundleCloseBundleResourceMap(mpHandle, mNum);
CFBundleCloseBundleResourceMap(mpHandle->get(), resource); mpHandle = nullptr;
} mNum = 0;
} }
#endif #endif
@@ -2062,9 +2062,7 @@ bool VSTEffect::Load()
// Open the resource map ... some plugins (like GRM Tools) need this. // Open the resource map ... some plugins (like GRM Tools) need this.
mResource = ResourceHandle{ mResource = ResourceHandle{
reinterpret_cast<char*>( mBundleRef.get(), CFBundleOpenBundleResourceMap(mBundleRef.get())
CFBundleOpenBundleResourceMap(mBundleRef.get())),
ResourceDeleter{&mBundleRef}
}; };
#elif defined(__WXMSW__) #elif defined(__WXMSW__)
@@ -2267,7 +2265,7 @@ void VSTEffect::Unload()
if (mModule) if (mModule)
{ {
#if defined(__WXMAC__) #if defined(__WXMAC__)
mResource = ResourceHandle{}; mResource.reset();
mBundleRef.reset(); mBundleRef.reset();
#endif #endif

View File

@@ -302,15 +302,27 @@ private:
BundleHandle mBundleRef; BundleHandle mBundleRef;
struct ResourceDeleter { struct ResourceHandle {
const BundleHandle *mpHandle; ResourceHandle(
ResourceDeleter(const BundleHandle *pHandle = nullptr) CFBundleRef pHandle = nullptr, CFBundleRefNum num = 0)
: mpHandle(pHandle) {} : mpHandle{ pHandle }, mNum{ num }
void operator() (void*) const; {}
ResourceHandle& operator=( ResourceHandle &&other )
{
if (this != &other) {
mpHandle = other.mpHandle;
mNum = other.mNum;
other.mpHandle = nullptr;
other.mNum = 0;
}
return *this;
}
~ResourceHandle() { reset(); }
void reset();
CFBundleRef mpHandle{};
CFBundleRefNum mNum{};
}; };
using ResourceHandle = std::unique_ptr<
char, ResourceDeleter
>;
ResourceHandle mResource; ResourceHandle mResource;
#endif #endif