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