autoptr.h
Upload User: caisha3
Upload Date: 2013-09-21
Package Size: 208739k
Code Size: 5k
Category:

Windows Develop

Development Platform:

Visual C++

  1. //+-------------------------------------------------------------------------
  2. //
  3. //  Microsoft Windows
  4. //
  5. //  Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. //  File:       autoptr.h
  8. //
  9. //--------------------------------------------------------------------------
  10. #ifndef _INC_CSCVIEW_AUTOPTR_H
  11. #define _INC_CSCVIEW_AUTOPTR_H
  12. ///////////////////////////////////////////////////////////////////////////////
  13. /*  File: autoptr.h
  14.     Description: Template auto pointer classes to support normal C++ pointers
  15.         as well as shell and COM object pointers.
  16.         This code was created by DavePl for the Entertainment Center project.
  17.         It worked very well so I've "borrowed" it (thanks Dave).  I think his
  18.         original implementation borrowed from the STL implementation.
  19.     Revision History:
  20.     Date        Description                                          Programmer
  21.     --------    ---------------------------------------------------  ----------
  22.     07/01/97    Initial creation.                                    BrianAu
  23. */
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // a_ptr
  26. //
  27. // Safe pointer class that knows to delete the referrent object when
  28. // the pointer goes out of scope or is replaced, etc.
  29. template<class _TYPE> class a_ptr 
  30. {
  31. public:
  32.     typedef _TYPE element_type;
  33.     
  34.     a_ptr(_TYPE *_P = 0) throw()
  35.             : _Owns(_P != 0), _Ptr(_P) 
  36.     {}
  37.     
  38.     typedef _TYPE _U;
  39.     
  40.     a_ptr(const a_ptr<_U>& _Y) throw()
  41.         : _Owns(_Y._Owns), _Ptr((_TYPE *)_Y.disown()) 
  42.     {}
  43.     virtual void nukeit() throw() = 0
  44.     {
  45.     }
  46.     a_ptr<_TYPE>& operator=(const a_ptr<_U>& _Y) throw()
  47.     {
  48.         if ((void *)this != (void *)&_Y)
  49.         {
  50.             if (_Owns)
  51.                 nukeit();
  52.             _Owns = _Y._Owns;
  53.             _Ptr = (_TYPE *)_Y.disown();
  54. //            ASSERT( !_Owns || _Ptr );
  55.         }
  56.         return (*this); 
  57.     }
  58.     a_ptr<_TYPE>& replace(const a_ptr<_U>& _Y) throw()
  59.     {
  60.         return *this = _Y;
  61.     }
  62.     virtual ~a_ptr() throw()
  63.     {
  64.     }
  65.     
  66.     operator _TYPE*() throw()
  67.     { 
  68.         return get(); 
  69.     }
  70.     operator const _TYPE*() const throw()
  71.     { 
  72.         return get(); 
  73.     }
  74.             
  75.     _TYPE& operator*() const throw()
  76.     {
  77.         return (*get()); 
  78.     }
  79.     _TYPE *get() const throw()
  80.     {
  81.         return (_Ptr); 
  82.     }
  83.     _TYPE *disown() const throw()
  84.     {
  85.         ((a_ptr<_TYPE> *)this)->_Owns = FALSE;
  86.         return (_Ptr); 
  87.     }
  88.     _TYPE ** getaddr() throw()
  89.     { 
  90.         *this = (_TYPE *) NULL;
  91.         _Owns = TRUE;
  92.         return (&_Ptr); 
  93.     }
  94. protected:
  95.     BOOL _Owns;
  96.     _TYPE *_Ptr;
  97. };
  98. // autoptr
  99. //
  100. template<class _TYPE>
  101. class autoptr : public a_ptr<_TYPE>
  102. {
  103.     virtual void nukeit() throw()
  104.     {
  105.         delete _Ptr;
  106.     }
  107. public:
  108.     ~autoptr() throw()
  109.     {
  110.         if (_Owns)
  111.             this->nukeit();
  112.     }
  113.     autoptr(_TYPE *_P = 0) throw()
  114.         : a_ptr<_TYPE>(_P)
  115.     {
  116.     }
  117.     _TYPE *operator->() const throw()
  118.     {
  119.         return (get()); 
  120.     }
  121. };
  122. template<class _TYPE>
  123. class array_autoptr : public a_ptr<_TYPE>
  124. {
  125.     virtual void nukeit() throw()
  126.     {
  127.         if (_Ptr)
  128.             delete[] _Ptr;
  129.     }
  130. public:
  131.     ~array_autoptr() throw()
  132.     {
  133.         if (_Owns)
  134.             this->nukeit();
  135.     }
  136.     array_autoptr(_TYPE *_P = 0) throw()
  137.         : a_ptr<_TYPE>(_P)
  138.     {
  139.     }
  140. };
  141. // sh_autoptr
  142. //
  143. // Smart pointer that manually runs the referent's destructor and then
  144. // calls the shell's task allocator to free the object's memory footprint
  145. template<class _TYPE>
  146. class sh_autoptr : virtual public a_ptr<_TYPE>
  147. {
  148.     virtual void nukeit() throw()
  149.     {
  150.         if (_Ptr)
  151.         {
  152.             IMalloc *pMalloc;
  153.             _Ptr->~_TYPE();
  154.             if (SUCCEEDED(SHGetMalloc(&pMalloc)))
  155.             {
  156.                 pMalloc->Free(_Ptr);
  157.                 pMalloc->Release();
  158.             }
  159.         }
  160.     }
  161. public:
  162.     ~sh_autoptr() throw()
  163.     {
  164.         if (_Owns)
  165.             this->nukeit();
  166.     }
  167.     sh_autoptr(_TYPE *_P = 0) throw()
  168.         : a_ptr<_TYPE>(_P)
  169.     {
  170.     }
  171.     _TYPE *operator->() const throw()
  172.     {
  173.         return (get()); 
  174.     }
  175. };
  176. // com_autoptr (nothing to do with ole automation... its an automatic ole ptr)
  177. //
  178. // Smart pointer that calls disown() on the referent when the pointer itself
  179. // goes out of scope
  180. template<class _TYPE>
  181. class com_autoptr : public a_ptr<_TYPE>
  182. {
  183.     virtual void nukeit() throw()
  184.     {
  185.         if (_Ptr)
  186.             _Ptr->Release();
  187.     }
  188. public:
  189.     ~com_autoptr() throw()
  190.     {
  191.         if (_Owns)
  192.             this->nukeit();
  193.     }
  194.     com_autoptr(_TYPE *_P = 0) throw()
  195.         : a_ptr<_TYPE>(_P)
  196.     {
  197.     }
  198.     _TYPE *operator->() const throw()
  199.     {
  200.         return (get()); 
  201.     }
  202. };
  203. #endif // _INC_CSCVIEW_AUTOPTR_H
  204.