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
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 };
00130
00131 #endif
00132