Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 837 was 827, checked in by landauf, 17 years ago

some template-specializations in Convert.h

File size: 4.8 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#include "Math.h"
40
41// DEFAULT CLASS
42template <typename FromType, typename ToType>
43class _UtilExport Converter
44{
45  public:
46    bool operator()(ToType* output, const FromType& input) const
47    {
48      return false;
49    }
50};
51
52// PARTIAL SPECIALIZATION TO CONVERT TO STRINGS
53template<typename FromType>
54class _UtilExport Converter<FromType, std::string>
55{
56  public:
57    bool operator()(std::string* output, const FromType& input) const
58    {
59      std::ostringstream oss;
60      if (oss << input)
61      {
62        (*output) = oss.str();
63        return true;
64      }
65      else
66        return false;
67    }
68};
69
70// PARTIAL SPECIALIZATION TO CONVERT FROM STRING
71template<typename ToType>
72class _UtilExport Converter<std::string, ToType>
73{
74  public:
75    bool operator()(ToType* output, const std::string& input) const
76    {
77      std::istringstream iss(input);
78      if (iss >> (*output))
79        return true;
80      else
81        return false;
82    }
83};
84
85// FUNCTION SO WE DO NOT HAVE TO TELL THE COMPILER ABOUT THE TYPE
86template<typename FromType, typename ToType>
87static _UtilExport bool ConvertValue(ToType* output, const FromType& input)
88{
89  Converter<FromType, ToType> converter;
90  return converter(output, input);
91}
92
93// THE SAME, BUT WITH DEFAULT VALUE
94template<typename FromType, typename ToType>
95static _UtilExport bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback)
96{
97  Converter<FromType, ToType> converter;
98  if (converter(output, input))
99    return true;
100
101  (*output) = fallback;
102  return false;
103}
104
105
106
107// MORE SPECIALISATIONS
108// Vector2 to std::string
109template <>
110class _UtilExport Converter<orxonox::Vector2, std::string>
111{
112  public:
113    bool operator()(std::string* output, const orxonox::Vector2& input) const
114    {
115      std::ostringstream ostream;
116      if (ostream << input.x << "," << input.y)
117      {
118        (*output) = ostream.str();
119        return true;
120      }
121
122      return false;
123    }
124};
125
126// Vector3 to std::string
127template <>
128class _UtilExport Converter<orxonox::Vector3, std::string>
129{
130  public:
131    bool operator()(std::string* output, const orxonox::Vector3& input) const
132    {
133      std::ostringstream ostream;
134      if (ostream << input.x << "," << input.y << "," << input.z)
135      {
136        (*output) = ostream.str();
137        return true;
138      }
139
140      return false;
141    }
142};
143
144// Vector4 to std::string
145template <>
146class _UtilExport Converter<orxonox::Vector4, std::string>
147{
148  public:
149    bool operator()(std::string* output, const orxonox::Vector4& input) const
150    {
151      std::ostringstream ostream;
152      if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w)
153      {
154        (*output) = ostream.str();
155        return true;
156      }
157
158      return false;
159    }
160};
161
162// Quaternion to std::string
163template <>
164class _UtilExport Converter<orxonox::Quaternion, std::string>
165{
166  public:
167    bool operator()(std::string* output, const orxonox::Quaternion& input) const
168    {
169      std::ostringstream ostream;
170      if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z)
171      {
172        (*output) = ostream.str();
173        return true;
174      }
175
176      return false;
177    }
178};
179
180// ColourValue to std::string
181template <>
182class _UtilExport Converter<orxonox::ColourValue, std::string>
183{
184  public:
185    bool operator()(std::string* output, const orxonox::ColourValue& input) const
186    {
187      std::ostringstream ostream;
188      if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a)
189      {
190        (*output) = ostream.str();
191        return true;
192      }
193
194      return false;
195    }
196};
197
198#endif /* _Convert_H__ */
Note: See TracBrowser for help on using the repository browser.