⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autoptr.h

📁 Windows CE 6.0 Server 源码
💻 H
📖 第 1 页 / 共 2 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft shared
// source or premium shared source license agreement under which you licensed
// this source code. If you did not accept the terms of the license agreement,
// you are not authorized to use this source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the SOURCE.RTF on your install media or the root of your tools installation.
// THE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
//+---------------------------------------------------------------------------------
//
//
// File:
//      autoptr.h
//
// Contents:
//
//      Autopointer classes
//
//----------------------------------------------------------------------------------

#ifndef __AUTOPTR_H_INCLUDED__
#define __AUTOPTR_H_INCLUDED__

////////////////////////////////////////////////////////////////////////////////////////////////////
//  CAutoBase - base class for all AutoPointers
//
////////////////////////////////////////////////////////////////////////////////////////////////////

#ifdef UNDER_CE
	//disable operator -> warning on templatessy
	#pragma warning(disable: 4284)
#endif


template <class T>
class CAutoBase
{
public:
    inline CAutoBase(T* pt);
    inline ~CAutoBase();

    inline T* operator= (T*);
    inline operator T* (void);
    inline operator const T* (void)const;
    inline T ** operator & (void);
    inline T* PvReturn(void);

protected:
    T* m_pt;

private:    // Never-to-use
    inline CAutoBase& operator= (CAutoBase&);
    CAutoBase(CAutoBase&);
};

