source: trunk/yat/utility/utility.h @ 1057

Last change on this file since 1057 was 1057, checked in by Peter, 14 years ago

extended convert function to accept nan and int (and -int if type is signed)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1#ifndef _theplu_yat_utility_utility_
2#define _theplu_yat_utility_utility_
3
4// $Id: utility.h 1057 2008-02-07 21:37:14Z peter $
5
6/*
7  Copyright (C) 2005 Jari Häkkinen, Markus Ringnér, Peter Johansson
8  Copyright (C) 2006 Jari Häkkinen
9  Copyright (C) 2007 Peter Johansson
10
11  This file is part of the yat library, http://trac.thep.lu.se/yat
12
13  The yat library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU General Public License as
15  published by the Free Software Foundation; either version 2 of the
16  License, or (at your option) any later version.
17
18  The yat library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22
23  You should have received a copy of the GNU General Public License
24  along with this program; if not, write to the Free Software
25  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26  02111-1307, USA.
27*/
28
29///
30/// @file utility/utility.h
31///
32/// @brief Some useful functions are placed here
33///
34
35#include <string>
36#include <stdexcept>
37#include <sstream>
38#include <utility>
39#include <vector>
40
41namespace theplu {
42namespace yat {
43namespace utility {
44
45  class vector;
46
47  /**
48     \brief convert string to (numerical) type
49
50     \throw runtime_error if conversion fails
51   */
52  template<typename T>
53  T convert(const std::string& s);
54
55  ///
56  /// @return true if string is a double
57  ///
58  bool is_double(const std::string&);
59
60  /**
61     @return true if string \a s fulfills regular expression \verbatim
62     ^\w* \endverbatim \a other \verbatim \w*$ \endverbatim (case
63     insensitive)
64  */
65  bool is_equal(std::string s, std::string other);
66
67  ///
68  /// @return true if string is a float
69  ///
70  bool is_float(const std::string&);
71
72  ///
73  /// @return true if string is an int
74  ///
75  bool is_int(const std::string&);
76
77  ///
78  /// @return true if string is "nan" (case-insensitive)
79  ///
80  bool is_nan(const std::string& s);
81
82
83  // template implementations
84  template<typename T>
85  T convert(const std::string& s)
86  {
87    if (is_nan(s))
88      return std::numeric_limits<T>::quiet_NaN();
89    if (is_equal(s, "inf"))
90      return std::numeric_limits<T>::infinity();
91    if (is_equal(s, "-inf"))
92      if (std::numeric_limits<T>::is_signed())
93        return -std::numeric_limits<T>::infinity();
94      else
95        throw std::runtime_error(std::string("convert(\"")+s+
96                                 std::string("\"): type is unsigned") );
97    std::stringstream ss(s);
98    T a;
99    ss >> a;
100    bool ok = true;
101    if(ss.fail()) 
102      ok = false;
103    // Check that nothing is left on stream
104    std::string b;
105    ss >> b;
106    if (!b.empty() || !ok)
107      throw std::runtime_error(std::string("convert(\"")+s+std::string("\")"));
108    return a;
109  }
110
111}}} // of namespace utility, yat, and theplu
112
113#endif
Note: See TracBrowser for help on using the repository browser.