home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / utility / CAutoPtr.h next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  1.5 KB  |  65 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*    CAutoPtr.h    */
  20.  
  21.  
  22. #ifndef CAutoPtr_H
  23. #define CAutoPtr_H
  24. #pragma once
  25.  
  26. template<class T>
  27. class CAutoPtr
  28. {
  29.     public:
  30.         CAutoPtr(T* p = 0) : p_(p) { }
  31.         
  32.         CAutoPtr(const CAutoPtr& r) : p_(r.release()) { }
  33.         
  34.         ~CAutoPtr() { delete p_; }
  35.         
  36.         CAutoPtr& operator = (const CAutoPtr& r) {
  37.             if ((void *)&r != (void *) this) {
  38.                 delete p_;
  39.                 p_ = r.release();
  40.             }
  41.             return *this;
  42.         }
  43.  
  44.         T& operator*(void) const{ return *p_; }
  45.         
  46.         T* operator->(void) const { return p_; }
  47.         
  48.         T* get(void) const { return p_; }
  49.         
  50.         void reset(T* p = 0) {
  51.             delete p_;
  52.             p_ = p;
  53.         }
  54.         
  55.         T* release(void) const { 
  56.             T* old = p_;
  57.             const_cast<CAutoPtr*>(this)->p_ = 0;
  58.             return old; 
  59.         }
  60.  
  61.     private:
  62.         T*    p_;
  63. };
  64.  
  65. #endif