ITK  5.4.0
Insight Toolkit
itkAutoPointer.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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  * https://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 
55  : m_Pointer(nullptr)
56  {}
57 
59  explicit AutoPointer(AutoPointer & p)
60  {
61  m_IsOwner = p.IsOwner(); // Ownership can only be taken from
62  // another owner
63  m_Pointer = p.ReleaseOwnership(); // release ownership if appropriate
64  }
68  explicit AutoPointer(ObjectType * p, bool takeOwnership)
69  : m_Pointer(p)
70  , m_IsOwner(takeOwnership)
71  {}
72 
74  ~AutoPointer() { this->Reset(); }
75 
77  ObjectType * operator->() const { return m_Pointer; }
78 
81  void
83  {
84  if (m_IsOwner)
85  {
86  delete m_Pointer;
87  }
88  m_Pointer = nullptr;
89  m_IsOwner = false;
90  }
94  void
96  {
97  m_IsOwner = true;
98  }
99 
101  void
103  {
104  if (m_IsOwner)
105  {
106  delete m_Pointer; // remove the current one
107  }
108  m_Pointer = objectptr;
109  m_IsOwner = true;
110  }
114  void
116  {
117  if (m_IsOwner)
118  {
119  delete m_Pointer; // remove the current one
120  }
121  m_Pointer = objectptr;
122  m_IsOwner = false;
123  }
127  bool
128  IsOwner() const
129  {
130  return m_IsOwner;
131  }
132 
145  ObjectType *
147  {
148  m_IsOwner = false;
149  return m_Pointer;
150  }
154  ObjectType *
155  GetPointer() const
156  {
157  return m_Pointer;
158  }
159 
161  bool
162  operator==(const AutoPointer & r) const
163  {
164  return (void *)m_Pointer == (void *)r.m_Pointer;
165  }
166 
168 
170  bool
171  operator<(const AutoPointer & r) const
172  {
173  return (void *)m_Pointer < (void *)r.m_Pointer;
174  }
175 
177  bool
178  operator>(const AutoPointer & r) const
179  {
180  return (void *)m_Pointer > (void *)r.m_Pointer;
181  }
182 
184  bool
185  operator<=(const AutoPointer & r) const
186  {
187  return (void *)m_Pointer <= (void *)r.m_Pointer;
188  }
189 
191  bool
192  operator>=(const AutoPointer & r) const
193  {
194  return (void *)m_Pointer >= (void *)r.m_Pointer;
195  }
196 
198  AutoPointer &
200  {
201  AutoPointer(r).Swap(*this);
202  return *this;
203  }
208  operator bool() const { return (m_Pointer != nullptr); }
209 
211  /* ObjectType *Print (std::ostream& os) const
212  {
213  // This prints the object pointed to by the pointer
214  m_Pointer->Print(os);
215  os << "Owner: " << m_IsOwner << std::endl;
216  return m_Pointer;
217  }
218  */
221 private:
223  void
224  Swap(AutoPointer & r) noexcept
225  {
226  ObjectType * temp = m_Pointer;
227 
228  m_Pointer = r.m_Pointer;
229  r.m_Pointer = temp;
230  }
231 
234  bool m_IsOwner{ false };
235 };
236 
237 template <typename T>
238 std::ostream &
239 operator<<(std::ostream & os, const AutoPointer<T> p)
240 {
241  p.Print(os);
242  os << "Owner: " << p.IsOwner() << std::endl;
243  return os;
244 }
245 
248 template <typename TAutoPointerBase, typename TAutoPointerDerived>
249 void
250 TransferAutoPointer(TAutoPointerBase & pa, TAutoPointerDerived & pb)
251 {
252  // give a chance to natural polymorphism
253  pa.TakeNoOwnership(pb.GetPointer());
254  if (pb.IsOwner())
255  {
256  pa.TakeOwnership(); // pa Take Ownership
257  pb.ReleaseOwnership(); // pb Release Ownership and clears
258  }
259 }
260 } // end namespace itk
263 #endif
itk::AutoPointer::ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
itk::AutoPointer::~AutoPointer
~AutoPointer()
Definition: itkAutoPointer.h:74
itk::AutoPointer
Implements an Automatic Pointer to an object.
Definition: itkAutoPointer.h:46
itk::AutoPointer::operator>
bool operator>(const AutoPointer &r) const
Definition: itkAutoPointer.h:178
itk::AutoPointer::ReleaseOwnership
ObjectType * ReleaseOwnership()
Definition: itkAutoPointer.h:146
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
itk::AutoPointer::operator->
ObjectType * operator->() const
Definition: itkAutoPointer.h:77
itk::AutoPointer::GetPointer
ObjectType * GetPointer() const
Definition: itkAutoPointer.h:155
itk::AutoPointer::TakeNoOwnership
void TakeNoOwnership(ObjectType *objectptr)
Definition: itkAutoPointer.h:115
itk::AutoPointer::TakeOwnership
void TakeOwnership()
Definition: itkAutoPointer.h:95
itk::AutoPointer::TakeOwnership
void TakeOwnership(ObjectType *objectptr)
Definition: itkAutoPointer.h:102
itk::AutoPointer::IsOwner
bool IsOwner() const
Definition: itkAutoPointer.h:128
itk::AutoPointer::AutoPointer
AutoPointer(ObjectType *p, bool takeOwnership)
Definition: itkAutoPointer.h:68
itkMacro.h
itk::AutoPointer::operator>=
bool operator>=(const AutoPointer &r) const
Definition: itkAutoPointer.h:192
itk::AutoPointer::operator<=
bool operator<=(const AutoPointer &r) const
Definition: itkAutoPointer.h:185
itk::AutoPointer::m_Pointer
ObjectType * m_Pointer
Definition: itkAutoPointer.h:233
itk::AutoPointer::Reset
void Reset()
Definition: itkAutoPointer.h:82
itk::AutoPointer::operator==
bool operator==(const AutoPointer &r) const
Definition: itkAutoPointer.h:162
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::AutoPointer::AutoPointer
AutoPointer()
Definition: itkAutoPointer.h:54
itk::AutoPointer::operator=
AutoPointer & operator=(AutoPointer &r)
Definition: itkAutoPointer.h:199
itk::TransferAutoPointer
void TransferAutoPointer(TAutoPointerBase &pa, TAutoPointerDerived &pb)
Definition: itkAutoPointer.h:250
itk::AutoPointer::operator<
bool operator<(const AutoPointer &r) const
Definition: itkAutoPointer.h:171
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::AutoPointer::Swap
void Swap(AutoPointer &r) noexcept
Definition: itkAutoPointer.h:224
itk::AutoPointer::ObjectType
TObjectType ObjectType
Definition: itkAutoPointer.h:50
itk::AutoPointer::m_IsOwner
bool m_IsOwner
Definition: itkAutoPointer.h:234
itk::AutoPointer::AutoPointer
AutoPointer(AutoPointer &p)
Definition: itkAutoPointer.h:59