home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / include / irefcnt.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  3.5 KB  |  126 lines

  1. #ifndef _IREFCNT_
  2. #define _IREFCNT_
  3. /*******************************************************************************
  4. * FILE NAME: irefcnt.hpp                                                       *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   Declaration of the classes:                                                *
  8. *     IRefCounted                                                              *
  9. *     IReference                                                               *
  10. *                                                                              *
  11. * COPYRIGHT:                                                                   *
  12. *   IBM Open Class Library                                                     *
  13. *   (C) Copyright International Business Machines Corporation 1992, 1996       *
  14. *   Licensed Material - Program-Property of IBM - All Rights Reserved.         *
  15. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  16. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  17. *                                                                              *
  18. *******************************************************************************/
  19. #include <ivbase.hpp>
  20. #include <iexcept.hpp>
  21.  
  22. #include <icconst.h>
  23.  
  24. #pragma pack(4)
  25.  
  26. class IRefCounted : public IVBase {
  27. typedef IVBase
  28.   Inherited;
  29. public:
  30. /*--------------------------- Reference Counting -----------------------------*/
  31. virtual void
  32.   addRef    ( ),
  33.   removeRef ( );
  34.  
  35. unsigned
  36.   useCount ( ) const
  37.     {
  38.     return this->refCount;
  39.     }
  40.  
  41. protected:
  42. /*------------------------------ Constructors --------------------------------*/
  43.   IRefCounted ( );
  44.  
  45.   ~IRefCounted ( );
  46.  
  47. private:
  48. /*--------------------------------- Private -----------------------------------*/
  49. unsigned
  50.   refCount;
  51. }; // class IRefCounted
  52.  
  53. template < class T >
  54. class IReference : public IBase {
  55. public:
  56. /*------------------------------ Constructors --------------------------------*/
  57.   IReference ( T *p = 0 )
  58.     : referent( p )
  59.     {
  60.     }
  61.  
  62.   IReference ( const IReference<T> &source )
  63.     : referent( source.referent )
  64.     {
  65.     if ( source.referent )
  66.       source.referent -> addRef();
  67.     }
  68.  
  69.   ~IReference ( )
  70.     {
  71.     if ( referent )
  72.       referent->removeRef();
  73.     }
  74.  
  75. IReference<T>
  76.  &operator = ( const IReference<T> &source )
  77.   {
  78.   if ( source.referent )
  79.     source.referent -> addRef();
  80.   if ( referent )
  81.     referent -> removeRef();
  82.   referent = source.referent;
  83.   return *this;
  84.   }
  85. IReference<T>
  86.  &operator = ( T *p )
  87.   {
  88.   if ( p )
  89.     p -> addRef();
  90.   if ( referent )
  91.     referent -> removeRef();
  92.   referent = p;
  93.   return *this;
  94.   }
  95.  
  96. /*-------------------------------- Operators ---------------------------------*/
  97. T
  98.  *operator -> ( ) const
  99.    {
  100.    if ( !referent )
  101.      ITHROWLIBRARYERROR(IC_NULL_IREFERENCE, IBaseErrorInfo::invalidRequest, IException::recoverable);
  102.    return referent;
  103.    }
  104. T
  105.  &operator *  ( ) const
  106.    {
  107.    if ( !referent )
  108.      ITHROWLIBRARYERROR(IC_NULL_IREFERENCE, IBaseErrorInfo::invalidRequest, IException::recoverable);
  109.    return *referent;
  110.    }
  111.  
  112.  operator T* ( ) const
  113.  {
  114.    return (referent);
  115.  }
  116. private:
  117. /*--------------------------------- Private -----------------------------------*/
  118. T
  119.  *referent;
  120.  
  121. }; // template class IReference
  122.  
  123. #pragma pack()
  124.  
  125. #endif /* _IREFCNT_ */
  126.