Mercurial > hg > orthanc
diff Core/EnumerationDictionary.h @ 2967:146eaed9b02c
avoid boost::bad_lexical_cast exception in happy path
author | am@osimis.io |
---|---|
date | Wed, 05 Dec 2018 16:30:11 +0100 |
parents | a191bbd55a2b |
children | e62e296a5714 |
line wrap: on
line diff
--- a/Core/EnumerationDictionary.h Wed Dec 05 15:27:01 2018 +0100 +++ b/Core/EnumerationDictionary.h Wed Dec 05 16:30:11 2018 +0100 @@ -37,6 +37,7 @@ #include "Toolbox.h" #include <boost/lexical_cast.hpp> +#include <boost/lexical_cast/try_lexical_convert.hpp> #include <string> #include <map> @@ -84,12 +85,10 @@ Enumeration Translate(const std::string& str) const { - try + int numericalValue; // at this stage, we don't know if str is an alias or a numerical value + if (boost::conversion::try_lexical_convert<int>(str, numericalValue)) { - return static_cast<Enumeration>(boost::lexical_cast<int>(str)); - } - catch (boost::bad_lexical_cast&) - { + return static_cast<Enumeration>(numericalValue); } typename StringToEnumeration::const_iterator