ITK  5.1.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 
51  WeakPointer() { m_Pointer = nullptr; }
52 
54  WeakPointer(const WeakPointer<ObjectType> & p) = default;
55 
57  WeakPointer(WeakPointer<ObjectType> && p) = default;
58 
61  : m_Pointer(p)
62  {}
63 
65  ~WeakPointer() = default;
66 
68  ObjectType * operator->() const { return m_Pointer; }
69 
71  operator ObjectType *() const { return m_Pointer; }
72 
74  template <typename R>
75  bool
76  operator==(R r) const
77  {
78  return (m_Pointer == (ObjectType *)r);
79  }
80 
81  template <typename R>
82  bool
83  operator!=(R r) const
84  {
85  return (m_Pointer != (ObjectType *)r);
86  }
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  // cppcheck-suppress operatorEqVarError
139  WeakPointer &
140  operator=(const WeakPointer & r) = default;
141 
142  WeakPointer &
143  operator=(WeakPointer && r) = default;
144 
146  WeakPointer &
148  {
149  m_Pointer = r;
150  return *this;
151  }
152 
154  ObjectType *
155  Print(std::ostream & os) const
156  {
157  if (this->IsNull())
158  {
159  os << "(null)";
160  }
161  else
162  {
163  // This prints the object pointed to by the pointer
164  (*m_Pointer).Print(os);
165  }
166  return m_Pointer;
167  }
169 
170 private:
173 };
174 
175 template <typename T>
176 std::ostream &
177 operator<<(std::ostream & os, const WeakPointer<T> p)
178 {
179  p.Print(os);
180  return os;
181 }
182 } // end namespace itk
183 
184 #endif
itk::WeakPointer::WeakPointer
WeakPointer()
Definition: itkWeakPointer.h:51
itk::WeakPointer::operator==
bool operator==(R r) const
Definition: itkWeakPointer.h:76
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:213
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::~WeakPointer
~WeakPointer()=default
itk::WeakPointer::operator!=
bool operator!=(R r) const
Definition: itkWeakPointer.h:83
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=
WeakPointer & operator=(const WeakPointer &r)=default
itk::WeakPointer::operator<=
bool operator<=(const WeakPointer &r) const
Definition: itkWeakPointer.h:125
itk::WeakPointer::m_Pointer
ObjectType * m_Pointer
Definition: itkWeakPointer.h:172
itk::WeakPointer::IsNull
bool IsNull() const
Definition: itkWeakPointer.h:104
itk::WeakPointer
Implements a weak reference to an object.
Definition: itkWeakPointer.h:44
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::WeakPointer::IsNotNull
bool IsNotNull() const
Definition: itkWeakPointer.h:97
itk::WeakPointer::WeakPointer
WeakPointer(ObjectType *p)
Definition: itkWeakPointer.h:60
itk::WeakPointer::Print
ObjectType * Print(std::ostream &os) const
Definition: itkWeakPointer.h:155
itk::WeakPointer::GetPointer
ObjectType * GetPointer() const
Definition: itkWeakPointer.h:90
itk::WeakPointer::operator=
WeakPointer & operator=(ObjectType *r)
Definition: itkWeakPointer.h:147
itk::WeakPointer::operator->
ObjectType * operator->() const
Definition: itkWeakPointer.h:68