00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _HANDLE_H_
00021 #define _HANDLE_H_
00022
00023 namespace Vamos_Geometry
00024 {
00025 template <class T> class Handle
00026 {
00027 private:
00028
00029 T* mp_rep;
00030
00031
00032
00033 int* mp_count;
00034
00035 public:
00036 Handle (T* rep = 0);
00037 Handle (const Handle& handle);
00038 ~Handle ();
00039
00040 Handle& operator = (const Handle& handle);
00041 T* operator -> () const { return mp_rep; }
00042 bool null () const { return mp_rep == 0; }
00043 };
00044 }
00045
00046 template <class T> Vamos_Geometry::Handle <T>::
00047 Handle (T* rep) : mp_rep (rep), mp_count (new int (1))
00048 {
00049 }
00050
00051 template <class T> Vamos_Geometry::Handle <T>::
00052 Handle (const Handle& handle)
00053 {
00054
00055 mp_rep = handle.mp_rep;
00056 mp_count = handle.mp_count;
00057 (*mp_count)++;
00058 }
00059
00060 template <class T> Vamos_Geometry::Handle <T>::
00061 ~Handle ()
00062 {
00063 --(*mp_count);
00064 if (*mp_count == 0)
00065 {
00066 delete mp_rep;
00067 delete mp_count;
00068 }
00069 }
00070
00071 template <class T> Vamos_Geometry::Handle <T>& Vamos_Geometry::Handle <T>::
00072 operator = ( const Handle& handle)
00073 {
00074 if (handle.mp_rep != mp_rep)
00075 {
00076
00077 if (--(*mp_count) == 0)
00078 {
00079 delete mp_rep;
00080 delete mp_count;
00081 }
00082
00083
00084 mp_rep = handle.mp_rep;
00085 mp_count = handle.mp_count;
00086 (*mp_count)++;
00087 }
00088 return *this;
00089 }
00090
00091 #endif // not _HANDLE_H_