home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / REFCNT.H < prev    next >
C/C++ Source or Header  |  1996-01-05  |  7KB  |  226 lines

  1. /****************************************************************************
  2.     $Id: refcnt.h 501.0 1995/03/07 12:26:46 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.     Declarations of smart pointer classes. 'Smart' pointers can be used to
  10.     perform automatic memory management. They are class objects that mimic
  11.     pointer semantics, while keeping track of the pointed to object.
  12.  
  13.     In general, they cannot reclaim storage in circular data structures.
  14.  
  15.     $Log: refcnt.h $
  16.     Revision 501.0  1995/03/07 12:26:46  RON
  17.     Updated for TLX 5.01
  18.     Revision 1.6  1995/01/31 16:29:24  RON
  19.     Update for release 012
  20.     Added partial support for SunPro C++ compiler
  21.     Revision 1.5  1994/11/16  15:22:48  ron
  22.     Changed TLRefPtr from private to public
  23.  
  24.     Revision 1.4  1994/09/27  20:25:28  ron
  25.     Changed path separator from / to \
  26.  
  27.     Revision 1.3  1994/09/26  15:20:28  ron
  28.     Moved inline functions to the bottom of the file
  29.     Removed handle/safe pointer classes
  30.     Renamed TLPointer<T> to TLCountedPtr<T>
  31.  
  32.     Revision 1.2  1994/09/06  14:01:31  ron
  33.     Changed TLRef to TLRefCount
  34.  
  35.     Revision 1.1  1994/08/16  18:06:50  ron
  36.     Initial revision
  37.  
  38. ****************************************************************************/
  39.  
  40. #ifndef _TLX_REFCNT_H
  41. #define _TLX_REFCNT_H
  42.  
  43. #ifndef _TLX_TLX_H
  44. #include <tlx\501\tlx.h>
  45. #endif
  46.  
  47. /*---------------------------------------------------------------------------
  48.     TLRefCount -
  49.  
  50.     Base class for all classes that must be managed by smart pointers.
  51.     It holds a reference count that is used by the smart pointers to
  52.     regulate memory management.
  53. ---------------------------------------------------------------------------*/
  54.  
  55. class _TLXCLASS TLRefCount
  56. {
  57.     friend class _TLXCLASS TLRefPtr;    // Uses reference count
  58.  
  59.     int            mRefCnt;    // The reference count
  60.  
  61. public:
  62.     // Constructors and destructor. Constructors make sure that the
  63.     // reference count is initialized to 0; destructor is made virtual
  64.     // to provide for derivation.
  65.  
  66.     TLRefCount();
  67.     TLRefCount(const TLRefCount &);
  68.     virtual ~TLRefCount();
  69.  
  70.     // Assignment operator must leave reference count alone
  71.  
  72.     TLRefCount &    operator =(const TLRefCount &);
  73.  
  74.     // Utility functions
  75.  
  76.     bool         IsProtected() const;
  77.     void        Protect();
  78.     void        Unprotect();
  79.     int            RefCount() const;
  80. };
  81.  
  82. /*---------------------------------------------------------------------------
  83.     TLRefPtr -
  84.  
  85.     Smart pointer to TLRefCount instances, destroys a pointed to object when
  86.     the pointer is detached from it and there are no other references
  87.     to the object.
  88. ---------------------------------------------------------------------------*/
  89.  
  90. class _TLXCLASS TLRefPtr
  91. {
  92.     friend class _TLXCLASS TLRefHnd;
  93.  
  94.     TLRefCount *    mPtr;        // Actual pointer
  95.  
  96. public:
  97.     // Constructors set up a possible link to the pointed to object;
  98.     // destructor deletes the object if this pointer was the last one
  99.     // pointing to it. One of the constructors provides for conversion
  100.     // from ordinary TLRefCount pointers.
  101.     //
  102.     // The destructor is NOT made virtual so as to keep the size of
  103.     // a TLRefPtr instance the same as an ordinary pointer. For the same
  104.     // reason, there are no other virtual functions either.
  105.  
  106.     TLRefPtr();
  107.     TLRefPtr(TLRefCount *);
  108.     TLRefPtr(const TLRefPtr &);
  109.     ~TLRefPtr();
  110.  
  111.     // Assignment operators manage reference counts as well, and delete
  112.     // the object previously pointed to if this pointer was the last
  113.     // pointer to it. To mimick ordinary pointers, both assignment operators
  114.     // return a pointer to the pointed to object, rather than a reference
  115.     // to the smart pointer.
  116.  
  117.     TLRefPtr &         operator =(TLRefCount *);
  118.     TLRefPtr &         operator =(const TLRefPtr &);
  119.  
  120.     // Operators to mimick normal pointer behavior. They enable the
  121.     // user to write expressions with TLRefPtr instances as if they
  122.     // were genuine pointers, and to convert from smart pointers
  123.     // to ordinary TLRefCount pointers.
  124.  
  125.             operator TLRefCount *() const;
  126.     TLRefCount *    operator ->() const;
  127.     TLRefCount &    operator *() const;
  128.     int            operator !() const;
  129.  
  130.     // Helper functions available to general public. They allow benign
  131.     // detachment of the smart pointer from the pointed to object, or
  132.     // delete the pointed to object through the smart pointer.
  133.  
  134.     TLRefCount *    Detach();
  135.     void        Delete();
  136.     TLRefCount *    GetRef() const;
  137.  
  138. protected:
  139.     // Helper function only for derived classes. It attaches a smart
  140.     // pointer to an object for management.
  141.  
  142.     TLRefCount *    Attach(TLRefCount *);
  143. };
  144.  
  145. /*---------------------------------------------------------------------------
  146.     TLCountedPtr<T> -
  147.  
  148.     Template derivation of TLRefPtr to manage objects of classes derived
  149.     from class TLRefCount.
  150. ---------------------------------------------------------------------------*/
  151.  
  152. template<class T> class TLCountedPtr: public TLRefPtr
  153. {
  154. public:
  155.     // Declarations for the constructors we cannot leave to the compiler.
  156.  
  157.     TLCountedPtr();
  158.     TLCountedPtr(T *);
  159.  
  160.     // Type-safe overloadings of TLRefPtr functions. They hide the functions
  161.     // inherited from class TLRefPtr.
  162.  
  163.     TLCountedPtr<T> &    operator =(T *);
  164.     TLCountedPtr<T> &    operator =(const TLCountedPtr<T> &);
  165.             operator T *() const;
  166.     T *            operator ->() const;
  167.     T &            operator *() const;
  168.     T *            Detach();
  169.     T *            GetRef() const;
  170. };
  171.  
  172. /*---------------------------------------------------------------------------
  173.     Inline functions
  174. ---------------------------------------------------------------------------*/
  175.  
  176. //-----    TLRefCount
  177.  
  178. inline TLRefCount &TLRefCount::operator =(const TLRefCount &)
  179.     { return *this; }
  180.  
  181. inline bool TLRefCount::IsProtected() const
  182.     { return mRefCnt > 0; }
  183.  
  184. inline int TLRefCount::RefCount() const
  185.     { return mRefCnt; }
  186.  
  187. //-----    TLRefPtr
  188.  
  189. inline TLRefPtr::operator TLRefCount *() const
  190.     { return mPtr; }
  191.  
  192. inline TLRefCount *TLRefPtr::operator ->() const
  193.     { return mPtr; }
  194.  
  195. inline int TLRefPtr::operator !() const
  196.     { return mPtr == 0; }
  197.  
  198. inline TLRefCount *TLRefPtr::GetRef() const
  199.     { return mPtr; }
  200.  
  201. //-----    TLCountedPtr<T>
  202.  
  203. template<class T> inline TLCountedPtr<T> &TLCountedPtr<T>::operator =(T *aPtr)
  204.     { TLRefPtr::operator =(aPtr); return *this; }
  205.  
  206. template<class T> inline
  207. TLCountedPtr<T> &TLCountedPtr<T>::operator =(const TLCountedPtr<T> &aPtr)
  208.     { TLRefPtr::operator =(aPtr); return *this; }
  209.  
  210. template<class T> inline TLCountedPtr<T>::operator T *() const
  211.     { return (T *) TLRefPtr::operator TLRefCount *(); }
  212.  
  213. template<class T> inline T *TLCountedPtr<T>::operator ->() const
  214.     { return (T *) TLRefPtr::operator ->(); }
  215.  
  216. template<class T> inline T &TLCountedPtr<T>::operator *() const
  217.     { return (T &) TLRefPtr::operator *(); }
  218.  
  219. template<class T> inline T *TLCountedPtr<T>::Detach()
  220.     { return (T *) TLRefPtr::Detach(); }
  221.  
  222. template<class T> inline T *TLCountedPtr<T>::GetRef() const
  223.     { return (T *)TLRefPtr::GetRef(); }
  224.  
  225. #endif    // _TLX_REFCNT_H
  226.