home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWFoundU / Sources / FWCouPtr.cpp next >
Encoding:
Text File  |  1995-11-08  |  4.4 KB  |  144 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWCouPtr.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    (c) 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef FWCOUPTR_H
  13. #include "FWCouPtr.h"
  14. #endif
  15.  
  16. #if FW_LIB_EXPORT_PRAGMAS
  17. #pragma lib_export on
  18. #endif
  19.  
  20. //========================================================================================
  21. // CLASS FW_CCountedPtr
  22. //========================================================================================
  23.  
  24. //----------------------------------------------------------------------------------------
  25. // FW_CCountedPtr::FW_CCountedPtr
  26. //----------------------------------------------------------------------------------------
  27.  
  28. FW_CCountedPtr::FW_CCountedPtr() :
  29.     fRep(NULL)
  30. {
  31.     FW_END_CONSTRUCTOR
  32. }
  33.  
  34. //----------------------------------------------------------------------------------------
  35. // FW_CCountedPtr::~FW_CCountedPtr
  36. //----------------------------------------------------------------------------------------
  37.  
  38. FW_CCountedPtr::~FW_CCountedPtr()
  39. {
  40.     FW_START_DESTRUCTOR
  41.     DownCount();
  42. }
  43.  
  44. //----------------------------------------------------------------------------------------
  45. // FW_CCountedPtr::FW_CCountedPtr
  46. //----------------------------------------------------------------------------------------
  47.  
  48. FW_CCountedPtr::FW_CCountedPtr(const FW_CCountedPtr& other) :
  49.     fRep(other.fRep)
  50. {
  51.     UpCount();
  52.     FW_END_CONSTRUCTOR
  53. }
  54.  
  55. //----------------------------------------------------------------------------------------
  56. // FW_CCountedPtr::FW_CCountedPtr
  57. //----------------------------------------------------------------------------------------
  58.  
  59. FW_CCountedPtr::FW_CCountedPtr(FW_CCountedPtrRep* rep) :
  60.     fRep(rep)
  61. {
  62.     UpCount();
  63.     FW_END_CONSTRUCTOR
  64. }
  65.  
  66. //----------------------------------------------------------------------------------------
  67. // FW_CCountedPtr::operator=
  68. //----------------------------------------------------------------------------------------
  69.  
  70. FW_CCountedPtr& FW_CCountedPtr::operator=(const FW_CCountedPtr& other)
  71. {
  72.     SetRep(other.fRep);
  73.     return *this;
  74. }
  75.  
  76. //----------------------------------------------------------------------------------------
  77. // FW_CCountedPtr::operator=
  78. //----------------------------------------------------------------------------------------
  79.  
  80. FW_CCountedPtr& FW_CCountedPtr::operator=(FW_CCountedPtrRep* rep)
  81. {
  82.     SetRep(rep);
  83.     return *this;
  84. }
  85.  
  86. //----------------------------------------------------------------------------------------
  87. // FW_CCountedPtr::SetRep
  88. //----------------------------------------------------------------------------------------
  89.  
  90. void FW_CCountedPtr::SetRep(FW_CCountedPtrRep* rep)
  91. {
  92.     if (fRep != rep)
  93.     {
  94.         DownCount();
  95.         fRep = rep;
  96.         UpCount();
  97.     }
  98. }
  99.         
  100. //----------------------------------------------------------------------------------------
  101. // FW_CCountedPtr::UpCount
  102. //----------------------------------------------------------------------------------------
  103.  
  104. void FW_CCountedPtr::UpCount()
  105. {
  106.     if (fRep)
  107.         fRep->IncrementReferenceCount();
  108. }
  109.  
  110. //----------------------------------------------------------------------------------------
  111. // FW_CCountedPtr::DownCount
  112. //----------------------------------------------------------------------------------------
  113.  
  114. void FW_CCountedPtr::DownCount()
  115. {
  116.     if (fRep && !fRep->DecrementReferenceCount())
  117.         delete fRep;
  118.     // It is not necessary to set fRep to 0. To see why, see how DownCount is used above.
  119.     // In all cases, fRep is either immediately reset, or the instance is being destroyed.
  120.     // Note too that DownCount is private, so the usages above all all possible usages.
  121. }
  122.  
  123. //========================================================================================
  124. // CLASS FW_CCountedPtrRep
  125. //========================================================================================
  126.  
  127. //----------------------------------------------------------------------------------------
  128. // FW_CCountedPtrRep::FW_CCountedPtrRep
  129. //----------------------------------------------------------------------------------------
  130.  
  131. FW_CCountedPtrRep::FW_CCountedPtrRep() :
  132.     fRefCount(0)
  133. {
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // FW_CCountedPtrRep::~FW_CCountedPtrRep
  138. //----------------------------------------------------------------------------------------
  139.  
  140. FW_CCountedPtrRep::~FW_CCountedPtrRep()
  141. {
  142. }
  143.  
  144.