home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / AutoPtr.h next >
C/C++ Source or Header  |  2005-11-14  |  884b  |  65 lines

  1. #ifndef __AUTOPTR_H__
  2. #define __AUTOPTR_H__
  3.  
  4.  
  5. // this class is NOT safe for array new's.  It will not properly call
  6. // the destructor for each element and you will silently leak memory.
  7. // it does work for classes requiring no destructor however(base types)
  8. template<typename type>
  9. class idAutoPtr
  10. {
  11. public:
  12.     explicit idAutoPtr(type *ptr = 0)
  13.         : mPtr(ptr)
  14.         {
  15.         }
  16.  
  17.     ~idAutoPtr()
  18.     {
  19.         delete mPtr;
  20.     }
  21.  
  22.     type &operator*() const
  23.         {
  24.         return *mPtr;
  25.         }
  26.  
  27.     type *operator->() const
  28.     {
  29.         return &**this;
  30.     }
  31.  
  32.     type *get() const
  33.     {
  34.         return mPtr;
  35.     }
  36.  
  37.     type *release()
  38.     {
  39.         type *ptr = mPtr;
  40.         mPtr = NULL;
  41.         return ptr;
  42.         }
  43.  
  44.     void reset(type *ptr = NULL)
  45.     {
  46.         if (ptr != mPtr)
  47.             delete mPtr;
  48.         mPtr = ptr;
  49.     }
  50.  
  51.     operator type*()
  52.     {
  53.         return get();
  54.     }
  55.  
  56. private:
  57.     // disallow copies
  58.     idAutoPtr<type> &operator=(idAutoPtr<type>& ptr);
  59.     idAutoPtr(idAutoPtr<type>& ptr);
  60.  
  61.     type *mPtr;
  62. };
  63.  
  64. #endif
  65.