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
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 }
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 *
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
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 */
221private:
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
237template <typename T>
238std::ostream &
239operator<<(std::ostream & os, const AutoPointer<T> p)
240{
241 p.Print(os);
242 os << "Owner: " << p.IsOwner() << std::endl;
243 return os;
244}
245
248template <typename TAutoPointerBase, typename TAutoPointerDerived>
249void
250TransferAutoPointer(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
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 * m_Pointer
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)