Medical Imaging Interaction Toolkit  2023.12.00
Medical Imaging Interaction Toolkit
berryWeakPointer.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef __BERRY_WEAK_POINTER_H__
14 #define __BERRY_WEAK_POINTER_H__
15 
16 #include <exception>
17 #include <iostream>
18 
19 #include "berryMessage.h"
20 #include "berrySmartPointer.h"
21 
22 namespace berry
23 {
24 
35 template<class TObjectType>
36 class WeakPointer
37 {
38 
39 public:
40 
41  typedef TObjectType ObjectType;
42 
45  m_Pointer(nullptr)
46  {
47 
48  }
49 
51  template<class Other>
53  m_Pointer(dynamic_cast<ObjectType*>(sptr.GetPointer()))
54  {
55 
56  if (m_Pointer)
57  m_Pointer->AddDestroyListener(MessageDelegate<WeakPointer> (this,
58  &WeakPointer::ObjectDestroyed));
59 
60  }
61 
63  template<class Other>
65  m_Pointer(dynamic_cast<ObjectType*>(p.m_Pointer))
66  {
67  if (m_Pointer)
68  m_Pointer->AddDestroyListener(MessageDelegate<WeakPointer> (this,
69  &WeakPointer::ObjectDestroyed));
70  }
71 
74  m_Pointer(p.m_Pointer)
75  {
76  if (m_Pointer)
77  m_Pointer->AddDestroyListener(MessageDelegate<WeakPointer> (this,
78  &WeakPointer::ObjectDestroyed));
79  }
80 
81  template<class Other>
83  {
84  if (m_Pointer)
85  m_Pointer->RemoveDestroyListener(MessageDelegate<WeakPointer> (this,
86  &WeakPointer::ObjectDestroyed));
87 
88  m_Pointer = const_cast<ObjectType*>(r.GetPointer());
89  if (m_Pointer)
90  m_Pointer->AddDestroyListener(MessageDelegate<WeakPointer> (this,
91  &WeakPointer::ObjectDestroyed));
92 
93  return *this;
94  }
95 
97  {
98  if (m_Pointer)
99  m_Pointer->RemoveDestroyListener(MessageDelegate<WeakPointer> (this,
100  &WeakPointer::ObjectDestroyed));
101 
102  this->m_Pointer = other.m_Pointer;
103  if (m_Pointer)
104  m_Pointer->AddDestroyListener(MessageDelegate<WeakPointer> (this,
105  &WeakPointer::ObjectDestroyed));
106 
107  return *this;
108  }
109 
110  template<class Other>
112  {
113  if (m_Pointer)
114  m_Pointer->RemoveDestroyListener(MessageDelegate<WeakPointer> (this,
115  &WeakPointer::ObjectDestroyed));
116 
117  this->m_Pointer = const_cast<ObjectType*>(other.m_Pointer);
118  if (m_Pointer)
119  m_Pointer->AddDestroyListener(MessageDelegate<WeakPointer> (this,
120  &WeakPointer::ObjectDestroyed));
121 
122  return *this;
123  }
124 
126  template<typename R>
127  bool operator ==(const R* o) const
128  {
129  return (m_Pointer == nullptr ? o == nullptr : (o && m_Pointer->operator==(o)));
130  }
131 
132  template<typename R>
133  bool operator ==(const WeakPointer<R>& r) const
134  {
135  const R* o = r.m_Pointer;
136  return (m_Pointer == nullptr ? o == nullptr : (o && m_Pointer->operator==(o)));
137  }
138 
140  bool operator <(const WeakPointer &r) const
141  {
142  return (void*) m_Pointer < (void*) r.m_Pointer;
143  }
144 
147  {
148  SmartPointer<ObjectType> sp(m_Pointer);
149  return sp;
150  }
151 
152  void Reset()
153  {
154  if (m_Pointer)
155  m_Pointer->RemoveDestroyListener(MessageDelegate<WeakPointer> (this,
156  &WeakPointer::ObjectDestroyed));
157 
158  m_Pointer = nullptr;
159  }
160 
161  bool Expired() const
162  {
163  return m_Pointer == nullptr;
164  }
165 
168  {
169  if (m_Pointer)
170  m_Pointer->RemoveDestroyListener(MessageDelegate<WeakPointer> (this,
171  &WeakPointer::ObjectDestroyed));
172  }
173 
174 private:
175 
176  template<class Y> friend class SmartPointer;
177  template<class Y> friend class WeakPointer;
178 
179  ObjectType *m_Pointer;
180 
181  void ObjectDestroyed()
182  {
183  m_Pointer = nullptr;
184  }
185 
186 };
187 
188 }
189 
190 #endif // __BERRY_WEAK_POINTER_H__
berry::WeakPointer::WeakPointer
WeakPointer(const WeakPointer &p)
Definition: berryWeakPointer.h:73
berry::WeakPointer::WeakPointer
WeakPointer(const WeakPointer< Other > &p)
Definition: berryWeakPointer.h:64
berry::SmartPointer
Implements transparent reference counting.
Definition: berryICommandCategoryListener.h:21
berry::WeakPointer::operator<
bool operator<(const WeakPointer &r) const
Definition: berryWeakPointer.h:140
berry::WeakPointer::~ WeakPointer
~ WeakPointer()
Definition: berryWeakPointer.h:167
berry::WeakPointer::Lock
SmartPointer< ObjectType > Lock() const
Definition: berryWeakPointer.h:146
berry::WeakPointer::WeakPointer
WeakPointer()
Definition: berryWeakPointer.h:44
berry::SmartPointer::GetPointer
ObjectType * GetPointer() const
Definition: berrySmartPointer.h:209
berryMessage.h
berry::MessageDelegate
Definition: berryMessage.h:124
berry::WeakPointer::Reset
void Reset()
Definition: berryWeakPointer.h:152
berry::WeakPointer::Expired
bool Expired() const
Definition: berryWeakPointer.h:161
berry::WeakPointer::operator==
bool operator==(const R *o) const
Definition: berryWeakPointer.h:127
berry::WeakPointer::operator=
WeakPointer & operator=(const SmartPointer< Other > &r)
Definition: berryWeakPointer.h:82
berry::WeakPointer::ObjectType
TObjectType ObjectType
Definition: berryWeakPointer.h:41
berrySmartPointer.h
berry::WeakPointer::operator=
WeakPointer & operator=(const WeakPointer< Other > &other)
Definition: berryWeakPointer.h:111
berry::WeakPointer::WeakPointer
WeakPointer(berry::SmartPointer< Other > sptr)
Definition: berryWeakPointer.h:52
berry::WeakPointer
implements a WeakPointer class to deal with circular reference problems.
Definition: berrySmartPointer.h:32
berry::WeakPointer::operator=
WeakPointer & operator=(const WeakPointer &other)
Definition: berryWeakPointer.h:96
berry
Definition: QmitkPropertyItemModel.h:24