00001 #ifndef _REFERENCE_H_ 00002 #define _REFERENCE_H_ 00003 00004 #include <stdlib.h> 00005 00007 00020 template <class T> 00021 class Reference 00022 { 00023 private: 00024 T *pointer_; 00025 00026 public: 00028 00031 inline Reference(T *_pointer = NULL) 00032 { 00033 if (_pointer != NULL) 00034 { 00035 _pointer->inc_ref(); 00036 } 00037 pointer_ = _pointer; 00038 } 00039 00041 00045 inline Reference(const Reference<T> &other) 00046 { 00047 pointer_ = other.pointer_; 00048 if (pointer_ != NULL) 00049 pointer_->inc_ref(); 00050 } 00051 00053 00058 inline Reference &operator=(T *_ptr) 00059 { 00060 if (_ptr != pointer_) 00061 { 00062 if (pointer_ != NULL) 00063 { 00064 pointer_->dec_ref(); 00065 } 00066 00067 pointer_ = _ptr; 00068 if (pointer_ != NULL) 00069 { 00070 pointer_->inc_ref(); 00071 } 00072 } 00073 return *this; 00074 } 00075 00077 inline Reference &operator=(const Reference<T> &other) 00078 { 00079 *this = other.pointer_; 00080 return *this; 00081 } 00082 00084 00088 inline T *operator->() const 00089 { 00090 return pointer_; 00091 } 00092 00094 inline operator bool() const 00095 { 00096 return pointer_ != NULL; 00097 } 00098 00100 inline bool operator==(T *_ptr) const 00101 { 00102 return pointer_ == _ptr; 00103 } 00104 00106 inline bool operator!=(T *_ptr) const 00107 { 00108 return pointer_ != _ptr; 00109 } 00110 00112 inline bool operator==(const Reference<T> &other) const 00113 { 00114 return pointer_ == other.pointer_; 00115 } 00116 00118 inline bool operator!=(const Reference<T> &other_) const 00119 { 00120 return pointer_ != other.pointer_; 00121 } 00122 00124 inline bool operator<(T *_ptr) const 00125 { 00126 return pointer_ < _ptr; 00127 } 00128 00130 inline bool operator<(const Reference<T> &other) 00131 { 00132 return pointer_ < other.pointer; 00133 } 00134 00136 00141 virtual ~Reference() 00142 { 00143 if (pointer_ != NULL) 00144 { 00145 pointer_->dec_ref(); 00146 pointer_ = NULL; 00147 } 00148 } 00149 00151 inline T *get_pointer() 00152 { 00153 return pointer_; 00154 } 00155 }; 00156 00158 template <class T> 00159 inline bool operator==(T *_ptr, const Reference<T> ref) 00160 { 00161 return ref == _ptr; 00162 } 00163 00165 template <class T> 00166 inline bool operator!=(T *_ptr, const Reference<T> ref) 00167 { 00168 return ref != _ptr; 00169 } 00170 00172 template <class T> 00173 inline bool operator<(T *_ptr, const Reference<T> ref) 00174 { 00175 return ref < _ptr; 00176 } 00177 00178 #endif /* _REFERENCE_H_ */