00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __REFERENCE_HPP__
00021 #define __REFERENCE_HPP__
00022
00023 #include <hash_map>
00024
00025
00030 template <class T>
00031 class Reference
00032 {
00033 private:
00037 static std::hash_map<T*, int> map;
00038
00042 T* ptr;
00043
00044 public:
00048 Reference(T* ptr)
00049 {
00050 this->ptr = ptr;
00051
00052 if(map.count(ptr) == 0)
00053 {
00054 map[ptr] = 0;
00055 }
00056 map[ptr]++;
00057 }
00058
00062 ~Reference()
00063 {
00064 map[ptr]--;
00065 if(map[ptr] == 0)
00066 {
00067 map.erase(ptr);
00068 delete ptr;
00069 }
00070
00071 ptr = NULL;
00072 }
00073
00077 operator T*()
00078 {
00079 return ptr;
00080 }
00081
00085 T* operator->()
00086 {
00087 return ptr;
00088 }
00089 };
00090
00091 #endif // __REFERENCE_HPP__