GDCM 2.0.17
|
00001 /*========================================================================= 00002 00003 Program: GDCM (Grassroots DICOM). A DICOM library 00004 Module: $URL$ 00005 00006 Copyright (c) 2006-2010 Mathieu Malaterre 00007 All rights reserved. 00008 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details. 00009 00010 This software is distributed WITHOUT ANY WARRANTY; without even 00011 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00012 PURPOSE. See the above copyright notice for more information. 00013 00014 =========================================================================*/ 00015 #ifndef GDCMSMARTPOINTER_H 00016 #define GDCMSMARTPOINTER_H 00017 00018 #include "gdcmObject.h" 00019 00020 namespace gdcm 00021 { 00039 template<class ObjectType> 00040 class SmartPointer 00041 { 00042 public: 00043 SmartPointer():Pointer(0) {} 00044 SmartPointer(const SmartPointer<ObjectType>& p):Pointer(p.Pointer) 00045 { Register(); } 00046 SmartPointer(ObjectType* p):Pointer(p) 00047 { Register(); } 00048 SmartPointer(ObjectType const & p) 00049 { 00050 Pointer = const_cast<ObjectType*>(&p); 00051 Register(); 00052 } 00053 ~SmartPointer() { 00054 UnRegister(); 00055 Pointer = 0; 00056 } 00057 00059 ObjectType *operator -> () const 00060 { return Pointer; } 00061 00062 ObjectType& operator * () const 00063 { return *Pointer; } 00064 00066 operator ObjectType * () const 00067 { return Pointer; } 00068 00070 SmartPointer &operator = (SmartPointer const &r) 00071 { return operator = (r.Pointer); } 00072 00074 SmartPointer &operator = (ObjectType *r) 00075 { 00076 // http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.22 00077 // DO NOT CHANGE THE ORDER OF THESE STATEMENTS! 00078 // (This order properly handles self-assignment) 00079 // (This order also properly handles recursion, e.g., if a ObjectType contains SmartPointer<ObjectType>s) 00080 if( Pointer != r ) 00081 { 00082 ObjectType* old = Pointer; 00083 Pointer = r; 00084 Register(); 00085 if ( old ) { old->UnRegister(); } 00086 } 00087 return *this; 00088 } 00089 00090 SmartPointer &operator = (ObjectType const &r) 00091 { 00092 ObjectType* tmp = const_cast<ObjectType*>(&r); 00093 return operator = (tmp); 00094 } 00095 00097 ObjectType *GetPointer() const 00098 { return Pointer; } 00099 00100 private: 00101 void Register() 00102 { 00103 if(Pointer) Pointer->Register(); 00104 } 00105 00106 void UnRegister() 00107 { 00108 if(Pointer) Pointer->UnRegister(); 00109 } 00110 00111 ObjectType* Pointer; 00112 }; 00113 00114 } // end namespace gdcm 00115 00116 #endif //GDCMSMARTPOINTER_H