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

Last change on this file since 1275 was 1275, checked in by Jari Häkkinen, 14 years ago

Updating copyright statements.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1#ifndef _theplu_yat_utility_utility_
2#define _theplu_yat_utility_utility_
3
4// $Id: utility.h 1275 2008-04-11 06:10:12Z jari $
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://trac.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 utility/utility.h
32///
33/// @brief Some useful functions are placed here
34///
35
36#include <string>
37#include <stdexcept>
38#include <sstream>
39#include <utility>
40#include <vector>
41
42namespace theplu {
43namespace yat {
44namespace utility {
45
46  class vector;
47
48  /**
49     \brief convert string to (numerical) type
50
51     \throw runtime_error if conversion fails
52   */
53  template<typename T>
54  T convert(const std::string& s);
55
56  ///
57  /// @return true if string is a double
58  ///
59  bool is_double(const std::string&);
60
61  /**
62     @return true if string \a s fulfills regular expression \verbatim
63     ^\w* \endverbatim \a other \verbatim \w*$ \endverbatim (case
64     insensitive)
65  */
66  bool is_equal(std::string s, std::string other);
67
68  ///
69  /// @return true if string is a float
70  ///
71  bool is_float(const std::string&);
72
73  ///
74  /// @return true if string is an int
75  ///
76  bool is_int(const std::string&);
77
78  ///
79  /// @return true if string is "nan" (case-insensitive)
80  ///
81  bool is_nan(const std::string& s);
82
83
84  // template implementations
85  template<typename T>
86  T convert(const std::string& s)
87  {
88    if (is_nan(s))
89      return std::numeric_limits<T>::quiet_NaN();
90    if (is_equal(s, "inf"))
91      return std::numeric_limits<T>::infinity();
92    if (is_equal(s, "-inf"))
93      if (std::numeric_limits<T>::is_signed)
94        return -std::numeric_limits<T>::infinity();
95      else
96        throw std::runtime_error(std::string("convert(\"")+s+
97                                 std::string("\"): type is unsigned") );
98    std::stringstream ss(s);
99    T a;
100    ss >> a;
101    bool ok = true;
102    if(ss.fail()) 
103      ok = false;
104    // Check that nothing is left on stream
105    std::string b;
106    ss >> b;
107    if (!b.empty() || !ok)
108      throw std::runtime_error(std::string("convert(\"")+s+std::string("\")"));
109    return a;
110  }
111
112}}} // of namespace utility, yat, and theplu
113
114#endif
Note: See TracBrowser for help on using the repository browser.