Remove gmx custom fixed int (e.g. gmx_int64_t) types
[alexxy/gromacs.git] / src / gromacs / utility / strconvert.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2017,2018, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \libinternal \file
36  * \brief
37  * Declares common utility functions for conversions to and from strings.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \author Mark Abraham <mark.j.abraham@gmail.com>
41  * \inlibraryapi
42  * \ingroup module_utility
43  */
44 #ifndef GMX_UTILITY_STRCONVERT_H
45 #define GMX_UTILITY_STRCONVERT_H
46
47 #include <string>
48
49 #include "gromacs/utility/basedefinitions.h"
50 #include "gromacs/utility/stringutil.h"
51
52 namespace gmx
53 {
54
55 //! \cond libapi
56 //! \addtogroup module_utility
57 //! \{
58
59 /*! \brief
60  * Parses a boolean from a string.
61  *
62  * \throws  InvalidInputError if `str` is not recognized as a boolean value.
63  */
64 bool boolFromString(const char *str);
65 /*! \brief
66  * Parses an integer from a string.
67  *
68  * \throws  InvalidInputError if `str` is not a valid integer.
69  *
70  * Also checks for overflow.
71  */
72 int intFromString(const char *str);
73 /*! \brief
74  * Parses a 64-bit integer from a string.
75  *
76  * \throws  InvalidInputError if `str` is not a valid integer.
77  *
78  * Also checks for overflow.
79  */
80 int64_t int64FromString(const char *str);
81 /*! \brief
82  * Parses a float value from a string.
83  *
84  * \throws  InvalidInputError if `str` is not a valid number.
85  *
86  * Also checks for overflow.
87  */
88 float floatFromString(const char *str);
89 /*! \brief
90  * Parses a double value from a string.
91  *
92  * \throws  InvalidInputError if `str` is not a valid number.
93  *
94  * Also checks for overflow.
95  */
96 double doubleFromString(const char *str);
97
98 /*! \brief
99  * Parses a value from a string to a given type.
100  *
101  * \tparam T Type of value to parse.
102  *
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.
106  */
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)
110 {
111     return fromString<T>(str.c_str());
112 }
113 /*! \copydoc fromString(const char *)
114  *
115  * Provided for situations where overload resolution cannot easily resolve the
116  * desired std::string parameter.
117  */
118 template <typename T> static inline T fromStdString(const std::string &str)
119 {
120     return fromString<T>(str.c_str());
121 }
122
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 int64_t fromString<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); }
138
139 /*! \brief
140  * Converts a boolean to a "true"/"false" string.
141  *
142  * Does not throw.
143  */
144 static inline const char *boolToString(bool value)
145 {
146     return value ? "true" : "false";
147 }
148 /*! \brief
149  * Returns a string containing the value of \c t.
150  *
151  * \throws std::bad_alloc if out of memory.
152  */
153 static inline std::string intToString(int t)
154 {
155     return formatString("%d", t);
156 }
157 //! \copydoc intToString(int)
158 static inline std::string int64ToString(int64_t t)
159 {
160     return formatString("%" PRId64, t);
161 }
162 //! \copydoc intToString(int)
163 static inline std::string doubleToString(double t)
164 {
165     return formatString("%g", t);
166 }
167
168 /*! \name
169  * Overloads for converting a value of a given type to a string.
170  *
171  * \throws std::bad_alloc if out of memory.
172  * \{
173  */
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(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 //! \}
181
182 //! \}
183 //! \endcond
184
185 } // namespace gmx
186
187 #endif