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 {
61 m_IsOwner = p.IsOwner(); // Ownership can only be taken from
62 // another owner
63 m_Pointer = p.ReleaseOwnership(); // release ownership if appropriate
64 }
65
67 explicit AutoPointer(ObjectType * p, bool takeOwnership)
68 : m_Pointer(p)
69 , m_IsOwner(takeOwnership)
70 {}
71
73 ~AutoPointer() { this->Reset(); }
74
77 operator->() const
78 {
79 return m_Pointer;
80 }
81
84 void
86 {
87 if (m_IsOwner)
88 {
89 delete m_Pointer;
90 }
91 m_Pointer = nullptr;
92 m_IsOwner = false;
93 }
94
96 void
98 {
99 m_IsOwner = true;
100 }
101
103 void
105 {
106 if (m_IsOwner)
107 {
108 delete m_Pointer; // remove the current one
109 }
110 m_Pointer = objectptr;
111 m_IsOwner = true;
112 }
113
115 void
117 {
118 if (m_IsOwner)
119 {
120 delete m_Pointer; // remove the current one
121 }
122 m_Pointer = objectptr;
123 m_IsOwner = false;
124 }
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 }
151
153 ObjectType *
155 {
156 return m_Pointer;
157 }
158
160 bool
161 operator==(const AutoPointer & r) const
162 {
163 return m_Pointer == r.m_Pointer;
164 }
165
167
169 bool
170 operator<(const AutoPointer & r) const
171 {
172 return m_Pointer < r.m_Pointer;
173 }
174
176 bool
177 operator>(const AutoPointer & r) const
178 {
179 return m_Pointer > r.m_Pointer;
180 }
181
183 bool
184 operator<=(const AutoPointer & r) const
185 {
186 return m_Pointer <= r.m_Pointer;
187 }
188
190 bool
191 operator>=(const AutoPointer & r) const
192 {
193 return m_Pointer >= r.m_Pointer;
194 }
195
199 {
200 AutoPointer(r).Swap(*this);
201 return *this;
202 }
203
206 operator bool() const { return (m_Pointer != nullptr); }
207
209 /* ObjectType *Print (std::ostream& os) const
210 {
211 // This prints the object pointed to by the pointer
212 m_Pointer->Print(os);
213 os << "Owner: " << m_IsOwner << std::endl;
214 return m_Pointer;
215 }
216 */
217
218private:
220 void
221 Swap(AutoPointer & r) noexcept
222 {
223 ObjectType * temp = m_Pointer;
224
225 m_Pointer = r.m_Pointer;
226 r.m_Pointer = temp;
227 }
228
231 bool m_IsOwner{ false };
232};
233
234template <typename T>
235std::ostream &
236operator<<(std::ostream & os, const AutoPointer<T> p)
237{
238 p.Print(os);
239 os << "Owner: " << p.IsOwner() << std::endl;
240 return os;
241}
242
245template <typename TAutoPointerBase, typename TAutoPointerDerived>
246void
247TransferAutoPointer(TAutoPointerBase & pa, TAutoPointerDerived & pb)
248{
249 // give a chance to natural polymorphism
250 pa.TakeNoOwnership(pb.GetPointer());
251 if (pb.IsOwner())
252 {
253 pa.TakeOwnership(); // pa Take Ownership
254 pb.ReleaseOwnership(); // pb Release Ownership and clears
255 }
256}
257
258} // end namespace itk
259
260#endif
Implements an Automatic Pointer to an object.
ObjectType * operator->() const
void Swap(AutoPointer &r) noexcept
bool operator<(const AutoPointer &r) const
TObjectType ObjectType
ObjectType * ReleaseOwnership()
bool operator>(const AutoPointer &r) const
AutoPointer(AutoPointer &p)
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
bool IsOwner() const
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)