From d564c16268e16f1a65abc1d1a557c38ec66e2494 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 28 Jun 2012 13:12:50 -0700 Subject: [PATCH] Lexical casts that don't throw. --- src/utils.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/utils.h b/src/utils.h index 1d15a1a9b0..0e1fead5cc 100644 --- a/src/utils.h +++ b/src/utils.h @@ -158,6 +158,31 @@ inline std::string strGetEnv(const std::string& strKey) { return getenv(strKey.c_str()) ? getenv(strKey.c_str()) : ""; } + +template T lexical_cast_s(const std::string& string) +{ // lexically cast a string to the selected type. Does not throw + try + { + return boost::lexical_cast(string); + } + catch (...) + { + return 0; + } +} + +template std::string lexical_cast_i(T t) +{ // lexicaly cast the selected type to a string. Does not throw + try + { + return boost::lexical_cast(t); + } + catch (...) + { + return ""; + } +} + #endif // vim:ts=4