ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkWeakPointer.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 itkWeakPointer_h
19#define itkWeakPointer_h
20
21#include "itkMacro.h"
22#include <iostream>
23
24namespace itk
25{
43template <typename TObjectType>
45{
46public:
48 using ObjectType = TObjectType;
49
55 WeakPointer() = default;
56
58 WeakPointer(std::nullptr_t) {}
59
62 : m_Pointer(p)
63 {}
64
67 operator->() const
68 {
69 return m_Pointer;
70 }
71
73 operator ObjectType *() const { return m_Pointer; }
74
76 template <typename R>
77 bool
78 operator==(R r) const
79 {
80 return (m_Pointer == (ObjectType *)r);
81 }
82
83#ifndef ITK_EXPERIMENTAL_CXX20_REWRITTEN_UNEQUAL_OPERATOR
84 template <typename R>
85 bool
86 operator!=(R r) const
87 {
88 return !(this->operator==(r));
89 }
90#endif
91
94 GetPointer() const
95 {
96 return m_Pointer;
97 }
98
100 bool
101 IsNotNull() const
102 {
103 return m_Pointer != nullptr;
104 }
105
107 bool
108 IsNull() const
109 {
110 return m_Pointer == nullptr;
111 }
112
114 bool
115 operator<(const WeakPointer & r) const
116 {
117 return (void *)m_Pointer < (void *)r.m_Pointer;
118 }
119
121 bool
122 operator>(const WeakPointer & r) const
123 {
124 return (void *)m_Pointer > (void *)r.m_Pointer;
125 }
126
128 bool
129 operator<=(const WeakPointer & r) const
130 {
131 return (void *)m_Pointer <= (void *)r.m_Pointer;
132 }
133
135 bool
136 operator>=(const WeakPointer & r) const
137 {
138 return (void *)m_Pointer >= (void *)r.m_Pointer;
139 }
140
142 ObjectType *
143 Print(std::ostream & os) const
144 {
145 if (this->IsNull())
146 {
147 os << "(null)";
148 }
149 else
150 {
151 // This prints the object pointed to by the pointer
152 m_Pointer->Print(os);
153 }
154 return m_Pointer;
155 }
156
157
158private:
160 ObjectType * m_Pointer{ nullptr };
161};
162
163template <typename T>
164std::ostream &
165operator<<(std::ostream & os, const WeakPointer<T> p)
166{
167 p.Print(os);
168 return os;
169}
170} // end namespace itk
171
172#endif
Implements a weak reference to an object.
ObjectType * GetPointer() const
bool IsNotNull() const
bool operator>=(const WeakPointer &r) const
ObjectType * Print(std::ostream &os) const
bool operator!=(R r) const
WeakPointer(ObjectType *p)
bool operator>(const WeakPointer &r) const
bool operator<=(const WeakPointer &r) const
bool operator==(R r) const
bool operator<(const WeakPointer &r) const
bool IsNull() const
ObjectType * operator->() const
WeakPointer()=default
WeakPointer(std::nullptr_t)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)