comparison Core/EnumerationDictionary.h @ 435:28ba73274919

registration of user-defined metadata
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 15 May 2013 15:57:05 +0200
parents ccf3a0a43dac
children d51186bf7602
comparison
equal deleted inserted replaced
434:ccf3a0a43dac 435:28ba73274919
53 StringToEnumeration stringToEnumeration_; 53 StringToEnumeration stringToEnumeration_;
54 54
55 public: 55 public:
56 void Add(Enumeration value, const std::string& str) 56 void Add(Enumeration value, const std::string& str)
57 { 57 {
58 // Check if these values are free
59 if (enumerationToString_.find(value) != enumerationToString_.end() ||
60 stringToEnumeration_.find(str) != stringToEnumeration_.end())
61 {
62 throw OrthancException(ErrorCode_BadRequest);
63 }
64
65 // Prevent the registration of a number
66 try
67 {
68 boost::lexical_cast<int>(str);
69 throw OrthancException(ErrorCode_BadRequest);
70 }
71 catch (boost::bad_lexical_cast)
72 {
73 // OK, the string is not a number
74 }
75
58 enumerationToString_[value] = str; 76 enumerationToString_[value] = str;
59 stringToEnumeration_[str] = value; 77 stringToEnumeration_[str] = value;
60 stringToEnumeration_[boost::lexical_cast<std::string>(static_cast<int>(value))] = value; 78 stringToEnumeration_[boost::lexical_cast<std::string>(static_cast<int>(value))] = value;
61 } 79 }
62 80
63 Enumeration Translate(const std::string& str) const 81 Enumeration Translate(const std::string& str) const
64 { 82 {
83 try
84 {
85 int value = boost::lexical_cast<int>(str);
86 return static_cast<Enumeration>(value);
87 }
88 catch (boost::bad_lexical_cast)
89 {
90 }
91
65 typename StringToEnumeration::const_iterator 92 typename StringToEnumeration::const_iterator
66 found = stringToEnumeration_.find(str); 93 found = stringToEnumeration_.find(str);
67 94
68 if (found == stringToEnumeration_.end()) 95 if (found == stringToEnumeration_.end())
69 { 96 {
80 typename EnumerationToString::const_iterator 107 typename EnumerationToString::const_iterator
81 found = enumerationToString_.find(e); 108 found = enumerationToString_.find(e);
82 109
83 if (found == enumerationToString_.end()) 110 if (found == enumerationToString_.end())
84 { 111 {
85 throw OrthancException(ErrorCode_InexistentItem); 112 throw OrthancException(ErrorCode_ParameterOutOfRange);
86 } 113 }
87 else 114 else
88 { 115 {
89 return found->second; 116 return found->second;
90 } 117 }