Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkPropertyDescriptions.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 #include <algorithm>
19 #include <regex>
20 #include <utility>
21 
23 {
24 }
25 
27 {
28 }
29 
30 bool mitk::PropertyDescriptions::AddDescription(const std::string &propertyName,
31  const std::string &description,
32  const std::string &className,
33  bool overwrite)
34 {
35  if (propertyName.empty())
36  return false;
37 
38  DescriptionMap &descriptions = m_Descriptions[className];
39  std::pair<DescriptionMapIterator, bool> ret = descriptions.insert(std::make_pair(propertyName, description));
40 
41  if (!ret.second && overwrite)
42  {
43  ret.first->second = description;
44  ret.second = true;
45  }
46 
47  return ret.second;
48 }
49 
50 bool mitk::PropertyDescriptions::AddDescriptionRegEx(const std::string &propertyRegEx,
51  const std::string &description,
52  const std::string &className,
53  bool overwrite)
54 {
55  if (propertyRegEx.empty())
56  return false;
57 
58  try
59  {
60  std::regex checker(propertyRegEx); // no exception => valid we can change the info
61  }
62  catch (std::regex_error)
63  {
64  return false;
65  }
66 
67  DescriptionMap &descriptions = m_DescriptionsRegEx[className];
68  std::pair<DescriptionMapIterator, bool> ret = descriptions.insert(std::make_pair(propertyRegEx, description));
69 
70  if (!ret.second && overwrite)
71  {
72  ret.first->second = description;
73  ret.second = true;
74  }
75 
76  return ret.second;
77 }
78 
79 std::string mitk::PropertyDescriptions::GetDescription(const std::string &propertyName,
80  const std::string &className,
81  bool allowNameRegEx) const
82 {
83  if (!propertyName.empty())
84  {
85  auto descriptionsIter = m_Descriptions.find(className);
86 
87  if (descriptionsIter != m_Descriptions.cend())
88  {
89  DescriptionMapConstIterator iter = descriptionsIter->second.find(propertyName);
90 
91  if (iter != descriptionsIter->second.end())
92  return iter->second;
93  }
94  }
95 
96  if (allowNameRegEx && !propertyName.empty())
97  {
98  auto selector = [propertyName](const DescriptionMap::value_type &x) {
99  std::regex ex(x.first);
100  return std::regex_match(propertyName, ex);
101  };
102 
103  auto descriptionsIter = m_DescriptionsRegEx.find(className);
104 
105  if (descriptionsIter != m_DescriptionsRegEx.cend())
106  {
107  auto finding = std::find_if(descriptionsIter->second.cbegin(), descriptionsIter->second.cend(), selector);
108 
109  if (finding != descriptionsIter->second.cend())
110  return finding->second;
111  }
112  }
113 
114  return "";
115 }
116 
117 bool mitk::PropertyDescriptions::HasDescription(const std::string &propertyName,
118  const std::string &className,
119  bool allowNameRegEx) const
120 {
121  if (!propertyName.empty())
122  {
123  auto descriptionsIter = m_Descriptions.find(className);
124 
125  if (descriptionsIter != m_Descriptions.cend())
126  {
127  DescriptionMapConstIterator iter = descriptionsIter->second.find(propertyName);
128 
129  if (iter != descriptionsIter->second.end())
130  return true;
131  }
132  }
133 
134  if (allowNameRegEx && !propertyName.empty())
135  {
136  auto selector = [propertyName](const DescriptionMap::value_type &x) {
137  std::regex ex(x.first);
138  return std::regex_match(propertyName, ex);
139  };
140 
141  auto descriptionsIter = m_DescriptionsRegEx.find(className);
142 
143  if (descriptionsIter != m_DescriptionsRegEx.cend())
144  {
145  auto finding = std::find_if(descriptionsIter->second.cbegin(), descriptionsIter->second.cend(), selector);
146 
147  if (finding != descriptionsIter->second.cend())
148  return true;
149  }
150  }
151 
152  return false;
153 }
154 
155 void mitk::PropertyDescriptions::RemoveAllDescriptions(const std::string &className)
156 {
157  m_Descriptions[className].clear();
158  m_DescriptionsRegEx[className].clear();
159 }
160 
161 void mitk::PropertyDescriptions::RemoveDescription(const std::string &propertyName, const std::string &className)
162 {
163  if (!propertyName.empty())
164  {
165  m_Descriptions[className].erase(propertyName);
166  m_DescriptionsRegEx[className].erase(propertyName);
167  }
168 }
void RemoveAllDescriptions(const std::string &className) override
Remove all descriptions.
bool HasDescription(const std::string &propertyName, const std::string &className, bool overwrite) const override
Check if a specific property has a description.
bool AddDescription(const std::string &propertyName, const std::string &description, const std::string &className, bool overwrite) override
Add a description for a specific property.
bool AddDescriptionRegEx(const std::string &propertyRegEx, const std::string &description, const std::string &className, bool overwrite) override
Add a description for all properties matching the property regulary expression.
std::string GetDescription(const std::string &propertyName, const std::string &className, bool overwrite) const override
Get the description for a specific property.
void RemoveDescription(const std::string &propertyName, const std::string &className) override
Remove description of specific property.