ITK  6.0.0
Insight Toolkit
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
66 ObjectType * operator->() const { return m_Pointer; }
67
69 operator ObjectType *() const { return m_Pointer; }
70
72 template <typename R>
73 bool
74 operator==(R r) const
75 {
76 return (m_Pointer == (ObjectType *)r);
77 }
78
79#ifndef ITK_EXPERIMENTAL_CXX20_REWRITTEN_UNEQUAL_OPERATOR
80 template <typename R>
81 bool
82 operator!=(R r) const
83 {
84 return !(this->operator==(r));
85 }
86#endif
87
90 GetPointer() const
91 {
92 return m_Pointer;
93 }
94
96 bool
97 IsNotNull() const
98 {
99 return m_Pointer != nullptr;
100 }
101
103 bool
104 IsNull() const
105 {
106 return m_Pointer == nullptr;
107 }
108
110 bool
111 operator<(const WeakPointer & r) const
112 {
113 return (void *)m_Pointer < (void *)r.m_Pointer;
114 }
115
117 bool
118 operator>(const WeakPointer & r) const
119 {
120 return (void *)m_Pointer > (void *)r.m_Pointer;
121 }
122
124 bool
125 operator<=(const WeakPointer & r) const
126 {
127 return (void *)m_Pointer <= (void *)r.m_Pointer;
128 }
129
131 bool
132 operator>=(const WeakPointer & r) const
133 {
134 return (void *)m_Pointer >= (void *)r.m_Pointer;
135 }
136
138 ObjectType *
139 Print(std::ostream & os) const
140 {
141 if (this->IsNull())
142 {
143 os << "(null)";
144 }
145 else
146 {
147 // This prints the object pointed to by the pointer
148 m_Pointer->Print(os);
149 }
150 return m_Pointer;
151 }
154private:
156 ObjectType * m_Pointer{ nullptr };
157};
158
159template <typename T>
160std::ostream &
161operator<<(std::ostream & os, const WeakPointer<T> p)
162{
163 p.Print(os);
164 return os;
165}
166} // end namespace itk
167
168#endif
Implements a weak reference to an object.
ObjectType * m_Pointer
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
TObjectType ObjectType
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)