Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkSmartPointerProperty.cpp
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 
18 
19 mitk::SmartPointerProperty::ReferenceCountMapType mitk::SmartPointerProperty::m_ReferenceCount;
20 mitk::SmartPointerProperty::ReferencesUIDMapType mitk::SmartPointerProperty::m_ReferencesUID;
21 mitk::SmartPointerProperty::ReadInSmartPointersMapType mitk::SmartPointerProperty::m_ReadInInstances;
22 mitk::SmartPointerProperty::ReadInTargetsMapType mitk::SmartPointerProperty::m_ReadInTargets;
23 mitk::UIDGenerator mitk::SmartPointerProperty::m_UIDGenerator("POINTER_");
24 
26 {
27  for (auto iter = m_ReadInInstances.begin(); iter != m_ReadInInstances.end(); ++iter)
28  {
29  if (m_ReadInTargets.find(iter->second) != m_ReadInTargets.end())
30  {
31  iter->first->SetSmartPointer(m_ReadInTargets[iter->second]);
32  }
33  }
34 
35  m_ReadInInstances.clear();
36 }
37 
39 unsigned int mitk::SmartPointerProperty::GetReferenceCountFor(itk::Object *object)
40 {
41  if (m_ReferenceCount.find(object) != m_ReferenceCount.end())
42  {
43  return m_ReferenceCount[object];
44  }
45  else
46  {
47  return 0;
48  }
49 }
50 
51 void mitk::SmartPointerProperty::RegisterPointerTarget(itk::Object *object, const std::string uid)
52 {
53  m_ReadInTargets[uid] = object;
54 }
55 
56 std::string mitk::SmartPointerProperty::GetReferenceUIDFor(itk::Object *object)
57 {
58  if (m_ReferencesUID.find(object) != m_ReferencesUID.end())
59  {
60  return m_ReferencesUID[object];
61  }
62  else
63  {
64  return std::string("invalid");
65  }
66 }
67 
68 bool mitk::SmartPointerProperty::IsEqual(const BaseProperty &property) const
69 {
70  return this->m_SmartPointer == static_cast<const Self &>(property).m_SmartPointer;
71 }
72 
73 bool mitk::SmartPointerProperty::Assign(const BaseProperty &property)
74 {
75  this->m_SmartPointer = static_cast<const Self &>(property).m_SmartPointer;
76  return true;
77 }
78 
80 {
81  SetSmartPointer(pointer);
82 }
83 
85  : BaseProperty(other), m_SmartPointer(other.m_SmartPointer)
86 {
87 }
88 
90 {
91  return m_SmartPointer;
92 }
93 
95 {
96  return this->GetSmartPointer();
97 }
98 
100 {
101  if (m_SmartPointer.GetPointer() != pointer)
102  {
103  // keep track of referenced objects
104  if (m_SmartPointer.GetPointer() &&
105  --m_ReferenceCount[m_SmartPointer.GetPointer()] == 0) // if there is no reference left, delete entry
106  {
107  m_ReferenceCount.erase(m_SmartPointer.GetPointer());
108  m_ReferencesUID.erase(m_SmartPointer.GetPointer());
109  }
110 
111  if (pointer && ++m_ReferenceCount[pointer] == 1) // first reference --> generate UID
112  {
113  m_ReferencesUID[pointer] = m_UIDGenerator.GetUID();
114  }
115 
116  // change pointer
117  m_SmartPointer = pointer;
118  Modified();
119  }
120 }
121 
123 {
124  this->SetSmartPointer(value.GetPointer());
125 }
126 
128 {
129  if (m_SmartPointer.IsNotNull())
130  return m_ReferencesUID[m_SmartPointer.GetPointer()];
131  else
132  return std::string("NULL");
133 }
134 
135 itk::LightObject::Pointer mitk::SmartPointerProperty::InternalClone() const
136 {
137  itk::LightObject::Pointer result(new Self(*this));
138  result->UnRegister();
139  return result;
140 }
itk::SmartPointer< Self > Pointer
Generated unique IDs.
Property containing a smart-pointer.
virtual std::string GetValueAsString() const override
mainly for XML output
SmartPointerProperty(itk::Object *=nullptr)
itk::Object::Pointer GetSmartPointer() const
Abstract base class for properties.
static std::string GetReferenceUIDFor(itk::Object *)
static void RegisterPointerTarget(itk::Object *, const std::string uid)
static unsigned int GetReferenceCountFor(itk::Object *)
Return the number of SmartPointerProperties that reference the object given as parameter.