ITK
4.1.0
Insight Segmentation and Registration Toolkit
|
00001 /*========================================================================= 00002 * 00003 * Copyright Insight Software Consortium 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0.txt 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 * 00017 *=========================================================================*/ 00018 #ifndef __itkAutoPointer_h 00019 #define __itkAutoPointer_h 00020 00021 #include "itkMacro.h" 00022 #include <iostream> 00023 00024 namespace itk 00025 { 00045 template< class TObjectType > 00046 class ITK_EXPORT AutoPointer 00047 { 00048 public: 00050 typedef TObjectType ObjectType; 00051 typedef AutoPointer Self; 00052 00054 AutoPointer ():m_Pointer(0), m_IsOwner(false) 00055 {} 00056 00058 explicit AutoPointer (AutoPointer & p) 00059 { 00060 m_IsOwner = p.IsOwner(); // Ownership can only be taken from 00061 // another owner 00062 m_Pointer = p.ReleaseOwnership(); // release ownership if appropriate 00063 } 00065 00067 explicit AutoPointer (ObjectType *p, bool takeOwnership): 00068 m_Pointer(p), m_IsOwner(takeOwnership) 00069 {} 00070 00072 ~AutoPointer () 00073 { 00074 if ( m_IsOwner && m_Pointer ) 00075 { 00076 delete m_Pointer; 00077 } 00078 m_Pointer = 0; 00079 m_IsOwner = false; 00080 } 00082 00084 ObjectType * operator->() const 00085 { return m_Pointer; } 00086 00089 void Reset(void) 00090 { 00091 if ( m_IsOwner && m_Pointer ) 00092 { 00093 delete m_Pointer; 00094 } 00095 m_Pointer = 0; 00096 m_IsOwner = false; 00097 } 00099 00101 void TakeOwnership(void) 00102 { m_IsOwner = true; } 00103 00105 void TakeOwnership(ObjectType *objectptr) 00106 { 00107 if ( m_IsOwner && m_Pointer ) 00108 { 00109 delete m_Pointer; // remove the current one 00110 } 00111 m_Pointer = objectptr; 00112 m_IsOwner = true; 00113 } 00115 00117 void TakeNoOwnership(ObjectType *objectptr) 00118 { 00119 if ( m_IsOwner && m_Pointer ) 00120 { 00121 delete m_Pointer; // remove the current one 00122 } 00123 m_Pointer = objectptr; 00124 m_IsOwner = false; 00125 } 00127 00129 bool IsOwner(void) const 00130 { return m_IsOwner; } 00131 00144 ObjectType * ReleaseOwnership(void) 00145 { 00146 m_IsOwner = false; 00147 return m_Pointer; 00148 } 00150 00152 ObjectType * GetPointer() const 00153 { return m_Pointer; } 00154 00156 bool operator==(const AutoPointer & r) const 00157 { return (void *)m_Pointer == (void *)r.m_Pointer; } 00158 00160 bool operator!=(const AutoPointer & r) const 00161 { return (void *)m_Pointer != (void *)r.m_Pointer; } 00162 00164 bool operator<(const AutoPointer & r) const 00165 { return (void *)m_Pointer < (void *)r.m_Pointer; } 00166 00168 bool operator>(const AutoPointer & r) const 00169 { return (void *)m_Pointer > (void *)r.m_Pointer; } 00170 00172 bool operator<=(const AutoPointer & r) const 00173 { return (void *)m_Pointer <= (void *)r.m_Pointer; } 00174 00176 bool operator>=(const AutoPointer & r) const 00177 { return (void *)m_Pointer >= (void *)r.m_Pointer; } 00178 00180 AutoPointer & operator=(AutoPointer & r) 00181 { 00182 AutoPointer(r).Swap(*this); 00183 return *this; 00184 } 00186 00189 operator bool() const 00190 { return ( m_Pointer != NULL ); } 00191 00193 /* ObjectType *Print (std::ostream& os) const 00194 { 00195 // This prints the object pointed to by the pointer 00196 (*m_Pointer).Print(os); 00197 os << "Owner: " << m_IsOwner << std::endl; 00198 return m_Pointer; 00199 } 00200 */ 00201 private: 00203 00205 void Swap(AutoPointer & r) 00206 throw( ) 00207 { 00208 ObjectType *temp = m_Pointer; 00209 00210 m_Pointer = r.m_Pointer; 00211 r.m_Pointer = temp; 00212 } 00213 00215 ObjectType *m_Pointer; 00216 bool m_IsOwner; 00217 }; 00218 00219 template< typename T > 00220 std::ostream & operator<<(std::ostream & os, AutoPointer< T > p) 00221 { 00222 p.Print(os); 00223 os << "Owner: " << p.IsOwner() << std::endl; 00224 return os; 00225 } 00226 00229 template< typename TAutoPointerBase, typename TAutoPointerDerived > 00230 void 00231 ITK_EXPORT TransferAutoPointer(TAutoPointerBase & pa, TAutoPointerDerived & pb) 00232 { 00233 // give a chance to natural polymorphism 00234 pa.TakeNoOwnership( pb.GetPointer() ); 00235 if ( pb.IsOwner() ) 00236 { 00237 pa.TakeOwnership(); // pa Take Ownership 00238 pb.ReleaseOwnership(); // pb Release Ownership and clears 00239 } 00240 } 00241 } // end namespace itk 00243 00244 #endif 00245