ITK  6.0.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 *
78  operator->() const
79  {
80  return m_Pointer;
81  }
82 
85  void
87  {
88  if (m_IsOwner)
89  {
90  delete m_Pointer;
91  }
92  m_Pointer = nullptr;
93  m_IsOwner = false;
94  }
98  void
100  {
101  m_IsOwner = true;
102  }
103 
105  void
107  {
108  if (m_IsOwner)
109  {
110  delete m_Pointer; // remove the current one
111  }
112  m_Pointer = objectptr;
113  m_IsOwner = true;
114  }
118  void
120  {
121  if (m_IsOwner)
122  {
123  delete m_Pointer; // remove the current one
124  }
125  m_Pointer = objectptr;
126  m_IsOwner = false;
127  }
131  bool
132  IsOwner() const
133  {
134  return m_IsOwner;
135  }
136 
149  ObjectType *
151  {
152  m_IsOwner = false;
153  return m_Pointer;
154  }
158  ObjectType *
159  GetPointer() const
160  {
161  return m_Pointer;
162  }
163 
165  bool
166  operator==(const AutoPointer & r) const
167  {
168  return m_Pointer == r.m_Pointer;
169  }
170 
172 
174  bool
175  operator<(const AutoPointer & r) const
176  {
177  return m_Pointer < r.m_Pointer;
178  }
179 
181  bool
182  operator>(const AutoPointer & r) const
183  {
184  return m_Pointer > r.m_Pointer;
185  }
186 
188  bool
189  operator<=(const AutoPointer & r) const
190  {
191  return m_Pointer <= r.m_Pointer;
192  }
193 
195  bool
196  operator>=(const AutoPointer & r) const
197  {
198  return m_Pointer >= r.m_Pointer;
199  }
200 
202  AutoPointer &
204  {
205  AutoPointer(r).Swap(*this);
206  return *this;
207  }
212  operator bool() const { return (m_Pointer != nullptr); }
213 
215  /* ObjectType *Print (std::ostream& os) const
216  {
217  // This prints the object pointed to by the pointer
218  m_Pointer->Print(os);
219  os << "Owner: " << m_IsOwner << std::endl;
220  return m_Pointer;
221  }
222  */
225 private:
227  void
228  Swap(AutoPointer & r) noexcept
229  {
230  ObjectType * temp = m_Pointer;
231 
232  m_Pointer = r.m_Pointer;
233  r.m_Pointer = temp;
234  }
235 
238  bool m_IsOwner{ false };
239 };
240 
241 template <typename T>
242 std::ostream &
243 operator<<(std::ostream & os, const AutoPointer<T> p)
244 {
245  p.Print(os);
246  os << "Owner: " << p.IsOwner() << std::endl;
247  return os;
248 }
249 
252 template <typename TAutoPointerBase, typename TAutoPointerDerived>
253 void
254 TransferAutoPointer(TAutoPointerBase & pa, TAutoPointerDerived & pb)
255 {
256  // give a chance to natural polymorphism
257  pa.TakeNoOwnership(pb.GetPointer());
258  if (pb.IsOwner())
259  {
260  pa.TakeOwnership(); // pa Take Ownership
261  pb.ReleaseOwnership(); // pb Release Ownership and clears
262  }
263 }
264 } // end namespace itk
267 #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:182
itk::AutoPointer::ReleaseOwnership
ObjectType * ReleaseOwnership()
Definition: itkAutoPointer.h:150
itk::AutoPointer::operator->
ObjectType * operator->() const
Definition: itkAutoPointer.h:78
itk::AutoPointer::GetPointer
ObjectType * GetPointer() const
Definition: itkAutoPointer.h:159
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::AutoPointer::TakeNoOwnership
void TakeNoOwnership(ObjectType *objectptr)
Definition: itkAutoPointer.h:119
itk::AutoPointer::TakeOwnership
void TakeOwnership()
Definition: itkAutoPointer.h:99
itk::AutoPointer::TakeOwnership
void TakeOwnership(ObjectType *objectptr)
Definition: itkAutoPointer.h:106
itk::AutoPointer::IsOwner
bool IsOwner() const
Definition: itkAutoPointer.h:132
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:196
itk::AutoPointer::operator<=
bool operator<=(const AutoPointer &r) const
Definition: itkAutoPointer.h:189
itk::AutoPointer::m_Pointer
ObjectType * m_Pointer
Definition: itkAutoPointer.h:237
itk::AutoPointer::Reset
void Reset()
Definition: itkAutoPointer.h:86
itk::AutoPointer::operator==
bool operator==(const AutoPointer &r) const
Definition: itkAutoPointer.h:166
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::AutoPointer::AutoPointer
AutoPointer()
Definition: itkAutoPointer.h:54
itk::AutoPointer::operator=
AutoPointer & operator=(AutoPointer &r)
Definition: itkAutoPointer.h:203
itk::TransferAutoPointer
void TransferAutoPointer(TAutoPointerBase &pa, TAutoPointerDerived &pb)
Definition: itkAutoPointer.h:254
itk::AutoPointer::operator<
bool operator<(const AutoPointer &r) const
Definition: itkAutoPointer.h:175
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::AutoPointer::Swap
void Swap(AutoPointer &r) noexcept
Definition: itkAutoPointer.h:228
itk::AutoPointer::ObjectType
TObjectType ObjectType
Definition: itkAutoPointer.h:50
itk::AutoPointer::m_IsOwner
bool m_IsOwner
Definition: itkAutoPointer.h:238
itk::AutoPointer::AutoPointer
AutoPointer(AutoPointer &p)
Definition: itkAutoPointer.h:59