added GlobalHandle

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@19741 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2003-03-23 20:36:51 +00:00
parent f7292ebeba
commit 51a2a728e5

View File

@ -398,6 +398,45 @@ private:
DECLARE_NO_COPY_CLASS(SelectInHDC)
};
// when working with global pointers (which is unfortunately still necessary
// sometimes, e.g. for clipboard) it is important to unlock them exactly as
// many times as we lock them which just asks for using a "smart lock" class
class GlobalHandle
{
public:
GlobalHandle(HGLOBAL hGlobal) : m_hGlobal(hGlobal)
{
m_ptr = ::GlobalLock(hGlobal);
if ( !m_ptr )
{
wxLogLastError(_T("GlobalLock"));
}
}
~GlobalHandle()
{
if ( !::GlobalUnlock(m_hGlobal) )
{
#ifdef __WXDEBUG__
// this might happen simply because the block became unlocked
DWORD dwLastError = ::GetLastError();
if ( dwLastError != NO_ERROR )
{
wxLogApiError(_T("GlobalUnlock"), dwLastError);
}
#endif // __WXDEBUG__
}
}
operator void *() const { return m_ptr; }
private:
HGLOBAL m_hGlobal;
void *m_ptr;
DECLARE_NO_COPY_CLASS(GlobalHandle)
};
// ---------------------------------------------------------------------------
// macros to make casting between WXFOO and FOO a bit easier: the GetFoo()
// returns Foo cast to the Windows type for oruselves, while GetFooOf() takes