ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkAutoPointer.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkAutoPointer_h
19 #define __itkAutoPointer_h
20 
21 #include "itkMacro.h"
22 #include <iostream>
23 
24 namespace itk
25 {
45 template< class TObjectType >
46 class ITK_EXPORT AutoPointer
47 {
48 public:
50  typedef TObjectType ObjectType;
51  typedef AutoPointer Self;
52 
54  AutoPointer ():m_Pointer(0), m_IsOwner(false)
55  {}
56 
58  explicit AutoPointer (AutoPointer & p)
59  {
60  m_IsOwner = p.IsOwner(); // Ownership can only be taken from
61  // another owner
62  m_Pointer = p.ReleaseOwnership(); // release ownership if appropriate
63  }
65 
67  explicit AutoPointer (ObjectType *p, bool takeOwnership):
68  m_Pointer(p), m_IsOwner(takeOwnership)
69  {}
70 
73  {
74  if ( m_IsOwner && m_Pointer )
75  {
76  delete m_Pointer;
77  }
78  m_Pointer = 0;
79  m_IsOwner = false;
80  }
82 
84  ObjectType * operator->() const
85  { return m_Pointer; }
86 
89  void Reset(void)
90  {
91  if ( m_IsOwner && m_Pointer )
92  {
93  delete m_Pointer;
94  }
95  m_Pointer = 0;
96  m_IsOwner = false;
97  }
99 
101  void TakeOwnership(void)
102  { m_IsOwner = true; }
103 
105  void TakeOwnership(ObjectType *objectptr)
106  {
107  if ( m_IsOwner && m_Pointer )
108  {
109  delete m_Pointer; // remove the current one
110  }
111  m_Pointer = objectptr;
112  m_IsOwner = true;
113  }
115 
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  }
127 
129  bool IsOwner(void) const
130  { return m_IsOwner; }
131 
144  ObjectType * ReleaseOwnership(void)
145  {
146  m_IsOwner = false;
147  return m_Pointer;
148  }
150 
152  ObjectType * GetPointer() const
153  { return m_Pointer; }
154 
156  bool operator==(const AutoPointer & r) const
157  { return (void *)m_Pointer == (void *)r.m_Pointer; }
158 
160  bool operator!=(const AutoPointer & r) const
161  { return (void *)m_Pointer != (void *)r.m_Pointer; }
162 
164  bool operator<(const AutoPointer & r) const
165  { return (void *)m_Pointer < (void *)r.m_Pointer; }
166 
168  bool operator>(const AutoPointer & r) const
169  { return (void *)m_Pointer > (void *)r.m_Pointer; }
170 
172  bool operator<=(const AutoPointer & r) const
173  { return (void *)m_Pointer <= (void *)r.m_Pointer; }
174 
176  bool operator>=(const AutoPointer & r) const
177  { return (void *)m_Pointer >= (void *)r.m_Pointer; }
178 
180  AutoPointer & operator=(AutoPointer & r)
181  {
182  AutoPointer(r).Swap(*this);
183  return *this;
184  }
186 
189  operator bool() const
190  { return ( m_Pointer != NULL ); }
191 
193 /* ObjectType *Print (std::ostream& os) const
194  {
195  // This prints the object pointed to by the pointer
196  (*m_Pointer).Print(os);
197  os << "Owner: " << m_IsOwner << std::endl;
198  return m_Pointer;
199  }
200 */
202 
203 private:
204 
206  void Swap(AutoPointer & r)
207  throw( )
208  {
209  ObjectType *temp = m_Pointer;
210 
211  m_Pointer = r.m_Pointer;
212  r.m_Pointer = temp;
213  }
214 
216  ObjectType *m_Pointer;
217  bool m_IsOwner;
218 };
219 
220 template< typename T >
221 std::ostream & operator<<(std::ostream & os, AutoPointer< T > p)
222 {
223  p.Print(os);
224  os << "Owner: " << p.IsOwner() << std::endl;
225  return os;
226 }
227 
230 template< typename TAutoPointerBase, typename TAutoPointerDerived >
231 void
232 ITK_EXPORT TransferAutoPointer(TAutoPointerBase & pa, TAutoPointerDerived & pb)
233 {
234  // give a chance to natural polymorphism
235  pa.TakeNoOwnership( pb.GetPointer() );
236  if ( pb.IsOwner() )
237  {
238  pa.TakeOwnership(); // pa Take Ownership
239  pb.ReleaseOwnership(); // pb Release Ownership and clears
240  }
241 }
242 } // end namespace itk
244 
245 #endif
246