Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
berryWeakPointer.h
Go to the documentation of this file.
1 /*===================================================================
2 
3 BlueBerry Platform
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef __BERRY_WEAK_POINTER_H__
18 #define __BERRY_WEAK_POINTER_H__
19 
20 #include <exception>
21 #include <iostream>
22 
23 #include "berryMessage.h"
24 #include "berrySmartPointer.h"
25 
26 namespace berry
27 {
28 
39 template<class TObjectType>
40 class WeakPointer
41 {
42 
43 public:
44 
45  typedef TObjectType ObjectType;
46 
49  m_Pointer(nullptr)
50  {
51 
52  }
53 
55  template<class Other>
57  m_Pointer(dynamic_cast<ObjectType*>(sptr.GetPointer()))
58  {
59 
60  if (m_Pointer)
61  m_Pointer->AddDestroyListener(MessageDelegate<WeakPointer> (this,
62  &WeakPointer::ObjectDestroyed));
63 
64  }
65 
67  template<class Other>
69  m_Pointer(dynamic_cast<ObjectType*>(p.m_Pointer))
70  {
71  if (m_Pointer)
72  m_Pointer->AddDestroyListener(MessageDelegate<WeakPointer> (this,
73  &WeakPointer::ObjectDestroyed));
74  }
75 
78  m_Pointer(p.m_Pointer)
79  {
80  if (m_Pointer)
81  m_Pointer->AddDestroyListener(MessageDelegate<WeakPointer> (this,
82  &WeakPointer::ObjectDestroyed));
83  }
84 
85  template<class Other>
87  {
88  if (m_Pointer)
89  m_Pointer->RemoveDestroyListener(MessageDelegate<WeakPointer> (this,
90  &WeakPointer::ObjectDestroyed));
91 
92  m_Pointer = const_cast<ObjectType*>(r.GetPointer());
93  if (m_Pointer)
94  m_Pointer->AddDestroyListener(MessageDelegate<WeakPointer> (this,
95  &WeakPointer::ObjectDestroyed));
96 
97  return *this;
98  }
99 
101  {
102  if (m_Pointer)
103  m_Pointer->RemoveDestroyListener(MessageDelegate<WeakPointer> (this,
104  &WeakPointer::ObjectDestroyed));
105 
106  this->m_Pointer = other.m_Pointer;
107  if (m_Pointer)
108  m_Pointer->AddDestroyListener(MessageDelegate<WeakPointer> (this,
109  &WeakPointer::ObjectDestroyed));
110 
111  return *this;
112  }
113 
114  template<class Other>
116  {
117  if (m_Pointer)
118  m_Pointer->RemoveDestroyListener(MessageDelegate<WeakPointer> (this,
119  &WeakPointer::ObjectDestroyed));
120 
121  this->m_Pointer = const_cast<ObjectType*>(other.m_Pointer);
122  if (m_Pointer)
123  m_Pointer->AddDestroyListener(MessageDelegate<WeakPointer> (this,
124  &WeakPointer::ObjectDestroyed));
125 
126  return *this;
127  }
128 
130  template<typename R>
131  bool operator ==(const R* o) const
132  {
133  return (m_Pointer == nullptr ? o == nullptr : (o && m_Pointer->operator==(o)));
134  }
135 
136  template<typename R>
137  bool operator ==(const WeakPointer<R>& r) const
138  {
139  const R* o = r.m_Pointer;
140  return (m_Pointer == nullptr ? o == nullptr : (o && m_Pointer->operator==(o)));
141  }
142 
144  bool operator <(const WeakPointer &r) const
145  {
146  return (void*) m_Pointer < (void*) r.m_Pointer;
147  }
148 
151  {
152  SmartPointer<ObjectType> sp(m_Pointer);
153  return sp;
154  }
155 
156  void Reset()
157  {
158  if (m_Pointer)
159  m_Pointer->RemoveDestroyListener(MessageDelegate<WeakPointer> (this,
160  &WeakPointer::ObjectDestroyed));
161 
162  m_Pointer = nullptr;
163  }
164 
165  bool Expired() const
166  {
167  return m_Pointer == nullptr;
168  }
169 
172  {
173  if (m_Pointer)
174  m_Pointer->RemoveDestroyListener(MessageDelegate<WeakPointer> (this,
175  &WeakPointer::ObjectDestroyed));
176  }
177 
178 private:
179 
180  template<class Y> friend class SmartPointer;
181  template<class Y> friend class WeakPointer;
182 
183  ObjectType *m_Pointer;
184 
185  void ObjectDestroyed()
186  {
187  m_Pointer = nullptr;
188  }
189 
190 };
191 
192 }
193 
194 #endif // __BERRY_WEAK_POINTER_H__
WeakPointer(const WeakPointer &p)
WeakPointer(const WeakPointer< Other > &p)
WeakPointer & operator=(const WeakPointer &other)
TObjectType ObjectType
bool operator<(const WeakPointer &r) const
Implements transparent reference counting.
implements a WeakPointer class to deal with circular reference problems.
WeakPointer(berry::SmartPointer< Other > sptr)
ObjectType * GetPointer() const
WeakPointer & operator=(const WeakPointer< Other > &other)
bool operator==(const R *o) const
SmartPointer< ObjectType > Lock() const
bool Expired() const
WeakPointer & operator=(const SmartPointer< Other > &r)