Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkPropertyList.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 
17 #include "mitkPropertyList.h"
18 
19 #include "mitkNumericTypes.h"
20 #include "mitkProperties.h"
21 #include "mitkStringProperty.h"
22 
23 mitk::BaseProperty *mitk::PropertyList::GetProperty(const std::string &propertyKey) const
24 {
25  PropertyMap::const_iterator it;
26 
27  it = m_Properties.find(propertyKey);
28  if (it != m_Properties.cend())
29  return it->second;
30  else
31  return nullptr;
32 }
33 
34 void mitk::PropertyList::SetProperty(const std::string &propertyKey, BaseProperty *property)
35 {
36  if (!property)
37  return;
38  // make sure that BaseProperty*, which may have just been created and never been
39  // assigned to a SmartPointer, is registered/unregistered properly. If we do not
40  // do that, it will a) not deleted in case it is identical to the old one or
41  // b) possibly deleted when temporarily added to a smartpointer somewhere below.
42  BaseProperty::Pointer tmpSmartPointerToProperty = property;
43 
44  auto it(m_Properties.find(propertyKey));
45 
46  // Is a property with key @a propertyKey contained in the list?
47  if (it != m_Properties.cend())
48  {
49  // yes
50  // is the property contained in the list identical to the new one?
51  if (it->second->operator==(*property))
52  {
53  // yes? do nothing and return.
54  return;
55  }
56 
57  if (it->second->AssignProperty(*property))
58  {
59  // The assignment was successfull
60  this->Modified();
61  }
62  else
63  {
64  MITK_ERROR << "In " __FILE__ ", l." << __LINE__ << ": Trying to set existing property " << it->first
65  << " of type " << it->second->GetNameOfClass() << " to a property with different type "
66  << property->GetNameOfClass() << "."
67  << " Use ReplaceProperty() instead." << std::endl;
68  }
69  return;
70  }
71 
72  // no? add it.
73  m_Properties.insert(PropertyMap::value_type(propertyKey, property));
74  this->Modified();
75 }
76 
77 void mitk::PropertyList::ReplaceProperty(const std::string &propertyKey, BaseProperty *property)
78 {
79  if (!property)
80  return;
81 
82  auto it(m_Properties.find(propertyKey));
83 
84  // Is a property with key @a propertyKey contained in the list?
85  if (it != m_Properties.cend())
86  {
87  it->second = nullptr;
88  m_Properties.erase(it);
89  }
90 
91  // no? add/replace it.
92  m_Properties.insert(PropertyMap::value_type(propertyKey, property));
93  Modified();
94 }
95 
97 {
98 }
99 
101 {
102  for (auto i = other.m_Properties.cbegin(); i != other.m_Properties.cend(); ++i)
103  {
104  m_Properties.insert(std::make_pair(i->first, i->second->Clone()));
105  }
106 }
107 
109 {
110  Clear();
111 }
112 
116 unsigned long mitk::PropertyList::GetMTime() const
117 {
118  for (auto it = m_Properties.cbegin(); it != m_Properties.cend(); ++it)
119  {
120  if (it->second.IsNull())
121  {
122  itkWarningMacro(<< "Property '" << it->first << "' contains nothing (NULL).");
123  continue;
124  }
125  if (Superclass::GetMTime() < it->second->GetMTime())
126  {
127  Modified();
128  break;
129  }
130  }
131 
132  return Superclass::GetMTime();
133 }
134 
135 bool mitk::PropertyList::DeleteProperty(const std::string &propertyKey)
136 {
137  PropertyMap::iterator it = m_Properties.find(propertyKey);
138 
139  if (it != m_Properties.end())
140  {
141  it->second = nullptr;
142  m_Properties.erase(it);
143  Modified();
144  return true;
145  }
146  return false;
147 }
148 
150 {
151  auto it = m_Properties.begin(), end = m_Properties.end();
152  while (it != end)
153  {
154  it->second = nullptr;
155  ++it;
156  }
157  m_Properties.clear();
158 }
159 
160 itk::LightObject::Pointer mitk::PropertyList::InternalClone() const
161 {
162  itk::LightObject::Pointer result(new Self(*this));
163  result->UnRegister();
164  return result;
165 }
166 
168 {
169  if (pList)
170  {
171  const PropertyMap *propertyMap = pList->GetMap();
172 
173  for (auto iter = propertyMap->cbegin(); // m_PropertyList is created in the constructor, so we don't check it here
174  iter != propertyMap->cend();
175  ++iter)
176  {
177  const std::string key = iter->first;
178  BaseProperty *value = iter->second;
179  if (replace)
180  {
181  ReplaceProperty(key.c_str(), value);
182  }
183  else
184  {
185  SetProperty(key.c_str(), value);
186  }
187  }
188  }
189 }
190 
191 bool mitk::PropertyList::GetBoolProperty(const char *propertyKey, bool &boolValue) const
192 {
193  BoolProperty *gp = dynamic_cast<BoolProperty *>(GetProperty(propertyKey));
194  if (gp != nullptr)
195  {
196  boolValue = gp->GetValue();
197  return true;
198  }
199  return false;
200  // Templated Method does not work on Macs
201  // return GetPropertyValue<bool>(propertyKey, boolValue);
202 }
203 
204 bool mitk::PropertyList::GetIntProperty(const char *propertyKey, int &intValue) const
205 {
206  IntProperty *gp = dynamic_cast<IntProperty *>(GetProperty(propertyKey));
207  if (gp != nullptr)
208  {
209  intValue = gp->GetValue();
210  return true;
211  }
212  return false;
213  // Templated Method does not work on Macs
214  // return GetPropertyValue<int>(propertyKey, intValue);
215 }
216 
217 bool mitk::PropertyList::GetFloatProperty(const char *propertyKey, float &floatValue) const
218 {
219  FloatProperty *gp = dynamic_cast<FloatProperty *>(GetProperty(propertyKey));
220  if (gp != nullptr)
221  {
222  floatValue = gp->GetValue();
223  return true;
224  }
225  return false;
226  // Templated Method does not work on Macs
227  // return GetPropertyValue<float>(propertyKey, floatValue);
228 }
229 
230 bool mitk::PropertyList::GetStringProperty(const char *propertyKey, std::string &stringValue) const
231 {
232  StringProperty *sp = dynamic_cast<StringProperty *>(GetProperty(propertyKey));
233  if (sp != nullptr)
234  {
235  stringValue = sp->GetValue();
236  return true;
237  }
238  return false;
239 }
240 
241 void mitk::PropertyList::SetIntProperty(const char *propertyKey, int intValue)
242 {
243  SetProperty(propertyKey, mitk::IntProperty::New(intValue));
244 }
245 
246 void mitk::PropertyList::SetBoolProperty(const char *propertyKey, bool boolValue)
247 {
248  SetProperty(propertyKey, mitk::BoolProperty::New(boolValue));
249 }
250 
251 void mitk::PropertyList::SetFloatProperty(const char *propertyKey, float floatValue)
252 {
253  SetProperty(propertyKey, mitk::FloatProperty::New(floatValue));
254 }
255 
256 void mitk::PropertyList::SetStringProperty(const char *propertyKey, const char *stringValue)
257 {
258  SetProperty(propertyKey, mitk::StringProperty::New(stringValue));
259 }
260 
261 void mitk::PropertyList::Set(const char *propertyKey, bool boolValue)
262 {
263  this->SetBoolProperty(propertyKey, boolValue);
264 }
265 
266 void mitk::PropertyList::Set(const char *propertyKey, int intValue)
267 {
268  this->SetIntProperty(propertyKey, intValue);
269 }
270 
271 void mitk::PropertyList::Set(const char *propertyKey, float floatValue)
272 {
273  this->SetFloatProperty(propertyKey, floatValue);
274 }
275 
276 void mitk::PropertyList::Set(const char *propertyKey, double doubleValue)
277 {
278  this->SetDoubleProperty(propertyKey, doubleValue);
279 }
280 
281 void mitk::PropertyList::Set(const char *propertyKey, const char *stringValue)
282 {
283  this->SetStringProperty(propertyKey, stringValue);
284 }
285 
286 void mitk::PropertyList::Set(const char *propertyKey, const std::string &stringValue)
287 {
288  this->SetStringProperty(propertyKey, stringValue.c_str());
289 }
290 
291 bool mitk::PropertyList::Get(const char *propertyKey, bool &boolValue) const
292 {
293  return this->GetBoolProperty(propertyKey, boolValue);
294 }
295 
296 bool mitk::PropertyList::Get(const char *propertyKey, int &intValue) const
297 {
298  return this->GetIntProperty(propertyKey, intValue);
299 }
300 
301 bool mitk::PropertyList::Get(const char *propertyKey, float &floatValue) const
302 {
303  return this->GetFloatProperty(propertyKey, floatValue);
304 }
305 
306 bool mitk::PropertyList::Get(const char *propertyKey, double &doubleValue) const
307 {
308  return this->GetDoubleProperty(propertyKey, doubleValue);
309 }
310 
311 bool mitk::PropertyList::Get(const char *propertyKey, std::string &stringValue) const
312 {
313  return this->GetStringProperty(propertyKey, stringValue);
314 }
315 
316 bool mitk::PropertyList::GetDoubleProperty(const char *propertyKey, double &doubleValue) const
317 {
318  DoubleProperty *gp = dynamic_cast<DoubleProperty *>(GetProperty(propertyKey));
319  if (gp != nullptr)
320  {
321  doubleValue = gp->GetValue();
322  return true;
323  }
324  return false;
325 }
326 
327 void mitk::PropertyList::SetDoubleProperty(const char *propertyKey, double doubleValue)
328 {
329  SetProperty(propertyKey, mitk::DoubleProperty::New(doubleValue));
330 }
static Pointer New()
mitk::BaseProperty * GetProperty(const std::string &propertyKey) const
Get a property by its name.
itk::SmartPointer< Self > Pointer
void SetBoolProperty(const char *propertyKey, bool boolValue)
Convenience method to set the value of a BoolProperty.
virtual unsigned long GetMTime() const override
Get the timestamp of the last change of the map or the last change of one of the properties store in ...
signed integer value
Definition: jsoncpp.h:348
#define MITK_ERROR
Definition: mitkLogMacros.h:24
void SetProperty(const std::string &propertyKey, BaseProperty *property)
Set a property in the list/map by value.
void SetDoubleProperty(const char *propertyKey, double doubleValue)
Convenience method to set the value of a DoubleProperty.
bool GetBoolProperty(const char *propertyKey, bool &boolValue) const
Convenience method to access the value of a BoolProperty.
void ConcatenatePropertyList(PropertyList *pList, bool replace=false)
Set a property object in the list/map by reference.
Key-value list holding instances of BaseProperty.
bool GetIntProperty(const char *propertyKey, int &intValue) const
Convenience method to access the value of an IntProperty.
bool DeleteProperty(const std::string &propertyKey)
Remove a property from the list/map.
virtual const char * GetValue() const
static Pointer New()
Abstract base class for properties.
std::map< std::string, BaseProperty::Pointer > PropertyMap
void ReplaceProperty(const std::string &propertyKey, BaseProperty *property)
Set a property object in the list/map by reference.
bool GetDoubleProperty(const char *propertyKey, double &doubleValue) const
Convenience method to access the value of a DoubleProperty.
PropertyMap m_Properties
Map of properties.
bool Get(const char *propertyKey, bool &boolValue) const
ShortCut for the above method.
static Pointer New()
static Pointer New()
UTF-8 string value.
Definition: jsoncpp.h:351
static const char * replace[]
This is a dictionary to replace long names of classes, modules, etc. to shorter versions in the conso...
void Set(const char *propertyKey, bool boolValue)
ShortCut for the above method.
void SetStringProperty(const char *propertyKey, const char *stringValue)
Convenience method to set the value of a StringProperty.
Property for strings.
void SetIntProperty(const char *propertyKey, int intValue)
Convenience method to set the value of an IntProperty.
void SetFloatProperty(const char *propertyKey, float floatValue)
Convenience method to set the value of a FloatProperty.
bool GetStringProperty(const char *propertyKey, std::string &stringValue) const
Convenience method to access the value of a StringProperty.
static Pointer New()
const PropertyMap * GetMap() const
bool GetFloatProperty(const char *propertyKey, float &floatValue) const
Convenience method to access the value of a FloatProperty.
virtual T GetValue() const