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