Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members

plcountedpointer.h

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

Generated on Mon Sep 13 16:16:40 2004 for paintlib by doxygen 1.3.2