home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / bbxxk / iref.h__ / IREF.H
Encoding:
C/C++ Source or Header  |  1992-10-26  |  1.9 KB  |  76 lines

  1. /* Copyright (c) IBM Corp. 1992 */
  2. #ifndef _IREF_H
  3. #define _IREF_H
  4.  
  5. #include <iglobals.h>
  6. #include <istdops.h>
  7.  
  8. template < class Element >
  9. class IRef {
  10. protected:
  11.   Element *ivPtr;
  12. public:
  13.   IRef () : ivPtr (0) {}
  14.   IRef (Element *ptr) : ivPtr (ptr) {}
  15.  
  16.   operator Element&       ()       { return *ivPtr; }
  17.   operator Element const& () const { return *ivPtr; }
  18.  
  19.   Element*       operator & ()       { return ivPtr; }
  20.   Element const* operator & () const { return ivPtr; }
  21.  
  22.   friend inline Element&       elementForOps (IRef < Element > &     ref)
  23.   { return *ref.ivPtr; }
  24.   friend inline Element const& elementForOps (IRef < Element > const&ref)
  25.   { return *ref.ivPtr; }
  26. };
  27.  
  28. template < class Element >
  29. class IMgRef {
  30. protected:
  31.   struct PtrRc {
  32.     Element *ivPtr;
  33.     unsigned int ivRc;
  34.     PtrRc (Element *ptr) : ivPtr (ptr), ivRc (1) {}
  35.     ~PtrRc () { delete ivPtr; }
  36.   } *ivPtrRc;
  37. public:
  38.  
  39.   IMgRef () : ivPtrRc (0) {}
  40.  
  41.   IMgRef (Element *ptr) {
  42.     ivPtrRc = new PtrRc (ptr);
  43.   }
  44.  
  45.   IMgRef (Element const& e) {
  46.     ivPtrRc = new PtrRc (new Element (e));
  47.   }
  48.  
  49.   IMgRef (IMgRef < Element > const& ref) {
  50.     ivPtrRc = ref.ivPtrRc;
  51.     ivPtrRc->ivRc++;
  52.   }
  53.  
  54.   ~IMgRef () {
  55.     if (ivPtrRc && --ivPtrRc->ivRc == 0) delete ivPtrRc;
  56.   }
  57.  
  58.   IMgRef < Element >& operator= (IMgRef < Element > const& ref) {
  59.     if (ivPtrRc == ref.ivPtrRc) return *this;
  60.     if (ivPtrRc && --ivPtrRc->ivRc == 0) delete ivPtrRc;
  61.     ivPtrRc = ref.ivPtrRc;
  62.     ivPtrRc->ivRc++;
  63.     return *this;
  64.   }
  65.  
  66.   operator Element&       ()       { return *ivPtrRc->ivPtr; }
  67.   operator Element const& () const { return *ivPtrRc->ivPtr; }
  68.  
  69.   friend inline Element&       elementForOps (IMgRef < Element > &     ref)
  70.   { return *ref.ivPtrRc->ivPtr; }
  71.   friend inline Element const& elementForOps (IMgRef < Element > const&ref)
  72.   { return *ref.ivPtrRc->ivPtr; }
  73. };
  74.  
  75. #endif
  76.