Medical Imaging Interaction Toolkit  2018.4.99-18ddd2ed
Medical Imaging Interaction Toolkit
berryObject.cpp
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 #include "berryObject.h"
14 
15 #ifdef BLUEBERRY_DEBUG_SMARTPOINTER
16 #include "berryDebugUtil.h"
17 #endif
18 
19 #include "berryLog.h"
20 #include "berryReflection.h"
21 
22 #include <QDebug>
23 
24 #include <list>
25 #include <memory>
26 #include <exception>
27 
28 namespace berry
29 {
30 
32 {
33  this->UnRegister();
34 }
35 
36 #ifdef _WIN32
37 void*
38 Object
39 ::operator new(size_t n)
40 {
41  return new char[n];
42 }
43 
44 void*
45 Object
46 ::operator new[](size_t n)
47 {
48  return new char[n];
49 }
50 
51 void
52 Object
53 ::operator delete(void* m)
54 {
55  delete [] (char*)m;
56 }
57 
58 void
59 Object
60 ::operator delete[](void* m, size_t)
61 {
62  delete [] (char*)m;
63 }
64 #endif
65 
67 {
68  return "berry::Object";
69 }
70 
71 QString Object::GetClassName() const
72 {
73  return Reflection::DemangleName(typeid(*this).name());
74 }
75 
77 {
78  return Reflection::TypeInfo::New<Self>();
79 }
80 
82 {
83  return Self::GetStaticTypeInfo();
84 }
85 
86 QList<Reflection::TypeInfo> Object::GetStaticSuperclasses()
87 {
88  return QList<Reflection::TypeInfo>();
89 }
90 
91 QList<Reflection::TypeInfo> Object::GetSuperclasses() const
92 {
93  return GetStaticSuperclasses();
94 }
95 
96 QDebug Object::Print(QDebug os, Indent indent) const
97 {
98  os = this->PrintHeader(os, indent);
99  os = this->PrintSelf(os, indent.GetNextIndent());
100  return this->PrintTrailer(os, indent);
101 }
102 
103 QString Object::ToString() const
104 {
105  QString str;
106  QDebug ss(&str);
107  this->Print(ss);
108  return str;
109 }
110 
112 {
113  return qHash(this);
114 }
115 
116 bool Object::operator<(const Object* o) const
117 {
118  return this < o;
119 }
120 
121 void Object::Register() const
122 {
123  m_ReferenceCount.ref();
124 }
125 
126 void Object::UnRegister(bool del) const
127 {
128  if (!m_ReferenceCount.deref() && del)
129  {
130  delete this;
131  }
132 }
133 
135 {
136  QMutexLocker lock(&m_ReferenceCountLock);
137  m_ReferenceCount.store(ref);
138 
139  if (ref == 0)
140  {
141  delete this;
142  }
143 }
144 
145 bool Object::operator==(const Object* o) const
146 {
147  return this == o;
148 }
149 
150 #ifdef BLUEBERRY_DEBUG_SMARTPOINTER
151 unsigned int Object::GetTraceId() const
152 {
153  return m_TraceId;
154 }
155 
156 unsigned int& Object::GetTraceIdCounter() const
157 {
158  static unsigned int traceId = 0;
159  return traceId;
160 }
161 #endif
162 
165 {
166 #ifdef BLUEBERRY_DEBUG_SMARTPOINTER
167  unsigned int& id = GetTraceIdCounter();
168  m_TraceId = ++id;
170 #endif
171 }
172 
174 {
179  if (m_ReferenceCount.load() > 0)
180  {
181  // A general exception safety rule is that destructors should
182  // never throw. Something is wrong with a program that reaches
183  // this point anyway. Also this is the least-derived class so the
184  // whole object has been destroyed by this point anyway. Just
185  // issue a warning.
186  BERRY_WARN << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n"
187  << this->GetClassName() << " (" << this
188  << "): Trying to delete object with non-zero reference count.";
189  }
190 
194  m_DestroyMessage.Send();
195 
196 #ifdef BLUEBERRY_DEBUG_SMARTPOINTER
198 #endif
199 }
200 
201 QDebug Object::PrintSelf(QDebug os, Indent Indent) const
202 {
203  QString demangledName = Reflection::DemangleName(typeid(*this).name());
204  os << Indent << "RTTI typeinfo: " << demangledName << '\n';
205  os << Indent << "Reference Count: " << m_ReferenceCount.load() << '\n';
206  return os;
207 }
208 
212 QDebug Object::PrintHeader(QDebug os, Indent Indent) const
213 {
214  os << Indent << this->GetClassName() << " (" << this << ")\n";
215  return os;
216 }
217 
221 QDebug Object::PrintTrailer(QDebug os, Indent /*Indent*/) const
222 {
223  return os;
224 }
225 
226 // ============== Indent related implementations ==============
227 
228 static const char blanks[41] = " ";
229 
231 {
232  int Indent = m_Indent + 2;
233  if (Indent > 40)
234  {
235  Indent = 40;
236  }
237  return Indent;
238 }
239 
240 QDebug operator<<(QDebug os, const berry::Indent& ind)
241 {
242  os.nospace() << blanks + (40 - ind.m_Indent);
243  return os;
244 }
245 
246 } // namespace berry
247 
248 
249 QDebug operator<<(QDebug os, const berry::Object& o)
250 {
251  return o.Print(os);
252 }
253 
254 QDebug operator<<(QDebug os, const berry::SmartPointer<const berry::Object>& o)
255 {
256  return o->Print(os);
257 }
258 
259 QDebug operator<<(QDebug os, const berry::SmartPointer<berry::Object>& o)
260 {
261  return o->Print(os);
262 }
263 
264 QTextStream& operator<<(QTextStream& os, const berry::Object& o)
265 {
266  os << o.ToString();
267  return os;
268 }
269 
270 QTextStream& operator<<(QTextStream& os, const berry::SmartPointer<const berry::Object>& o)
271 {
272  os << o->ToString();
273  return os;
274 }
275 
276 //QTextStream& operator<<(QTextStream& os, const berry::SmartPointer<berry::Object>& o)
277 //{
278 // os << o->ToString();
279 // return os;
280 //}
281 
283 {
284  return o.HashCode();
285 }
virtual QString GetClassName() const
Definition: berryObject.cpp:71
void SetReferenceCount(int)
virtual QDebug PrintTrailer(QDebug os, Indent indent) const
static QList< Reflection::TypeInfo > GetStaticSuperclasses()
Definition: berryObject.cpp:86
virtual bool operator==(const Object *) const
Light weight base class for most BlueBerry classes.
Definition: berryObject.h:72
virtual QDebug PrintHeader(QDebug os, Indent indent) const
Indent GetNextIndent()
QDebug Print(QDebug os, Indent Indent=0) const
Definition: berryObject.cpp:96
static const char * GetStaticClassName()
Definition: berryObject.cpp:66
void Register() const
QString DemangleName(const char *mangledName)
void UnRegister(bool del=true) const
static Reflection::TypeInfo GetStaticTypeInfo()
Definition: berryObject.cpp:76
virtual QString ToString() const
virtual bool operator<(const Object *) const
virtual void Delete()
Definition: berryObject.cpp:31
virtual Reflection::TypeInfo GetTypeInfo() const
Definition: berryObject.cpp:81
static void RegisterObject(const Object *objectPointer)
void Send() const
Definition: berryMessage.h:744
#define BERRY_WARN
Definition: berryLog.h:21
unsigned int uint
QAtomicInt m_ReferenceCount
Definition: berryObject.h:191
virtual ~Object()
QDebug operator<<(QDebug os, const berry::Indent &ind)
QMutex m_ReferenceCountLock
Definition: berryObject.h:194
virtual QList< Reflection::TypeInfo > GetSuperclasses() const
Definition: berryObject.cpp:91
virtual uint HashCode() const
static void UnregisterObject(const Object *objectPointer)
uint qHash(const berry::Object &o)
static const char blanks[41]
virtual QDebug PrintSelf(QDebug os, Indent indent) const