Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core/src/util/Convert.h @ 796

Last change on this file since 796 was 792, checked in by landauf, 17 years ago

upload of the work i did before the exams (not yet finished nor working)

File size: 2.6 KB
Line 
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 *      Benjamin Grauer
23 *   Co-authors:
24 *      Fabian 'x3n' Landau
25 */
26
27/*!
28    @file Convert.h
29    @brief Definition and Implementation of the Convert class.
30*/
31
32#ifndef _Convert_H__
33#define _Convert_H__
34
35#include <string>
36#include <sstream>
37
38#include "UtilPrereqs.h"
39
40// DEFAULT CLASS
41template <typename FromType, typename ToType>
42class _UtilExport Converter
43{
44 public:
45  bool operator()(ToType* output, const FromType& input) const
46  {
47    return false;
48  }
49};
50
51// PARTIAL SPECIALIZATION TO CONVERT TO STRINGS
52template<typename FromType>
53class _UtilExport Converter<FromType, std::string>
54{
55 public:
56  bool operator()(std::string* output, const FromType& input) const
57  {
58    std::ostringstream oss;
59    if (oss << input)
60    {
61      (*output) = oss.str();
62      return true;
63    }
64    else
65      return false;
66  }
67};
68
69// PARTIAL SPECIALIZATION TO CONVERT FROM STRING
70template<typename ToType>
71class _UtilExport Converter<std::string, ToType>
72{
73 public:
74  bool operator()(ToType* output, const std::string& input) const
75  {
76    std::istringstream iss(input);
77    if (iss >> (*output))
78      return true;
79    else
80      return false;
81  }
82};
83
84// FUNCTION SO WE DO NOT HAVE TO TELL THE COMPILER ABOUT THE TYPE
85template<typename FromType, typename ToType>
86static _UtilExport bool ConvertValue(ToType* output, const FromType& input)
87{
88  Converter<FromType, ToType> converter;
89  return converter(output, input);
90}
91
92// THE SAME, BUT WITH DEFAULT VALUE
93template<typename FromType, typename ToType>
94static _UtilExport bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback)
95{
96  Converter<FromType, ToType> converter;
97  if (converter(output, input))
98    return true;
99
100  (*output) = fallback;
101  return false;
102}
103
104#endif /* _Convert_H__ */
Note: See TracBrowser for help on using the repository browser.