Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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  //##
23  //## Example usage:
24  //## \code
25  //## SomeFilter* filter = GetSomeFilter();
26  //## ReferenceCountWatcher::Pointer filterWatcher;
27  //## filterWatcher = new ReferenceCountWatcher(filter, "name of filter");
28  //## filterWatcher->GetReferenceCount();
29  //## \endcode
30  //## @ingroup Testing
31  class ReferenceCountWatcher : public itk::Object
32  {
33  public:
34  typedef itk::SimpleMemberCommand<ReferenceCountWatcher> CommandType;
35 
37 
38  protected:
39  //##Documentation
40  //## @brief Object to be watched
41  itk::Object *m_Object;
42 
43  //##Documentation
44  //## @brief Optional comment, e.g. for debugging output
45  std::string m_Comment;
46 
47  //##Documentation
48  //## @brief If \a true, \a m_Object is no longer valid
49  //## and the returned reference count will be 0.
50  bool m_Deleted;
51 
52  //##Documentation
53  //## @brief itk::Command to get a notification when the object
54  //## is deleted.
55  CommandType::Pointer m_DeleteCommand;
56 
57  public:
58  //##Documentation
59  //## @brief Constructor requiring object to be watched and allowing
60  //## an optional comment.
61  ReferenceCountWatcher(itk::Object *o, const char *comment = "")
62  : m_Object(o), m_Comment(comment), m_Deleted(false), m_ObserverTag(0)
63  {
64  m_DeleteCommand = CommandType::New();
65  m_DeleteCommand->SetCallbackFunction(this, &ReferenceCountWatcher::DeleteObserver);
66  if (m_Object != nullptr)
67  m_ObserverTag = m_Object->AddObserver(itk::DeleteEvent(), m_DeleteCommand);
68  m_ReferenceCount = 0;
69  }
70  //##Documentation
71  //## @brief Destructor: remove observer
73  {
74  if ((m_Deleted == false) && (m_Object != nullptr))
75  {
76  m_Object->RemoveObserver(m_ObserverTag);
77  }
78  }
79  //##Documentation
80  //## @brief Return the reference count of the watched object or
81  //## 0 if it has been destroyed
82  int GetReferenceCount() const override
83  {
84  if (m_Object == nullptr)
85  return -1;
86  if (m_Deleted)
87  return 0;
88  return m_Object->GetReferenceCount();
89  }
90 
91  //##Documentation
92  //## @brief Return the optional string comment
93  itkGetStringMacro(Comment);
94 
95  protected:
96  //##Documentation
97  //## @brief Callback called on itk::DeleteEvent() of wathched object.
98  void DeleteObserver() { m_Deleted = true; }
99  unsigned long m_ObserverTag;
100  };
101 }
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:49
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.