home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / IPTR.H < prev    next >
Text File  |  1993-09-22  |  3KB  |  83 lines

  1. /*******************************************************************************
  2. *                                                                              *
  3. * COPYRIGHT:                                                                   *
  4. *   IBM C/C++ Tools Version 2.01 - Collection Class Library                    *
  5. *   Licensed Materials - Property of IBM                                       *
  6. *   (C) Copyright IBM Corporation 1992, 1993                                   *
  7. *   All Rights Reserved                                                        *
  8. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  9. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  10. *                                                                              *
  11. *******************************************************************************/
  12. #ifndef _IPTR_H
  13. #define _IPTR_H
  14.  
  15. #include <iglobals.h>
  16. #include <istdops.h>
  17.  
  18. template < class Element >
  19. class IPtr {
  20. protected:
  21.   Element *ivPtr;
  22. public:
  23.   IPtr () : ivPtr (0) {}
  24.   IPtr (Element *ptr) : ivPtr (ptr) {}
  25.  
  26.   Element&       operator * () const { return *ivPtr; }
  27.   Element*       operator-> () const { return ivPtr; }
  28.  
  29.   friend inline Element&       elementForOps (IPtr < Element >& ptr)
  30.   { return *ptr.ivPtr; }
  31.   friend inline Element const& elementForOps (IPtr < Element > const& ptr)
  32.   { return *ptr.ivPtr; }
  33. };
  34.  
  35. template < class Element >
  36. class IMgPtr {
  37. protected:
  38.   struct PtrRc
  39.   { Element *ivPtr;
  40.     unsigned int ivRc;
  41.     PtrRc (Element *ptr) : ivPtr (ptr), ivRc (1) {}
  42.     ~PtrRc () { delete ivPtr; }
  43.   } *ivPtrRc;
  44. public:
  45.  
  46.   IMgPtr () : ivPtrRc (0) {}
  47.  
  48.   IMgPtr (Element *ptr)
  49.   { ivPtrRc = new PtrRc (ptr);
  50.   }
  51.  
  52.   IMgPtr (Element const& e)
  53.   { ivPtrRc = new PtrRc (new Element (e));
  54.   }
  55.  
  56.   IMgPtr (IMgPtr < Element > const& ptr)
  57.   { ivPtrRc = ptr.ivPtrRc;
  58.     ivPtrRc->ivRc++;
  59.   }
  60.  
  61.   ~IMgPtr ()
  62.   { if (ivPtrRc && --ivPtrRc->ivRc == 0) delete ivPtrRc;
  63.   }
  64.  
  65.   IMgPtr < Element >& operator= (IMgPtr < Element > const& ptr)
  66.   { if (ivPtrRc == ptr.ivPtrRc) return *this;
  67.     if (ivPtrRc && --ivPtrRc->ivRc == 0) delete ivPtrRc;
  68.     ivPtrRc = ptr.ivPtrRc;
  69.     ivPtrRc->ivRc++;
  70.     return *this;
  71.   }
  72.  
  73.   Element&       operator * () const { return *ivPtrRc->ivPtr; }
  74.   Element*       operator-> () const { return ivPtrRc->ivPtr; }
  75.  
  76.   friend inline Element&       elementForOps (IMgPtr < Element >& ptr)
  77.   { return *ptr.ivPtrRc->ivPtr; }
  78.   friend inline Element const& elementForOps (IMgPtr < Element > const& ptr)
  79.   { return *ptr.ivPtrRc->ivPtr; }
  80. };
  81.  
  82. #endif
  83.