Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

PhxPtr.h

Go to the documentation of this file.
00001 #ifndef PHX_PTR_H
00002 #define PHX_PTR_H
00003 
00012 #include <Phx/PhxConfig.h>
00013 #include <iostream>
00014 
00015 namespace Phx {
00016 
00037   template <class T>
00038   class Ptr {
00039   public:
00040     // Default constructor makes NULL pointer
00041     Ptr() : mPtr(0) { }
00042 
00043     // create a new Ptr from a raw pointer.
00044     Ptr(T* p) : mPtr(p) { if (mPtr) mPtr->newReference(); }
00045 
00046     Ptr(const Ptr<T>& p) : mPtr(p.mPtr) { if (mPtr) mPtr->newReference(); }
00047 
00048     template <class Y> /* explicit? */
00049     Ptr(const Ptr<Y>& p) : mPtr(p.ptr()) { if (mPtr) mPtr->newReference(); }
00050 
00051     ~Ptr() { if (mPtr) mPtr->deleteReference(); }
00052 
00053     // assignment
00054     const Ptr<T>& operator=(const Ptr<T>& rhs) {
00055       T* oldPtr = mPtr;
00056       mPtr = rhs.mPtr;
00057       if (mPtr) mPtr->newReference();
00058       if (oldPtr) oldPtr->deleteReference();
00059       return *this;
00060     }
00061 
00062     // less-than comparison only (for containers, mostly)
00063     bool operator<(const Ptr<T>& rhs) { return mPtr < rhs.ptr(); }
00064 
00065     // equality comparison
00066     bool operator==(const Ptr<T>& rhs) const {
00067       return mPtr == rhs.mPtr;
00068     }
00069 
00070     // pointer operator overloading
00071     T* operator->() const { return mPtr; }
00072     T& operator*() const { return *mPtr; }
00073     bool operator!() const { return !mPtr; }
00074 
00077     operator bool() const { return mPtr ? true : false; }
00078 
00083     T* ptr() const { return mPtr; }
00084 
00098     template <class Y>
00099     Ptr<Y> dynamicCast(void) const { return Ptr<Y>(dynamic_cast<Y*>(mPtr)); }
00100 
00114     template <class Y>
00115     Ptr<Y> staticCast(void) const { return Ptr<Y>(static_cast<Y*>(mPtr)); }
00116 
00117   private:
00118     T* mPtr;
00119   };
00120 
00124   template <class T> std::ostream& operator<<(std::ostream& os, const Ptr<T>& ptr) {
00125     os << ptr.ptr();
00126     return os;
00127   }
00128 
00129 }; // namespace Phx
00130 
00131 #endif /* PHX_PTR_H */
00132 

Generated on Wed Dec 21 22:05:37 2005 for Phoenix OSFS by  doxygen 1.4.2