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         // Constructors:                                         //
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         // Standard Operators:                                   //
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         // Debugging Stuff:                                      //
00046         //                                                       //
00048         
00049 #ifdef DEBUG
00050         void debugPrint( ostream& ostr ) {
00051                 ostr << "this : " << this << "; theRep : " << theRep << "; xrefs : "
00052                         << look()->nxrefs();
00053         }
00054         
00055         //friend int main( );
00056 #endif
00057         
00058         
00059 protected:
00060         
00062         //                                                       //
00063         // Representation Access:                                //
00064         //                                                       //
00066         
00067         const Rep* look( ) const { return theRep; }
00068         // For safe read-only access.
00069         
00070         Rep* enhance( ) const { return theRep; }
00071         // DANGEROUS: for altering an object without triggering cloning.
00072         // Use to change theRep without altering semantics.
00073         
00074         Rep* change( ) {
00075                 if ( theRep->lastRef() ) return theRep;
00076                 else { theRep->delRef(); return theRep = (Rep*)theRep->clone(); }
00077         }
00078         // For safe read/write access.
00079         
00080         void acquireRep( const Rep* rep )
00081         {
00082                 ((Rep*&)rep)->addRef();
00083                 // cast away physical constness to permit logically const
00084                 // incrementation of ref count
00085                 if ( theRep->lastRef() ) delete theRep; else theRep->delRef();
00086                 theRep = ((Rep*&)rep);
00087                 // cast away physical constness of representation for
00088                 // acquisition through new object; semantics of look() and
00089                 // and change() guarantee that logical constness is maintained
00090         }
00091         // special `assignment-like' member: to permit an object to acquire
00092         // another's representation consistently
00093         // useful in delegation of object-level methods
00094         
00096         //                                                       //
00097         // Special Constructors:                                 //
00098         //                                                       //
00100         
00101         ObjectOf( Rep* newrep ) { theRep = newrep; }
00102         // To wrap new representations
00103         
00104 private:
00105         
00107         //                                                       //
00108         // Data Members:                                         //
00109         //                                                       //
00111         
00112         Rep* theRep;
00113         
00115         //                                                       //
00116         // Safety:                                               //
00117         //                                                       //
00119         
00120         void force_derivation( ) { RefCounter* rc = theRep; }
00121         // With this member RefCounter is forced to be 
00122         // an accessible base class of Rep
00123         
00124 };
00125 
00126 
00127 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Mon Sep 26 18:43:45 2011 for CRyptography And Groups (CRAG) by  doxygen 1.6.1