Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkPropertyAliases.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 "mitkPropertyAliases.h"
14 #include <algorithm>
15 #include <utility>
16 
17 #ifdef _MSC_VER
18 #pragma warning(disable : 4503) // "decorated name length exceeded, name was truncated"
19 #endif
20 
21 class AliasEquals
22 {
23 public:
24  AliasEquals(const std::string &alias) : m_Alias(alias) {}
25  bool operator()(const std::pair<std::string, std::vector<std::string>> &element)
26  {
27  auto iter = std::find(element.second.begin(), element.second.end(), m_Alias);
28  return iter != element.second.end();
29  }
30 
31 private:
32  std::string m_Alias;
33 };
34 
36 {
37 }
38 
40 {
41 }
42 
43 bool mitk::PropertyAliases::AddAlias(const std::string &propertyName,
44  const std::string &alias,
45  const std::string &className)
46 {
47  if (alias.empty())
48  return false;
49 
50  AliasesMap &aliases = m_Aliases[className];
51  auto iter = aliases.find(propertyName);
52 
53  if (iter != aliases.end())
54  {
55  if (std::find(iter->second.begin(), iter->second.end(), alias) == iter->second.end())
56  iter->second.push_back(alias);
57  }
58  else
59  {
60  aliases.insert(std::make_pair(propertyName, std::vector<std::string>(1, alias)));
61  }
62 
63  return true;
64 }
65 
66 std::vector<std::string> mitk::PropertyAliases::GetAliases(const std::string &propertyName,
67  const std::string &className)
68 {
69  if (!propertyName.empty())
70  {
71  AliasesMap &aliases = m_Aliases[className];
72  AliasesMapConstIterator iter = aliases.find(propertyName);
73 
74  if (iter != aliases.end())
75  return iter->second;
76  }
77 
78  return std::vector<std::string>();
79 }
80 
81 std::string mitk::PropertyAliases::GetPropertyName(const std::string &alias, const std::string &className)
82 {
83  if (!alias.empty())
84  {
85  AliasesMap &aliases = m_Aliases[className];
86  AliasesMapConstIterator iter = std::find_if(aliases.begin(), aliases.end(), AliasEquals(alias));
87 
88  if (iter != aliases.end())
89  return iter->first;
90  }
91 
92  return "";
93 }
94 
95 bool mitk::PropertyAliases::HasAliases(const std::string &propertyName, const std::string &className)
96 {
97  const AliasesMap &aliases = m_Aliases[className];
98 
99  return !propertyName.empty() ? aliases.find(propertyName) != aliases.end() : false;
100 }
101 
102 void mitk::PropertyAliases::RemoveAlias(const std::string &propertyName,
103  const std::string &alias,
104  const std::string &className)
105 {
106  if (!propertyName.empty() && !alias.empty())
107  {
108  AliasesMap &aliases = m_Aliases[className];
109  auto iter = aliases.find(propertyName);
110 
111  if (iter != aliases.end())
112  {
113  auto aliasIter = std::find(iter->second.begin(), iter->second.end(), alias);
114 
115  if (aliasIter != iter->second.end())
116  {
117  iter->second.erase(aliasIter);
118 
119  if (iter->second.empty())
120  aliases.erase(propertyName);
121  }
122  }
123  }
124 }
125 
126 void mitk::PropertyAliases::RemoveAliases(const std::string &propertyName, const std::string &className)
127 {
128  if (!propertyName.empty())
129  {
130  AliasesMap &aliases = m_Aliases[className];
131  aliases.erase(propertyName);
132  }
133 }
134 
135 void mitk::PropertyAliases::RemoveAllAliases(const std::string &className)
136 {
137  AliasesMap &aliases = m_Aliases[className];
138  aliases.clear();
139 }
std::string GetPropertyName(const std::string &alias, const std::string &className) override
Get property name that is associated to specific alias.
bool HasAliases(const std::string &propertyName, const std::string &className) override
Check if a specific property has aliases.
bool AddAlias(const std::string &propertyName, const std::string &alias, const std::string &className) override
Add an alias for a specific property.
void RemoveAliases(const std::string &propertyName, const std::string &className) override
Remove all aliases of a specific property.
std::vector< std::string > GetAliases(const std::string &propertyName, const std::string &className) override
Get aliases for a specific property.
void RemoveAlias(const std::string &propertyName, const std::string &alias, const std::string &className) override
Remove specific alias of a certain property.
void RemoveAllAliases(const std::string &className) override
Remove all aliases of all properties.