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 
24 namespace itk
25 {
43 template <typename TObjectType>
45 {
46 public:
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 
89  ObjectType *
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  }
154 private:
156  ObjectType * m_Pointer{ nullptr };
157 };
158 
159 template <typename T>
160 std::ostream &
161 operator<<(std::ostream & os, const WeakPointer<T> p)
162 {
163  p.Print(os);
164  return os;
165 }
166 } // end namespace itk
167 
168 #endif
itk::WeakPointer::operator==
bool operator==(R r) const
Definition: itkWeakPointer.h:74
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::WeakPointer::operator<
bool operator<(const WeakPointer &r) const
Definition: itkWeakPointer.h:111
itk::WeakPointer::operator>
bool operator>(const WeakPointer &r) const
Definition: itkWeakPointer.h:118
itkMacro.h
itk::WeakPointer::operator!=
bool operator!=(R r) const
Definition: itkWeakPointer.h:82
itk::WeakPointer::operator>=
bool operator>=(const WeakPointer &r) const
Definition: itkWeakPointer.h:132
itk::WeakPointer::ObjectType
TObjectType ObjectType
Definition: itkWeakPointer.h:48
itk::WeakPointer::operator<=
bool operator<=(const WeakPointer &r) const
Definition: itkWeakPointer.h:125
itk::WeakPointer::m_Pointer
ObjectType * m_Pointer
Definition: itkWeakPointer.h:156
itk::WeakPointer::IsNull
bool IsNull() const
Definition: itkWeakPointer.h:104
itk::WeakPointer
Implements a weak reference to an object.
Definition: itkWeakPointer.h:44
itk::WeakPointer::WeakPointer
WeakPointer(std::nullptr_t)
Definition: itkWeakPointer.h:58
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::WeakPointer::IsNotNull
bool IsNotNull() const
Definition: itkWeakPointer.h:97
itk::WeakPointer::WeakPointer
WeakPointer(ObjectType *p)
Definition: itkWeakPointer.h:61
itk::WeakPointer::Print
ObjectType * Print(std::ostream &os) const
Definition: itkWeakPointer.h:139
itk::WeakPointer::GetPointer
ObjectType * GetPointer() const
Definition: itkWeakPointer.h:90
itk::WeakPointer::WeakPointer
WeakPointer()=default
itk::WeakPointer::operator->
ObjectType * operator->() const
Definition: itkWeakPointer.h:66