Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
berryObject.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 BERRYOBJECT_H_
14 #define BERRYOBJECT_H_
15 
17 
18 #include "berryMacros.h"
19 #include "berryMessage.h"
20 
21 #include <berryConfig.h>
22 
23 #include <QString>
24 #include <QMetaType>
25 #include <QMutex>
26 #include <QAtomicInt>
27 
28 class QDebug;
29 class QTextStream;
30 
31 namespace berry {
32 
34 {
35 
36 public:
37 
39  typedef Indent Self;
40 
42  Indent(int ind = 0) { m_Indent=ind; }
43 
49 
51  friend org_blueberry_core_runtime_EXPORT QDebug operator<<(QDebug os, const Indent& o);
52 
53 private:
54 
55  int m_Indent;
56 };
57 
58 
68 {
69 
70 private:
71 
72  mutable Message<> m_DestroyMessage;
73 
74 public:
75 
76  typedef Object Self;
81 
82  static const char* GetStaticClassName();
83  virtual QString GetClassName() const;
84 
87 
88  static QList<Reflection::TypeInfo> GetStaticSuperclasses();
89  virtual QList<Reflection::TypeInfo> GetSuperclasses() const;
90 
94  virtual void Delete();
95 
102  QDebug Print(QDebug os, Indent Indent=0) const;
103 
108  virtual QString ToString() const;
109 
113  virtual uint HashCode() const;
114 
119  virtual bool operator<(const Object*) const;
120 
122  void Register() const;
123 
127  void UnRegister(bool del = true) const;
128 
130  int GetReferenceCount() const
131  {
132  return m_ReferenceCount.loadRelaxed();
133  }
134 
137  void SetReferenceCount(int);
138 
139  inline void AddDestroyListener(const MessageAbstractDelegate<>& delegate) const
140  { m_DestroyMessage += delegate; }
141  inline void RemoveDestroyListener(const MessageAbstractDelegate<>& delegate) const
142  { m_DestroyMessage -= delegate; }
143 
144 
149  virtual bool operator==(const Object*) const;
150 
151 #ifdef BLUEBERRY_DEBUG_SMARTPOINTER
152  unsigned int GetTraceId() const;
153 private:
154  unsigned int m_TraceId;
155  unsigned int& GetTraceIdCounter() const;
156 public:
157 #endif
158 
159 protected:
160 
162 
164  virtual ~Object();
165 
172  virtual QDebug PrintSelf(QDebug os, Indent indent) const;
173  virtual QDebug PrintHeader(QDebug os, Indent indent) const;
174  virtual QDebug PrintTrailer(QDebug os, Indent indent) const;
175 
177  mutable QAtomicInt m_ReferenceCount;
178 
180  mutable QMutex m_ReferenceCountLock;
181 
182 private:
183 
184  Object(const Self&); //purposely not implemented
185  void operator=(const Self&); //purposely not implemented
186 
187 };
188 
189 // A custom deleter for QScopedPointer
190 // berry::Object instances in a QScopedPointer should have reference count one,
191 // such that they are not accidentally deleted when a temporary smart pointer
192 // pointing to it goes out of scope. This deleter fixes the reference count and
193 // always deletes the instance. Use a berry::SmartPointer if the lifetime should
194 // exceed the one of the pointer.
196 {
197  static inline void cleanup(Object* obj)
198  {
199  if (obj == nullptr) return;
200  obj->UnRegister(false);
201  delete obj;
202  }
203 };
204 
206 
207 }
208 
218 
219 org_blueberry_core_runtime_EXPORT QTextStream& operator<<(QTextStream& os, const berry::Object& o);
221 //org_blueberry_core_runtime_EXPORT QTextStream& operator<<(QTextStream& os, const berry::SmartPointer<berry::Object>& o);
222 
223 Q_DECLARE_METATYPE(berry::Object::Pointer)
224 
226 
227 #endif /*BERRYOBJECT_H_*/
org_blueberry_core_runtime_EXPORT QDebug operator<<(QDebug os, const berry::Object &o)
org_blueberry_core_runtime_EXPORT uint qHash(const berry::Object &o)
friend org_blueberry_core_runtime_EXPORT QDebug operator<<(QDebug os, const Indent &o)
Indent(int ind=0)
Definition: berryObject.h:42
Indent GetNextIndent()
Event/message/notification class.
Definition: berryMessage.h:717
Light weight base class for most BlueBerry classes.
Definition: berryObject.h:68
virtual QDebug PrintHeader(QDebug os, Indent indent) const
berry::WeakPointer< const Self > ConstWeakPtr
Definition: berryObject.h:80
virtual Reflection::TypeInfo GetTypeInfo() const
berry::SmartPointer< const Self > ConstPointer
Definition: berryObject.h:78
berry::WeakPointer< Self > WeakPtr
Definition: berryObject.h:79
virtual QDebug PrintSelf(QDebug os, Indent indent) const
void Register() const
virtual uint HashCode() const
QMutex m_ReferenceCountLock
Definition: berryObject.h:180
QAtomicInt m_ReferenceCount
Definition: berryObject.h:177
void SetReferenceCount(int)
virtual ~Object()
virtual void Delete()
virtual QList< Reflection::TypeInfo > GetSuperclasses() const
berry::SmartPointer< Self > Pointer
Definition: berryObject.h:77
virtual bool operator==(const Object *) const
QDebug Print(QDebug os, Indent Indent=0) const
virtual bool operator<(const Object *) const
void UnRegister(bool del=true) const
static Reflection::TypeInfo GetStaticTypeInfo()
virtual QString GetClassName() const
static const char * GetStaticClassName()
virtual QDebug PrintTrailer(QDebug os, Indent indent) const
void RemoveDestroyListener(const MessageAbstractDelegate<> &delegate) const
Definition: berryObject.h:141
int GetReferenceCount() const
Definition: berryObject.h:130
static QList< Reflection::TypeInfo > GetStaticSuperclasses()
virtual QString ToString() const
void AddDestroyListener(const MessageAbstractDelegate<> &delegate) const
Definition: berryObject.h:139
Implements transparent reference counting.
implements a WeakPointer class to deal with circular reference problems.
org_blueberry_core_runtime_EXPORT QDebug operator<<(QDebug os, const berry::Indent &o)
#define org_blueberry_core_runtime_EXPORT
static void cleanup(Object *obj)
Definition: berryObject.h:197