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

Last change on this file since 1437 was 1437, checked in by Peter, 13 years ago

merge patch release 0.4.2 to trunk. Delta 0.4.2-0.4.1

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