|
// AutoResources.h
//
// Author : David Shepherd
// Copyright (c) 2002, DaeDoe-Software
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_AUTORESOURCES_H__E60244A3_2E09_11D6_B6A6_C8D760853445__INCLUDED_)
#define AFX_AUTORESOURCES_H__E60244A3_2E09_11D6_B6A6_C8D760853445__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CAutoClipboard
class CAutoClipboard
{
private:
// this will be set TRUE if we opened the clipboard
BOOL m_Open;
public:
CAutoClipboard(HWND hWndOwner=NULL)
{
// open the clipboard
m_Open=OpenClipboard(hWndOwner);
if(m_Open==FALSE)
{
throw std::exception();
}
}
~CAutoClipboard()
{
// close the clipboard
if(m_Open)
{
(void)CloseClipboard();
}
}
// todo : make this work correctly accross multiple instances
NOCOPY(CAutoClipboard);
};
/////////////////////////////////////////////////////////////////////////////
// CAutoDCState
class CAutoDCState
{
public:
// this will be set TRUE if the device context state was saved
BOOL m_Saved;
// the device context handle
HDC m_hdc;
public:
CAutoDCState(HDC hdc) : m_hdc(hdc)
{
// save the device context state
m_Saved=SaveDC(m_hdc);
if(m_Saved==FALSE)
{
throw std::exception();
}
}
~CAutoDCState()
{
// restore the device context state
if(m_Saved)
{
(void)RestoreDC(m_hdc,-1);
}
}
// todo : make this work correctly accross multiple instances
NOCOPY(CAutoDCState);
};
/////////////////////////////////////////////////////////////////////////////
// CAutoTypeAttrPtr
class CAutoTypeAttrPtr
{
private:
// type attributes pointer
TYPEATTR *m_pTypeAttr;
// owner
CComPtr<ITypeInfo> m_spOwner;
public:
CAutoTypeAttrPtr() :
m_pTypeAttr(NULL)
{}
CAutoTypeAttrPtr(const CComPtr<ITypeInfo> &spTypeInfo) :
m_pTypeAttr(NULL)
{
// get the type attributes
Get(spTypeInfo);
}
~CAutoTypeAttrPtr()
{
// release the type attributes
if(m_pTypeAttr!=NULL and m_spOwner!=NULL)
{
(void)m_spOwner->ReleaseTypeAttr(m_pTypeAttr);
}
}
void Get(const CComPtr<ITypeInfo> &spTypeInfo)
{
// check parameters
if(spTypeInfo==NULL)
{
throw std::exception();
}
// release the type attributes
if(m_pTypeAttr!=NULL and m_spOwner!=NULL)
{
(void)m_spOwner->ReleaseTypeAttr(m_pTypeAttr);
m_pTypeAttr=NULL;
}
// get the type attributes
if(!SUCCEEDED(spTypeInfo->GetTypeAttr(&m_pTypeAttr)))
{
throw std::exception();
}
// save the owner
m_spOwner=spTypeInfo;
}
TYPEATTR *operator->() const
{
// return the type attributes pointer
return m_pTypeAttr;
}
operator TYPEATTR *() const
{
// return the type attributes pointer
return m_pTypeAttr;
}
// todo : make this work correctly accross multiple instances
NOCOPY(CAutoTypeAttrPtr);
};
/////////////////////////////////////////////////////////////////////////////
// CAutoFuncDescPtr
class CAutoFuncDescPtr
{
private:
// function description pointer
FUNCDESC *m_pFuncDesc;
// owner
CComPtr<ITypeInfo> m_spOwner;
public:
CAutoFuncDescPtr() :
m_pFuncDesc(NULL)
{}
CAutoFuncDescPtr(long Ix,const CComPtr<ITypeInfo> &spTypeInfo) :
m_pFuncDesc(NULL)
{
// get the function description
Get(Ix,spTypeInfo);
}
~CAutoFuncDescPtr()
{
// release the function description
if(m_pFuncDesc!=NULL and m_spOwner!=NULL)
{
(void)m_spOwner->ReleaseFuncDesc(m_pFuncDesc);
}
}
void Get(long Ix,const CComPtr<ITypeInfo> &spTypeInfo)
{
// check parameters
if(spTypeInfo==NULL)
{
throw std::exception();
}
// release the function description
if(m_pFuncDesc!=NULL and m_spOwner!=NULL)
{
(void)m_spOwner->ReleaseFuncDesc(m_pFuncDesc);
m_pFuncDesc=NULL;
}
// get the function description
if(!SUCCEEDED(spTypeInfo->GetFuncDesc(Ix,&m_pFuncDesc)))
{
throw std::exception();
}
// save the owner
m_spOwner=spTypeInfo;
}
FUNCDESC *operator->() const
{
// return the function description pointer
return m_pFuncDesc;
}
operator FUNCDESC *() const
{
// return the function description pointer
return m_pFuncDesc;
}
// todo : make this work correctly accross multiple instances
NOCOPY(CAutoFuncDescPtr);
};
/////////////////////////////////////////////////////////////////////////////
// CAutoVarDescPtr
class CAutoVarDescPtr
{
private:
// variable description pointer
VARDESC *m_pVarDesc;
// owner
CComPtr<ITypeInfo> m_spOwner;
public:
CAutoVarDescPtr() :
m_pVarDesc(NULL)
{}
CAutoVarDescPtr(long Ix,const CComPtr<ITypeInfo> &spTypeInfo) :
m_pVarDesc(NULL)
{
// get the variable description
Get(Ix,spTypeInfo);
}
~CAutoVarDescPtr()
{
// release the variable description
if(m_pVarDesc!=NULL and m_spOwner!=NULL)
{
(void)m_spOwner->ReleaseVarDesc(m_pVarDesc);
}
}
void Get(long Ix,const CComPtr<ITypeInfo> &spTypeInfo)
{
// check parameters
if(spTypeInfo==NULL)
{
throw std::exception();
}
// release the variable description
if(m_pVarDesc!=NULL and m_spOwner!=NULL)
{
(void)m_spOwner->ReleaseVarDesc(m_pVarDesc);
m_pVarDesc=NULL;
}
// get the variable description
if(!SUCCEEDED(spTypeInfo->GetVarDesc(Ix,&m_pVarDesc)))
{
throw std::exception();
}
// save the owner
m_spOwner=spTypeInfo;
}
VARDESC *operator->() const
{
// return the variable description pointer
return m_pVarDesc;
}
operator VARDESC *() const
{
// return the variable description pointer
return m_pVarDesc;
}
// todo : make this work correctly accross multiple instances
NOCOPY(CAutoVarDescPtr);
};
/////////////////////////////////////////////////////////////////////////////
// CAutoGlobalMemory
class CAutoGlobalMemory
{
private:
// this will be set TRUE if we own the global memory
BOOL m_Owns;
// the global memory handle
HGLOBAL m_hGlobal;
public:
explicit CAutoGlobalMemory(HGLOBAL hGlobal=NULL) :
m_Owns((hGlobal==NULL) ? FALSE : TRUE),
m_hGlobal(hGlobal)
{}
CAutoGlobalMemory(CAutoGlobalMemory &Obj) :
m_Owns(Obj.m_Owns),
m_hGlobal(Obj.Release())
{}
~CAutoGlobalMemory()
{
// free the global memory
if(m_Owns)
{
(void)GlobalFree(m_hGlobal);
}
}
const CAutoGlobalMemory &operator=(CAutoGlobalMemory &Obj)
{
// check for self assignment
if(this==&Obj)
{
return *this;
}
// assign from a different global memory handle
if(m_hGlobal!=Obj.Get())
{
if(m_Owns)
{
(void)GlobalFree(m_hGlobal);
}
m_Owns=Obj.m_Owns;
}
// assign from the same global memory handle
else if(Obj.m_Owns)
{
m_Owns=TRUE;
}
return m_hGlobal=Obj.Release(), *this;
}
HGLOBAL Release()
{
// release ownership
m_Owns=FALSE;
// return the global memory handle
return m_hGlobal;
}
HGLOBAL Get() const
{
// return the global memory handle
return m_hGlobal;
}
operator HGLOBAL() const
{
// return the global memory handle
return m_hGlobal;
}
};
/////////////////////////////////////////////////////////////////////////////
// CAutoCoTaskMem
template <class T>
class CAutoCoTaskMem
{
private:
// this will be set TRUE if we own the memory
BOOL m_Owns;
// the memory pointer
T *m_pMem;
public:
explicit CAutoCoTaskMem(T *pMem=NULL) :
m_Owns((pMem==NULL) ? FALSE : TRUE),
m_pMem(pMem)
{}
CAutoCoTaskMem(CAutoCoTaskMem<T> &Obj) :
m_Owns(Obj.m_Owns),
m_pMem(Obj.Release())
{}
~CAutoCoTaskMem()
{
// free the memory
if(m_Owns and
// check the pointer too since ownership may have
// been set via operator&
m_pMem!=NULL)
{
CoTaskMemFree(m_pMem);
}
}
const CAutoCoTaskMem &operator=(CAutoCoTaskMem<T> &Obj)
{
// check for self assignment
if(this==Obj.GetThis())
{
return *this;
}
// assign from a different memory pointer
if(m_pMem!=Obj.Get())
{
if(m_Owns and
// check the pointer too since ownership may have
// been set via operator&
m_pMem!=NULL)
{
CoTaskMemFree(m_pMem);
}
m_Owns=Obj.m_Owns;
}
// assign from the same memory pointer
else if(Obj.m_Owns)
{
m_Owns=TRUE;
}
return m_pMem=Obj.Release(), *this;
}
const CAutoCoTaskMem *GetThis()
{
// return the this pointer
// this is required since operator& is overloaded
return this;
}
T *Release()
{
// release ownership
m_Owns=FALSE;
// return the memory pointer
return m_pMem;
}
T *Get() const
{
// return the memory pointer
return m_pMem;
}
operator T *() const
{
// return the memory pointer
return m_pMem;
}
T **operator&()
{
// assume com assignment
ATLASSERT(m_Owns==FALSE);
m_Owns=TRUE;
// return a pointer to the memory pointer
return &m_pMem;
}
};
/////////////////////////////////////////////////////////////////////////////
// CAutoSafeArrayPtr
class CAutoSafeArrayPtr
{
private:
// this will be set TRUE if we own the safe array pointer
BOOL m_Owns;
// the safe array pointer
SAFEARRAY *m_pSafeArray;
public:
explicit CAutoSafeArrayPtr(SAFEARRAY *pSafeArray=NULL) :
m_Owns((pSafeArray==NULL) ? FALSE : TRUE),
m_pSafeArray(pSafeArray)
{}
CAutoSafeArrayPtr(CAutoSafeArrayPtr &Obj) :
m_Owns(Obj.m_Owns),
m_pSafeArray(Obj.Release())
{}
~CAutoSafeArrayPtr()
{
// destroy the safe array
if(m_Owns)
{
(void)SafeArrayDestroy(m_pSafeArray);
}
}
const CAutoSafeArrayPtr &operator=(CAutoSafeArrayPtr &Obj)
{
// check for self assignment
if(this==&Obj)
{
return *this;
}
// assign from a different safe array pointer
if(m_pSafeArray!=Obj.Get())
{
if(m_Owns)
{
(void)SafeArrayDestroy(m_pSafeArray);
}
m_Owns=Obj.m_Owns;
}
// assign from the same safe array pointer
else if(Obj.m_Owns)
{
m_Owns=TRUE;
}
return m_pSafeArray=Obj.Release(), *this;
}
SAFEARRAY *Release()
{
// release ownership
m_Owns=FALSE;
// return the safe array pointer
return m_pSafeArray;
}
SAFEARRAY *Get() const
{
// return the safe array pointer
return m_pSafeArray;
}
operator SAFEARRAY *() const
{
// return the safe array pointer
return m_pSafeArray;
}
};
/////////////////////////////////////////////////////////////////////////////
// CAutoWindowHandle
class CAutoWindowHandle
{
private:
// this will be set TRUE if we own the window handle
BOOL m_Owns;
// the window handle
HWND m_hWnd;
public:
explicit CAutoWindowHandle(HWND hWnd=NULL) :
m_Owns((hWnd==NULL) ? FALSE : TRUE),
m_hWnd(hWnd)
{}
CAutoWindowHandle(CAutoWindowHandle &Obj) :
m_Owns(Obj.m_Owns),
m_hWnd(Obj.Release())
{}
~CAutoWindowHandle()
{
// destroy the window
if(m_Owns and IsWindow(m_hWnd))
{
(void)DestroyWindow(m_hWnd);
}
}
const CAutoWindowHandle &operator=(CAutoWindowHandle &Obj)
{
// check for self assignment
if(this==&Obj)
{
return *this;
}
// assign from a different window handle
if(m_hWnd!=Obj.Get())
{
if(m_Owns and IsWindow(m_hWnd))
{
(void)DestroyWindow(m_hWnd);
}
m_Owns=Obj.m_Owns;
}
// assign from the same window handle
else if(Obj.m_Owns)
{
m_Owns=TRUE;
}
return m_hWnd=Obj.Release(), *this;
}
HWND Release()
{
// release ownership
m_Owns=FALSE;
// return the window handle
return m_hWnd;
}
HWND Get() const
{
// return the window handle
return m_hWnd;
}
operator HWND() const
{
// return the window handle
return m_hWnd;
}
};
#endif // !defined(AFX_AUTORESOURCES_H__E60244A3_2E09_11D6_B6A6_C8D760853445__INCLUDED_)
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.