diff --git a/src/utils.h b/src/utils.h index 91c5ff9b36..eab185b34a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -171,7 +171,7 @@ template T lexical_cast_s(const std::string& string) } } -template std::string lexical_cast_i(T t) +template std::string lexical_cast_i(const T& t) { // lexicaly cast the selected type to a string. Does not throw try { @@ -183,6 +183,44 @@ template std::string lexical_cast_i(T t) } } +template T lexical_cast_st(const std::string& string) +{ // lexically cast a string to the selected type. Does throw + return boost::lexical_cast(string); +} + +template std::string lexical_cast_it(const T& t) +{ // lexicaly cast the selected type to a string. Does not throw + return boost::lexical_cast(t); +} + +template T range_check(const T& value, const T& minimum, const T& maximum) +{ + if ((value < minimum) || (value > maximum)) + throw std::runtime_error("Value out of range"); + return value; +} + +template T range_check_min(const T& value, const T& minimum) +{ + if (value < minimum) + throw std::runtime_error("Value out of range"); + return value; +} + +template T range_check_max(const T& value, const T& maximum) +{ + if (value > maximum) + throw std::runtime_error("Value out of range"); + return value; +} + +template T range_check_cast(const U& value, const T& minimum, const T& maximum) +{ + if ((value < minimum) || (value > maximum)) + throw std::runtime_error("Value out of range"); + return static_cast(value); +} + #endif // vim:ts=4