Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPropertyPersistence.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 <algorithm>
14 #include <regex>
15 #include <utility>
16 
18 
20 {
21 }
22 
24 {
25 }
26 
28 {
29  if (!info)
30  {
31  return false;
32  }
33 
34  if (info->GetName().empty())
35  {
36  return false;
37  }
38 
40 
41  auto infoRange = m_InfoMap.equal_range(info->GetName());
42 
43  auto predicate = [mime](const std::pair<const std::string, mitk::PropertyPersistenceInfo::ConstPointer> &x) {
44  return x.second.IsNotNull() && x.second->GetMimeTypeName() == mime;
45  };
46 
47  auto finding = std::find_if(infoRange.first, infoRange.second, predicate);
48 
49  bool exists = finding != infoRange.second;
50  bool result = false;
51 
52  if (!exists || overwrite)
53  {
54  if (exists && overwrite)
55  {
56  m_InfoMap.erase(finding);
57  }
58  result = true;
59  m_InfoMap.insert(std::make_pair(info->GetName(), info));
60  }
61 
62  return result;
63 }
64 
65 mitk::PropertyPersistence::InfoMap mitk::PropertyPersistence::SelectInfo(const InfoMap &infoMap,
66  const SelectFunctionType &selectFunction)
67 {
68  InfoMap result;
69 
70  for (auto pos : infoMap)
71  {
72  if (selectFunction(pos))
73  {
74  result.insert(pos);
75  }
76  }
77 
78  return result;
79 };
80 
82  bool allowNameRegEx) const
83 {
84  SelectFunctionType select = [propertyName](const InfoMap::value_type &x) {
85  return x.second.IsNotNull() && !x.second->IsRegEx() && x.second->GetName() == propertyName;
86  };
87 
88  InfoMap selection = SelectInfo(m_InfoMap, select);
89 
90  InfoResultType result;
91  for (const auto &pos : selection)
92  {
93  result.push_back(pos.second->UnRegExByName(propertyName).GetPointer());
94  }
95 
96  if (allowNameRegEx)
97  {
98  select = [propertyName](const InfoMap::value_type &x) {
99  if (x.second.IsNotNull() && x.second->IsRegEx())
100  {
101  std::regex ex(x.second->GetName());
102  return std::regex_match(propertyName, ex);
103  }
104  return false;
105  };
106 
107  selection = SelectInfo(m_InfoMap, select);
108 
109  for (const auto &pos : selection)
110  {
111  result.push_back(pos.second->UnRegExByName(propertyName).GetPointer());
112  }
113  }
114 
115  return result;
116 }
117 
118 bool infoPredicate(const std::multimap<const std::string, mitk::PropertyPersistenceInfo::ConstPointer>::value_type &x,
119  const std::string &propertyName,
120  const std::string &mime)
121 {
122  return x.second.IsNotNull() && !x.second->IsRegEx() && x.second->GetName() == propertyName &&
123  x.second->GetMimeTypeName() == mime;
124 }
125 
127  const std::multimap<const std::string, mitk::PropertyPersistenceInfo::ConstPointer>::value_type &x,
128  const std::string &propertyName,
129  const std::string &mime)
130 {
131  if (x.second.IsNotNull() && x.second->IsRegEx())
132  {
133  std::regex ex(x.second->GetName());
134  return std::regex_match(propertyName, ex) && x.second->GetMimeTypeName() == mime;
135  }
136  return false;
137 }
138 
140  const MimeTypeNameType &mime,
141  bool allowMimeWildCard,
142  bool allowNameRegEx) const
143 {
144  SelectFunctionType select = [propertyName, mime](const InfoMap::value_type &x) {
145  return infoPredicate(x, propertyName, mime);
146  };
147 
148  InfoMap selection = SelectInfo(m_InfoMap, select);
149 
150  if (allowNameRegEx)
151  {
152  select = [propertyName, mime](const InfoMap::value_type &x) { return infoPredicateRegEx(x, propertyName, mime); };
153 
154  InfoMap regExSelection = SelectInfo(m_InfoMap, select);
155 
156  selection.insert(regExSelection.begin(), regExSelection.end());
157  }
158 
159  if (selection.empty() && allowMimeWildCard)
160  { // no perfect match => second run through with "any mime type"
161  select = [propertyName](const InfoMap::value_type &x) {
163  };
164 
165  selection = SelectInfo(m_InfoMap, select);
166 
167  if (allowNameRegEx)
168  {
169  select = [propertyName](const InfoMap::value_type &x) {
171  };
172 
173  InfoMap regExSelection = SelectInfo(m_InfoMap, select);
174 
175  selection.insert(regExSelection.begin(), regExSelection.end());
176  }
177  }
178 
179  InfoResultType result;
180  for (const auto &pos : selection)
181  {
182  result.push_back(pos.second->UnRegExByName(propertyName).GetPointer());
183  }
184 
185  return result;
186 }
187 
189  bool allowKeyRegEx) const
190 {
191  InfoResultType result;
192 
193  for (const auto &pos : m_InfoMap)
194  {
195  if (pos.second.IsNotNull())
196  {
197  bool valid = pos.second->GetKey() == persistenceKey;
198  if (!valid && pos.second->IsRegEx() && allowKeyRegEx)
199  {
200  std::regex ex(pos.second->GetKey());
201  valid = std::regex_match(persistenceKey, ex);
202  }
203 
204  if (valid)
205  {
206  result.push_back(pos.second->UnRegExByKey(persistenceKey).GetPointer());
207  }
208  }
209  }
210 
211  return result;
212 }
213 
214 bool mitk::PropertyPersistence::HasInfo(const std::string &propertyName, bool allowNameRegEx) const
215 {
216  return !this->GetInfo(propertyName, allowNameRegEx).empty();
217 }
218 
220 {
221  m_InfoMap.clear();
222 }
223 
224 void mitk::PropertyPersistence::RemoveInfo(const std::string &propertyName)
225 {
226  if (!propertyName.empty())
227  {
228  m_InfoMap.erase(propertyName);
229  }
230 }
231 
232 void mitk::PropertyPersistence::RemoveInfo(const std::string &propertyName, const MimeTypeNameType &mime)
233 {
234  auto itr = m_InfoMap.begin();
235  while (itr != m_InfoMap.end())
236  {
237  if (itr->first == propertyName && itr->second.IsNotNull() && itr->second->GetMimeTypeName() == mime)
238  {
239  itr = m_InfoMap.erase(itr);
240  }
241  else
242  {
243  ++itr;
244  }
245  }
246 }
247 
249 {
250  return new PropertyPersistence();
251 };
void RemoveAllInfo() override
Remove all persistence info.
InfoResultType GetInfo(const std::string &propertyName, bool allowNameRegEx) const override
Get the persistence info for a specific base data property.
void RemoveInfo(const std::string &propertyName) override
Remove persistence info instances of a specific property name/regex.
MITKCORE_EXPORT IPropertyPersistence * CreateTestInstancePropertyPersistence()
IPropertyPersistence::InfoResultType InfoResultType
static void info(const char *fmt,...)
Definition: svm.cpp:86
Property persistence info. This class is used to specify the way the persistance of a property of Bas...
const MimeTypeNameType & GetMimeTypeName() const
bool infoPredicate(const std::multimap< const std::string, mitk::PropertyPersistenceInfo::ConstPointer >::value_type &x, const std::string &propertyName, const std::string &mime)
bool AddInfo(const PropertyPersistenceInfo *info, bool overwrite) override
Add persistence info for a specific base data property. If there is already a property info instance ...
bool HasInfo(const std::string &propertyName, bool allowNameRegEx) const override
Check if a specific base data property has persistence info.
bool infoPredicateRegEx(const std::multimap< const std::string, mitk::PropertyPersistenceInfo::ConstPointer >::value_type &x, const std::string &propertyName, const std::string &mime)
Interface of property persistence service.
std::list< PropertyPersistenceInfo::ConstPointer > InfoResultType
InfoResultType GetInfoByKey(const std::string &persistenceKey, bool allowKeyRegEx) const override
Get the persistence info that will use the specified key.
PropertyPersistenceInfo::MimeTypeNameType MimeTypeNameType