Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/misc/Convert.h @ 725

Last change on this file since 725 was 724, checked in by landauf, 18 years ago

reference → pointer (for better readability)

File size: 3.9 KB
RevLine 
[721]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28/*!
29    @file Convert.h
30    @brief Definition and Implementation of the Convert class.
31*/
32
33#ifndef _Convert_H__
34#define _Convert_H__
35
36#include <string>
37#include <sstream>
38
39//! The Convert class has some static member functions to convert strings to values and values to strings.
40class Convert
41{
42  public:
43    /**
44        @brief Converts a value of any type to a string.
45        @param output The string to write the result in
46        @param input The variable to convert
47        @return True if the conversion succeded
48
49        @example
50        float f = 3.14;
51        std::string output;
52        bool success = Convert::ToString(output, f);
53    */
54    template <typename T>
[724]55    static bool ToString(std::string* output, T input)
[721]56    {
57        std::ostringstream oss;
58        if (oss << input)
59        {
[724]60            (*output) = oss.str();
[721]61            return true;
62        }
63
64        return false;
65    }
66
67    /**
68        @brief Converts a value of any type to a string and assigns a defaultvalue if the conversion fails.
69        @param output The string to write the result in
70        @param input The variable to convert
71        @param fallbackString The assigned string if the conversion fails.
72        @return True if the conversion succeeded
73
74        @example
75        float f = 3.14;
76        std::string output;
77        bool success = Convert::ToString(output, f, "0.000000");
78    */
79    template <typename T>
[724]80    static bool ToString(std::string* output, T input, const std::string& fallbackString)
[721]81    {
82        if (Convert::ToString(output, input))
83            return true;
84
[724]85        (*output) = fallbackString;
[721]86        return false;
87    }
88
89    /**
90        @brief Converts a string to a value of any type.
91        @param output The variable to assign the result to
92        @param input The string to convert
93        @return True if the conversion succeeded
94
95        @example
96        std::string input = "3.14";
97        float f;
98        bool success = string2Number(f, input);
99    */
100    template <typename T>
[724]101    static bool FromString(T* output, const std::string& input)
[721]102    {
103        std::istringstream iss(input);
[724]104        if (iss >> (*output))
[721]105            return true;
106
107        return false;
108    }
109
110    /**
111        @brief Converts a string to a value of any type.
112        @param output The variable to assign the result to
113        @param input The string to convert
114        @param fallbackValue The assigned value if the conversion fails
115        @return True if the conversion succeeded
116
117        @example
118        std::string input = "3.14";
119        float f;
120        bool success = string2Number(f, input, 0.000000);
121    */
122    template <typename T>
[724]123    static bool FromString(T* output, const std::string& input, T fallbackValue)
[721]124    {
125        if (Convert::FromString(output, input))
126            return true;
127
[724]128        (*output) = fallbackValue;
[721]129        return false;
130    }
131};
132
133#endif /* _Convert_H__ */
Note: See TracBrowser for help on using the repository browser.