////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline CAutoBase<T>::CAutoBase(T *pt)
//
//  parameters:
//
//  description:
//        Create a CAutoBase giving the array of objects pointed to by pt, allows NULL to be passed in
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline CAutoBase<T>::CAutoBase(T *pt)
{
    m_pt = pt;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline CAutoBase<T>::~CAutoBase()
//
//  parameters:
//
//  description:
//        Destructor. Asserts that the object has been free'd (set to NULL).
//        Setting to NULL does not happen in the retail build
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline CAutoBase<T>::~CAutoBase()
{
    ASSERT(NULL == m_pt);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline T* CAutoBase<T>::operator=(T* pt)
//
//  parameters:
//
//  description:
//        Assigns to variable after construction. May be dangerous so it ASSERTs that the variable is NULL
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline T* CAutoBase<T>::operator=(T* pt)
{
    ASSERT(m_pt == NULL);
    m_pt = pt;
    return pt;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline CAutoBase<T>::operator T*(void)
//
//  parameters:
//
//  description:
//        Cast operator used to "unwrap" the pointed object as if the CAutoBase 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 CAutoBase<T>::operator T*(void)
{
    return m_pt;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline CAutoBase<T>::operator const T*(void)const
//
//  parameters:
//
//  description:
//        Const cast operator
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline CAutoBase<T>::operator const T*(void)const
{
    return m_pt;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline T ** CAutoBase<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.
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline T ** CAutoBase<T>::operator & ()
{
    return & m_pt;
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline T * CAutoBase<T>::PvReturn(void)
//
//  parameters:
//
//  description:
//        Returns the object(s) pointed to by the CAutoBase 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 * CAutoBase<T>::PvReturn(void)
{
    T *ptT = m_pt;
    m_pt = NULL;
    return ptT;
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//  CAutoRg - autopointers to arrays
//
//    This derived class is primarily used to implement the vector deleting destructor.
//    Should only be used on objects allocated with new[]
////////////////////////////////////////////////////////////////////////////////////////////////////

template <class T>
class CAutoRg : public CAutoBase<T>
{
public:
    inline CAutoRg(T *pt);
    inline ~CAutoRg();

    inline T* operator= (T*);
    inline void Clear(void);

private:    // Never-to-use
    inline CAutoRg& operator= (CAutoRg&);
    CAutoRg(CAutoRg&);
};

////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline CAutoRg<T>::CAutoRg(T *pt)
//
//  parameters:
//
//  description:
//        Create a CAutoRg giving the array of objects pointed to by pt, Allows NULL to be passed in.
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline CAutoRg<T>::CAutoRg(T *pt)
  : CAutoBase<T>(pt)
{
}

//--------------------------------------------------------------------
// @mfunc Destructor.  When an object of class CAutoRg goes out
// of scope, free the associated object (if any).
// @side calls the vector delete method
// @rdesc None
//

////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline CAutoRg<T>::~CAutoRg()
//
//  parameters:
//
//  description:
//        Destructor.  When an object of class CAutoRg goes out of scope, free the associated
//        object (if any).
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline CAutoRg<T>::~CAutoRg()
{
    delete [] m_pt;

    DBG_CODE(m_pt = NULL);
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline void CAutoRg<T>::Clear(void)
//
//  parameters:
//
//  description:
//        Clear
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline void CAutoRg<T>::Clear(void)
{
    if (m_pt)
    {
        delete [] m_pt;
        m_pt = NULL;
    }
}


//--------------------------------------------------------------------
// @mfunc Assigns to variable after construction.  May be dangerous
//        so it ASSERT's that the variable is NULL
// @side None
// @rdesc None
//
// @ex Assign CAutoRg variable after construction. |
//
//        CAutoRg<lt>char<gt>    rgb;
//        /* ... */
//        rgb(NewG char[100]);
//

template <class T>
inline T* CAutoRg<T>::operator=(T* pt)
{
    return ((CAutoBase<T> *) this)->operator= (pt);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//  CAutoRg - autopointers to scalars
//
//    This is analagous to CAutoRg but calls scalar delete of an object rather than arrays.
////////////////////////////////////////////////////////////////////////////////////////////////////

template <class T>
class CAutoP : public CAutoBase<T>
{
public:
    inline CAutoP();
    inline CAutoP(T *pt);
    inline ~CAutoP();
    inline T* operator= (T*);
    inline T* operator->(void);
    inline void Clear(void);

private:    // Never-to-use
    inline CAutoP& operator= (CAutoP&);
    CAutoP(CAutoP&);
};


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline CAutoP<T>::CAutoP(T *pt)
//
//  parameters:
//
//  description:
//        Create a CAutoP giving the object pointed to by pt, allows NULL to be passed in
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline CAutoP<T>::CAutoP(T *pt)
: CAutoBase<T>(pt)
{
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline CAutoP<T>::CAutoP()
//
//  parameters:
//
//  description:
//        Create an empty CAutoP
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline CAutoP<T>::CAutoP()
: CAutoBase<T>(NULL)
{
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline CAutoP<T>::~CAutoP()
//
//  parameters:
//
//  description:
//        Destructor, delete the object pointed by CAutoP variable if any.
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline CAutoP<T>::~CAutoP()
{
    delete m_pt;
    DBG_CODE(m_pt = NULL);
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline void CAutoP<T>::Clear(void)
//
//  parameters:
//
//  description:
//        Clear, delete the object pointed by CAutoP variable if any.
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline void CAutoP<T>::Clear(void)
{
    if (m_pt)
    {    
        delete m_pt;
        m_pt = NULL;
    }
}



////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline T* CAutoP<T>::operator=(T* pt)
//
//  parameters:
//
//  description:
//        Assigns to variable after construction.  May be dangerous so it ASSERT's that the
//        variable is NULL. Assign operator is not inherited, so it has to be written again.
//        Just calls base class assignment.
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline T* CAutoP<T>::operator=(T* pt)
{
    return ((CAutoBase<T> *) this)->operator= (pt);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: inline T * CAutoP<T>::operator->()
//
//  parameters:
//
//  description:
//        The 'follow' operator on the CAutoP allows an CAutoP variable to act like a pointer of type T.
//        This overloading generally makes using a CAutoP simple as using a regular T pointer.
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
inline T * CAutoP<T>::operator->()
{
    ASSERT(m_pt != NULL);
    return m_pt;
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//  CAutoRefc - autopointers to refcounted objects
//
//    Pointer to reference counted objects.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -