Mercurial > hg > orthanc
diff 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 |
line wrap: on
line diff
--- a/Core/EnumerationDictionary.h Fri May 29 19:43:28 2020 +0200 +++ b/Core/EnumerationDictionary.h Fri May 29 21:23:57 2020 +0200 @@ -42,81 +42,78 @@ namespace Orthanc { - namespace Toolbox + template <typename Enumeration> + class EnumerationDictionary { - template <typename Enumeration> - class EnumerationDictionary - { - private: - typedef std::map<Enumeration, std::string> EnumerationToString; - typedef std::map<std::string, Enumeration> StringToEnumeration; + private: + typedef std::map<Enumeration, std::string> EnumerationToString; + typedef std::map<std::string, Enumeration> StringToEnumeration; + + EnumerationToString enumerationToString_; + StringToEnumeration stringToEnumeration_; - EnumerationToString enumerationToString_; - StringToEnumeration stringToEnumeration_; + public: + void Clear() + { + enumerationToString_.clear(); + stringToEnumeration_.clear(); + } - public: - void Clear() + bool Contains(Enumeration value) const + { + return enumerationToString_.find(value) != enumerationToString_.end(); + } + + void Add(Enumeration value, const std::string& str) + { + // Check if these values are free + if (enumerationToString_.find(value) != enumerationToString_.end() || + stringToEnumeration_.find(str) != stringToEnumeration_.end() || + Toolbox::IsInteger(str) /* Prevent the registration of a number */) { - enumerationToString_.clear(); - stringToEnumeration_.clear(); - } - - bool Contains(Enumeration value) const - { - return enumerationToString_.find(value) != enumerationToString_.end(); + throw OrthancException(ErrorCode_BadRequest); } - void Add(Enumeration value, const std::string& str) + // OK, the string is free and is not a number + enumerationToString_[value] = str; + stringToEnumeration_[str] = value; + stringToEnumeration_[boost::lexical_cast<std::string>(static_cast<int>(value))] = value; + } + + Enumeration Translate(const std::string& str) const + { + if (Toolbox::IsInteger(str)) { - // Check if these values are free - if (enumerationToString_.find(value) != enumerationToString_.end() || - stringToEnumeration_.find(str) != stringToEnumeration_.end() || - Toolbox::IsInteger(str) /* Prevent the registration of a number */) - { - throw OrthancException(ErrorCode_BadRequest); - } - - // OK, the string is free and is not a number - enumerationToString_[value] = str; - stringToEnumeration_[str] = value; - stringToEnumeration_[boost::lexical_cast<std::string>(static_cast<int>(value))] = value; + return static_cast<Enumeration>(boost::lexical_cast<int>(str)); } - Enumeration Translate(const std::string& str) const - { - if (Toolbox::IsInteger(str)) - { - return static_cast<Enumeration>(boost::lexical_cast<int>(str)); - } + typename StringToEnumeration::const_iterator + found = stringToEnumeration_.find(str); - typename StringToEnumeration::const_iterator - found = stringToEnumeration_.find(str); - - if (found == stringToEnumeration_.end()) - { - throw OrthancException(ErrorCode_InexistentItem); - } - else - { - return found->second; - } + if (found == stringToEnumeration_.end()) + { + throw OrthancException(ErrorCode_InexistentItem); + } + else + { + return found->second; } + } - std::string Translate(Enumeration e) const - { - typename EnumerationToString::const_iterator - found = enumerationToString_.find(e); + std::string Translate(Enumeration e) const + { + typename EnumerationToString::const_iterator + found = enumerationToString_.find(e); - if (found == enumerationToString_.end()) - { - // No name for this item - return boost::lexical_cast<std::string>(static_cast<int>(e)); - } - else - { - return found->second; - } + if (found == enumerationToString_.end()) + { + // No name for this item + return boost::lexical_cast<std::string>(static_cast<int>(e)); } - }; - } + else + { + return found->second; + } + } + }; }