From 1c67ac8fd0db8a916f868501a0d9e7c024e187af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Mon, 20 Dec 2004 13:24:08 +0000 Subject: [PATCH] from string conversion throws exception in case of failure --- mrw/string.hpp | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/mrw/string.hpp b/mrw/string.hpp index 6c090db..78d3048 100644 --- a/mrw/string.hpp +++ b/mrw/string.hpp @@ -9,6 +9,9 @@ @license LGPL, see file COPYING $Log$ + Revision 1.4 2004/12/20 13:24:08 marc + from string conversion throws exception in case of failure + Revision 1.3 2004/12/20 07:40:36 marc documentation improved, new grouping @@ -23,10 +26,10 @@ #ifndef __MRW__STRING__HPP__ #define __MRW__STRING__HPP__ +#include #include #include - namespace mrw { /** @defgroup StdExt Extensions for C++ Standard Libraries @@ -116,6 +119,10 @@ namespace mrw { operators. If you don't want this, just don't include any of these include files files. There's no impact from this module, if you don't include a header, since all code is inline. + + @note The signature of some string functions has changed: + They may now throw exceptions if conversion fails! + (since 1.4.1) */ //@{ /** @defgroup stdextstring String extensions @@ -146,6 +153,10 @@ namespace mrw { s += 4; s = 13.5 + s + 24.8; @endcode + + @note The signature of some string functions has changed: + They may now throw exceptions if conversion fails! + (since 1.4.1) */ //@{ @@ -159,7 +170,6 @@ namespace mrw { @param o a value to be converted to std::string @pre #include @pre T must support operator<< to a stream - */ template std::string string(const T& o) throw(std::bad_exception) { @@ -174,14 +184,20 @@ namespace mrw { int i = mrw::to("15"); @endcode + @throw mrw::invalid_argument if value can not be created from string @param s the string where a value of type @c T is extracted from @pre #include @pre T must support operator>> from a stream + @pre operator>> from T must not throw anything else than std::exception + + @note The signature has changed: + It may now throw exceptions if conversion fails! + (since 1.4.1) */ - template T to(const std::string& s) throw(std::bad_exception) { + template T to(const std::string& s) throw(std::exception) { T o; std::stringstream ss(s); - ss>>o; + if (!(ss>>o)) throw mrw::invalid_argument(s); return o; } @@ -224,6 +240,7 @@ template std::string& operator<<(std::string& s, const T& o) // now: s1=="" s2=="length:" i==15 s3=="mm" @endcode + @throw mrw::invalid_argument if value can not be created from string @param s the string, from which o is extracted @param o the value to extract from s @note when something is extracted from a string, it is removed @@ -231,11 +248,15 @@ template std::string& operator<<(std::string& s, const T& o) shortened by the shifted element @pre #include @pre T must support operator>> from a stream + + @note The signature has changed: + It may now throw exceptions if conversion fails! + (since 1.4.1) */ template std::string& operator>>(std::string& s, T& o) - throw(std::bad_exception) { + throw(std::exception) { std::stringstream ss(s); - ss>>o; + if (!(ss>>o)) throw mrw::invalid_argument(s); return (s=ss.tellg()>0?s.substr(ss.tellg()):""); }