comparison Core/EnumerationDictionary.h @ 3991:5d2348b39392

turning toolbox namespaces into classes to control visibility in shared libraries
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 29 May 2020 21:23:57 +0200
parents 94f4a18a79cc
children
comparison
equal deleted inserted replaced
3990:226ab94a33cd 3991:5d2348b39392
40 #include <string> 40 #include <string>
41 #include <map> 41 #include <map>
42 42
43 namespace Orthanc 43 namespace Orthanc
44 { 44 {
45 namespace Toolbox 45 template <typename Enumeration>
46 class EnumerationDictionary
46 { 47 {
47 template <typename Enumeration> 48 private:
48 class EnumerationDictionary 49 typedef std::map<Enumeration, std::string> EnumerationToString;
50 typedef std::map<std::string, Enumeration> StringToEnumeration;
51
52 EnumerationToString enumerationToString_;
53 StringToEnumeration stringToEnumeration_;
54
55 public:
56 void Clear()
49 { 57 {
50 private: 58 enumerationToString_.clear();
51 typedef std::map<Enumeration, std::string> EnumerationToString; 59 stringToEnumeration_.clear();
52 typedef std::map<std::string, Enumeration> StringToEnumeration; 60 }
53 61
54 EnumerationToString enumerationToString_; 62 bool Contains(Enumeration value) const
55 StringToEnumeration stringToEnumeration_; 63 {
64 return enumerationToString_.find(value) != enumerationToString_.end();
65 }
56 66
57 public: 67 void Add(Enumeration value, const std::string& str)
58 void Clear() 68 {
69 // Check if these values are free
70 if (enumerationToString_.find(value) != enumerationToString_.end() ||
71 stringToEnumeration_.find(str) != stringToEnumeration_.end() ||
72 Toolbox::IsInteger(str) /* Prevent the registration of a number */)
59 { 73 {
60 enumerationToString_.clear(); 74 throw OrthancException(ErrorCode_BadRequest);
61 stringToEnumeration_.clear();
62 } 75 }
63 76
64 bool Contains(Enumeration value) const 77 // OK, the string is free and is not a number
78 enumerationToString_[value] = str;
79 stringToEnumeration_[str] = value;
80 stringToEnumeration_[boost::lexical_cast<std::string>(static_cast<int>(value))] = value;
81 }
82
83 Enumeration Translate(const std::string& str) const
84 {
85 if (Toolbox::IsInteger(str))
65 { 86 {
66 return enumerationToString_.find(value) != enumerationToString_.end(); 87 return static_cast<Enumeration>(boost::lexical_cast<int>(str));
67 } 88 }
68 89
69 void Add(Enumeration value, const std::string& str) 90 typename StringToEnumeration::const_iterator
91 found = stringToEnumeration_.find(str);
92
93 if (found == stringToEnumeration_.end())
70 { 94 {
71 // Check if these values are free 95 throw OrthancException(ErrorCode_InexistentItem);
72 if (enumerationToString_.find(value) != enumerationToString_.end() || 96 }
73 stringToEnumeration_.find(str) != stringToEnumeration_.end() || 97 else
74 Toolbox::IsInteger(str) /* Prevent the registration of a number */) 98 {
75 { 99 return found->second;
76 throw OrthancException(ErrorCode_BadRequest); 100 }
77 } 101 }
78 102
79 // OK, the string is free and is not a number 103 std::string Translate(Enumeration e) const
80 enumerationToString_[value] = str; 104 {
81 stringToEnumeration_[str] = value; 105 typename EnumerationToString::const_iterator
82 stringToEnumeration_[boost::lexical_cast<std::string>(static_cast<int>(value))] = value; 106 found = enumerationToString_.find(e);
107
108 if (found == enumerationToString_.end())
109 {
110 // No name for this item
111 return boost::lexical_cast<std::string>(static_cast<int>(e));
83 } 112 }
84 113 else
85 Enumeration Translate(const std::string& str) const
86 { 114 {
87 if (Toolbox::IsInteger(str)) 115 return found->second;
88 {
89 return static_cast<Enumeration>(boost::lexical_cast<int>(str));
90 }
91
92 typename StringToEnumeration::const_iterator
93 found = stringToEnumeration_.find(str);
94
95 if (found == stringToEnumeration_.end())
96 {
97 throw OrthancException(ErrorCode_InexistentItem);
98 }
99 else
100 {
101 return found->second;
102 }
103 } 116 }
104 117 }
105 std::string Translate(Enumeration e) const 118 };
106 {
107 typename EnumerationToString::const_iterator
108 found = enumerationToString_.find(e);
109
110 if (found == enumerationToString_.end())
111 {
112 // No name for this item
113 return boost::lexical_cast<std::string>(static_cast<int>(e));
114 }
115 else
116 {
117 return found->second;
118 }
119 }
120 };
121 }
122 } 119 }