Orxonox  0.0.5 Codename: Arcturus
Convert.h
Go to the documentation of this file.
1 /*
2  * ORXONOX - the hottest 3D action shooter ever to exist
3  * > www.orxonox.net <
4  *
5  *
6  * License notice:
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * Author:
23  * Reto Grieder
24  * Fabian 'x3n' Landau
25  * Benjamin Grauer
26  * Co-authors:
27  * ...
28  */
29 
122 #ifndef _Converter_H__
123 #define _Converter_H__
124 
125 #include "UtilPrereqs.h"
126 
127 #include <string>
128 #include <sstream>
129 #include <typeinfo>
130 #include <type_traits>
131 #include <loki/TypeManip.h>
132 
133 #include "Output.h"
134 
135 // disable warnings about possible loss of data
136 #ifdef ORXONOX_COMPILER_MSVC
137 # pragma warning(push)
138 # pragma warning(disable:4244)
139 #endif
140 
141 namespace orxonox
142 {
144  // No Conversion //
146 
148  template <class FromType, class ToType>
150  {
151  ORX_FORCEINLINE static bool convert(ToType* /*output*/, const FromType& /*input*/)
152  {
153  orxout(internal_warning) << "Could not convert value of type " << typeid(FromType).name()
154  << " to type " << typeid(ToType).name() << endl;
155  return false;
156  }
157  };
158 
160  template <class FromType, class ToType>
161  struct ConverterFallback<FromType*, ToType*>
162  {
163  ORX_FORCEINLINE static bool convert(ToType** output, FromType* const input)
164  {
165  ToType* temp = dynamic_cast<ToType*>(input);
166  if (temp)
167  {
168  *output = temp;
169  return true;
170  }
171  else
172  return false;
173  }
174  };
175 }
176 
177 
179 // ConverterFallback //
181 
187 template <class FromType, class ToType>
189 {
190  ORX_FORCEINLINE static bool convert(ToType* output, const FromType& input)
191  {
193  }
194 };
195 
196 
198 // OStream //
200 
203 {
205  template <class FromType>
206  ORX_FORCEINLINE bool operator <<(std::ostream& outstream, const FromType& input)
207  {
208  std::string temp;
210  {
211  std::operator <<(outstream, temp);
212  return true;
213  }
214  else
215  return false;
216  }
217 }
218 
220 template <class FromType>
221 struct ConverterStringStream<FromType, std::string>
222 {
223  ORX_FORCEINLINE static bool convert(std::string* output, const FromType& input)
224  {
225  using namespace fallbackTemplates;
226  // this operator call only chooses fallbackTemplates::operator<<()
227  // if there's no other fitting function
228  std::ostringstream oss;
229  // Note: std::ostream has operator!() to tell whether any error flag was set
230  if (oss << input)
231  {
232  (*output) = oss.str();
233  return true;
234  }
235  else
236  return false;
237  }
238 };
239 
240 
242 // IStream //
244 
245 namespace fallbackTemplates
246 {
248  template <class ToType>
249  ORX_FORCEINLINE bool operator >>(std::istream& instream, ToType& output)
250  {
251  std::string input(static_cast<std::istringstream&>(instream).str());
253  }
254 }
255 
257 template <class ToType>
259 {
260  ORX_FORCEINLINE static bool convert(ToType* output, const std::string& input)
261  {
262  using namespace fallbackTemplates;
263  // this operator call chooses fallbackTemplates::operator>>()
264  // only if there's no other fitting function
265  std::istringstream iss(input);
266  // Note: std::istream has operator!() to tell whether any error flag was set
267  if (iss >> (*output))
268  {
269  return true;
270  }
271  else
272  return false;
273  }
274 };
275 
276 namespace orxonox
277 {
279  // Implicit Cast //
281 
283  template <class FromType, class ToType>
284  ORX_FORCEINLINE bool convertImplicitely(ToType* output, const FromType& input, Loki::Int2Type<false>)
285  {
287  }
288 
290  template <class FromType, class ToType>
291  ORX_FORCEINLINE bool convertImplicitely(ToType* output, const FromType& input, Loki::Int2Type<true>)
292  {
293  (*output) = static_cast<ToType>(input);
294  return true;
295  }
296 
297 
299  // ConverterExplicit Fallback //
301 
307  template <class FromType, class ToType>
309  {
310  static constexpr bool probe = std::is_convertible<FromType, ToType>::value;
311  ORX_FORCEINLINE static bool convert(ToType* output, const FromType& input)
312  {
313  // Use the probe's value to delegate to the right function
314  return convertImplicitely(output, input, Loki::Int2Type<probe>());
315  }
316  };
317 
318 
320  // Public Functions //
322 
334  template <class FromType, class ToType>
335  ORX_FORCEINLINE bool convertValue(ToType* output, const FromType& input)
336  {
337  return ConverterExplicit<FromType, ToType>::convert(output, input);
338  }
339 
340  // Calls convertValue and returns true if the conversion was successful.
341  // Otherwise the fallback is used.
355  template<class FromType, class ToType>
356  ORX_FORCEINLINE bool convertValue(ToType* output, const FromType& input, const ToType& fallback)
357  {
358  if (convertValue(output, input))
359  return true;
360  else
361  {
362  (*output) = fallback;
363  return false;
364  }
365  }
366 
368  template<class FromType, class ToType>
369  ORX_FORCEINLINE ToType getConvertedValue(const FromType& input, const ToType& fallback)
370  {
371  ToType output;
372  convertValue(&output, input, fallback);
373  return output;
374  }
375 
387  template<class ToType, class FromType>
388  ORX_FORCEINLINE ToType multi_cast(const FromType& input)
389  {
390  ToType output;
391  convertValue(&output, input);
392  return output;
393  }
394 
396  // Special string conversions //
398 
400  template <class ToType>
401  struct ConverterExplicit<const char*, ToType>
402  {
403  ORX_FORCEINLINE static bool convert(ToType* output, const char* input)
404  {
405  return convertValue<std::string, ToType>(output, input);
406  }
407  };
408 
410  template <>
411  struct ConverterExplicit<char, std::string>
412  {
413  ORX_FORCEINLINE static bool convert(std::string* output, const char input)
414  {
415  *output = input;
416  return true;
417  }
418  };
420  template <>
421  struct ConverterExplicit<unsigned char, std::string>
422  {
423  ORX_FORCEINLINE static bool convert(std::string* output, const unsigned char input)
424  {
425  *output = input;
426  return true;
427  }
428  };
430  template <>
431  struct ConverterExplicit<std::string, char>
432  {
433  ORX_FORCEINLINE static bool convert(char* output, const std::string& input)
434  {
435  if (!input.empty())
436  *output = input[0];
437  else
438  *output = '\0';
439  return true;
440  }
441  };
443  template <>
444  struct ConverterExplicit<std::string, unsigned char>
445  {
446  ORX_FORCEINLINE static bool convert(unsigned char* output, const std::string& input)
447  {
448  if (!input.empty())
449  *output = input[0];
450  else
451  *output = '\0';
452  return true;
453  }
454  };
455 
456 
458  template <>
459  struct ConverterExplicit<bool, std::string>
460  {
461  ORX_FORCEINLINE static bool convert(std::string* output, const bool& input)
462  {
463  if (input)
464  *output = "true";
465  else
466  *output = "false";
467  return true;
468  }
469  };
470 
472  template <>
474  {
475  static bool convert(bool* output, const std::string& input);
476  };
477 }
478 
479 // Reinstate warnings
480 #ifdef ORXONOX_COMPILER_MSVC
481 # pragma warning(pop)
482 #endif
483 
484 #endif /* _Convert_H__ */
static ORX_FORCEINLINE bool convert(ToType *output, const char *input)
Definition: Convert.h:403
static ORX_FORCEINLINE bool convert(std::string *output, const unsigned char input)
Definition: Convert.h:423
#define _UtilExport
Definition: UtilPrereqs.h:60
ORX_FORCEINLINE ToType multi_cast(const FromType &input)
Converts any value to any other as long as there exists a conversion.
Definition: Convert.h:388
ORX_FORCEINLINE bool operator<<(std::ostream &outstream, const FromType &input)
Fallback operator <<() (delegates to orxonox::ConverterFallback)
Definition: Convert.h:206
static ORX_FORCEINLINE bool convert(ToType *output, const FromType &input)
Definition: Convert.h:190
Definition: TypeManip.h:34
::std::string string
Definition: gtest-port.h:756
static ORX_FORCEINLINE bool convert(std::string *output, const bool &input)
Definition: Convert.h:461
Default template if no orxonox::ConverterExplicit is available.
Definition: Convert.h:308
STL namespace.
Output level, used for warnings which are important for developers.
Definition: OutputDefinitions.h:96
Default template. No conversion available at all.
Definition: Convert.h:149
ORX_FORCEINLINE ToType getConvertedValue(const FromType &input, const ToType &fallback)
Directly returns the converted value, but uses the fallback on failure.
Definition: Convert.h:369
static ORX_FORCEINLINE bool convert(std::string *output, const FromType &input)
Definition: Convert.h:223
Fallback template for stringstream.
Definition: Convert.h:188
static ORX_FORCEINLINE bool convert(char *output, const std::string &input)
Definition: Convert.h:433
ORX_FORCEINLINE bool convertImplicitely(ToType *output, const FromType &input, Loki::Int2Type< false >)
Template delegates to ConverterStringStream
Definition: Convert.h:284
OutputStream & orxout(OutputLevel level=level::debug_output, const OutputContextContainer &context=context::undefined())
This helper function returns a reference to a commonly used instance of OutputStream.
Definition: Output.h:81
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
ORX_FORCEINLINE bool convertValue(ToType *output, const FromType &input)
Converts any value to any other as long as there exists a conversion.
Definition: Convert.h:335
static ORX_FORCEINLINE bool convert(unsigned char *output, const std::string &input)
Definition: Convert.h:446
Shared library macros, enums, constants and forward declarations for the util library ...
std::istream & operator>>(std::istream &in, orxonox::Radian &radian)
Function for reading a Radian from a stream.
Definition: Math.cc:66
Extra namespace to avoid exposing the iostream operators in it.
Definition: Convert.h:202
std::ostream & operator<<(std::ostream &out, const std::set< const Identifier * > &list)
Lists the names of all Identifiers in a std::set<const Identifier*>.
Definition: Identifier.cc:466
static ORX_FORCEINLINE bool convert(ToType **output, FromType *const input)
Definition: Convert.h:163
Defines the helper function orxout() and includes all necessary headers to use the output system...
ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION() fallback()
Fallback implementation, returns an empty list.
Definition: ArgumentCompletionFunctions.cc:67
#define ORX_FORCEINLINE
Definition: OrxonoxConfig.h:95
static ORX_FORCEINLINE bool convert(ToType *output, const std::string &input)
Definition: Convert.h:260
static ORX_FORCEINLINE bool convert(ToType *output, const FromType &input)
Definition: Convert.h:311
static ORX_FORCEINLINE bool convert(std::string *output, const char input)
Definition: Convert.h:413
static ORX_FORCEINLINE bool convert(ToType *, const FromType &)
Definition: Convert.h:151