ITK  5.0.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< typename TObjectType >
47 {
48 public:
50  using ObjectType = TObjectType;
51  using Self = AutoPointer;
52 
54  AutoPointer ():m_Pointer(nullptr)
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  this->Reset();
75  }
76 
79  { return m_Pointer; }
80 
83  void Reset()
84  {
85  if ( m_IsOwner )
86  {
87  delete m_Pointer;
88  }
89  m_Pointer = nullptr;
90  m_IsOwner = false;
91  }
93 
96  { m_IsOwner = true; }
97 
99  void TakeOwnership(ObjectType *objectptr)
100  {
101  if ( m_IsOwner )
102  {
103  delete m_Pointer; // remove the current one
104  }
105  m_Pointer = objectptr;
106  m_IsOwner = true;
107  }
109 
111  void TakeNoOwnership(ObjectType *objectptr)
112  {
113  if ( m_IsOwner )
114  {
115  delete m_Pointer; // remove the current one
116  }
117  m_Pointer = objectptr;
118  m_IsOwner = false;
119  }
121 
123  bool IsOwner() const
124  { return m_IsOwner; }
125 
139  {
140  m_IsOwner = false;
141  return m_Pointer;
142  }
144 
147  { return m_Pointer; }
148 
150  bool operator==(const AutoPointer & r) const
151  { return (void *)m_Pointer == (void *)r.m_Pointer; }
152 
154  bool operator!=(const AutoPointer & r) const
155  { return (void *)m_Pointer != (void *)r.m_Pointer; }
156 
158  bool operator<(const AutoPointer & r) const
159  { return (void *)m_Pointer < (void *)r.m_Pointer; }
160 
162  bool operator>(const AutoPointer & r) const
163  { return (void *)m_Pointer > (void *)r.m_Pointer; }
164 
166  bool operator<=(const AutoPointer & r) const
167  { return (void *)m_Pointer <= (void *)r.m_Pointer; }
168 
170  bool operator>=(const AutoPointer & r) const
171  { return (void *)m_Pointer >= (void *)r.m_Pointer; }
172 
175  {
176  AutoPointer(r).Swap(*this);
177  return *this;
178  }
180 
183  operator bool() const
184  { return ( m_Pointer != nullptr ); }
185 
187 /* ObjectType *Print (std::ostream& os) const
188  {
189  // This prints the object pointed to by the pointer
190  (*m_Pointer).Print(os);
191  os << "Owner: " << m_IsOwner << std::endl;
192  return m_Pointer;
193  }
194 */
196 
197 private:
198 
200  void Swap(AutoPointer & r)
201  noexcept
202  {
203  ObjectType *temp = m_Pointer;
204 
205  m_Pointer = r.m_Pointer;
206  r.m_Pointer = temp;
207  }
208 
211  bool m_IsOwner{false};
212 };
213 
214 template< typename T >
215 std::ostream & operator<<(std::ostream & os, AutoPointer< T > p)
216 {
217  p.Print(os);
218  os << "Owner: " << p.IsOwner() << std::endl;
219  return os;
220 }
221 
224 template< typename TAutoPointerBase, typename TAutoPointerDerived >
225 void
226  TransferAutoPointer(TAutoPointerBase & pa, TAutoPointerDerived & pb)
227 {
228  // give a chance to natural polymorphism
229  pa.TakeNoOwnership( pb.GetPointer() );
230  if ( pb.IsOwner() )
231  {
232  pa.TakeOwnership(); // pa Take Ownership
233  pb.ReleaseOwnership(); // pb Release Ownership and clears
234  }
235 }
236 } // end namespace itk
238 
239 #endif
bool operator==(const AutoPointer &r) const
bool operator<=(const AutoPointer &r) const
ObjectType * operator->() const
ObjectType * GetPointer() const
ObjectType * m_Pointer
bool operator>=(const AutoPointer &r) const
TObjectType ObjectType
void TakeNoOwnership(ObjectType *objectptr)
bool operator!=(const AutoPointer &r) const
ObjectType * ReleaseOwnership()
bool operator>(const AutoPointer &r) const
AutoPointer(ObjectType *p, bool takeOwnership)
AutoPointer(AutoPointer &p)
void TransferAutoPointer(TAutoPointerBase &pa, TAutoPointerDerived &pb)
AutoPointer & operator=(AutoPointer &r)
void Swap(AutoPointer &r) noexcept
void TakeOwnership(ObjectType *objectptr)
bool operator<(const AutoPointer &r) const
Implements an Automatic Pointer to an object.
bool IsOwner() const