ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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
24namespace itk
25{
45template <typename TObjectType>
47{
48public:
50 using ObjectType = TObjectType;
52
55 : m_Pointer(nullptr)
56 {}
57
60 : m_IsOwner(p.IsOwner())
62 {}
63
65 explicit AutoPointer(ObjectType * p, bool takeOwnership)
66 : m_IsOwner(takeOwnership)
67 , m_Pointer(p)
68 {}
69
71 ~AutoPointer() { this->Reset(); }
72
75 operator->() const
76 {
77 return m_Pointer;
78 }
79
82 void
84 {
85 if (m_IsOwner)
86 {
87 delete m_Pointer;
88 }
89 m_Pointer = nullptr;
90 m_IsOwner = false;
91 }
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 }
111
113 void
115 {
116 if (m_IsOwner)
117 {
118 delete m_Pointer; // remove the current one
119 }
120 m_Pointer = objectptr;
121 m_IsOwner = false;
122 }
123
125 [[nodiscard]] bool
126 IsOwner() const
127 {
128 return m_IsOwner;
129 }
130
143 ObjectType *
145 {
146 m_IsOwner = false;
147 return m_Pointer;
148 }
149
151 [[nodiscard]] ObjectType *
153 {
154 return m_Pointer;
155 }
156
158 bool
159 operator==(const AutoPointer & r) const
160 {
161 return m_Pointer == r.m_Pointer;
162 }
163
165
167 bool
168 operator<(const AutoPointer & r) const
169 {
170 return m_Pointer < r.m_Pointer;
171 }
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
197 {
198 AutoPointer(r).Swap(*this);
199 return *this;
200 }
201
204 operator bool() const { return (m_Pointer != nullptr); }
205
207 /* ObjectType *Print (std::ostream& os) const
208 {
209 // This prints the object pointed to by the pointer
210 m_Pointer->Print(os);
211 os << "Owner: " << m_IsOwner << std::endl;
212 return m_Pointer;
213 }
214 */
215
216private:
218 void
219 Swap(AutoPointer & r) noexcept
220 {
221 ObjectType * temp = m_Pointer;
222
223 m_Pointer = r.m_Pointer;
224 r.m_Pointer = temp;
225 }
226
228 bool m_IsOwner{ false };
230};
231
232template <typename T>
233std::ostream &
234operator<<(std::ostream & os, const AutoPointer<T> p)
235{
236 p.Print(os);
237 os << "Owner: " << p.IsOwner() << std::endl;
238 return os;
239}
240
243template <typename TAutoPointerBase, typename TAutoPointerDerived>
244void
245TransferAutoPointer(TAutoPointerBase & pa, TAutoPointerDerived & pb)
246{
247 // give a chance to natural polymorphism
248 pa.TakeNoOwnership(pb.GetPointer());
249 if (pb.IsOwner())
250 {
251 pa.TakeOwnership(); // pa Take Ownership
252 pb.ReleaseOwnership(); // pb Release Ownership and clears
253 }
254}
255
256} // end namespace itk
257
258#endif
Implements an Automatic Pointer to an object.
ObjectType * operator->() const
void Swap(AutoPointer &r) noexcept
bool operator<(const AutoPointer &r) const
TObjectType ObjectType
bool operator>(const AutoPointer &r) const
AutoPointer(AutoPointer &p)
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
bool operator>=(const AutoPointer &r) const
bool operator<=(const AutoPointer &r) const
void TakeOwnership(ObjectType *objectptr)
AutoPointer(ObjectType *p, bool takeOwnership)
bool operator==(const AutoPointer &r) const
ObjectType * GetPointer() const
void TakeNoOwnership(ObjectType *objectptr)
AutoPointer & operator=(AutoPointer &r)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
void TransferAutoPointer(TAutoPointerBase &pa, TAutoPointerDerived &pb)