| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkSmartPointer.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:48 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
|
=========================================================================*/ |
| 17 |
|
#ifndef __itkSmartPointer_h |
| 18 |
|
#define __itkSmartPointer_h |
| 19 |
|
|
| 20 |
|
#include "itkMacro.h" |
| 21 |
|
#include <iostream> |
| 22 |
|
|
| 23 |
|
namespace itk |
| 24 |
|
{ |
| 25 |
|
|
| 26 |
|
/** \class SmartPointer |
| 27 |
|
* \brief Implements transparent reference counting. |
| 28 |
|
* |
| 29 |
|
* SmartPointer implements reference counting by overloading |
| 30 |
|
* operator -> (and *) among others. This allows natural interface |
| 31 |
|
* to the class referred to by the pointer without having to invoke |
| 32 |
|
* special Register()/UnRegister() methods directly. |
| 33 |
|
* |
| 34 |
|
* To compile / test this class |
| 35 |
|
* Windows: cl SmartPointerTest.cxx; .\SmartPointerTest.exe |
| 36 |
|
* linux: c++ SmartPointerTest.cxx ./a.out |
| 37 |
|
* other: CCcompiler SmartPointerTest.cxx ./a.out |
| 38 |
|
* |
| 39 |
|
* \ingroup ITKSystemObjects |
| 40 |
|
* \ingroup DataAccess |
| 41 |
|
*/ |
| 42 |
|
template <class TObjectType> |
| 43 |
|
class ITK_EXPORT SmartPointer |
| 44 |
|
{ |
| 45 |
|
public: |
| 46 |
|
typedef TObjectType ObjectType; |
| 47 |
|
|
| 48 |
|
/** Constructor */ |
| 49 |
|
SmartPointer () |
| 50 |
|
{ m_Pointer = 0; } |
| 51 |
|
|
| 52 |
|
/** Copy constructor */ |
| 53 |
|
SmartPointer (const SmartPointer<ObjectType> &p): |
| 54 |
IND |
****m_Pointer(p.m_Pointer) |
| 55 |
|
{ this->Register(); } |
| 56 |
|
|
| 57 |
|
/** Constructor to pointer p */ |
| 58 |
|
SmartPointer (ObjectType *p): |
| 59 |
IND |
****m_Pointer(p) |
| 60 |
|
{ this->Register(); } |
| 61 |
|
|
| 62 |
|
/** Destructor */ |
| 63 |
|
~SmartPointer () |
| 64 |
|
{ |
| 65 |
|
this->UnRegister(); |
| 66 |
|
m_Pointer = 0; |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
/** Overload operator -> */ |
| 70 |
|
ObjectType *operator -> () const |
| 71 |
|
{ return m_Pointer; } |
| 72 |
|
|
| 73 |
|
/** Return pointer to object. */ |
| 74 |
|
operator ObjectType * () const |
| 75 |
|
{ return m_Pointer; } |
| 76 |
|
|
| 77 |
|
/** Test if the pointer has been initialized */ |
| 78 |
|
bool IsNotNull() const |
| 79 |
IND |
**{ return m_Pointer != 0; } |
| 80 |
|
bool IsNull() const |
| 81 |
IND |
**{ return m_Pointer == 0; } |
| 82 |
|
|
| 83 |
|
/** Template comparison operators. */ |
| 84 |
|
template <typename R> |
| 85 |
|
bool operator == ( R r ) const |
| 86 |
|
{ return (m_Pointer == static_cast<const ObjectType*>(r) ); } |
| 87 |
|
|
| 88 |
|
template <typename R> |
| 89 |
|
bool operator != ( R r ) const |
| 90 |
|
{ return (m_Pointer != static_cast<const ObjectType*>(r) ); } |
| 91 |
|
|
| 92 |
|
/** Access function to pointer. */ |
| 93 |
|
ObjectType *GetPointer () const |
| 94 |
|
{ return m_Pointer; } |
| 95 |
|
|
| 96 |
|
/** Comparison of pointers. Less than comparison. */ |
| 97 |
|
bool operator < (const SmartPointer &r) const |
| 98 |
|
{ return (void*)m_Pointer < (void*) r.m_Pointer; } |
| 99 |
|
|
| 100 |
|
/** Comparison of pointers. Greater than comparison. */ |
| 101 |
|
bool operator > (const SmartPointer &r) const |
| 102 |
|
{ return (void*)m_Pointer > (void*) r.m_Pointer; } |
| 103 |
|
|
| 104 |
|
/** Comparison of pointers. Less than or equal to comparison. */ |
| 105 |
|
bool operator <= (const SmartPointer &r) const |
| 106 |
|
{ return (void*)m_Pointer <= (void*) r.m_Pointer; } |
| 107 |
|
|
| 108 |
|
/** Comparison of pointers. Greater than or equal to comparison. */ |
| 109 |
|
bool operator >= (const SmartPointer &r) const |
| 110 |
|
{ return (void*)m_Pointer >= (void*) r.m_Pointer; } |
| 111 |
|
|
| 112 |
|
/** Overload operator assignment. */ |
| 113 |
|
SmartPointer &operator = (const SmartPointer &r) |
| 114 |
|
{ return this->operator = (r.GetPointer()); } |
| 115 |
|
|
| 116 |
|
/** Overload operator assignment. */ |
| 117 |
|
SmartPointer &operator = (ObjectType *r) |
| 118 |
|
{ |
| 119 |
|
if (m_Pointer != r) |
| 120 |
|
{ |
| 121 |
LEN |
ObjectType* tmp = m_Pointer; //avoid recursive unregisters by retaining temporarily |
| 122 |
|
m_Pointer = r; |
| 123 |
|
this->Register(); |
| 124 |
|
if ( tmp ) { tmp->UnRegister(); } |
| 125 |
|
} |
| 126 |
|
return *this; |
| 127 |
|
} |
| 128 |
|
|
| 129 |
|
/** Function to print object pointed to */ |
| 130 |
|
ObjectType *Print (std::ostream& os) const |
| 131 |
|
{ |
| 132 |
IND |
******// This prints the object pointed to by the pointer |
| 133 |
IND |
******(*m_Pointer).Print(os); |
| 134 |
IND |
******return m_Pointer; |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
private: |
| 138 |
|
/** The pointer to the object referrred to by this smart pointer. */ |
| 139 |
|
ObjectType* m_Pointer; |
| 140 |
|
|
| 141 |
|
void Register() |
| 142 |
|
{ |
| 143 |
|
if(m_Pointer) { m_Pointer->Register(); } |
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
void UnRegister() |
| 147 |
|
{ |
| 148 |
|
if(m_Pointer) { m_Pointer->UnRegister(); } |
| 149 |
|
} |
| 150 |
|
}; |
| 151 |
|
|
| 152 |
|
|
| 153 |
|
template <typename T> |
| 154 |
|
std::ostream& operator<< (std::ostream& os, SmartPointer<T> p) |
| 155 |
|
{ |
| 156 |
|
p.Print(os); |
| 157 |
|
return os; |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
} // end namespace itk |
| 161 |
|
|
| 162 |
|
#endif |
| 163 |
|
|