home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / SRC / REFPTR.CPP < prev    next >
C/C++ Source or Header  |  1996-07-08  |  6KB  |  190 lines

  1. /****************************************************************************
  2.     $Id: refptr.cpp 501.0 1995/03/07 12:26:20 RON Exp $
  3.  
  4.     Copyright (c) 1991-95 Tarma Software Research. All rights reserved.
  5.  
  6.     Project:    Tarma Library for C++ V5.0
  7.     Author:    Ron van der Wal
  8.  
  9.     Implementation of class TLRefPtr.
  10.  
  11.     $Log: refptr.cpp $
  12.     Revision 501.0  1995/03/07 12:26:20  RON
  13.     Updated for TLX 5.01
  14.     Revision 1.8  1995/01/31 16:30:22  RON
  15.     Update for release 012
  16.     Added partial support for SunPro C++ compiler
  17.     Revision 1.7  1995/01/06  15:58:16  ron
  18.     Corrected Revision keyword
  19.  
  20.     Revision 1.6  1994/11/16  15:42:32  ron
  21.     Added module info; rearranged #include directives
  22.  
  23.     Revision 1.5  1994/09/28  14:20:55  ron
  24.     Removed Macintosh-style #include references
  25.  
  26.     Revision 1.4  1994/09/27  20:22:53  ron
  27.     Changed path separator from / to \
  28.  
  29.     Revision 1.3  1994/09/26  15:46:22  ron
  30.     Changed include file references
  31.  
  32.     Revision 1.2  1994/09/06  14:11:35  ron
  33.     Adapted to change from TLRef to TLRefCount
  34.  
  35.     Revision 1.1  1994/08/16  18:13:11  ron
  36.     Initial revision
  37.  
  38. ****************************************************************************/
  39.  
  40. #include <tlx\501\_build.h>
  41.  
  42. TLX_MODULE_INFO("$Revision: 501.0 $");
  43.  
  44. #include <tlx\501\except.h>        // Exception handling
  45. #include <tlx\501\refcnt.h>        // Class declaration
  46.  
  47. /*-------------------------------------------------------------------------*/
  48.     TLRefPtr::TLRefPtr()
  49.  
  50. /*  Default constructor. Initializes smart pointer to 0.
  51. ---------------------------------------------------------------------------*/
  52. : mPtr(0)
  53. {
  54. }
  55.  
  56. /*-------------------------------------------------------------------------*/
  57.     TLRefPtr::TLRefPtr(TLRefCount *aPtr)
  58.  
  59. /*  Constructor that provides for conversion from ordinary TLRefCount pointers.
  60.     It attaches the smart pointer to the pointed to object.
  61. ---------------------------------------------------------------------------*/
  62. : mPtr(0)
  63. {
  64.     Attach(aPtr);
  65.     TLX_ASSERT(aPtr == mPtr);
  66. }
  67.  
  68. /*-------------------------------------------------------------------------*/
  69.     TLRefPtr::TLRefPtr(const TLRefPtr &aPtr)
  70.  
  71. /*  Copy constructor. Attaches the smart pointer to the same object as the
  72.     other smart pointer points to.
  73. ---------------------------------------------------------------------------*/
  74. : mPtr(0)
  75. {
  76.     Attach(aPtr.mPtr);
  77.     TLX_ASSERT(aPtr.mPtr == mPtr);
  78. }
  79.  
  80. /*-------------------------------------------------------------------------*/
  81.     TLRefPtr::~TLRefPtr()
  82.  
  83. /*  Destructor. Calls Delete(), which has the effect of detaching the
  84.     smart pointer from the pointed to object, and deleting the pointed
  85.     to object if that was the last reference to it.
  86. ---------------------------------------------------------------------------*/
  87. {
  88.     Delete();
  89. }
  90.  
  91. /*-------------------------------------------------------------------------*/
  92.     TLRefCount *TLRefPtr::Attach(TLRefCount *aPtr)
  93.  
  94. /*  Attaches the smart pointer to an object to be managed. If the object
  95.     exists (i.e. if the pointer is non-0), its reference count is
  96.     incremented.
  97.  
  98.     This function should only be called if the smart pointer is not
  99.     currently attached to an object.
  100. ---------------------------------------------------------------------------*/
  101. {
  102.     TLX_ASSERT_NULL(mPtr);
  103.  
  104.     if ((mPtr = aPtr) != 0)
  105.     mPtr->Protect();
  106.  
  107.     return mPtr;
  108. }
  109.  
  110. /*-------------------------------------------------------------------------*/
  111.     TLRefCount *TLRefPtr::Detach()
  112.  
  113. /*  Detaches the smart pointer from an object it is associated with. The
  114.     pointed to object is not destroyed, not even if we just removed the
  115.     last reference.
  116. ---------------------------------------------------------------------------*/
  117. {
  118.     if (mPtr)
  119.     mPtr->Unprotect();
  120.  
  121.     TLRefCount *tmp = mPtr;
  122.     mPtr = 0;
  123.     return tmp;
  124. }
  125.  
  126. /*-------------------------------------------------------------------------*/
  127.     void TLRefPtr::Delete()
  128.  
  129. /*  Detaches the smart pointer from an object it is associated with, then
  130.     delete the object previously pointed to if we were the last pointer to
  131.     it.
  132. ---------------------------------------------------------------------------*/
  133. {
  134.     if (mPtr)
  135.     {
  136.     mPtr->Unprotect();
  137.     if (!mPtr->IsProtected())
  138.         delete mPtr;
  139.         mPtr = 0;
  140.     }
  141. }
  142.  
  143. /*-------------------------------------------------------------------------*/
  144.     TLRefPtr &TLRefPtr::operator =(TLRefCount *aPtr)
  145.  
  146. /*  Overloading of assignment operator to allow assignment of ordinary
  147.     TLRefCount pointers to the smart pointer class. If the smart pointer was
  148.     pointing to another object before the assignment, that attachment
  149.     is broken (possibly deleting the old object in the process).
  150. ---------------------------------------------------------------------------*/
  151. {
  152.     if (aPtr != mPtr)
  153.     {
  154.     Delete();
  155.     Attach(aPtr);
  156.     }
  157.     return *this;
  158. }
  159.  
  160. /*-------------------------------------------------------------------------*/
  161.     TLRefPtr &TLRefPtr::operator =(const TLRefPtr &aPtr)
  162.  
  163. /*  Overloading of assignment operator to handle assignment of smart
  164.     pointers to each other. If the current smart pointer was pointing to
  165.     another object before the assignment, that attachment is broken
  166.     (possibly deleting the old object in the process).
  167. ---------------------------------------------------------------------------*/
  168. {
  169.     if (mPtr != aPtr.mPtr)
  170.     {
  171.     Delete();
  172.     Attach(aPtr.mPtr);
  173.     }
  174.     return *this;
  175. }
  176.  
  177. /*-------------------------------------------------------------------------*/
  178.     TLRefCount &TLRefPtr::operator *() const
  179.  
  180. /*  Overloading of pointer dereference operator, in order to make it
  181.     possible to use TLRefPtr instances as if they were ordinary pointers.
  182. ---------------------------------------------------------------------------*/
  183. {
  184.     if (!mPtr)
  185.     THROW(TLXNullPtr(LOCUS));
  186.  
  187.     return *mPtr;
  188. }
  189.  
  190.