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