home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / gui / own_ptr.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  2.1 KB  |  114 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. #pragma once
  20.  
  21. template <class T>
  22. class own_ptr
  23.         /*
  24.             ...implements strict ownership model (as |auto_ptr| once did, before it was ruined).
  25.         */
  26.     {
  27.         public:
  28.  
  29.             explicit
  30.             own_ptr( T* ptr = 0 )
  31.                     : _ptr(ptr)
  32.                 {
  33.                     // nothing else to do
  34.                 }
  35.  
  36.             own_ptr( own_ptr<T>& P )
  37.                     : _ptr( P.release() )
  38.                 {
  39.                     // nothing else to do
  40.                 }
  41.  
  42.             ~own_ptr()
  43.                 {
  44.                     delete _ptr;
  45.                 }
  46.  
  47.  
  48.             own_ptr<T>&
  49.             operator=( T* P )
  50.                 {
  51.                     if ( _ptr != P )
  52.                         {
  53.                             delete _ptr;
  54.                             _ptr = P;
  55.                         }
  56.                     return *this;
  57.                 }
  58.  
  59.             own_ptr<T>&
  60.             operator=( own_ptr<T>& P )
  61.                 {
  62.                     return operator=( P.release() );    // (also) handles the self-assignment case
  63.                 }
  64.  
  65.             T&
  66.             operator*() const
  67.                 {
  68.                     // PRECONDITION( _ptr );
  69.  
  70. //#if !defined(__NO_bad_dereference__)
  71. //                    if ( !_ptr )
  72. //                        throw bad_dereference();
  73. //#endif
  74.                     return *_ptr;
  75.                 }
  76.  
  77.             T*
  78.             operator->() const
  79.                 {
  80.                     // PRECONDITION( _ptr );
  81.  
  82. //#if !defined(__NO_bad_dereference__)
  83. //                    if ( !_ptr )
  84. //                        throw bad_dereference();
  85. //#endif
  86.                     return _ptr;
  87.                 }
  88.  
  89.             T*
  90.             get() const
  91.                 {
  92.                     return _ptr;
  93.                 }
  94.  
  95.             T*
  96.             release()
  97.                 {
  98.                     T* P = _ptr;
  99.                     _ptr = 0;
  100.                     return P;
  101.                 }
  102.  
  103.             bool
  104.             is_void() const
  105.                 {
  106.                     return _ptr == 0;
  107.                 }
  108.  
  109.  
  110.  
  111.         private:
  112.             T* _ptr;
  113.     };
  114.