from string conversion throws exception in case of failure

master
Marc Wäckerlin 20 years ago
parent 0a3a928547
commit 1c67ac8fd0
  1. 33
      mrw/string.hpp

@ -9,6 +9,9 @@
@license LGPL, see file <a href="license.html">COPYING</a>
$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 <mrw/exception.hpp>
#include <string>
#include <sstream>
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 <mrw/string.hpp>
@pre T must support operator<< to a stream
*/
template <typename T> std::string string(const T& o)
throw(std::bad_exception) {
@ -174,14 +184,20 @@ namespace mrw {
int i = mrw::to<int>("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 <mrw/string.hpp>
@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 <typename T> T to(const std::string& s) throw(std::bad_exception) {
template <typename T> 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 <typename T> 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 <typename T> std::string& operator<<(std::string& s, const T& o)
shortened by the shifted element
@pre #include <mrw/string.hpp>
@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 <typename T> 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()):"");
}

Loading…
Cancel
Save