home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Found / FWFoundU / Include / FWCouPtr.h next >
Encoding:
C/C++ Source or Header  |  1994-04-21  |  7.4 KB  |  231 lines  |  [TEXT/MPS ]

  1. #ifndef FWCOUPTR_H
  2. #define FWCOUPTR_H
  3. //========================================================================================
  4. //
  5. //    File:                FWCouPtr.h
  6. //    Release Version:    $ 1.0d1 $
  7. //
  8. //    Creation Date:        3/28/94
  9. //
  10. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  11. //
  12. //========================================================================================
  13.  
  14. #ifndef FWEXCDEF_H
  15. #include "FWExcDef.h"
  16. #endif
  17.  
  18. //========================================================================================
  19. // CLASS FW_TCountedPtr<tRepresentation>
  20. //========================================================================================
  21.  
  22. template <class tRepresentation>
  23. class FW_TCountedPtr : public _FW_CAutoDestructObject
  24. {
  25. public:
  26.     ~FW_TCountedPtr();
  27.         // Destroy the pointer.  
  28.         // Decrements the count in the rep, and deletes the rep if count is zero.
  29.         
  30.     FW_TCountedPtr();
  31.         // Creates a "NULL" pointer.
  32.  
  33.     FW_TCountedPtr(const FW_TCountedPtr<tRepresentation>& other);
  34.         // FW_SPlatformPoint this pointer to the same rep as the other pointer points to.
  35.  
  36.     FW_TCountedPtr(tRepresentation* rep);
  37.         // Creates a pointer pointing at rep.
  38.     
  39.     FW_TCountedPtr<tRepresentation>& operator=(const FW_TCountedPtr<tRepresentation>& other);
  40.         // Assignment, point this ponter to the same rep as the other pointer points to.
  41.         
  42.     FW_TCountedPtr<tRepresentation>& operator=(tRepresentation* rep);
  43.         // Assignment, point this ponter to rep.
  44.         
  45.     tRepresentation* operator->() const;
  46.         // Provide access to members of rep.
  47.         
  48.     tRepresentation& operator*() const;
  49.         // Provide access to rep.
  50.         // This operator should be used only if operator->() is not sufficient.
  51.     
  52.     int operator==(const FW_TCountedPtr<tRepresentation>& other) const;
  53.         // Points to the same representation object?
  54.  
  55.     int operator!=(const FW_TCountedPtr<tRepresentation>& other) const;
  56.         // Points to a different representation object?
  57.  
  58.     operator const void*() const;
  59.         // Use to test if this is a "NULL" pointer.
  60.  
  61. private:
  62.  
  63.     void UpCount();
  64.         // Implementation method, increment the reference count in rep.
  65.         
  66.     void DownCount();
  67.         // Implementation method, decrement the reference count in rep.
  68.         // Deletes rep if reference count is zero.
  69.     
  70.     tRepresentation *fRep;
  71. };
  72.  
  73. //----------------------------------------------------------------------------------------
  74. // FW_TCountedPtr<tRepresentation>::~FW_TCountedPtr
  75. //----------------------------------------------------------------------------------------
  76.  
  77. template <class tRepresentation>
  78. FW_TCountedPtr<tRepresentation>::~FW_TCountedPtr()
  79. {
  80.     FW_START_DESTRUCTOR
  81.     DownCount();
  82. }
  83.  
  84. //----------------------------------------------------------------------------------------
  85. // FW_TCountedPtr<tRepresentation>::FW_TCountedPtr
  86. //----------------------------------------------------------------------------------------
  87.  
  88. template <class tRepresentation>
  89. FW_TCountedPtr<tRepresentation>::FW_TCountedPtr() :
  90.     fRep(0)
  91. {
  92.     FW_END_CONSTRUCTOR
  93. }
  94.  
  95. //----------------------------------------------------------------------------------------
  96. // FW_TCountedPtr<tRepresentation>::FW_TCountedPtr
  97. //----------------------------------------------------------------------------------------
  98.  
  99. template <class tRepresentation>
  100. FW_TCountedPtr<tRepresentation>::FW_TCountedPtr(
  101.                                     const FW_TCountedPtr<tRepresentation>& other) :
  102.     fRep(other.fRep)
  103. {
  104.     UpCount();
  105.     FW_END_CONSTRUCTOR
  106. }
  107.  
  108. //----------------------------------------------------------------------------------------
  109. // FW_TCountedPtr<tRepresentation>::FW_TCountedPtr
  110. //----------------------------------------------------------------------------------------
  111.  
  112. template <class tRepresentation>
  113. FW_TCountedPtr<tRepresentation>::FW_TCountedPtr(tRepresentation* rep) :
  114.     fRep(rep)
  115. {
  116.     UpCount();
  117.     FW_END_CONSTRUCTOR
  118. }
  119.  
  120. //----------------------------------------------------------------------------------------
  121. // FW_TCountedPtr<tRepresentation>::operator=
  122. //----------------------------------------------------------------------------------------
  123.  
  124. template <class tRepresentation>
  125. FW_TCountedPtr<tRepresentation>& 
  126. FW_TCountedPtr<tRepresentation>::operator=(
  127.                                     const FW_TCountedPtr<tRepresentation>& other)
  128. {
  129.     if (fRep != other.fRep)
  130.     {
  131.         DownCount();
  132.         fRep = other.fRep;
  133.         UpCount();
  134.     }
  135.     return *this;
  136. }
  137.  
  138. //----------------------------------------------------------------------------------------
  139. // FW_TCountedPtr<tRepresentation>::operator=
  140. //----------------------------------------------------------------------------------------
  141.  
  142. template <class tRepresentation>
  143. FW_TCountedPtr<tRepresentation>& 
  144. FW_TCountedPtr<tRepresentation>::operator=(tRepresentation* rep)
  145. {
  146.     if (fRep != rep)
  147.     {
  148.         DownCount();
  149.         fRep = rep;
  150.         UpCount();
  151.     }
  152.     return *this;
  153. }
  154.         
  155. //----------------------------------------------------------------------------------------
  156. // FW_TCountedPtr<tRepresentation>::operator->
  157. //----------------------------------------------------------------------------------------
  158.  
  159. template <class tRepresentation>
  160. inline tRepresentation* FW_TCountedPtr<tRepresentation>::operator->() const
  161. {
  162.     return fRep;
  163. }
  164.  
  165. //----------------------------------------------------------------------------------------
  166. // FW_TCountedPtr<tRepresentation>::operator*
  167. //----------------------------------------------------------------------------------------
  168.  
  169. template <class tRepresentation>
  170. inline tRepresentation& FW_TCountedPtr<tRepresentation>::operator*() const
  171. {
  172.     return *fRep;
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. // FW_TCountedPtr<tRepresentation>::UpCount
  177. //----------------------------------------------------------------------------------------
  178.  
  179. template <class tRepresentation>
  180. void FW_TCountedPtr<tRepresentation>::UpCount()
  181. {
  182.     if (fRep)
  183.         fRep->IncrementReferenceCount();
  184. }
  185.  
  186. //----------------------------------------------------------------------------------------
  187. // FW_TCountedPtr<tRepresentation>::DownCount
  188. //----------------------------------------------------------------------------------------
  189.  
  190. template <class tRepresentation>
  191. void FW_TCountedPtr<tRepresentation>::DownCount()
  192. {
  193.     if (fRep && !fRep->DecrementReferenceCount())
  194.         delete fRep;
  195.     // It is not necessary to set fRep to 0. To see why, see how DownCount is used above.
  196.     // In all cases, fRep is either immediately reset, or the instance is being destroyed.
  197.     // Note too that DownCount is private, so the usages above all all possible usages.
  198. }
  199.  
  200. //----------------------------------------------------------------------------------------
  201. // FW_TCountedPtr<tRepresentation>::operator==
  202. //----------------------------------------------------------------------------------------
  203.  
  204. template <class tRepresentation>
  205. inline int FW_TCountedPtr<tRepresentation>::operator==(const FW_TCountedPtr<tRepresentation>& other) const
  206. {
  207.     return (fRep == other.fRep);
  208. }
  209.  
  210. //----------------------------------------------------------------------------------------
  211. // FW_TCountedPtr<tRepresentation>::operator!=
  212. //----------------------------------------------------------------------------------------
  213.  
  214. template <class tRepresentation>
  215. inline int FW_TCountedPtr<tRepresentation>::operator!=(const FW_TCountedPtr<tRepresentation>& other) const
  216. {
  217.     return !operator==(other);
  218. }
  219.  
  220. //----------------------------------------------------------------------------------------
  221. // FW_TCountedPtr<tRepresentation>::operator const void*
  222. //----------------------------------------------------------------------------------------
  223.  
  224. template <class tRepresentation>
  225. inline FW_TCountedPtr<tRepresentation>::operator const void*() const
  226. {
  227.     return (const void*) fRep;
  228. }
  229.  
  230. #endif
  231.