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