Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

handle.h

Go to the documentation of this file.
00001 // ------------------
00002 // Life4/Handle.h
00003 // Copyright (C) 2001
00004 // Glenn P. Downing
00005 // ------------------
00006 
00007 #ifndef Handle_h
00008 #define Handle_h
00009 
00010 // --------
00011 // Includes
00012 // --------
00013 
00014 #include <cassert> // assert
00015 
00016 // ----------
00017 // Namespaces
00018 // ----------
00019 
00020 using namespace std;
00021 
00022 // ---------
00023 // Handle<T>
00024 // ---------
00025 
00026 template <class T>
00027 class Handle {
00028     public:
00029         // ------------
00030         // Constructors
00031         // ------------
00032 
00033         Handle () {
00034             p = 0;}
00035 
00036         Handle (T * rhs) {
00037             p = rhs;}
00038 
00039         Handle (const Handle& rhs) {
00040             if (!rhs.p)
00041                 p = 0;
00042             else
00043                 p = rhs.p->clone();}
00044 
00045         // ----------
00046         // Destructor
00047         // ----------
00048 
00049         ~Handle () {
00050             delete p;}
00051 
00052         // ---------
00053         // Operators
00054         // ---------
00055 
00056         Handle& operator = (const Handle& rhs) {
00057             if (this != &rhs) {
00058                 delete p;
00059 
00060                 if (!rhs.p)
00061                     p = 0;
00062                 else
00063                     p = rhs.p->clone();}
00064 
00065             return *this;}
00066 
00067         void replace(T * new_ptr) {
00068           delete p;
00069           p = new_ptr;
00070         }
00071 
00072         operator void* () const {
00073             return p;}
00074 
00075         T& operator * () const {
00076             assert(p);
00077             return *p;}
00078 
00079         T* operator -> () const {
00080             assert(p);
00081             return p;}
00082 
00083         // -------
00084         // Methods
00085         // -------
00086 
00087         void clear () {
00088             delete p;
00089             p = 0;}
00090 
00091         template <class U> void put_pointer_in( U * & ptr) {
00092           ptr = (U *) p;
00093         }
00094 
00095     private:
00096         // ----
00097         // Data
00098         // ----
00099 
00100         T* p;};
00101 
00102 #endif // // Handle_h
00103 

Generated on Thu Jan 10 12:06:19 2002 for C-Breeze by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001