comparison Core/EnumerationDictionary.h @ 1102:ce6386b37afd

avoid unnecessary exceptions on Orthanc startup
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 07 Aug 2014 10:51:35 +0200
parents 2d0a347e8cfc
children bec1eccf976c
comparison
equal deleted inserted replaced
1101:e5686a703c63 1102:ce6386b37afd
32 32
33 #pragma once 33 #pragma once
34 34
35 #include "OrthancException.h" 35 #include "OrthancException.h"
36 36
37 #include "Toolbox.h"
37 #include <boost/lexical_cast.hpp> 38 #include <boost/lexical_cast.hpp>
38 #include <string> 39 #include <string>
39 #include <map> 40 #include <map>
40 41
41 namespace Orthanc 42 namespace Orthanc
55 public: 56 public:
56 void Add(Enumeration value, const std::string& str) 57 void Add(Enumeration value, const std::string& str)
57 { 58 {
58 // Check if these values are free 59 // Check if these values are free
59 if (enumerationToString_.find(value) != enumerationToString_.end() || 60 if (enumerationToString_.find(value) != enumerationToString_.end() ||
60 stringToEnumeration_.find(str) != stringToEnumeration_.end()) 61 stringToEnumeration_.find(str) != stringToEnumeration_.end() ||
62 Toolbox::IsInteger(str) /* Prevent the registration of a number */)
61 { 63 {
62 throw OrthancException(ErrorCode_BadRequest); 64 throw OrthancException(ErrorCode_BadRequest);
63 } 65 }
64 66
65 // Prevent the registration of a number 67 // OK, the string is free and is not 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
76 enumerationToString_[value] = str; 68 enumerationToString_[value] = str;
77 stringToEnumeration_[str] = value; 69 stringToEnumeration_[str] = value;
78 stringToEnumeration_[boost::lexical_cast<std::string>(static_cast<int>(value))] = value; 70 stringToEnumeration_[boost::lexical_cast<std::string>(static_cast<int>(value))] = value;
79 } 71 }
80 72