ITK  5.2.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  * 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 
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  }
66 
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  }
92 
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  }
112 
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  }
125 
127  bool
128  IsOwner() const
129  {
130  return m_IsOwner;
131  }
132 
145  ObjectType *
147  {
148  m_IsOwner = false;
149  return m_Pointer;
150  }
152 
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  bool
169  operator!=(const AutoPointer & r) const
170  {
171  return (void *)m_Pointer != (void *)r.m_Pointer;
172  }
173 
175  bool
176  operator<(const AutoPointer & r) const
177  {
178  return (void *)m_Pointer < (void *)r.m_Pointer;
179  }
180 
182  bool
183  operator>(const AutoPointer & r) const
184  {
185  return (void *)m_Pointer > (void *)r.m_Pointer;
186  }
187 
189  bool
190  operator<=(const AutoPointer & r) const
191  {
192  return (void *)m_Pointer <= (void *)r.m_Pointer;
193  }
194 
196  bool
197  operator>=(const AutoPointer & r) const
198  {
199  return (void *)m_Pointer >= (void *)r.m_Pointer;
200  }
201 
203  AutoPointer &
205  {
206  AutoPointer(r).Swap(*this);
207  return *this;
208  }
210 
213  operator bool() const { return (m_Pointer != nullptr); }
214 
216  /* ObjectType *Print (std::ostream& os) const
217  {
218  // This prints the object pointed to by the pointer
219  (*m_Pointer).Print(os);
220  os << "Owner: " << m_IsOwner << std::endl;
221  return m_Pointer;
222  }
223  */
225 
226 private:
228  void
229  Swap(AutoPointer & r) noexcept
230  {
231  ObjectType * temp = m_Pointer;
232 
233  m_Pointer = r.m_Pointer;
234  r.m_Pointer = temp;
235  }
236 
239  bool m_IsOwner{ false };
240 };
241 
242 template <typename T>
243 std::ostream &
244 operator<<(std::ostream & os, const AutoPointer<T> p)
245 {
246  p.Print(os);
247  os << "Owner: " << p.IsOwner() << std::endl;
248  return os;
249 }
250 
253 template <typename TAutoPointerBase, typename TAutoPointerDerived>
254 void
255 TransferAutoPointer(TAutoPointerBase & pa, TAutoPointerDerived & pb)
256 {
257  // give a chance to natural polymorphism
258  pa.TakeNoOwnership(pb.GetPointer());
259  if (pb.IsOwner())
260  {
261  pa.TakeOwnership(); // pa Take Ownership
262  pb.ReleaseOwnership(); // pb Release Ownership and clears
263  }
264 }
265 } // end namespace itk
267 
268 #endif
itk::AutoPointer::operator!=
bool operator!=(const AutoPointer &r) const
Definition: itkAutoPointer.h:169
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:183
itk::AutoPointer::ReleaseOwnership
ObjectType * ReleaseOwnership()
Definition: itkAutoPointer.h:146
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:213
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:197
itk::AutoPointer::operator<=
bool operator<=(const AutoPointer &r) const
Definition: itkAutoPointer.h:190
itk::AutoPointer::m_Pointer
ObjectType * m_Pointer
Definition: itkAutoPointer.h:238
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:204
itk::TransferAutoPointer
void TransferAutoPointer(TAutoPointerBase &pa, TAutoPointerDerived &pb)
Definition: itkAutoPointer.h:255
itk::AutoPointer::operator<
bool operator<(const AutoPointer &r) const
Definition: itkAutoPointer.h:176
itk::AutoPointer::Swap
void Swap(AutoPointer &r) noexcept
Definition: itkAutoPointer.h:229
itk::AutoPointer::ObjectType
TObjectType ObjectType
Definition: itkAutoPointer.h:50
itk::AutoPointer::m_IsOwner
bool m_IsOwner
Definition: itkAutoPointer.h:239
itk::AutoPointer::AutoPointer
AutoPointer(AutoPointer &p)
Definition: itkAutoPointer.h:59