ITK  5.3.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  * http://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  template <typename R>
80  bool
81  operator!=(R r) const
82  {
83  return (m_Pointer != (ObjectType *)r);
84  }
85 
87  ObjectType *
88  GetPointer() const
89  {
90  return m_Pointer;
91  }
92 
94  bool
95  IsNotNull() const
96  {
97  return m_Pointer != nullptr;
98  }
99 
101  bool
102  IsNull() const
103  {
104  return m_Pointer == nullptr;
105  }
106 
108  bool
109  operator<(const WeakPointer & r) const
110  {
111  return (void *)m_Pointer < (void *)r.m_Pointer;
112  }
113 
115  bool
116  operator>(const WeakPointer & r) const
117  {
118  return (void *)m_Pointer > (void *)r.m_Pointer;
119  }
120 
122  bool
123  operator<=(const WeakPointer & r) const
124  {
125  return (void *)m_Pointer <= (void *)r.m_Pointer;
126  }
127 
129  bool
130  operator>=(const WeakPointer & r) const
131  {
132  return (void *)m_Pointer >= (void *)r.m_Pointer;
133  }
134 
136  ObjectType *
137  Print(std::ostream & os) const
138  {
139  if (this->IsNull())
140  {
141  os << "(null)";
142  }
143  else
144  {
145  // This prints the object pointed to by the pointer
146  (*m_Pointer).Print(os);
147  }
148  return m_Pointer;
149  }
151 
152 private:
154  ObjectType * m_Pointer{ nullptr };
155 };
156 
157 template <typename T>
158 std::ostream &
159 operator<<(std::ostream & os, const WeakPointer<T> p)
160 {
161  p.Print(os);
162  return os;
163 }
164 } // end namespace itk
165 
166 #endif
itk::WeakPointer::operator==
bool operator==(R r) const
Definition: itkWeakPointer.h:74
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:218
itk::WeakPointer::operator<
bool operator<(const WeakPointer &r) const
Definition: itkWeakPointer.h:109
itk::WeakPointer::operator>
bool operator>(const WeakPointer &r) const
Definition: itkWeakPointer.h:116
itkMacro.h
itk::WeakPointer::operator!=
bool operator!=(R r) const
Definition: itkWeakPointer.h:81
itk::WeakPointer::operator>=
bool operator>=(const WeakPointer &r) const
Definition: itkWeakPointer.h:130
itk::WeakPointer::ObjectType
TObjectType ObjectType
Definition: itkWeakPointer.h:48
itk::WeakPointer::operator<=
bool operator<=(const WeakPointer &r) const
Definition: itkWeakPointer.h:123
itk::WeakPointer::m_Pointer
ObjectType * m_Pointer
Definition: itkWeakPointer.h:154
itk::WeakPointer::IsNull
bool IsNull() const
Definition: itkWeakPointer.h:102
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: itkAnnulusOperator.h:24
itk::WeakPointer::IsNotNull
bool IsNotNull() const
Definition: itkWeakPointer.h:95
itk::WeakPointer::WeakPointer
WeakPointer(ObjectType *p)
Definition: itkWeakPointer.h:61
itk::WeakPointer::Print
ObjectType * Print(std::ostream &os) const
Definition: itkWeakPointer.h:137
itk::WeakPointer::GetPointer
ObjectType * GetPointer() const
Definition: itkWeakPointer.h:88
itk::WeakPointer::WeakPointer
WeakPointer()=default
itk::WeakPointer::operator->
ObjectType * operator->() const
Definition: itkWeakPointer.h:66