13 #ifndef mitkWeakPointer_h
14 #define mitkWeakPointer_h
16 #include <itkCommand.h>
28 : m_RawPointer(
nullptr)
33 : m_RawPointer(rawPointer)
35 this->AddDeleteEventObserver();
39 : m_RawPointer(other.m_RawPointer)
41 this->AddDeleteEventObserver();
45 : m_RawPointer(other.m_RawPointer)
47 other.RemoveDeleteEventObserver();
48 other.m_RawPointer =
nullptr;
49 this->AddDeleteEventObserver();
56 this->RemoveDeleteEventObserver();
72 this->RemoveDeleteEventObserver();
73 m_RawPointer = other.m_RawPointer;
74 this->AddDeleteEventObserver();
85 this->RemoveDeleteEventObserver();
86 m_RawPointer = other.m_RawPointer;
87 other.m_RawPointer =
nullptr;
88 this->AddDeleteEventObserver();
95 this->RemoveDeleteEventObserver();
96 m_RawPointer =
nullptr;
103 if (m_RawPointer != other)
105 this->RemoveDeleteEventObserver();
106 m_RawPointer = other;
107 this->AddDeleteEventObserver();
113 explicit operator bool() const noexcept
115 return nullptr != m_RawPointer;
130 m_DeleteEventCallback = callback;
134 void AddDeleteEventObserver()
136 if (
nullptr != m_RawPointer)
138 auto command = itk::SimpleMemberCommand<WeakPointer>::New();
139 command->SetCallbackFunction(
this, &WeakPointer::OnDeleteEvent);
141 using TWithoutConst =
typename std::remove_const_t<T>;
144 auto nonConstPointer =
const_cast<TWithoutConst*
>(m_RawPointer);
145 m_ObserverTag = nonConstPointer->AddObserver(itk::DeleteEvent(), command);
149 void RemoveDeleteEventObserver()
151 if (
nullptr != m_RawPointer)
153 using TWithoutConst =
typename std::remove_const_t<T>;
156 auto nonConstPointer =
const_cast<TWithoutConst*
>(m_RawPointer);
157 nonConstPointer->RemoveObserver(m_ObserverTag);
161 void OnDeleteEvent() noexcept
166 m_RawPointer =
nullptr;
168 if (m_DeleteEventCallback)
169 m_DeleteEventCallback();
179 return left.m_RawPointer == right.m_RawPointer;
186 return left.m_RawPointer == right;
193 return std::less<T*>()(left.m_RawPointer, right.m_RawPointer);
198 return std::less<T*>()(left.m_RawPointer, right);
203 return std::less<T*>()(left, right.m_RawPointer);
208 return std::less<T*>()(left.m_RawPointer, right);
213 return std::less<T*>()(left, right.m_RawPointer);
221 unsigned long m_ObserverTag;
230 return !(left == right);
236 return !(right < left);
248 return !(left < right);
260 return !(left == right);
272 return !(left == right);
278 return !(right < left);
290 return !(left < right);
296 return !(right < left);
308 return !(left < right);
314 return !(left == right);
320 return !(right < left);
332 return !(left < right);
338 return right == left;
344 return !(right == left);
350 return !(right < left);
362 return !(left < right);
368 return !(left == right);
374 return !(right < left);
386 return !(left < right);
392 return right == left;
398 return !(right == left);
404 return !(right < left);
416 return !(left < right);