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