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