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
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 *
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
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 */
225private:
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
241template <typename T>
242std::ostream &
243operator<<(std::ostream & os, const AutoPointer<T> p)
244{
245 p.Print(os);
246 os << "Owner: " << p.IsOwner() << std::endl;
247 return os;
248}
249
252template <typename TAutoPointerBase, typename TAutoPointerDerived>
253void
254TransferAutoPointer(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
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)