C-Breeze
C Compiler Infrastructure

[ Project home page]

handle.h

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

Generated on February 1, 2006
Back to the C-Breeze home page