| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkAutoPointer.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:33 $ |
| 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 __itkAutoPointer_h |
| 18 |
|
#define __itkAutoPointer_h |
| 19 |
|
|
| 20 |
|
#include "itkMacro.h" |
| 21 |
|
#include <iostream> |
| 22 |
|
|
| 23 |
|
namespace itk |
| 24 |
|
{ |
| 25 |
|
|
| 26 |
|
/** \class AutoPointer |
| 27 |
|
* \brief Implements an Automatic Pointer to an object. |
| 28 |
|
* |
| 29 |
|
* AutoPointer is intended to facilitate the construction of |
| 30 |
|
* objects on the fly for those objects that are not to be shared. |
| 31 |
|
* An AutoPointer destroys its object when it goes out of scope. |
| 32 |
|
* Ownership of the object is transferred from one AutoPointer |
| 33 |
|
* to another AutoPointer when the assignement operator is used. |
| 34 |
|
* An AutoPointer can release ownership of the object it holds. |
| 35 |
|
* |
| 36 |
|
* This class follows the design of the std::auto_ptr class. The main |
| 37 |
|
* reason for not using the std version is to avoid templated methods, |
| 38 |
|
* which greatly increase the difficulty of wrapping for Tcl, Python |
| 39 |
|
* and Java. |
| 40 |
|
* |
| 41 |
|
* \ingroup ITKSystemObjects |
| 42 |
|
* \ingroup DataAccess |
| 43 |
|
*/ |
| 44 |
|
template <class TObjectType> |
| 45 |
|
class ITK_EXPORT AutoPointer |
| 46 |
|
{ |
| 47 |
|
public: |
| 48 |
|
/** Extract information from template parameter. */ |
| 49 |
|
typedef TObjectType ObjectType; |
| 50 |
|
typedef AutoPointer Self; |
| 51 |
|
|
| 52 |
|
/** Constructor. */ |
| 53 |
|
AutoPointer (): |
| 54 |
IND |
****m_Pointer(0), |
| 55 |
IND |
****m_IsOwner(false) |
| 56 |
|
{ } |
| 57 |
|
|
| 58 |
|
/** Copy constructor. */ |
| 59 |
|
explicit AutoPointer ( AutoPointer & p ) |
| 60 |
|
{ |
| 61 |
|
m_IsOwner = p.IsOwner(); // Ownership can only be taken from another owner |
| 62 |
|
m_Pointer = p.ReleaseOwnership(); // release ownership if appropriate |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
/** Constructor to pointer p. */ |
| 67 |
|
explicit AutoPointer ( ObjectType * p, bool takeOwnership ): |
| 68 |
IND |
********************m_Pointer(p), |
| 69 |
IND |
********************m_IsOwner(takeOwnership) |
| 70 |
IND |
******{ } |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
/** Destructor. */ |
| 74 |
|
~AutoPointer () |
| 75 |
|
{ |
| 76 |
|
if( m_IsOwner && m_Pointer ) |
| 77 |
|
{ |
| 78 |
|
delete m_Pointer; |
| 79 |
|
} |
| 80 |
|
m_Pointer = 0; |
| 81 |
|
m_IsOwner = false; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
/** Overload operator ->. */ |
| 85 |
|
ObjectType *operator -> () const |
| 86 |
|
{ return m_Pointer; } |
| 87 |
|
|
| 88 |
|
/** Clear the AutoPointer. If it had a pointer the object |
| 89 |
IND |
******is deleted and the pointer is set to null. */ |
| 90 |
|
void Reset( void ) |
| 91 |
|
{ |
| 92 |
|
if( m_IsOwner && m_Pointer ) |
| 93 |
|
{ |
| 94 |
|
delete m_Pointer; |
| 95 |
|
} |
| 96 |
|
m_Pointer = 0; |
| 97 |
|
m_IsOwner = false; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
/** Explicitly set the ownership */ |
| 102 |
|
void TakeOwnership(void) |
| 103 |
|
{ m_IsOwner = true; } |
| 104 |
|
|
| 105 |
|
/** Explicitly set the ownership */ |
| 106 |
|
void TakeOwnership(ObjectType * objectptr) |
| 107 |
|
{ |
| 108 |
|
if( m_IsOwner && m_Pointer ) |
| 109 |
|
{ |
| 110 |
|
delete m_Pointer; // remove the current one |
| 111 |
|
} |
| 112 |
|
m_Pointer = objectptr; |
| 113 |
|
m_IsOwner = true; |
| 114 |
|
} |
| 115 |
|
|
| 116 |
|
/** Explicitly reject ownership */ |
| 117 |
|
void TakeNoOwnership(ObjectType * objectptr) |
| 118 |
|
{ |
| 119 |
|
if( m_IsOwner && m_Pointer ) |
| 120 |
|
{ |
| 121 |
|
delete m_Pointer; // remove the current one |
| 122 |
|
} |
| 123 |
|
m_Pointer = objectptr; |
| 124 |
|
m_IsOwner = false; |
| 125 |
|
} |
| 126 |
|
|
| 127 |
|
/** Query for the ownership */ |
| 128 |
|
bool IsOwner(void) const |
| 129 |
|
{ return m_IsOwner; } |
| 130 |
|
|
| 131 |
|
/** Release the pointer hold by the current AutoPointer |
| 132 |
IND |
******and return the raw pointer so it can be hold by |
| 133 |
IND |
******another AutoPointer. This operation is intended to |
| 134 |
IND |
******be used for facilitating polymorphism. |
| 135 |
|
|
| 136 |
IND |
******Example: if class Cow derives from Mammal, |
| 137 |
IND |
******AutoPointer<Cow> onecow = new Cow; |
| 138 |
IND |
******AutoPointer<Mammal> onemammal = onecow.ReleaseOwnership(); |
| 139 |
|
|
| 140 |
IND |
******Note that the AutoPointer still points to the object after the |
| 141 |
IND |
******ReleaseOwnership operation, but it doesn't own the object any |
| 142 |
IND |
******more. |
| 143 |
|
|
| 144 |
IND |
*****/ |
| 145 |
|
ObjectType * ReleaseOwnership( void ) |
| 146 |
|
{ |
| 147 |
|
m_IsOwner = false; |
| 148 |
|
return m_Pointer; |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
/** Access function to pointer. */ |
| 152 |
|
ObjectType *GetPointer () const |
| 153 |
|
{ return m_Pointer; } |
| 154 |
|
|
| 155 |
IND |
***/** Comparison of pointers. Equal comparison. */ |
| 156 |
|
bool operator == (const AutoPointer &r) const |
| 157 |
|
{ return (void*)m_Pointer == (void*) r.m_Pointer; } |
| 158 |
|
|
| 159 |
|
/** Comparison of pointers. NonEqual comparison. */ |
| 160 |
|
bool operator != (const AutoPointer &r) const |
| 161 |
|
{ return (void*)m_Pointer != (void*) r.m_Pointer; } |
| 162 |
|
|
| 163 |
|
/** Comparison of pointers. Less than comparison. */ |
| 164 |
|
bool operator < (const AutoPointer &r) const |
| 165 |
|
{ return (void*)m_Pointer < (void*) r.m_Pointer; } |
| 166 |
|
|
| 167 |
|
/** Comparison of pointers. Greater than comparison. */ |
| 168 |
|
bool operator > (const AutoPointer &r) const |
| 169 |
|
{ return (void*)m_Pointer > (void*) r.m_Pointer; } |
| 170 |
|
|
| 171 |
|
/** Comparison of pointers. Less than or equal to comparison. */ |
| 172 |
|
bool operator <= (const AutoPointer &r) const |
| 173 |
|
{ return (void*)m_Pointer <= (void*) r.m_Pointer; } |
| 174 |
|
|
| 175 |
|
/** Comparison of pointers. Greater than or equal to comparison. */ |
| 176 |
|
bool operator >= (const AutoPointer &r) const |
| 177 |
|
{ return (void*)m_Pointer >= (void*) r.m_Pointer; } |
| 178 |
|
|
| 179 |
|
/** Overload operator assignment. */ |
| 180 |
|
AutoPointer &operator = (AutoPointer &r) const |
| 181 |
|
{ |
| 182 |
|
AutoPointer( r ).Swap( *this ); |
| 183 |
|
return *this; |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
/** Casting operator to boolean. This is used in conditional |
| 187 |
IND |
******statments to check the content of the pointer against null */ |
| 188 |
|
operator bool () const |
| 189 |
|
{ return (m_Pointer!=NULL); } |
| 190 |
|
|
| 191 |
|
/** Function to print object pointed to. */ |
| 192 |
|
ObjectType *Print (std::ostream& os) const |
| 193 |
|
{ |
| 194 |
|
// This prints the object pointed to by the pointer |
| 195 |
|
(*m_Pointer).Print(os); |
| 196 |
|
os << "Owner: " << m_IsOwner << std::endl; |
| 197 |
|
return m_Pointer; |
| 198 |
|
} |
| 199 |
|
|
| 200 |
|
private: |
| 201 |
|
|
| 202 |
|
/** Exchange the content of two AutoPointers */ |
| 203 |
|
void Swap(AutoPointer &r) throw() |
| 204 |
|
{ |
| 205 |
|
ObjectType * temp = m_Pointer; |
| 206 |
|
m_Pointer = r.m_Pointer; |
| 207 |
|
r.m_Pointer = temp; |
| 208 |
|
} |
| 209 |
|
|
| 210 |
|
|
| 211 |
|
/** The pointer to the object referrred to by this smart pointer. */ |
| 212 |
|
ObjectType* m_Pointer; |
| 213 |
|
bool m_IsOwner; |
| 214 |
|
}; |
| 215 |
|
|
| 216 |
|
|
| 217 |
|
template <typename T> |
| 218 |
|
std::ostream& operator<< (std::ostream& os, AutoPointer<T> p) |
| 219 |
|
{ |
| 220 |
|
p.Print(os); |
| 221 |
|
os << "Owner: " << p.IsOwner() << std::endl; |
| 222 |
|
return os; |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
|
| 226 |
|
/** This templated function is intended to facilitate the |
| 227 |
|
transfer between AutoPointers of Derived class to Base class */ |
| 228 |
|
template <typename TAutoPointerBase, typename TAutoPointerDerived> |
| 229 |
|
void |
| 230 |
|
ITK_EXPORT TransferAutoPointer(TAutoPointerBase & pa, TAutoPointerDerived & pb) |
| 231 |
|
{ |
| 232 |
LEN |
pa.TakeNoOwnership( pb.GetPointer() ); // give a chance to natural polymorphism |
| 233 |
|
if( pb.IsOwner() ) |
| 234 |
|
{ |
| 235 |
|
pa.TakeOwnership(); // pa Take Ownership |
| 236 |
|
pb.ReleaseOwnership(); // pb Release Ownership and clears |
| 237 |
|
} |
| 238 |
|
} |
| 239 |
|
|
| 240 |
|
|
| 241 |
|
} // end namespace itk |
| 242 |
|
|
| 243 |
|
#endif |
| 244 |
|
|