Orxonox  0.0.5 Codename: Arcturus
Classes | Namespaces | Functions
Convert.h File Reference

Functions that convert values between different types. More...

#include "UtilPrereqs.h"
#include <string>
#include <sstream>
#include <typeinfo>
#include <type_traits>
#include <loki/TypeManip.h>
#include "Output.h"

Go to the source code of this file.

Classes

struct  ConverterStringStream< FromType, ToType >
 Fallback template for stringstream. More...
 
struct  ConverterStringStream< FromType, std::string >
 Template that evaluates whether we can convert to std::string via ostringstream. More...
 
struct  ConverterStringStream< std::string, ToType >
 Template that evaluates whether we can convert from std::string via istringstream. More...
 
struct  orxonox::ConverterExplicit< FromType, ToType >
 Default template if no orxonox::ConverterExplicit is available. More...
 
struct  orxonox::ConverterExplicit< bool, std::string >
 Conversion from bool to std::string. More...
 
struct  orxonox::ConverterExplicit< char, std::string >
 Conversion would exhibit ambiguous << or >> operators when using iostream. More...
 
struct  orxonox::ConverterExplicit< const char *, ToType >
 Delegates conversion from const char* to std::string. More...
 
struct  orxonox::ConverterExplicit< std::string, bool >
 Conversion from std::string to bool. More...
 
struct  orxonox::ConverterExplicit< std::string, char >
 Conversion would exhibit ambiguous << or >> operators when using iostream. More...
 
struct  orxonox::ConverterExplicit< std::string, unsigned char >
 Conversion would exhibit ambiguous << or >> operators when using iostream. More...
 
struct  orxonox::ConverterExplicit< unsigned char, std::string >
 Conversion would exhibit ambiguous << or >> operators when using iostream. More...
 
struct  orxonox::ConverterFallback< FromType, ToType >
 Default template. No conversion available at all. More...
 
struct  orxonox::ConverterFallback< FromType *, ToType * >
 If all else fails, try a dynamic_cast for pointer types. More...
 

Namespaces

 fallbackTemplates
 Extra namespace to avoid exposing the iostream operators in it.
 
 orxonox
 Die Wagnis Klasse hat die folgenden Aufgaben:
 

Functions

template<class FromType , class ToType >
ORX_FORCEINLINE bool orxonox::convertImplicitely (ToType *output, const FromType &input, Loki::Int2Type< false >)
 Template delegates to ConverterStringStream More...
 
template<class FromType , class ToType >
ORX_FORCEINLINE bool orxonox::convertImplicitely (ToType *output, const FromType &input, Loki::Int2Type< true >)
 Makes an implicit cast from FromType to ToType. More...
 
template<class FromType , class ToType >
ORX_FORCEINLINE bool orxonox::convertValue (ToType *output, const FromType &input)
 Converts any value to any other as long as there exists a conversion. More...
 
template<class FromType , class ToType >
ORX_FORCEINLINE bool orxonox::convertValue (ToType *output, const FromType &input, const ToType &fallback)
 Converts any value to any other as long as there exists a conversion. More...
 
template<class FromType , class ToType >
ORX_FORCEINLINE ToType orxonox::getConvertedValue (const FromType &input, const ToType &fallback)
 Directly returns the converted value, but uses the fallback on failure. More...
 
template<class ToType , class FromType >
ORX_FORCEINLINE ToType orxonox::multi_cast (const FromType &input)
 Converts any value to any other as long as there exists a conversion. More...
 
template<class FromType >
ORX_FORCEINLINE bool fallbackTemplates::operator<< (std::ostream &outstream, const FromType &input)
 Fallback operator <<() (delegates to orxonox::ConverterFallback) More...
 
template<class ToType >
ORX_FORCEINLINE bool fallbackTemplates::operator>> (std::istream &instream, ToType &output)
 Fallback operator >>() (delegates to orxonox::ConverterFallback) More...
 

Detailed Description

Functions that convert values between different types.

Usage
There are three ways to use the conversions depending on what you need.
The back end of these functions are the actual implementations for the
specific conversions, for instance from Ogre::Vector3 to std::string and
vice versa. Some of them also use the iostream operators. <br>
The real deal is evaluating which function is needed for a conversion based
on the input and output type. But there are lots of catches in conjunction
with templates which explains why there are so many functions in this file.
<br> <br>
Search Order
Finding the right function is governed by priority rules:
  1. (Partial) template specialisation of orxonox::ConverterExplicit::convert()
  2. An implicit conversion. This includes 'FooBar' to 'int' if FooBar defines operator int() or float().
  3. Global or member operators for iostream when converting from or to std::string (and FROM const char*)
  4. (Partial) template specialisation of orxonox::ConverterFallback::convert()
  5. Fallback function that displays "Could not convert value" with type information obtained from typeid().
Implementing conversion functions
To do that you probably need to know a thing or two about the types involved. So, get ready with that.
Usually the best way to do it is specialising of the orxonox::ConverterFallback template, like this:
template <>
struct _UtilExport ConverterFallback<std::string, MyType>
{
static bool convert(MyType* output, const std::string& input)
{
...
return success;
}
};
This piece of code converts an std::string to MyType and returns whether the conversion was successful. You can also use partial specialisation.
The advantage with orxonox::ConverterFallback is that it has a low priority meaning that when there is an implicit conversion or an iostream method, that comes first and you don't have to deal with it (and the accompanying function call ambiguity).
However sometimes you would like to explicitely replace such a conversion. That's where orxonox::ConverterExplicit comes in handy (for instance we replaced the operator << conversions for Ogre::VectorX with our own functions).
Note
There has to be an exact type match when using template specialisations.
Template specialisations can be defined after including this file. But any implicit cast function or iostream operator has to be included in this file!
Understanding the Code
In order to understand how the templates work, it is probably best to study the functions in order of calling. There are lots of comments explaining what happens, but you'll need to understand a deal about partial template specialisation and function headers are matched in C++.