diff 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
line wrap: on
line diff
--- a/Core/EnumerationDictionary.h	Wed May 15 14:54:58 2013 +0200
+++ b/Core/EnumerationDictionary.h	Wed May 15 15:57:05 2013 +0200
@@ -55,6 +55,24 @@
     public:
       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())
+        {
+          throw OrthancException(ErrorCode_BadRequest);
+        }
+
+        // Prevent the registration of a number
+        try
+        {
+          boost::lexical_cast<int>(str);
+          throw OrthancException(ErrorCode_BadRequest);
+        }
+        catch (boost::bad_lexical_cast)
+        {
+          // OK, the string is not a number
+        }
+
         enumerationToString_[value] = str;
         stringToEnumeration_[str] = value;
         stringToEnumeration_[boost::lexical_cast<std::string>(static_cast<int>(value))] = value;
@@ -62,6 +80,15 @@
 
       Enumeration Translate(const std::string& str) const
       {
+        try
+        {
+          int value = boost::lexical_cast<int>(str);
+          return static_cast<Enumeration>(value);
+        }
+        catch (boost::bad_lexical_cast)
+        {
+        }
+
         typename StringToEnumeration::const_iterator
           found = stringToEnumeration_.find(str);
 
@@ -82,7 +109,7 @@
 
         if (found == enumerationToString_.end())
         {
-          throw OrthancException(ErrorCode_InexistentItem);
+          throw OrthancException(ErrorCode_ParameterOutOfRange);
         }
         else
         {