/*============================================================================= Project: ManagedITK Program: Insight Segmentation & Registration Toolkit Module: itkManagedAutoPtr.cxx Language: C++/CLI Author: Dan Mueller $Date: 2007-04-13 08:13:36 +1000 (Fri, 13 Apr 2007) $ $Revision: 137 $ This file was based on the following article: "Best Practices for Writing Efficient and Reliable Code with C++/CLI" by Kenny Kerr (May 2006) http://msdn.microsoft.com/library/en-us/dnvs05/html/CplusCLIBP.asp Portions of this code are covered under the ITK and VTK copyright. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =============================================================================*/ #pragma once #pragma warning( disable : 4635 ) // Disable warnings about XML doc comments #ifndef __itkAutoPtr_cxx #define __itkAutoPtr_cxx using namespace System::Diagnostics; namespace itk { template ref struct itkAutoPtr { private: T* m_ptr; bool m_disposed; public: itkAutoPtr() : m_ptr(0), m_disposed(false) {} itkAutoPtr(T* ptr) : m_ptr(ptr), m_disposed(false) { if (ptr != 0) ptr->Register(); } itkAutoPtr(itkAutoPtr% right) : m_ptr(right.Release()), disposed(false) {} ~itkAutoPtr() { if (!m_disposed) Reset(); m_disposed = true; } //!itkAutoPtr() //{ // // Dispose of the managed pointer if the user forgot // if (!m_disposed) Reset(); // m_disposed = true; //} T* operator->() { return m_ptr; } T* Get() { return m_ptr; } T* Release() { T* released = m_ptr; m_ptr = 0; return released; } void Reset() { Reset(0); } void Reset(T* ptr) { if (m_ptr != 0 && ptr != m_ptr) { // Free the existing native pointer m_ptr->RemoveAllObservers(); m_ptr->SetReferenceCount(0); } // Set the new native pointer m_ptr = ptr; if (m_ptr != 0) m_ptr->Register(); } }; } // end namespace itk #endif