00001 #ifndef PLCOUNTED_POINTER_H
00002 #define PLCOUNTED_POINTER_H
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifdef _MSC_VER
00012 #pragma warning(disable: 4284)
00013 #endif
00014
00015 template <class type>
00016 class PLCountedPointer
00017 {
00018 public:
00019 explicit PLCountedPointer(type * pType = 0)
00020 : pBody(pType), pCount(new size_t(1)) {}
00021 PLCountedPointer(const PLCountedPointer & copy)
00022 : pBody(copy.pBody), pCount(copy.pCount) { incCount(); }
00023 ~PLCountedPointer() { decCount(); }
00024
00025 PLCountedPointer& operator=(const PLCountedPointer & rhs);
00026
00027 type * operator -> () const { return pBody; }
00028 type & operator * () const { return *pBody; }
00029
00030 type * get() const { return pBody; }
00031
00032 bool operator == (const PLCountedPointer & rhs) const { return pBody == rhs.pBody; }
00033 bool operator != (const PLCountedPointer & rhs) const { return pBody != rhs.pBody; }
00034
00035 operator bool () const { return pBody != 0; }
00036
00037 private:
00038 void incCount() { ++*pCount; }
00039 void decCount();
00040
00041 private:
00042 type * pBody;
00043 size_t * pCount;
00044 };
00045
00046 template <class type>
00047 PLCountedPointer<type>& PLCountedPointer<type>::operator=(const PLCountedPointer & rhs)
00048 {
00049 if (pBody != rhs.pBody)
00050 {
00051 decCount();
00052 pBody = rhs.pBody;
00053 pCount = rhs.pCount;
00054 incCount();
00055 }
00056 return *this;
00057 }
00058
00059 template <class type>
00060 void PLCountedPointer<type>::decCount()
00061 {
00062 if (!--*pCount)
00063 {
00064 delete pBody;
00065 delete pCount;
00066 }
00067 }
00068
00069 template <class type>
00070 class PLCountedArrayPointer
00071 {
00072 public:
00073 explicit PLCountedArrayPointer(type * pType = 0)
00074 : pBody(pType), pCount(new size_t(1)) {}
00075 PLCountedArrayPointer(const PLCountedArrayPointer & copy)
00076 : pBody(copy.pBody), pCount(copy.pCount) { incCount(); }
00077 ~PLCountedArrayPointer() { decCount(); }
00078
00079 PLCountedArrayPointer& operator=(const PLCountedArrayPointer & rhs);
00080
00081 type * operator -> () const { return pBody; }
00082 type & operator * () const { return *pBody; }
00083
00084 type * get() const { return pBody; }
00085
00086 bool operator == (const PLCountedArrayPointer & rhs) const { return pBody == rhs.pBody; }
00087 bool operator != (const PLCountedArrayPointer & rhs) const { return pBody != rhs.pBody; }
00088
00089 type & operator [] (size_t i) { return pBody[i]; }
00090 const type & operator [] (size_t i) const { return pBody[i]; }
00091
00092 operator bool () const { return pBody != 0; }
00093
00094 private:
00095 void incCount() { ++*pCount; }
00096 void decCount();
00097
00098 private:
00099 type * pBody;
00100 size_t * pCount;
00101 };
00102
00103 template <class type>
00104 PLCountedArrayPointer<type>& PLCountedArrayPointer<type>::operator=(const PLCountedArrayPointer & rhs)
00105 {
00106 if (pBody != rhs.pBody)
00107 {
00108 decCount();
00109 pBody = rhs.pBody;
00110 pCount = rhs.pCount;
00111 incCount();
00112 }
00113 return *this;
00114 }
00115
00116 template <class type>
00117 void PLCountedArrayPointer<type>::decCount()
00118 {
00119 if (!--*pCount)
00120 {
00121 delete [] pBody;
00122 delete pCount;
00123 }
00124 }
00125
00126 #endif