📄 autoptr.h
字号:
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
class CAutoRefc
{
public:
CAutoRefc();
CAutoRefc(T* pt);
CAutoRefc(CAutoRefc<T> &pt);
CAutoRefc(REFIID riid_T, IUnknown* punk);
~CAutoRefc();
T *operator=(T* pt);
T *operator=(const CAutoRefc<T> &p);
operator T* ();
T ** operator & ();
T* operator->();
T* PvReturn(void);
void Acquire(T *pT);
void Clear();
ULONG AddRef();
protected:
T* m_pt;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline CAutoRefc<T>::CAutoRefc(T *pt)
//
// parameters:
//
// description:
// Create a CAutoRefc giving the object pointed to by pt
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline CAutoRefc<T>::CAutoRefc(T *pt)
{
m_pt = pt;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline CAutoRefc<T>::CAutoRefc()
//
// parameters:
//
// description:
// Create an empty CAutoRefc.
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline CAutoRefc<T>::CAutoRefc()
: m_pt(0)
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline CAutoRefc<T>::CAutoRefc<T>(REFIID riid_t, IUnknown *pt)
//
// parameters:
//
// description:
// Create a CAutoRefc given an IUnknown and a desired interface. QueryInterface is used on
// pt to attempt to get the desired interface. If the QI fails, the CAutoRefc is set to null.
// When the CAutoRefc destructs, the AddRef by the QI is released.
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline CAutoRefc<T>::CAutoRefc(REFIID riid_t, IUnknown *pt)
{
ASSERT(NULL != pt);
pt->QueryInterface(riid_t, (void **)&m_pt);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline CAutoRefc<T>::~CAutoRefc<T>()
//
// parameters:
//
// description:
// CAutoRefc destructor. When an object of class CAutoRefc goes out of scope, free the
// associated object (if any).
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline CAutoRefc<T>::~CAutoRefc()
{
ReleaseInterface((const T*&)m_pt);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline T* CAutoRefc<T>::operator=(T* pt)
//
// parameters:
//
// description:
// Initialize a CAutoRefc that was not initalized by the construtor.
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline T* CAutoRefc<T>::operator=(T* pt)
{
ASSERT(m_pt == 0);
m_pt = pt;
return pt;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline void CAutoRefc<T>::Clear()
//
// parameters:
//
// description:
// Clear
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline void CAutoRefc<T>::Clear()
{
ReleaseInterface((const T*&)m_pt);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline CAutoRefc<T>::operator T*(void)
//
// parameters:
//
// description:
// Cast operator used to "unwrap" the pointed object as if the CAutoRefc variable were
// a pointer of type T. In many situations this is enough for an autopointer to appear
// exactly like an ordinary pointer.
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline CAutoRefc<T>::operator T*(void)
{
return m_pt;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline T ** CAutoRefc<T>::operator & ()
//
// parameters:
//
// description:
// Address-of operator is used to make the autopointer even more similar to an ordinary pointer.
// When you take an address of an autopointer, you actually get an address of the wrapped pointer.
// This is very useful for doing QI, like this:
//
// CAutoRefc<lt>IFoo<gt> pfoo;
// pbar->QueryInterface (IID_IFoo, (void **) &pfoo) \| check;
//
// This technique is different from the REFIID constructor (see above) in that it allows for an
// explicit check of the HRESULT from QI.
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline T ** CAutoRefc<T>::operator & ()
{
return & m_pt;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline T * CAutoRefc<T>::operator->(void)
//
// parameters:
//
// description:
// The 'follow' operator on the CAutoRef. Works analogously as with CAutoP.
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline T * CAutoRefc<T>::operator->(void)
{
ASSERT(m_pt != 0);
return m_pt;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline T * CAutoRefc<T>::PvReturn()
//
// parameters:
//
// description:
// Returns the object(s) pointed to by the CAutoRefc variable. In addition this method
// 'unhooks' the object(s), such that the scope of the object(s) are no longer local.
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline T * CAutoRefc<T>::PvReturn()
{
T *ptT = m_pt;
m_pt = 0;
return ptT;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline ULONG CAutoRefc<T>::AddRef()
//
// parameters:
//
// description:
// AddRefs embedded interface pointer.
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline ULONG CAutoRefc<T>::AddRef()
{
if (m_pt)
{
return m_pt->AddRef();
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// CAutoBSTR - Autopointers to BSTRs
//
// PREFIX/PREFAST Note: Using this class will cause false positives to be flagged.
// CAutoBstr is templatized as a WCHAR*, and a BSTR is a WCHAR*.
// WCHAR* non-bstr-allocated memory can be passed to the Assign fcn.
// Although pointer operations return a T* (WCHAR*) they are
// returning a BSTR allocated block of memory.
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class CAutoBSTR : public CAutoBase<WCHAR>
{
public:
inline CAutoBSTR(); // will be initialized with NULL
inline ~CAutoBSTR();
inline void Clear();
inline HRESULT CopyTo(BSTR *pbstr);
inline BOOL isEmpty(void);
inline UINT Len(void);
inline HRESULT AllocBSTR(UINT len);
inline HRESULT Assign(BSTR bstr, bool fCopy = TRUE);
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline CAutoBSTR::CAutoBSTR()
//
// parameters:
//
// description:
// Constructor
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
inline CAutoBSTR::CAutoBSTR()
: CAutoBase<WCHAR>(NULL)
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline CAutoBSTR::~CAutoBSTR()
//
// parameters:
//
// description:
// Destructor
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
inline CAutoBSTR::~CAutoBSTR()
{
Clear();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline void CAutoBSTR::Clear()
//
// parameters:
//
// description:
// Clear
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void CAutoBSTR::Clear()
{
::SysFreeString(m_pt);
m_pt = NULL;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline HRESULT CAutoBSTR::Assign(BSTR bstr, bool fCopy)
//
// parameters:
//
// description:
// Assignment implementation w/ optional copy of string
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
inline HRESULT CAutoBSTR::Assign(BSTR bstr, bool fCopy)
{
Clear();
if (fCopy)
{
if (bstr != 0)
{
m_pt = ::SysAllocString(bstr);
return m_pt ? S_OK : E_OUTOFMEMORY;
}
}
else
{
m_pt = bstr;
}
return S_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline HRESULT CAutoBSTR::CopyTo(BSTR *pbstr)
//
// parameters:
//
// description:
// Create copy
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
inline HRESULT CAutoBSTR::CopyTo(BSTR *pbstr)
{
if (!pbstr)
return E_INVALIDARG;
*pbstr = NULL;
if (! isEmpty())
{
*pbstr = ::SysAllocString(m_pt);
if (*pbstr == NULL)
return E_OUTOFMEMORY;
}
return S_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline BOOL CAutoBSTR::isEmpty(void)
//
// parameters:
//
// description:
// Is string empty?
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
inline BOOL CAutoBSTR::isEmpty(void)
{
return (Len() == 0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline UINT CAutoBSTR::Len(void)
//
// parameters:
//
// description:
// Returns length of the stored string
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
inline UINT CAutoBSTR::Len(void)
{
return ::SysStringLen(m_pt);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: inline UINT CAutoBSTR::AllocBSTR(UINT len)
//
// parameters:
//
// description:
// allocates enough memory for len OLECHAR plus terminating zero
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
inline HRESULT CAutoBSTR::AllocBSTR(UINT len)
{
Clear();
m_pt = ::SysAllocStringLen(0, len);
return m_pt ? S_OK : E_OUTOFMEMORY;
}
#endif // __AUTOPTR_H_INCLUDED__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -