include/vamos/geometry/Handle.h

Go to the documentation of this file.
00001 // Handle.h - a reference-counting pointer interface.
00002 
00003 //      Vamos Automotive Simulator
00004 //  Copyright (C) 2002 Sam Varner
00005 //
00006 //  This program is free software; you can redistribute it and/or modify
00007 //  it under the terms of the GNU General Public License as published by
00008 //  the Free Software Foundation; either version 2 of the License, or
00009 //  (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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         // The actual pointer.
00029         T* mp_rep;
00030         
00031         // A pointer to the reference count.  mp_count is a pointer so
00032         // that all references can modify the count.
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   // Copy the pointers and increment the reference count.
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           // Assigning removes the current reference.
00077           if (--(*mp_count) == 0)
00078                 {
00079                   delete mp_rep;
00080                   delete mp_count;
00081                 }
00082           
00083           // Copy the pointers and increment the reference count.
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_

Generated on Thu Oct 19 04:05:47 2006 by  doxygen 1.4.6