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
00041 Ptr() : mPtr(0) { }
00042
00043
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>
00049 Ptr(const Ptr<Y>& p) : mPtr(p.ptr()) { if (mPtr) mPtr->newReference(); }
00050
00051 ~Ptr() {if (mPtr) mPtr->deleteReference();}
00052
00053
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
00063 bool operator<(const Ptr<T>& rhs) { return mPtr < rhs.ptr(); }
00064
00065
00066 bool operator==(const Ptr<T>& rhs) const {
00067 return mPtr == rhs.mPtr;
00068 }
00069
00070 bool operator!=(const Ptr<T>& rhs) const {
00071 return mPtr != rhs.mPtr;
00072 }
00073
00074
00075 T* operator->() const { return mPtr; }
00076 T& operator*() const { return *mPtr; }
00077 bool operator!() const { return !mPtr; }
00078
00081 operator bool() const { return mPtr ? true : false; }
00082
00087 T* ptr() const { return mPtr; }
00088
00102 template <class Y>
00103 Ptr<Y> dynamicCast(void) const { return Ptr<Y>(dynamic_cast<Y*>(mPtr)); }
00104
00118 template <class Y>
00119 Ptr<Y> staticCast(void) const { return Ptr<Y>(static_cast<Y*>(mPtr)); }
00120
00121 private:
00122 T* mPtr;
00123 };
00124
00128 template <class T> std::ostream& operator<<(std::ostream& os, const Ptr<T>& ptr) {
00129 os << ptr.ptr();
00130 return os;
00131 }
00132
00133 };
00134
00135 #endif
00136