Medical Imaging Interaction Toolkit  2024.06.00
Medical Imaging Interaction Toolkit
mitkReferenceCountWatcher.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 mitkReferenceCountWatcher_h
14 #define mitkReferenceCountWatcher_h
15 
16 #include "itkCommand.h"
17 #include <MitkCoreExports.h>
18 #include <mitkCommon.h>
19 
20 namespace mitk
21 {
22  //##Documentation
23  //## @brief Keeps track of the reference count of an object even if
24  //## it is destroyed.
25  //## @ingroup Testing
26  class ReferenceCountWatcher : public itk::Object
27  {
28  public:
29  typedef itk::SimpleMemberCommand<ReferenceCountWatcher> CommandType;
30 
32 
33  protected:
34  //##Documentation
35  //## @brief Object to be watched
36  itk::Object *m_Object;
37 
38  //##Documentation
39  //## @brief Optional comment, e.g. for debugging output
40  std::string m_Comment;
41 
42  //##Documentation
43  //## @brief If \a true, \a m_Object is no longer valid
44  //## and the returned reference count will be 0.
45  bool m_Deleted;
46 
47  //##Documentation
48  //## @brief itk::Command to get a notification when the object
49  //## is deleted.
50  CommandType::Pointer m_DeleteCommand;
51 
52  public:
53  //##Documentation
54  //## @brief Constructor requiring object to be watched and allowing
55  //## an optional comment.
56  ReferenceCountWatcher(itk::Object *o, const char *comment = "")
57  : m_Object(o), m_Comment(comment), m_Deleted(false), m_ObserverTag(0)
58  {
59  m_DeleteCommand = CommandType::New();
60  m_DeleteCommand->SetCallbackFunction(this, &ReferenceCountWatcher::DeleteObserver);
61  if (m_Object != nullptr)
62  m_ObserverTag = m_Object->AddObserver(itk::DeleteEvent(), m_DeleteCommand);
63  m_ReferenceCount = 0;
64  }
65  //##Documentation
66  //## @brief Destructor: remove observer
68  {
69  if ((m_Deleted == false) && (m_Object != nullptr))
70  {
71  m_Object->RemoveObserver(m_ObserverTag);
72  }
73  }
74  //##Documentation
75  //## @brief Return the reference count of the watched object or
76  //## 0 if it has been destroyed
77  int GetReferenceCount() const override
78  {
79  if (m_Object == nullptr)
80  return -1;
81  if (m_Deleted)
82  return 0;
83  return m_Object->GetReferenceCount();
84  }
85 
86  //##Documentation
87  //## @brief Return the optional string comment
88  itkGetStringMacro(Comment);
89 
90  protected:
91  //##Documentation
92  //## @brief Callback called on itk::DeleteEvent() of wathched object.
93  void DeleteObserver() { m_Deleted = true; }
94  unsigned long m_ObserverTag;
95  };
96 }
97 
98 #endif
mitk::ReferenceCountWatcher::~ReferenceCountWatcher
~ReferenceCountWatcher() override
Destructor: remove observer.
Definition: mitkReferenceCountWatcher.h:67
mitkClassMacroItkParent
#define mitkClassMacroItkParent(className, SuperClassName)
Definition: mitkCommon.h:45
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitk::ReferenceCountWatcher::GetReferenceCount
int GetReferenceCount() const override
Return the reference count of the watched object or 0 if it has been destroyed.
Definition: mitkReferenceCountWatcher.h:77
MitkCoreExports.h
mitk::ReferenceCountWatcher::m_Object
itk::Object * m_Object
Object to be watched.
Definition: mitkReferenceCountWatcher.h:31
mitk::ReferenceCountWatcher::DeleteObserver
void DeleteObserver()
Callback called on itk::DeleteEvent() of wathched object.
Definition: mitkReferenceCountWatcher.h:93
mitkCommon.h
mitk::ReferenceCountWatcher::ReferenceCountWatcher
ReferenceCountWatcher(itk::Object *o, const char *comment="")
Constructor requiring object to be watched and allowing an optional comment.
Definition: mitkReferenceCountWatcher.h:56
mitk::ReferenceCountWatcher::CommandType
itk::SimpleMemberCommand< ReferenceCountWatcher > CommandType
Definition: mitkReferenceCountWatcher.h:29
mitk::ReferenceCountWatcher::m_ObserverTag
unsigned long m_ObserverTag
Definition: mitkReferenceCountWatcher.h:94
mitk::ReferenceCountWatcher::m_Comment
std::string m_Comment
Optional comment, e.g. for debugging output.
Definition: mitkReferenceCountWatcher.h:40
mitk::ReferenceCountWatcher::m_Deleted
bool m_Deleted
If true, m_Object is no longer valid and the returned reference count will be 0.
Definition: mitkReferenceCountWatcher.h:45
mitk::ReferenceCountWatcher::m_DeleteCommand
CommandType::Pointer m_DeleteCommand
itk::Command to get a notification when the object is deleted.
Definition: mitkReferenceCountWatcher.h:50
mitk::ReferenceCountWatcher
Keeps track of the reference count of an object even if it is destroyed.
Definition: mitkReferenceCountWatcher.h:26