From: Vadim Zeitlin Date: Fri, 29 Jul 2005 12:18:17 +0000 (+0000) Subject: added GlobalPtr: GlobalAlloc/Free() wrapper X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/2be57a51314dab0a6b558eedb65bb6affb5c73f9 added GlobalPtr: GlobalAlloc/Free() wrapper git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@34983 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index 73c03ad10c..f165bf13e8 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -535,6 +535,33 @@ private: DECLARE_NO_COPY_CLASS(HDCClipper) }; +// smart buffeer using GlobalAlloc/GlobalFree() +class GlobalPtr +{ +public: + // allocates a block of given size + GlobalPtr(size_t size, unsigned flags = GMEM_MOVEABLE) + { + m_hGlobal = ::GlobalAlloc(flags, size); + if ( !m_hGlobal ) + wxLogLastError(_T("GlobalAlloc")); + } + + ~GlobalPtr() + { + if ( m_hGlobal && ::GlobalFree(m_hGlobal) ) + wxLogLastError(_T("GlobalFree")); + } + + // implicit conversion + operator HGLOBAL() const { return m_hGlobal; } + +private: + HGLOBAL m_hGlobal; + + DECLARE_NO_COPY_CLASS(GlobalPtr) +}; + // 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