home *** CD-ROM | disk | FTP | other *** search
- #ifndef FWCOUPTR_H
- #define FWCOUPTR_H
- //========================================================================================
- //
- // File: FWCouPtr.h
- // Release Version: $ 1.0d1 $
- //
- // Creation Date: 3/28/94
- //
- // Copyright: © 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef FWEXCDEF_H
- #include "FWExcDef.h"
- #endif
-
- //========================================================================================
- // CLASS FW_TCountedPtr<tRepresentation>
- //========================================================================================
-
- template <class tRepresentation>
- class FW_TCountedPtr : public _FW_CAutoDestructObject
- {
- public:
- ~FW_TCountedPtr();
- // Destroy the pointer.
- // Decrements the count in the rep, and deletes the rep if count is zero.
-
- FW_TCountedPtr();
- // Creates a "NULL" pointer.
-
- FW_TCountedPtr(const FW_TCountedPtr<tRepresentation>& other);
- // FW_SPlatformPoint this pointer to the same rep as the other pointer points to.
-
- FW_TCountedPtr(tRepresentation* rep);
- // Creates a pointer pointing at rep.
-
- FW_TCountedPtr<tRepresentation>& operator=(const FW_TCountedPtr<tRepresentation>& other);
- // Assignment, point this ponter to the same rep as the other pointer points to.
-
- FW_TCountedPtr<tRepresentation>& operator=(tRepresentation* rep);
- // Assignment, point this ponter to rep.
-
- tRepresentation* operator->() const;
- // Provide access to members of rep.
-
- tRepresentation& operator*() const;
- // Provide access to rep.
- // This operator should be used only if operator->() is not sufficient.
-
- int operator==(const FW_TCountedPtr<tRepresentation>& other) const;
- // Points to the same representation object?
-
- int operator!=(const FW_TCountedPtr<tRepresentation>& other) const;
- // Points to a different representation object?
-
- operator const void*() const;
- // Use to test if this is a "NULL" pointer.
-
- private:
-
- void UpCount();
- // Implementation method, increment the reference count in rep.
-
- void DownCount();
- // Implementation method, decrement the reference count in rep.
- // Deletes rep if reference count is zero.
-
- tRepresentation *fRep;
- };
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRepresentation>::~FW_TCountedPtr
- //----------------------------------------------------------------------------------------
-
- template <class tRepresentation>
- FW_TCountedPtr<tRepresentation>::~FW_TCountedPtr()
- {
- FW_START_DESTRUCTOR
- DownCount();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRepresentation>::FW_TCountedPtr
- //----------------------------------------------------------------------------------------
-
- template <class tRepresentation>
- FW_TCountedPtr<tRepresentation>::FW_TCountedPtr() :
- fRep(0)
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRepresentation>::FW_TCountedPtr
- //----------------------------------------------------------------------------------------
-
- template <class tRepresentation>
- FW_TCountedPtr<tRepresentation>::FW_TCountedPtr(
- const FW_TCountedPtr<tRepresentation>& other) :
- fRep(other.fRep)
- {
- UpCount();
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRepresentation>::FW_TCountedPtr
- //----------------------------------------------------------------------------------------
-
- template <class tRepresentation>
- FW_TCountedPtr<tRepresentation>::FW_TCountedPtr(tRepresentation* rep) :
- fRep(rep)
- {
- UpCount();
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRepresentation>::operator=
- //----------------------------------------------------------------------------------------
-
- template <class tRepresentation>
- FW_TCountedPtr<tRepresentation>&
- FW_TCountedPtr<tRepresentation>::operator=(
- const FW_TCountedPtr<tRepresentation>& other)
- {
- if (fRep != other.fRep)
- {
- DownCount();
- fRep = other.fRep;
- UpCount();
- }
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRepresentation>::operator=
- //----------------------------------------------------------------------------------------
-
- template <class tRepresentation>
- FW_TCountedPtr<tRepresentation>&
- FW_TCountedPtr<tRepresentation>::operator=(tRepresentation* rep)
- {
- if (fRep != rep)
- {
- DownCount();
- fRep = rep;
- UpCount();
- }
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRepresentation>::operator->
- //----------------------------------------------------------------------------------------
-
- template <class tRepresentation>
- inline tRepresentation* FW_TCountedPtr<tRepresentation>::operator->() const
- {
- return fRep;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRepresentation>::operator*
- //----------------------------------------------------------------------------------------
-
- template <class tRepresentation>
- inline tRepresentation& FW_TCountedPtr<tRepresentation>::operator*() const
- {
- return *fRep;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRepresentation>::UpCount
- //----------------------------------------------------------------------------------------
-
- template <class tRepresentation>
- void FW_TCountedPtr<tRepresentation>::UpCount()
- {
- if (fRep)
- fRep->IncrementReferenceCount();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRepresentation>::DownCount
- //----------------------------------------------------------------------------------------
-
- template <class tRepresentation>
- void FW_TCountedPtr<tRepresentation>::DownCount()
- {
- if (fRep && !fRep->DecrementReferenceCount())
- delete fRep;
- // It is not necessary to set fRep to 0. To see why, see how DownCount is used above.
- // In all cases, fRep is either immediately reset, or the instance is being destroyed.
- // Note too that DownCount is private, so the usages above all all possible usages.
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRepresentation>::operator==
- //----------------------------------------------------------------------------------------
-
- template <class tRepresentation>
- inline int FW_TCountedPtr<tRepresentation>::operator==(const FW_TCountedPtr<tRepresentation>& other) const
- {
- return (fRep == other.fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRepresentation>::operator!=
- //----------------------------------------------------------------------------------------
-
- template <class tRepresentation>
- inline int FW_TCountedPtr<tRepresentation>::operator!=(const FW_TCountedPtr<tRepresentation>& other) const
- {
- return !operator==(other);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRepresentation>::operator const void*
- //----------------------------------------------------------------------------------------
-
- template <class tRepresentation>
- inline FW_TCountedPtr<tRepresentation>::operator const void*() const
- {
- return (const void*) fRep;
- }
-
- #endif
-