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