Merge branch release-2016 into release-2018
[gromacs.git] / src / gromacs / utility / strconvert.h
blob4b6f13c91354e16bf97b844c418a2d1571cde24d
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
35 /*! \libinternal \file
36 * \brief
37 * Declares common utility functions for conversions to and from strings.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \author Mark Abraham <mark.j.abraham@gmail.com>
41 * \inlibraryapi
42 * \ingroup module_utility
44 #ifndef GMX_UTILITY_STRCONVERT_H
45 #define GMX_UTILITY_STRCONVERT_H
47 #include <string>
49 #include "gromacs/utility/basedefinitions.h"
50 #include "gromacs/utility/stringutil.h"
52 namespace gmx
55 //! \cond libapi
56 //! \addtogroup module_utility
57 //! \{
59 /*! \brief
60 * Parses a boolean from a string.
62 * \throws InvalidInputError if `str` is not recognized as a boolean value.
64 bool boolFromString(const char *str);
65 /*! \brief
66 * Parses an integer from a string.
68 * \throws InvalidInputError if `str` is not a valid integer.
70 * Also checks for overflow.
72 int intFromString(const char *str);
73 /*! \brief
74 * Parses a 64-bit integer from a string.
76 * \throws InvalidInputError if `str` is not a valid integer.
78 * Also checks for overflow.
80 gmx_int64_t int64FromString(const char *str);
81 /*! \brief
82 * Parses a float value from a string.
84 * \throws InvalidInputError if `str` is not a valid number.
86 * Also checks for overflow.
88 float floatFromString(const char *str);
89 /*! \brief
90 * Parses a double value from a string.
92 * \throws InvalidInputError if `str` is not a valid number.
94 * Also checks for overflow.
96 double doubleFromString(const char *str);
98 /*! \brief
99 * Parses a value from a string to a given type.
101 * \tparam T Type of value to parse.
103 * `T` can only be one of the types that is explicity supported.
104 * The main use for this function is to write `fromString<real>(value)`,
105 * but it can also be used for other types for consistency.
107 template <typename T> static inline T fromString(const char *str);
108 //! \copydoc fromString(const char *)
109 template <typename T> static inline T fromString(const std::string &str)
111 return fromString<T>(str.c_str());
113 /*! \copydoc fromString(const char *)
115 * Provided for situations where overload resolution cannot easily resolve the
116 * desired std::string parameter.
118 template <typename T> static inline T fromStdString(const std::string &str)
120 return fromString<T>(str.c_str());
123 //! Implementation for boolean values.
124 template <> inline
125 bool fromString<bool>(const char *str) { return boolFromString(str); }
126 //! Implementation for integer values.
127 template <> inline
128 int fromString<int>(const char *str) { return intFromString(str); }
129 //! Implementation for 64-bit integer values.
130 template <> inline
131 gmx_int64_t fromString<gmx_int64_t>(const char *str) { return int64FromString(str); }
132 //! Implementation for float values.
133 template <> inline
134 float fromString<float>(const char *str) { return floatFromString(str); }
135 //! Implementation for double values.
136 template <> inline
137 double fromString<double>(const char *str) { return doubleFromString(str); }
139 /*! \brief
140 * Converts a boolean to a "true"/"false" string.
142 * Does not throw.
144 static inline const char *boolToString(bool value)
146 return value ? "true" : "false";
148 /*! \brief
149 * Returns a string containing the value of \c t.
151 * \throws std::bad_alloc if out of memory.
153 static inline std::string intToString(int t)
155 return formatString("%d", t);
157 //! \copydoc intToString(int)
158 static inline std::string int64ToString(gmx_int64_t t)
160 return formatString("%" GMX_PRId64, t);
162 //! \copydoc intToString(int)
163 static inline std::string doubleToString(double t)
165 return formatString("%g", t);
168 /*! \name
169 * Overloads for converting a value of a given type to a string.
171 * \throws std::bad_alloc if out of memory.
172 * \{
174 static inline std::string toString(bool t) { return boolToString(t); }
175 static inline std::string toString(int t) { return intToString(t); }
176 static inline std::string toString(gmx_int64_t t) { return int64ToString(t); }
177 static inline std::string toString(float t) { return doubleToString(t); }
178 static inline std::string toString(double t) { return doubleToString(t); }
179 static inline std::string toString(std::string t) { return t; }
180 //! \}
182 //! \}
183 //! \endcond
185 } // namespace gmx
187 #endif