cgma
TDUPtr.hpp
Go to the documentation of this file.
00001 //-------------------------------------------------------------------------
00002 // Filename      : TDUPtr.hpp
00003 //
00004 // Purpose       : Smart pointer that becomes NULL when pointed-to object
00005 //                 is destroted.
00006 //
00007 // Special Notes : Uses ToolData mechanism.  Pointed-to object must be
00008 //                 a ToolDataUser
00009 //
00010 // Creator       : Jason Kraftcheck
00011 //
00012 // Creation Date : 03/12/03
00013 //-------------------------------------------------------------------------
00014 
00015 /* Example Usage:
00016   
00017   TDUPtr<Battleship> my_ptr = new Battleship;
00018   
00019   // ...
00020   // do some stuff
00021   // ...
00022   
00023   if ( my_ptr == NULL ) 
00024     PRINT_INFO("You sunk my Battleship!\n");
00025   else
00026     PRINT_INFO("Location: %d, %d\n", my_ptr->x(), my_ptr->y() );
00027 */
00028 
00029 
00030 #ifndef TDU_PTR_HPP
00031 #define TDU_PTR_HPP
00032 
00033 #include "ToolData.hpp"
00034 #include "CubitMessage.hpp"
00035 #include "CGMUtilConfigure.h"
00036 
00037   // Base class for TDUPtr - used because TDUPtr is a template,
00038   // so a common base type for all the template types is required.
00039 class TDUPtrListNode
00040 {
00041   public:
00042 
00043   virtual ~TDUPtrListNode(){}
00044   
00045   inline TDUPtrListNode() : nextInTD(0) {}
00046   
00047   private:
00048   
00049   friend class TDPtr;
00050 
00051   virtual void nullify() = 0;
00052   TDUPtrListNode* nextInTD;
00053 };
00054 
00055   // The actuall pointer class -- this is the one you
00056   // should be using.
00057 template <class T> class TDUPtr : public TDUPtrListNode
00058 {
00059 public:
00060 
00061   inline TDUPtr() : ptrTo(0) {}
00062   
00063   inline TDUPtr( T* ptr ) : ptrTo(0)
00064     { change_ptr(ptr); }
00065 
00066   inline TDUPtr( const TDUPtr<T>& copy ) : TDUPtrListNode(), ptrTo(0)
00067     { change_ptr(copy.ptrTo); }
00068   
00069   inline virtual ~TDUPtr() 
00070     { change_ptr(0); }
00071   
00072   inline T& operator*() const   { return *ptrTo; }
00073   inline T* operator->() const  { return ptrTo; }
00074   inline operator bool() const { return ptrTo != 0; }
00075   
00076   inline TDUPtr<T>& operator=( const TDUPtr<T>& copy )
00077     { change_ptr(copy.ptrTo); return *this; }
00078     
00079   inline TDUPtr<T>& operator=( T* ptr )
00080     { change_ptr(ptr); return *this; }
00081   
00082   inline bool operator==( const TDUPtr<T>& other ) const
00083     { return ptrTo == other.ptrTo; }
00084   inline bool operator!=( const TDUPtr<T>& other ) const
00085     { return ptrTo != other.ptrTo; }
00086   inline bool operator==( const T* ptr ) const
00087     { return ptrTo == ptr; }
00088   inline bool operator!=( const T* ptr ) const
00089     { return ptrTo != ptr; }
00090 
00091   inline void change_ptr( T* ptr );
00092   
00093   inline T* ptr() const
00094     { return ptrTo; }
00095 
00096 private:
00097 
00098   virtual void nullify()
00099     { ptrTo = 0; }
00100 
00101   T* ptrTo;
00102 
00103 };
00104 
00105   // The ToolData used to associate pointer objects with TDU.
00106 class CUBIT_UTIL_EXPORT TDPtr : public ToolData
00107 {
00108 public:
00109   static void add_to_TD( TDUPtrListNode* add, ToolDataUser* to );
00110   static void remove_from_TD( TDUPtrListNode* remove, ToolDataUser* from );
00111   
00112   virtual ~TDPtr();
00113   
00114 private:
00115 
00116   inline TDPtr() : listHead(0) { }
00117   
00118   static int is_ptr( const ToolData* td )
00119     { return dynamic_cast<const TDPtr*>(td) != 0; }
00120   
00121   TDUPtrListNode* listHead;
00122 };
00123 
00124 template <class T> inline void TDUPtr<T>::change_ptr( T* ptr )
00125 {
00126   if( ptr != ptrTo ) 
00127   {
00128     TDPtr::remove_from_TD(this, ptrTo);
00129     ptrTo = ptr;
00130     TDPtr::add_to_TD(this, ptrTo);
00131   }
00132 }
00133 
00134 
00135 #endif
00136 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines