00001 #ifndef MANGO_CHECKED_PTR
00002 #define MANGO_CHECKED_PTR
00003
00023 #include <assert.h>
00024 #include "nullness.hh"
00025
00026 namespace MangoPtr
00027 {
00028
00057 template <typename T>
00058 class CheckedPtr
00059 {
00060 public:
00062 template <typename O2Type> friend class CheckedPtr;
00063
00065 CheckedPtr(T* ptr): ptr(ptr) {}
00067 CheckedPtr(const CheckedPtr& rhs): ptr(rhs.ptr) {}
00069 template <typename T2>
00070 CheckedPtr(const CheckedPtr<T2>& rhs): ptr(rhs.ptr) {}
00071
00073
00074 template <typename T2>
00075 bool operator==(const CheckedPtr<T2>& rhs) const {return ptr == rhs.ptr;}
00076 template <typename T2>
00077 bool operator!=(const CheckedPtr<T2>& rhs) const {return ptr != rhs.ptr;}
00078 template <typename T2>
00079 bool operator< (const CheckedPtr<T2>& rhs) const {return ptr < rhs.ptr;}
00080
00081 bool operator==(T* rhs) const {return ptr == rhs;}
00082 bool operator!=(T* rhs) const {return ptr != rhs;}
00083 bool operator< (T* rhs) const {return ptr < rhs;}
00085
00087 T* operator->() const {
00088 assert(isNotNull(ptr));
00089 return ptr;}
00090
00092 T& operator*() const {
00093 assert(isNotNull(ptr));
00094 return *ptr;}
00095
00097 operator T*() const {return ptr;}
00098
00099 private:
00101 T* const ptr;
00102 };
00103
00104 }
00105
00106
00111 template <typename T>
00112 inline bool
00113 isNull(const MangoPtr::CheckedPtr<T>& p) {return 0==(T*)p;}
00114
00119 template <typename T>
00120 inline bool
00121 isNotNull(const MangoPtr::CheckedPtr<T>& p) {return 0!=(T*)p;}
00122
00123
00124 #endif // MANGO_CHECKED_PTR
00125