ObjectOf.h
Go to the documentation of this file.00001 
00002 #ifndef _OBJECT_OF_H_
00003 #define _OBJECT_OF_H_
00004 
00005 
00006 #include <iostream>
00007 using namespace std;
00008 
00009 #include "RefCounter.h"
00010 
00011 
00012 template<class Rep> class ObjectOf
00013 {
00014         
00015 public:
00016         
00018         
00019         
00020         
00022         
00023         ObjectOf( const ObjectOf& o ) { theRep = o.theRep; theRep->addRef(); }
00024         
00025         ~ObjectOf() { if (theRep->lastRef()) delete theRep; else theRep->delRef(); }
00026         
00028         
00029         
00030         
00032         
00033 #ifndef SWIG
00034         ObjectOf& operator = ( const ObjectOf& o )
00035         {
00036                 o.theRep->addRef();
00037                 if ( theRep->lastRef() ) delete theRep; else theRep->delRef();
00038                 theRep = o.theRep;
00039                 return *this;
00040         }
00041 #endif
00042 
00044         
00045         
00046         
00048         
00049 #ifdef DEBUG
00050         void debugPrint( ostream& ostr ) {
00051                 ostr << "this : " << this << "; theRep : " << theRep << "; xrefs : "
00052                         << look()->nxrefs();
00053         }
00054         
00055         
00056 #endif
00057         
00058         
00059 protected:
00060         
00062         
00063         
00064         
00066         
00067         const Rep* look( ) const { return theRep; }
00068         
00069         
00070         Rep* enhance( ) const { return theRep; }
00071         
00072         
00073         
00074         Rep* change( ) {
00075                 if ( theRep->lastRef() ) return theRep;
00076                 else { theRep->delRef(); return theRep = (Rep*)theRep->clone(); }
00077         }
00078         
00079         
00080         void acquireRep( const Rep* rep )
00081         {
00082                 ((Rep*&)rep)->addRef();
00083                 
00084                 
00085                 if ( theRep->lastRef() ) delete theRep; else theRep->delRef();
00086                 theRep = ((Rep*&)rep);
00087                 
00088                 
00089                 
00090         }
00091         
00092         
00093         
00094         
00096         
00097         
00098         
00100         
00101         ObjectOf( Rep* newrep ) { theRep = newrep; }
00102         
00103         
00104 private:
00105         
00107         
00108         
00109         
00111         
00112         Rep* theRep;
00113         
00115         
00116         
00117         
00119         
00120         void force_derivation( ) { RefCounter* rc = theRep; }
00121         
00122         
00123         
00124 };
00125 
00126 
00127 #endif